]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/job-queue/handlers/utils/activitypub-http-utils.ts
Split types and typings
[github/Chocobozzz/PeerTube.git] / server / lib / job-queue / handlers / utils / activitypub-http-utils.ts
CommitLineData
e307e4fc 1import { buildSignedActivity } from '../../../../helpers/activitypub'
94a5ff8a 2import { ActorModel } from '../../../../models/activitypub/actor'
84ebcf34 3import { ACTIVITY_PUB, HTTP_SIGNATURE } from '../../../../initializers/constants'
26d6bf65 4import { MActor } from '../../../../types/models'
8dc8a34e
C
5import { getServerActor } from '@server/models/application/application'
6import { buildDigest } from '@server/helpers/peertube-crypto'
e307e4fc 7import { ContextType } from '@shared/models/activitypub/context'
94a5ff8a 8
598edb8a 9type Payload = { body: any, contextType?: ContextType, signatureActorId?: number }
729bb184
C
10
11async function computeBody (payload: Payload) {
94a5ff8a
C
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.')
598edb8a 17 body = await buildSignedActivity(actorSignature, payload.body, payload.contextType)
94a5ff8a
C
18 }
19
20 return body
21}
22
729bb184 23async function buildSignedRequestOptions (payload: Payload) {
453e83ea
C
24 let actor: MActor | null
25
94a5ff8a
C
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
c28bcdd1 34 const keyId = actor.url
94a5ff8a 35 return {
41f2ebae
C
36 algorithm: HTTP_SIGNATURE.ALGORITHM,
37 authorizationHeaderName: HTTP_SIGNATURE.HEADER_NAME,
94a5ff8a 38 keyId,
729bb184 39 key: actor.privateKey,
41f2ebae 40 headers: HTTP_SIGNATURE.HEADERS_TO_SIGN
729bb184
C
41 }
42}
43
df66d815 44function buildGlobalHeaders (body: any) {
729bb184 45 return {
84ebcf34
C
46 'Digest': buildDigest(body),
47 'Content-Type': 'application/activity+json',
48 'Accept': ACTIVITY_PUB.ACCEPT_HEADER
94a5ff8a
C
49 }
50}
51
52export {
729bb184 53 buildGlobalHeaders,
94a5ff8a
C
54 computeBody,
55 buildSignedRequestOptions
56}