]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/job-queue/handlers/utils/activitypub-http-utils.ts
Fix control bar alignment
[github/Chocobozzz/PeerTube.git] / server / lib / job-queue / handlers / utils / activitypub-http-utils.ts
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 '../../../../types/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'
8
9 type Payload = { body: any, contextType?: ContextType, signatureActorId?: number }
10
11 async function computeBody (payload: Payload) {
12 let body = payload.body
13
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)
18 }
19
20 return body
21 }
22
23 async function buildSignedRequestOptions (payload: Payload) {
24 let actor: MActor | null
25
26 if (payload.signatureActorId) {
27 actor = await ActorModel.load(payload.signatureActorId)
28 if (!actor) throw new Error('Unknown signature actor id.')
29 } else {
30 // We need to sign the request, so use the server
31 actor = await getServerActor()
32 }
33
34 const keyId = actor.url
35 return {
36 algorithm: HTTP_SIGNATURE.ALGORITHM,
37 authorizationHeaderName: HTTP_SIGNATURE.HEADER_NAME,
38 keyId,
39 key: actor.privateKey,
40 headers: HTTP_SIGNATURE.HEADERS_TO_SIGN
41 }
42 }
43
44 function buildGlobalHeaders (body: any) {
45 return {
46 'Digest': buildDigest(body),
47 'Content-Type': 'application/activity+json',
48 'Accept': ACTIVITY_PUB.ACCEPT_HEADER
49 }
50 }
51
52 export {
53 buildGlobalHeaders,
54 computeBody,
55 buildSignedRequestOptions
56 }