diff options
Diffstat (limited to 'server/lib/jobs/activitypub-http-job-scheduler')
3 files changed, 31 insertions, 7 deletions
diff --git a/server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-broadcast-handler.ts b/server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-broadcast-handler.ts index 8040dde2a..3c4d5556f 100644 --- a/server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-broadcast-handler.ts +++ b/server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-broadcast-handler.ts | |||
@@ -1,15 +1,17 @@ | |||
1 | import { doRequest, logger } from '../../../helpers' | 1 | import { doRequest, logger } from '../../../helpers' |
2 | import { ActivityPubHttpPayload, computeBody, maybeRetryRequestLater } from './activitypub-http-job-scheduler' | 2 | import { ActivityPubHttpPayload, buildSignedRequestOptions, computeBody, maybeRetryRequestLater } from './activitypub-http-job-scheduler' |
3 | 3 | ||
4 | async function process (payload: ActivityPubHttpPayload, jobId: number) { | 4 | async function process (payload: ActivityPubHttpPayload, jobId: number) { |
5 | logger.info('Processing ActivityPub broadcast in job %d.', jobId) | 5 | logger.info('Processing ActivityPub broadcast in job %d.', jobId) |
6 | 6 | ||
7 | const body = await computeBody(payload) | 7 | const body = await computeBody(payload) |
8 | const httpSignatureOptions = await buildSignedRequestOptions(payload) | ||
8 | 9 | ||
9 | const options = { | 10 | const options = { |
10 | method: 'POST', | 11 | method: 'POST', |
11 | uri: '', | 12 | uri: '', |
12 | json: body | 13 | json: body, |
14 | httpSignature: httpSignatureOptions | ||
13 | } | 15 | } |
14 | 16 | ||
15 | for (const uri of payload.uris) { | 17 | for (const uri of payload.uris) { |
diff --git a/server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-job-scheduler.ts b/server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-job-scheduler.ts index 95a5d3ff2..88885cf97 100644 --- a/server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-job-scheduler.ts +++ b/server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-job-scheduler.ts | |||
@@ -1,5 +1,5 @@ | |||
1 | import { JobCategory } from '../../../../shared' | 1 | import { JobCategory } from '../../../../shared' |
2 | import { buildSignedActivity, logger } from '../../../helpers' | 2 | import { buildSignedActivity, getServerActor, logger } from '../../../helpers' |
3 | import { ACTIVITY_PUB } from '../../../initializers' | 3 | import { ACTIVITY_PUB } from '../../../initializers' |
4 | import { ActorModel } from '../../../models/activitypub/actor' | 4 | import { ActorModel } from '../../../models/activitypub/actor' |
5 | import { JobHandler, JobScheduler } from '../job-scheduler' | 5 | import { JobHandler, JobScheduler } from '../job-scheduler' |
@@ -46,16 +46,36 @@ async function computeBody (payload: ActivityPubHttpPayload) { | |||
46 | 46 | ||
47 | if (payload.signatureActorId) { | 47 | if (payload.signatureActorId) { |
48 | const actorSignature = await ActorModel.load(payload.signatureActorId) | 48 | const actorSignature = await ActorModel.load(payload.signatureActorId) |
49 | if (!actorSignature) throw new Error('Unknown signature account id.') | 49 | if (!actorSignature) throw new Error('Unknown signature actor id.') |
50 | body = await buildSignedActivity(actorSignature, payload.body) | 50 | body = await buildSignedActivity(actorSignature, payload.body) |
51 | } | 51 | } |
52 | 52 | ||
53 | return body | 53 | return body |
54 | } | 54 | } |
55 | 55 | ||
56 | async function buildSignedRequestOptions (payload: ActivityPubHttpPayload) { | ||
57 | let actor: ActorModel | ||
58 | if (payload.signatureActorId) { | ||
59 | actor = await ActorModel.load(payload.signatureActorId) | ||
60 | if (!actor) throw new Error('Unknown signature actor id.') | ||
61 | } else { | ||
62 | // We need to sign the request, so use the server | ||
63 | actor = await getServerActor() | ||
64 | } | ||
65 | |||
66 | const keyId = actor.getWebfingerUrl() | ||
67 | return { | ||
68 | algorithm: 'rsa-sha256', | ||
69 | authorizationHeaderName: 'Signature', | ||
70 | keyId, | ||
71 | key: actor.privateKey | ||
72 | } | ||
73 | } | ||
74 | |||
56 | export { | 75 | export { |
57 | ActivityPubHttpPayload, | 76 | ActivityPubHttpPayload, |
58 | activitypubHttpJobScheduler, | 77 | activitypubHttpJobScheduler, |
59 | maybeRetryRequestLater, | 78 | maybeRetryRequestLater, |
60 | computeBody | 79 | computeBody, |
80 | buildSignedRequestOptions | ||
61 | } | 81 | } |
diff --git a/server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-unicast-handler.ts b/server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-unicast-handler.ts index f16cfcec3..7a5caa679 100644 --- a/server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-unicast-handler.ts +++ b/server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-unicast-handler.ts | |||
@@ -1,16 +1,18 @@ | |||
1 | import { doRequest, logger } from '../../../helpers' | 1 | import { doRequest, logger } from '../../../helpers' |
2 | import { ActivityPubHttpPayload, computeBody, maybeRetryRequestLater } from './activitypub-http-job-scheduler' | 2 | import { ActivityPubHttpPayload, buildSignedRequestOptions, computeBody, maybeRetryRequestLater } from './activitypub-http-job-scheduler' |
3 | 3 | ||
4 | async function process (payload: ActivityPubHttpPayload, jobId: number) { | 4 | async function process (payload: ActivityPubHttpPayload, jobId: number) { |
5 | logger.info('Processing ActivityPub unicast in job %d.', jobId) | 5 | logger.info('Processing ActivityPub unicast in job %d.', jobId) |
6 | 6 | ||
7 | const body = await computeBody(payload) | 7 | const body = await computeBody(payload) |
8 | const httpSignatureOptions = await buildSignedRequestOptions(payload) | ||
8 | 9 | ||
9 | const uri = payload.uris[0] | 10 | const uri = payload.uris[0] |
10 | const options = { | 11 | const options = { |
11 | method: 'POST', | 12 | method: 'POST', |
12 | uri, | 13 | uri, |
13 | json: body | 14 | json: body, |
15 | httpSignature: httpSignatureOptions | ||
14 | } | 16 | } |
15 | 17 | ||
16 | try { | 18 | try { |