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 { isSignatureVerified } from '../helpers/peertube-crypto'
6 import { ACCEPT_HEADERS, ACTIVITY_PUB } from '../initializers'
7 import { getOrCreateActorAndServerAndModel } from '../lib/activitypub'
8 import { ActorModel } from '../models/activitypub/actor'
10 async function checkSignature (req: Request, res: Response, next: NextFunction) {
11 const signatureObject: ActivityPubSignature = req.body.signature
13 const [ creator ] = signatureObject.creator.split('#')
15 logger.debug('Checking signature of actor %s...', creator)
19 actor = await getOrCreateActorAndServerAndModel(creator)
21 logger.error('Cannot create remote actor and check signature.', { err })
22 return res.sendStatus(403)
25 const verified = await isSignatureVerified(actor, req.body)
26 if (verified === false) return res.sendStatus(403)
28 res.locals.signature = {
35 function executeIfActivityPub (fun: RequestHandler | RequestHandler[]) {
36 return (req: Request, res: Response, next: NextFunction) => {
37 const accepted = req.accepts(ACCEPT_HEADERS)
38 if (accepted === false || ACTIVITY_PUB.POTENTIAL_ACCEPT_HEADERS.indexOf(accepted) === -1) {
42 logger.debug('ActivityPub request for %s.', req.url)
44 if (Array.isArray(fun) === true) {
45 return eachSeries(fun as RequestHandler[], (f, cb) => {
50 return (fun as RequestHandler)(req, res, next)
54 // ---------------------------------------------------------------------------