1
0
Fork 0
peertube/server/lib/job-queue/handlers/utils/activitypub-http-utils.ts

57 lines
1.7 KiB
TypeScript
Raw Normal View History

import { buildSignedActivity } from '../../../../helpers/activitypub'
import { ActorModel } from '../../../../models/activitypub/actor'
2020-03-12 09:14:00 -04:00
import { ACTIVITY_PUB, HTTP_SIGNATURE } from '../../../../initializers/constants'
2019-08-15 05:53:26 -04:00
import { MActor } from '../../../../typings/models'
2020-04-23 03:32:53 -04:00
import { getServerActor } from '@server/models/application/application'
import { buildDigest } from '@server/helpers/peertube-crypto'
import { ContextType } from '@shared/models/activitypub/context'
type Payload = { body: any, contextType?: ContextType, signatureActorId?: number }
2018-10-10 02:51:58 -04:00
async function computeBody (payload: Payload) {
let body = payload.body
if (payload.signatureActorId) {
const actorSignature = await ActorModel.load(payload.signatureActorId)
if (!actorSignature) throw new Error('Unknown signature actor id.')
body = await buildSignedActivity(actorSignature, payload.body, payload.contextType)
}
return body
}
2018-10-10 02:51:58 -04:00
async function buildSignedRequestOptions (payload: Payload) {
2019-08-15 05:53:26 -04:00
let actor: MActor | null
if (payload.signatureActorId) {
actor = await ActorModel.load(payload.signatureActorId)
if (!actor) throw new Error('Unknown signature actor id.')
} else {
// We need to sign the request, so use the server
actor = await getServerActor()
}
2019-04-25 09:19:53 -04:00
const keyId = actor.url
return {
algorithm: HTTP_SIGNATURE.ALGORITHM,
authorizationHeaderName: HTTP_SIGNATURE.HEADER_NAME,
keyId,
2018-10-10 02:51:58 -04:00
key: actor.privateKey,
headers: HTTP_SIGNATURE.HEADERS_TO_SIGN
2018-10-10 02:51:58 -04:00
}
}
function buildGlobalHeaders (body: any) {
2018-10-10 02:51:58 -04:00
return {
2020-03-12 09:14:00 -04:00
'Digest': buildDigest(body),
'Content-Type': 'application/activity+json',
'Accept': ACTIVITY_PUB.ACCEPT_HEADER
}
}
export {
2018-10-10 02:51:58 -04:00
buildGlobalHeaders,
computeBody,
buildSignedRequestOptions
}