]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/job-queue/handlers/utils/activitypub-http-utils.ts
Merge branch 'release/1.4.0' into develop
[github/Chocobozzz/PeerTube.git] / server / lib / job-queue / handlers / utils / activitypub-http-utils.ts
index fd9c743413daa42fa8a7af747ea715355e5d4bb4..d3bde6e6a25f2d57897d02e1dcc4ce6b7f79faba 100644 (file)
@@ -2,7 +2,8 @@ import { buildSignedActivity } from '../../../../helpers/activitypub'
 import { getServerActor } from '../../../../helpers/utils'
 import { ActorModel } from '../../../../models/activitypub/actor'
 import { sha256 } from '../../../../helpers/core-utils'
-import { HTTP_SIGNATURE } from '../../../../initializers'
+import { HTTP_SIGNATURE } from '../../../../initializers/constants'
+import { MActor } from '../../../../typings/models'
 
 type Payload = { body: any, signatureActorId?: number }
 
@@ -19,7 +20,8 @@ async function computeBody (payload: Payload) {
 }
 
 async function buildSignedRequestOptions (payload: Payload) {
-  let actor: ActorModel | null
+  let actor: MActor | null
+
   if (payload.signatureActorId) {
     actor = await ActorModel.load(payload.signatureActorId)
     if (!actor) throw new Error('Unknown signature actor id.')
@@ -28,7 +30,7 @@ async function buildSignedRequestOptions (payload: Payload) {
     actor = await getServerActor()
   }
 
-  const keyId = actor.getWebfingerUrl()
+  const keyId = actor.url
   return {
     algorithm: HTTP_SIGNATURE.ALGORITHM,
     authorizationHeaderName: HTTP_SIGNATURE.HEADER_NAME,
@@ -38,15 +40,20 @@ async function buildSignedRequestOptions (payload: Payload) {
   }
 }
 
-function buildGlobalHeaders (body: object) {
-  const digest = 'SHA-256=' + sha256(JSON.stringify(body), 'base64')
-
+function buildGlobalHeaders (body: any) {
   return {
-    'Digest': digest
+    'Digest': buildDigest(body)
   }
 }
 
+function buildDigest (body: any) {
+  const rawBody = typeof body === 'string' ? body : JSON.stringify(body)
+
+  return 'SHA-256=' + sha256(rawBody, 'base64')
+}
+
 export {
+  buildDigest,
   buildGlobalHeaders,
   computeBody,
   buildSignedRequestOptions