]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/job-queue/handlers/utils/activitypub-http-utils.ts
Remove activitypub helper
[github/Chocobozzz/PeerTube.git] / server / lib / job-queue / handlers / utils / activitypub-http-utils.ts
CommitLineData
7d9ba5c0 1import { buildDigest } from '@server/helpers/peertube-crypto'
7e98a7df 2import { buildSignedActivity } from '@server/lib/activitypub/activity'
7d9ba5c0
C
3import { getServerActor } from '@server/models/application/application'
4import { ContextType } from '@shared/models/activitypub/context'
84ebcf34 5import { ACTIVITY_PUB, HTTP_SIGNATURE } from '../../../../initializers/constants'
7d9ba5c0 6import { ActorModel } from '../../../../models/actor/actor'
26d6bf65 7import { MActor } from '../../../../types/models'
94a5ff8a 8
db4b15f2 9type Payload <T> = { body: T, contextType?: ContextType, signatureActorId?: number }
729bb184 10
db4b15f2
C
11async 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 26async 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 47function 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
55export {
729bb184 56 buildGlobalHeaders,
94a5ff8a
C
57 computeBody,
58 buildSignedRequestOptions
59}