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