2020-04-24 11:33:01 +02:00
|
|
|
import { buildSignedActivity } from '../../../../helpers/activitypub'
|
2018-01-25 15:05:18 +01:00
|
|
|
import { ActorModel } from '../../../../models/activitypub/actor'
|
2020-03-12 14:14:00 +01:00
|
|
|
import { ACTIVITY_PUB, HTTP_SIGNATURE } from '../../../../initializers/constants'
|
2020-06-18 10:45:25 +02:00
|
|
|
import { MActor } from '../../../../types/models'
|
2020-04-23 09:32:53 +02:00
|
|
|
import { getServerActor } from '@server/models/application/application'
|
|
|
|
import { buildDigest } from '@server/helpers/peertube-crypto'
|
2020-04-24 11:33:01 +02:00
|
|
|
import { ContextType } from '@shared/models/activitypub/context'
|
2018-01-25 15:05:18 +01:00
|
|
|
|
2020-02-03 11:12:29 +01:00
|
|
|
type Payload = { body: any, contextType?: ContextType, signatureActorId?: number }
|
2018-10-10 08:51:58 +02:00
|
|
|
|
|
|
|
async function computeBody (payload: Payload) {
|
2018-01-25 15:05:18 +01:00
|
|
|
let body = payload.body
|
|
|
|
|
|
|
|
if (payload.signatureActorId) {
|
|
|
|
const actorSignature = await ActorModel.load(payload.signatureActorId)
|
|
|
|
if (!actorSignature) throw new Error('Unknown signature actor id.')
|
2020-02-03 11:12:29 +01:00
|
|
|
body = await buildSignedActivity(actorSignature, payload.body, payload.contextType)
|
2018-01-25 15:05:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return body
|
|
|
|
}
|
|
|
|
|
2018-10-10 08:51:58 +02:00
|
|
|
async function buildSignedRequestOptions (payload: Payload) {
|
2019-08-15 11:53:26 +02:00
|
|
|
let actor: MActor | null
|
|
|
|
|
2018-01-25 15:05:18 +01:00
|
|
|
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 15:19:53 +02:00
|
|
|
const keyId = actor.url
|
2018-01-25 15:05:18 +01:00
|
|
|
return {
|
2018-10-19 11:41:19 +02:00
|
|
|
algorithm: HTTP_SIGNATURE.ALGORITHM,
|
|
|
|
authorizationHeaderName: HTTP_SIGNATURE.HEADER_NAME,
|
2018-01-25 15:05:18 +01:00
|
|
|
keyId,
|
2018-10-10 08:51:58 +02:00
|
|
|
key: actor.privateKey,
|
2018-10-19 11:41:19 +02:00
|
|
|
headers: HTTP_SIGNATURE.HEADERS_TO_SIGN
|
2018-10-10 08:51:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-23 11:38:48 +02:00
|
|
|
function buildGlobalHeaders (body: any) {
|
2018-10-10 08:51:58 +02:00
|
|
|
return {
|
2020-03-12 14:14:00 +01:00
|
|
|
'Digest': buildDigest(body),
|
|
|
|
'Content-Type': 'application/activity+json',
|
|
|
|
'Accept': ACTIVITY_PUB.ACCEPT_HEADER
|
2018-01-25 15:05:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export {
|
2018-10-10 08:51:58 +02:00
|
|
|
buildGlobalHeaders,
|
2018-01-25 15:05:18 +01:00
|
|
|
computeBody,
|
|
|
|
buildSignedRequestOptions
|
|
|
|
}
|