1 import { buildSignedActivity } from '../../../../helpers/activitypub'
2 import { ActorModel } from '../../../../models/activitypub/actor'
3 import { ACTIVITY_PUB, HTTP_SIGNATURE } from '../../../../initializers/constants'
4 import { MActor } from '../../../../typings/models'
5 import { getServerActor } from '@server/models/application/application'
6 import { buildDigest } from '@server/helpers/peertube-crypto'
7 import { ContextType } from '@shared/models/activitypub/context'
9 type Payload = { body: any, contextType?: ContextType, signatureActorId?: number }
11 async function computeBody (payload: Payload) {
12 let body = payload.body
14 if (payload.signatureActorId) {
15 const actorSignature = await ActorModel.load(payload.signatureActorId)
16 if (!actorSignature) throw new Error('Unknown signature actor id.')
17 body = await buildSignedActivity(actorSignature, payload.body, payload.contextType)
23 async function buildSignedRequestOptions (payload: Payload) {
24 let actor: MActor | null
26 if (payload.signatureActorId) {
27 actor = await ActorModel.load(payload.signatureActorId)
28 if (!actor) throw new Error('Unknown signature actor id.')
30 // We need to sign the request, so use the server
31 actor = await getServerActor()
34 const keyId = actor.url
36 algorithm: HTTP_SIGNATURE.ALGORITHM,
37 authorizationHeaderName: HTTP_SIGNATURE.HEADER_NAME,
39 key: actor.privateKey,
40 headers: HTTP_SIGNATURE.HEADERS_TO_SIGN
44 function buildGlobalHeaders (body: any) {
46 'Digest': buildDigest(body),
47 'Content-Type': 'application/activity+json',
48 'Accept': ACTIVITY_PUB.ACCEPT_HEADER
55 buildSignedRequestOptions