1 import { eachSeries } from 'async'
2 import { NextFunction, Request, RequestHandler, Response } from 'express'
3 import { ActivityPubSignature } from '../../shared'
4 import { logger } from '../helpers/logger'
5 import { isHTTPSignatureVerified, isJsonLDSignatureVerified, parseHTTPSignature } from '../helpers/peertube-crypto'
6 import { ACCEPT_HEADERS, ACTIVITY_PUB, HTTP_SIGNATURE } from '../initializers'
7 import { getOrCreateActorAndServerAndModel } from '../lib/activitypub'
8 import { ActorModel } from '../models/activitypub/actor'
9 import { loadActorUrlOrGetFromWebfinger } from '../helpers/webfinger'
11 async function checkSignature (req: Request, res: Response, next: NextFunction) {
13 const httpSignatureChecked = await checkHttpSignature(req, res)
14 if (httpSignatureChecked !== true) return
16 const actor: ActorModel = res.locals.signature.actor
19 const bodyActor = req.body.actor
20 const bodyActorId = bodyActor && bodyActor.id ? bodyActor.id : bodyActor
21 if (bodyActorId && bodyActorId !== actor.url) {
22 const jsonLDSignatureChecked = await checkJsonLDSignature(req, res)
23 if (jsonLDSignatureChecked !== true) return
28 logger.error('Error in ActivityPub signature checker.', err)
29 return res.sendStatus(403)
33 function executeIfActivityPub (fun: RequestHandler | RequestHandler[]) {
34 return (req: Request, res: Response, next: NextFunction) => {
35 const accepted = req.accepts(ACCEPT_HEADERS)
36 if (accepted === false || ACTIVITY_PUB.POTENTIAL_ACCEPT_HEADERS.indexOf(accepted) === -1) {
40 logger.debug('ActivityPub request for %s.', req.url)
42 if (Array.isArray(fun) === true) {
43 return eachSeries(fun as RequestHandler[], (f, cb) => {
48 return (fun as RequestHandler)(req, res, next)
52 // ---------------------------------------------------------------------------
60 // ---------------------------------------------------------------------------
62 async function checkHttpSignature (req: Request, res: Response) {
63 // FIXME: mastodon does not include the Signature scheme
64 const sig = req.headers[HTTP_SIGNATURE.HEADER_NAME] as string
65 if (sig && sig.startsWith('Signature ') === false) req.headers[HTTP_SIGNATURE.HEADER_NAME] = 'Signature ' + sig
67 const parsed = parseHTTPSignature(req)
69 const keyId = parsed.keyId
75 logger.debug('Checking HTTP signature of actor %s...', keyId)
77 let [ actorUrl ] = keyId.split('#')
78 if (actorUrl.startsWith('acct:')) {
79 actorUrl = await loadActorUrlOrGetFromWebfinger(actorUrl.replace(/^acct:/, ''))
82 const actor = await getOrCreateActorAndServerAndModel(actorUrl)
84 const verified = isHTTPSignatureVerified(parsed, actor)
85 if (verified !== true) {
90 res.locals.signature = { actor }
95 async function checkJsonLDSignature (req: Request, res: Response) {
96 const signatureObject: ActivityPubSignature = req.body.signature
98 if (!signatureObject || !signatureObject.creator) {
103 const [ creator ] = signatureObject.creator.split('#')
105 logger.debug('Checking JsonLD signature of actor %s...', creator)
107 const actor = await getOrCreateActorAndServerAndModel(creator)
108 const verified = await isJsonLDSignatureVerified(actor, req.body)
110 if (verified !== true) {
115 res.locals.signature = { actor }