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