2022-03-23 10:14:33 -05:00
|
|
|
import { buildDigest, signJsonLDObject } from '@server/helpers/peertube-crypto'
|
|
|
|
import { ACTIVITY_PUB, HTTP_SIGNATURE } from '@server/initializers/constants'
|
|
|
|
import { ActorModel } from '@server/models/actor/actor'
|
2021-05-11 04:15:29 -05:00
|
|
|
import { getServerActor } from '@server/models/application/application'
|
2022-03-23 10:14:33 -05:00
|
|
|
import { MActor } from '@server/types/models'
|
2021-05-11 04:15:29 -05:00
|
|
|
import { ContextType } from '@shared/models/activitypub/context'
|
2022-03-23 10:14:33 -05:00
|
|
|
import { activityPubContextify } from '../context'
|
2018-01-25 08:05:18 -06:00
|
|
|
|
2022-03-23 10:14:33 -05:00
|
|
|
type Payload <T> = { body: T, contextType: ContextType, signatureActorId?: number }
|
2018-10-10 01:51:58 -05:00
|
|
|
|
2021-03-08 07:24:11 -06:00
|
|
|
async function computeBody <T> (
|
|
|
|
payload: Payload<T>
|
|
|
|
): Promise<T | T & { type: 'RsaSignature2017', creator: string, created: string }> {
|
2018-01-25 08:05:18 -06:00
|
|
|
let body = payload.body
|
|
|
|
|
|
|
|
if (payload.signatureActorId) {
|
|
|
|
const actorSignature = await ActorModel.load(payload.signatureActorId)
|
|
|
|
if (!actorSignature) throw new Error('Unknown signature actor id.')
|
2021-03-08 07:24:11 -06:00
|
|
|
|
2022-03-23 10:14:33 -05:00
|
|
|
body = await signAndContextify(actorSignature, payload.body, payload.contextType)
|
2018-01-25 08:05:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return body
|
|
|
|
}
|
|
|
|
|
2021-03-08 07:24:11 -06:00
|
|
|
async function buildSignedRequestOptions (payload: Payload<any>) {
|
2019-08-15 04:53:26 -05:00
|
|
|
let actor: MActor | null
|
|
|
|
|
2018-01-25 08:05:18 -06:00
|
|
|
if (payload.signatureActorId) {
|
|
|
|
actor = await ActorModel.load(payload.signatureActorId)
|
|
|
|
if (!actor) throw new Error('Unknown signature actor id.')
|
|
|
|
} else {
|
|
|
|
// We need to sign the request, so use the server
|
|
|
|
actor = await getServerActor()
|
|
|
|
}
|
|
|
|
|
2019-04-25 08:19:53 -05:00
|
|
|
const keyId = actor.url
|
2018-01-25 08:05:18 -06:00
|
|
|
return {
|
2018-10-19 04:41:19 -05:00
|
|
|
algorithm: HTTP_SIGNATURE.ALGORITHM,
|
|
|
|
authorizationHeaderName: HTTP_SIGNATURE.HEADER_NAME,
|
2018-01-25 08:05:18 -06:00
|
|
|
keyId,
|
2018-10-10 01:51:58 -05:00
|
|
|
key: actor.privateKey,
|
2018-10-19 04:41:19 -05:00
|
|
|
headers: HTTP_SIGNATURE.HEADERS_TO_SIGN
|
2018-10-10 01:51:58 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-23 04:38:48 -05:00
|
|
|
function buildGlobalHeaders (body: any) {
|
2018-10-10 01:51:58 -05:00
|
|
|
return {
|
2021-03-10 04:17:20 -06:00
|
|
|
'digest': buildDigest(body),
|
|
|
|
'content-type': 'application/activity+json',
|
|
|
|
'accept': ACTIVITY_PUB.ACCEPT_HEADER
|
2018-01-25 08:05:18 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-10 05:01:21 -06:00
|
|
|
async function signAndContextify <T> (byActor: MActor, data: T, contextType: ContextType | null) {
|
2022-03-23 10:14:33 -05:00
|
|
|
const activity = contextType
|
2023-03-10 05:01:21 -06:00
|
|
|
? await activityPubContextify(data, contextType)
|
2022-03-23 10:14:33 -05:00
|
|
|
: data
|
|
|
|
|
|
|
|
return signJsonLDObject(byActor, activity)
|
|
|
|
}
|
|
|
|
|
2018-01-25 08:05:18 -06:00
|
|
|
export {
|
2018-10-10 01:51:58 -05:00
|
|
|
buildGlobalHeaders,
|
2018-01-25 08:05:18 -06:00
|
|
|
computeBody,
|
2022-03-23 10:14:33 -05:00
|
|
|
buildSignedRequestOptions,
|
|
|
|
signAndContextify
|
2018-01-25 08:05:18 -06:00
|
|
|
}
|