2017-11-17 15:20:42 +01:00
|
|
|
import { eachSeries } from 'async'
|
|
|
|
import { NextFunction, Request, RequestHandler, Response } from 'express'
|
2017-11-09 17:51:58 +01:00
|
|
|
import { ActivityPubSignature } from '../../shared'
|
2017-12-28 11:16:08 +01:00
|
|
|
import { logger } from '../helpers/logger'
|
|
|
|
import { isSignatureVerified } from '../helpers/peertube-crypto'
|
2017-12-12 17:53:50 +01:00
|
|
|
import { ACCEPT_HEADERS, ACTIVITY_PUB } from '../initializers'
|
2017-12-14 17:38:41 +01:00
|
|
|
import { getOrCreateActorAndServerAndModel } from '../lib/activitypub'
|
|
|
|
import { ActorModel } from '../models/activitypub/actor'
|
2017-11-09 17:51:58 +01:00
|
|
|
|
|
|
|
async function checkSignature (req: Request, res: Response, next: NextFunction) {
|
|
|
|
const signatureObject: ActivityPubSignature = req.body.signature
|
|
|
|
|
2017-12-19 10:34:56 +01:00
|
|
|
const [ creator ] = signatureObject.creator.split('#')
|
|
|
|
|
|
|
|
logger.debug('Checking signature of actor %s...', creator)
|
2017-11-09 17:51:58 +01:00
|
|
|
|
2017-12-14 17:38:41 +01:00
|
|
|
let actor: ActorModel
|
|
|
|
try {
|
2017-12-19 10:34:56 +01:00
|
|
|
actor = await getOrCreateActorAndServerAndModel(creator)
|
2017-12-14 17:38:41 +01:00
|
|
|
} catch (err) {
|
2018-10-01 15:18:07 +02:00
|
|
|
logger.warn('Cannot create remote actor %s and check signature.', creator, { err })
|
2017-12-14 17:38:41 +01:00
|
|
|
return res.sendStatus(403)
|
2017-11-09 17:51:58 +01:00
|
|
|
}
|
|
|
|
|
2017-12-14 17:38:41 +01:00
|
|
|
const verified = await isSignatureVerified(actor, req.body)
|
2017-11-09 17:51:58 +01:00
|
|
|
if (verified === false) return res.sendStatus(403)
|
|
|
|
|
2017-11-14 17:31:26 +01:00
|
|
|
res.locals.signature = {
|
2017-12-14 17:38:41 +01:00
|
|
|
actor
|
2017-11-14 17:31:26 +01:00
|
|
|
}
|
2017-11-09 17:51:58 +01:00
|
|
|
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
|
2017-11-14 17:31:26 +01:00
|
|
|
function executeIfActivityPub (fun: RequestHandler | RequestHandler[]) {
|
2017-11-09 17:51:58 +01:00
|
|
|
return (req: Request, res: Response, next: NextFunction) => {
|
2017-11-30 13:37:11 +01:00
|
|
|
const accepted = req.accepts(ACCEPT_HEADERS)
|
|
|
|
if (accepted === false || ACTIVITY_PUB.POTENTIAL_ACCEPT_HEADERS.indexOf(accepted) === -1) {
|
2017-11-09 17:51:58 +01:00
|
|
|
return next()
|
|
|
|
}
|
|
|
|
|
2017-11-29 11:34:44 +01:00
|
|
|
logger.debug('ActivityPub request for %s.', req.url)
|
|
|
|
|
2017-11-09 17:51:58 +01:00
|
|
|
if (Array.isArray(fun) === true) {
|
2017-11-14 17:31:26 +01:00
|
|
|
return eachSeries(fun as RequestHandler[], (f, cb) => {
|
|
|
|
f(req, res, cb)
|
|
|
|
}, next)
|
2017-11-09 17:51:58 +01:00
|
|
|
}
|
|
|
|
|
2017-11-14 17:31:26 +01:00
|
|
|
return (fun as RequestHandler)(req, res, next)
|
2017-11-09 17:51:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
checkSignature,
|
|
|
|
executeIfActivityPub
|
|
|
|
}
|