]>
Commit | Line | Data |
---|---|---|
598edb8a | 1 | import { buildSignedActivity, ContextType } from '../../../../helpers/activitypub' |
94a5ff8a | 2 | import { ActorModel } from '../../../../models/activitypub/actor' |
84ebcf34 | 3 | import { ACTIVITY_PUB, HTTP_SIGNATURE } from '../../../../initializers/constants' |
453e83ea | 4 | import { MActor } from '../../../../typings/models' |
8dc8a34e C |
5 | import { getServerActor } from '@server/models/application/application' |
6 | import { buildDigest } from '@server/helpers/peertube-crypto' | |
94a5ff8a | 7 | |
598edb8a | 8 | type Payload = { body: any, contextType?: ContextType, signatureActorId?: number } |
729bb184 C |
9 | |
10 | async function computeBody (payload: Payload) { | |
94a5ff8a C |
11 | let body = payload.body |
12 | ||
13 | if (payload.signatureActorId) { | |
14 | const actorSignature = await ActorModel.load(payload.signatureActorId) | |
15 | if (!actorSignature) throw new Error('Unknown signature actor id.') | |
598edb8a | 16 | body = await buildSignedActivity(actorSignature, payload.body, payload.contextType) |
94a5ff8a C |
17 | } |
18 | ||
19 | return body | |
20 | } | |
21 | ||
729bb184 | 22 | async function buildSignedRequestOptions (payload: Payload) { |
453e83ea C |
23 | let actor: MActor | null |
24 | ||
94a5ff8a C |
25 | if (payload.signatureActorId) { |
26 | actor = await ActorModel.load(payload.signatureActorId) | |
27 | if (!actor) throw new Error('Unknown signature actor id.') | |
28 | } else { | |
29 | // We need to sign the request, so use the server | |
30 | actor = await getServerActor() | |
31 | } | |
32 | ||
c28bcdd1 | 33 | const keyId = actor.url |
94a5ff8a | 34 | return { |
41f2ebae C |
35 | algorithm: HTTP_SIGNATURE.ALGORITHM, |
36 | authorizationHeaderName: HTTP_SIGNATURE.HEADER_NAME, | |
94a5ff8a | 37 | keyId, |
729bb184 | 38 | key: actor.privateKey, |
41f2ebae | 39 | headers: HTTP_SIGNATURE.HEADERS_TO_SIGN |
729bb184 C |
40 | } |
41 | } | |
42 | ||
df66d815 | 43 | function buildGlobalHeaders (body: any) { |
729bb184 | 44 | return { |
84ebcf34 C |
45 | 'Digest': buildDigest(body), |
46 | 'Content-Type': 'application/activity+json', | |
47 | 'Accept': ACTIVITY_PUB.ACCEPT_HEADER | |
94a5ff8a C |
48 | } |
49 | } | |
50 | ||
51 | export { | |
729bb184 | 52 | buildGlobalHeaders, |
94a5ff8a C |
53 | computeBody, |
54 | buildSignedRequestOptions | |
55 | } |