]>
Commit | Line | Data |
---|---|---|
9a27cdc2 C |
1 | import { eachSeries } from 'async' |
2 | import { NextFunction, Request, RequestHandler, Response } from 'express' | |
e4f97bab | 3 | import { ActivityPubSignature } from '../../shared' |
350e31d6 | 4 | import { isSignatureVerified, logger } from '../helpers' |
3fd3ab2d | 5 | import { ACCEPT_HEADERS, ACTIVITY_PUB } from '../initializers' |
50d6de9c C |
6 | import { getOrCreateActorAndServerAndModel } from '../lib/activitypub' |
7 | import { ActorModel } from '../models/activitypub/actor' | |
e4f97bab C |
8 | |
9 | async function checkSignature (req: Request, res: Response, next: NextFunction) { | |
10 | const signatureObject: ActivityPubSignature = req.body.signature | |
11 | ||
50d6de9c | 12 | logger.debug('Checking signature of actor %s...', signatureObject.creator) |
e4f97bab | 13 | |
50d6de9c C |
14 | let actor: ActorModel |
15 | try { | |
16 | actor = await getOrCreateActorAndServerAndModel(signatureObject.creator) | |
17 | } catch (err) { | |
18 | logger.error('Cannot create remote actor and check signature.', err) | |
19 | return res.sendStatus(403) | |
e4f97bab C |
20 | } |
21 | ||
50d6de9c | 22 | const verified = await isSignatureVerified(actor, req.body) |
e4f97bab C |
23 | if (verified === false) return res.sendStatus(403) |
24 | ||
350e31d6 | 25 | res.locals.signature = { |
50d6de9c | 26 | actor |
350e31d6 | 27 | } |
e4f97bab C |
28 | |
29 | return next() | |
30 | } | |
31 | ||
350e31d6 | 32 | function executeIfActivityPub (fun: RequestHandler | RequestHandler[]) { |
e4f97bab | 33 | return (req: Request, res: Response, next: NextFunction) => { |
4f491371 C |
34 | const accepted = req.accepts(ACCEPT_HEADERS) |
35 | if (accepted === false || ACTIVITY_PUB.POTENTIAL_ACCEPT_HEADERS.indexOf(accepted) === -1) { | |
e4f97bab C |
36 | return next() |
37 | } | |
38 | ||
165cdc75 C |
39 | logger.debug('ActivityPub request for %s.', req.url) |
40 | ||
e4f97bab | 41 | if (Array.isArray(fun) === true) { |
350e31d6 C |
42 | return eachSeries(fun as RequestHandler[], (f, cb) => { |
43 | f(req, res, cb) | |
44 | }, next) | |
e4f97bab C |
45 | } |
46 | ||
350e31d6 | 47 | return (fun as RequestHandler)(req, res, next) |
e4f97bab C |
48 | } |
49 | } | |
50 | ||
51 | // --------------------------------------------------------------------------- | |
52 | ||
53 | export { | |
54 | checkSignature, | |
55 | executeIfActivityPub | |
56 | } |