]>
Commit | Line | Data |
---|---|---|
94a5ff8a C |
1 | import { buildSignedActivity } from '../../../../helpers/activitypub' |
2 | import { getServerActor } from '../../../../helpers/utils' | |
3 | import { ActorModel } from '../../../../models/activitypub/actor' | |
729bb184 | 4 | import { sha256 } from '../../../../helpers/core-utils' |
74dc3bca | 5 | import { HTTP_SIGNATURE } from '../../../../initializers/constants' |
94a5ff8a | 6 | |
729bb184 C |
7 | type Payload = { body: any, signatureActorId?: number } |
8 | ||
9 | async function computeBody (payload: Payload) { | |
94a5ff8a C |
10 | let body = payload.body |
11 | ||
12 | if (payload.signatureActorId) { | |
13 | const actorSignature = await ActorModel.load(payload.signatureActorId) | |
14 | if (!actorSignature) throw new Error('Unknown signature actor id.') | |
15 | body = await buildSignedActivity(actorSignature, payload.body) | |
16 | } | |
17 | ||
18 | return body | |
19 | } | |
20 | ||
729bb184 | 21 | async function buildSignedRequestOptions (payload: Payload) { |
c1e791ba | 22 | let actor: ActorModel | null |
94a5ff8a C |
23 | if (payload.signatureActorId) { |
24 | actor = await ActorModel.load(payload.signatureActorId) | |
25 | if (!actor) throw new Error('Unknown signature actor id.') | |
26 | } else { | |
27 | // We need to sign the request, so use the server | |
28 | actor = await getServerActor() | |
29 | } | |
30 | ||
c28bcdd1 | 31 | const keyId = actor.url |
94a5ff8a | 32 | return { |
41f2ebae C |
33 | algorithm: HTTP_SIGNATURE.ALGORITHM, |
34 | authorizationHeaderName: HTTP_SIGNATURE.HEADER_NAME, | |
94a5ff8a | 35 | keyId, |
729bb184 | 36 | key: actor.privateKey, |
41f2ebae | 37 | headers: HTTP_SIGNATURE.HEADERS_TO_SIGN |
729bb184 C |
38 | } |
39 | } | |
40 | ||
df66d815 | 41 | function buildGlobalHeaders (body: any) { |
729bb184 | 42 | return { |
df66d815 | 43 | 'Digest': buildDigest(body) |
94a5ff8a C |
44 | } |
45 | } | |
46 | ||
df66d815 C |
47 | function buildDigest (body: any) { |
48 | const rawBody = typeof body === 'string' ? body : JSON.stringify(body) | |
49 | ||
50 | return 'SHA-256=' + sha256(rawBody, 'base64') | |
51 | } | |
52 | ||
94a5ff8a | 53 | export { |
df66d815 | 54 | buildDigest, |
729bb184 | 55 | buildGlobalHeaders, |
94a5ff8a C |
56 | computeBody, |
57 | buildSignedRequestOptions | |
58 | } |