]>
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 C |
5 | import { ACCEPT_HEADERS, ACTIVITY_PUB } from '../initializers' |
6 | import { fetchRemoteAccount, saveAccountAndServerIfNotExist } from '../lib/activitypub' | |
7 | import { AccountModel } from '../models/account/account' | |
e4f97bab C |
8 | |
9 | async function checkSignature (req: Request, res: Response, next: NextFunction) { | |
10 | const signatureObject: ActivityPubSignature = req.body.signature | |
11 | ||
12 | logger.debug('Checking signature of account %s...', signatureObject.creator) | |
13 | ||
3fd3ab2d | 14 | let account = await AccountModel.loadByUrl(signatureObject.creator) |
e4f97bab C |
15 | |
16 | // We don't have this account in our database, fetch it on remote | |
17 | if (!account) { | |
0f91ae62 | 18 | account = await fetchRemoteAccount(signatureObject.creator) |
e4f97bab | 19 | |
0f91ae62 | 20 | if (!account) { |
e4f97bab C |
21 | return res.sendStatus(403) |
22 | } | |
23 | ||
0f91ae62 C |
24 | // Save our new account and its server in database |
25 | await saveAccountAndServerIfNotExist(account) | |
e4f97bab C |
26 | } |
27 | ||
28 | const verified = await isSignatureVerified(account, req.body) | |
29 | if (verified === false) return res.sendStatus(403) | |
30 | ||
350e31d6 C |
31 | res.locals.signature = { |
32 | account | |
33 | } | |
e4f97bab C |
34 | |
35 | return next() | |
36 | } | |
37 | ||
350e31d6 | 38 | function executeIfActivityPub (fun: RequestHandler | RequestHandler[]) { |
e4f97bab | 39 | return (req: Request, res: Response, next: NextFunction) => { |
4f491371 C |
40 | const accepted = req.accepts(ACCEPT_HEADERS) |
41 | if (accepted === false || ACTIVITY_PUB.POTENTIAL_ACCEPT_HEADERS.indexOf(accepted) === -1) { | |
e4f97bab C |
42 | return next() |
43 | } | |
44 | ||
165cdc75 C |
45 | logger.debug('ActivityPub request for %s.', req.url) |
46 | ||
e4f97bab | 47 | if (Array.isArray(fun) === true) { |
350e31d6 C |
48 | return eachSeries(fun as RequestHandler[], (f, cb) => { |
49 | f(req, res, cb) | |
50 | }, next) | |
e4f97bab C |
51 | } |
52 | ||
350e31d6 | 53 | return (fun as RequestHandler)(req, res, next) |
e4f97bab C |
54 | } |
55 | } | |
56 | ||
57 | // --------------------------------------------------------------------------- | |
58 | ||
59 | export { | |
60 | checkSignature, | |
61 | executeIfActivityPub | |
62 | } |