2018-01-25 08:05:18 -06:00
|
|
|
import { buildSignedActivity } from '../../../../helpers/activitypub'
|
|
|
|
import { getServerActor } from '../../../../helpers/utils'
|
|
|
|
import { ActorModel } from '../../../../models/activitypub/actor'
|
2018-10-10 01:51:58 -05:00
|
|
|
import { sha256 } from '../../../../helpers/core-utils'
|
2018-01-25 08:05:18 -06:00
|
|
|
|
2018-10-10 01:51:58 -05:00
|
|
|
type Payload = { body: any, signatureActorId?: number }
|
|
|
|
|
|
|
|
async function computeBody (payload: Payload) {
|
2018-01-25 08:05:18 -06:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
return body
|
|
|
|
}
|
|
|
|
|
2018-10-10 01:51:58 -05:00
|
|
|
async function buildSignedRequestOptions (payload: Payload) {
|
2018-07-25 15:01:25 -05:00
|
|
|
let actor: ActorModel | null
|
2018-01-25 08:05:18 -06: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()
|
|
|
|
}
|
|
|
|
|
|
|
|
const keyId = actor.getWebfingerUrl()
|
|
|
|
return {
|
|
|
|
algorithm: 'rsa-sha256',
|
|
|
|
authorizationHeaderName: 'Signature',
|
|
|
|
keyId,
|
2018-10-10 01:51:58 -05:00
|
|
|
key: actor.privateKey,
|
|
|
|
headers: [ 'date', 'host', 'digest', '(request-target)' ]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function buildGlobalHeaders (body: object) {
|
|
|
|
const digest = 'SHA-256=' + sha256(JSON.stringify(body), 'base64')
|
|
|
|
|
|
|
|
return {
|
|
|
|
'Digest': digest
|
2018-01-25 08:05:18 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export {
|
2018-10-10 01:51:58 -05:00
|
|
|
buildGlobalHeaders,
|
2018-01-25 08:05:18 -06:00
|
|
|
computeBody,
|
|
|
|
buildSignedRequestOptions
|
|
|
|
}
|