diff options
Diffstat (limited to 'server/lib/activitypub/send/http.ts')
-rw-r--r-- | server/lib/activitypub/send/http.ts | 73 |
1 files changed, 0 insertions, 73 deletions
diff --git a/server/lib/activitypub/send/http.ts b/server/lib/activitypub/send/http.ts deleted file mode 100644 index b461aa55d..000000000 --- a/server/lib/activitypub/send/http.ts +++ /dev/null | |||
@@ -1,73 +0,0 @@ | |||
1 | import { buildDigest, signJsonLDObject } from '@server/helpers/peertube-crypto' | ||
2 | import { ACTIVITY_PUB, HTTP_SIGNATURE } from '@server/initializers/constants' | ||
3 | import { ActorModel } from '@server/models/actor/actor' | ||
4 | import { getServerActor } from '@server/models/application/application' | ||
5 | import { MActor } from '@server/types/models' | ||
6 | import { ContextType } from '@shared/models/activitypub/context' | ||
7 | import { activityPubContextify } from '../context' | ||
8 | |||
9 | type Payload <T> = { body: T, contextType: ContextType, signatureActorId?: number } | ||
10 | |||
11 | async function computeBody <T> ( | ||
12 | payload: Payload<T> | ||
13 | ): Promise<T | T & { type: 'RsaSignature2017', creator: string, created: string }> { | ||
14 | let body = payload.body | ||
15 | |||
16 | if (payload.signatureActorId) { | ||
17 | const actorSignature = await ActorModel.load(payload.signatureActorId) | ||
18 | if (!actorSignature) throw new Error('Unknown signature actor id.') | ||
19 | |||
20 | body = await signAndContextify(actorSignature, payload.body, payload.contextType) | ||
21 | } | ||
22 | |||
23 | return body | ||
24 | } | ||
25 | |||
26 | async function buildSignedRequestOptions (options: { | ||
27 | signatureActorId?: number | ||
28 | hasPayload: boolean | ||
29 | }) { | ||
30 | let actor: MActor | null | ||
31 | |||
32 | if (options.signatureActorId) { | ||
33 | actor = await ActorModel.load(options.signatureActorId) | ||
34 | if (!actor) throw new Error('Unknown signature actor id.') | ||
35 | } else { | ||
36 | // We need to sign the request, so use the server | ||
37 | actor = await getServerActor() | ||
38 | } | ||
39 | |||
40 | const keyId = actor.url | ||
41 | return { | ||
42 | algorithm: HTTP_SIGNATURE.ALGORITHM, | ||
43 | authorizationHeaderName: HTTP_SIGNATURE.HEADER_NAME, | ||
44 | keyId, | ||
45 | key: actor.privateKey, | ||
46 | headers: options.hasPayload | ||
47 | ? HTTP_SIGNATURE.HEADERS_TO_SIGN_WITH_PAYLOAD | ||
48 | : HTTP_SIGNATURE.HEADERS_TO_SIGN_WITHOUT_PAYLOAD | ||
49 | } | ||
50 | } | ||
51 | |||
52 | function buildGlobalHeaders (body: any) { | ||
53 | return { | ||
54 | 'digest': buildDigest(body), | ||
55 | 'content-type': 'application/activity+json', | ||
56 | 'accept': ACTIVITY_PUB.ACCEPT_HEADER | ||
57 | } | ||
58 | } | ||
59 | |||
60 | async function signAndContextify <T> (byActor: MActor, data: T, contextType: ContextType | null) { | ||
61 | const activity = contextType | ||
62 | ? await activityPubContextify(data, contextType) | ||
63 | : data | ||
64 | |||
65 | return signJsonLDObject(byActor, activity) | ||
66 | } | ||
67 | |||
68 | export { | ||
69 | buildGlobalHeaders, | ||
70 | computeBody, | ||
71 | buildSignedRequestOptions, | ||
72 | signAndContextify | ||
73 | } | ||