]>
Commit | Line | Data |
---|---|---|
1 | import { buildDigest } from '@server/helpers/peertube-crypto' | |
2 | import { getServerActor } from '@server/models/application/application' | |
3 | import { ContextType } from '@shared/models/activitypub/context' | |
4 | import { buildSignedActivity } from '../../../../helpers/activitypub' | |
5 | import { ACTIVITY_PUB, HTTP_SIGNATURE } from '../../../../initializers/constants' | |
6 | import { ActorModel } from '../../../../models/actor/actor' | |
7 | import { MActor } from '../../../../types/models' | |
8 | ||
9 | type Payload <T> = { body: T, contextType?: ContextType, signatureActorId?: number } | |
10 | ||
11 | async function computeBody <T> ( | |
12 | payload: Payload<T> | |
13 | ): Promise<T | T & { type: 'RsaSignature2017', creator: string, created: string }> { | |
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.') | |
19 | ||
20 | body = await buildSignedActivity(actorSignature, payload.body, payload.contextType) | |
21 | } | |
22 | ||
23 | return body | |
24 | } | |
25 | ||
26 | async function buildSignedRequestOptions (payload: Payload<any>) { | |
27 | let actor: MActor | null | |
28 | ||
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 | ||
37 | const keyId = actor.url | |
38 | return { | |
39 | algorithm: HTTP_SIGNATURE.ALGORITHM, | |
40 | authorizationHeaderName: HTTP_SIGNATURE.HEADER_NAME, | |
41 | keyId, | |
42 | key: actor.privateKey, | |
43 | headers: HTTP_SIGNATURE.HEADERS_TO_SIGN | |
44 | } | |
45 | } | |
46 | ||
47 | function buildGlobalHeaders (body: any) { | |
48 | return { | |
49 | 'digest': buildDigest(body), | |
50 | 'content-type': 'application/activity+json', | |
51 | 'accept': ACTIVITY_PUB.ACCEPT_HEADER | |
52 | } | |
53 | } | |
54 | ||
55 | export { | |
56 | buildGlobalHeaders, | |
57 | computeBody, | |
58 | buildSignedRequestOptions | |
59 | } |