1
0
Fork 0
mirror of https://github.com/Chocobozzz/PeerTube.git synced 2025-10-05 10:49:28 +02:00

Sign JSON objects in worker threads

This commit is contained in:
Chocobozzz 2023-10-24 10:45:17 +02:00
parent 9e2166a16f
commit edc3ff6085
No known key found for this signature in database
GPG key ID: 583A612D890159BE
12 changed files with 185 additions and 48 deletions

View file

@ -2,11 +2,14 @@ import { ContextType } from '@peertube/peertube-models'
import { ACTIVITY_PUB } from '@server/initializers/constants.js'
import { buildDigest, signJsonLDObject } from './peertube-crypto.js'
type ContextFilter = <T> (arg: T) => Promise<T>
export type ContextFilter = <T> (arg: T) => Promise<T>
export function buildGlobalHTTPHeaders (body: any) {
export function buildGlobalHTTPHeaders (
body: any,
digestBuilder: typeof buildDigest
) {
return {
'digest': buildDigest(body),
'digest': digestBuilder(body),
'content-type': 'application/activity+json',
'accept': ACTIVITY_PUB.ACCEPT_HEADER
}
@ -16,17 +19,20 @@ export async function activityPubContextify <T> (data: T, type: ContextType, con
return { ...await getContextData(type, contextFilter), ...data }
}
export async function signAndContextify <T> (
byActor: { url: string, privateKey: string },
data: T,
contextType: ContextType | null,
export async function signAndContextify <T> (options: {
byActor: { url: string, privateKey: string }
data: T
contextType: ContextType | null
contextFilter: ContextFilter
) {
signerFunction: typeof signJsonLDObject<T>
}) {
const { byActor, data, contextType, contextFilter, signerFunction } = options
const activity = contextType
? await activityPubContextify(data, contextType, contextFilter)
: data
return signJsonLDObject(byActor, activity)
return signerFunction({ byActor, data: activity })
}
// ---------------------------------------------------------------------------