]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/activitypub.ts
Move config in its own file
[github/Chocobozzz/PeerTube.git] / server / middlewares / activitypub.ts
CommitLineData
e65c0c5b 1import { NextFunction, Request, Response } from 'express'
e4f97bab 2import { ActivityPubSignature } from '../../shared'
da854ddd 3import { logger } from '../helpers/logger'
41f2ebae
C
4import { isHTTPSignatureVerified, isJsonLDSignatureVerified, parseHTTPSignature } from '../helpers/peertube-crypto'
5import { ACCEPT_HEADERS, ACTIVITY_PUB, HTTP_SIGNATURE } from '../initializers'
50d6de9c 6import { getOrCreateActorAndServerAndModel } from '../lib/activitypub'
41f2ebae 7import { loadActorUrlOrGetFromWebfinger } from '../helpers/webfinger'
e4f97bab
C
8
9async function checkSignature (req: Request, res: Response, next: NextFunction) {
41f2ebae
C
10 try {
11 const httpSignatureChecked = await checkHttpSignature(req, res)
12 if (httpSignatureChecked !== true) return
e4f97bab 13
dae86118 14 const actor = res.locals.signature.actor
e12a0092 15
41f2ebae
C
16 // Forwarded activity
17 const bodyActor = req.body.actor
18 const bodyActorId = bodyActor && bodyActor.id ? bodyActor.id : bodyActor
19 if (bodyActorId && bodyActorId !== actor.url) {
20 const jsonLDSignatureChecked = await checkJsonLDSignature(req, res)
21 if (jsonLDSignatureChecked !== true) return
22 }
e4f97bab 23
41f2ebae 24 return next()
50d6de9c 25 } catch (err) {
41f2ebae 26 logger.error('Error in ActivityPub signature checker.', err)
50d6de9c 27 return res.sendStatus(403)
e4f97bab 28 }
e4f97bab
C
29}
30
e65c0c5b
C
31function executeIfActivityPub (req: Request, res: Response, next: NextFunction) {
32 const accepted = req.accepts(ACCEPT_HEADERS)
33 if (accepted === false || ACTIVITY_PUB.POTENTIAL_ACCEPT_HEADERS.indexOf(accepted) === -1) {
34 // Bypass this route
35 return next('route')
36 }
e4f97bab 37
e65c0c5b 38 logger.debug('ActivityPub request for %s.', req.url)
165cdc75 39
e65c0c5b 40 return next()
e4f97bab
C
41}
42
43// ---------------------------------------------------------------------------
44
45export {
46 checkSignature,
df66d815
C
47 executeIfActivityPub,
48 checkHttpSignature
e4f97bab 49}
41f2ebae
C
50
51// ---------------------------------------------------------------------------
52
53async function checkHttpSignature (req: Request, res: Response) {
54 // FIXME: mastodon does not include the Signature scheme
55 const sig = req.headers[HTTP_SIGNATURE.HEADER_NAME] as string
56 if (sig && sig.startsWith('Signature ') === false) req.headers[HTTP_SIGNATURE.HEADER_NAME] = 'Signature ' + sig
57
58 const parsed = parseHTTPSignature(req)
59
60 const keyId = parsed.keyId
61 if (!keyId) {
62 res.sendStatus(403)
63 return false
64 }
65
66 logger.debug('Checking HTTP signature of actor %s...', keyId)
67
68 let [ actorUrl ] = keyId.split('#')
69 if (actorUrl.startsWith('acct:')) {
70 actorUrl = await loadActorUrlOrGetFromWebfinger(actorUrl.replace(/^acct:/, ''))
71 }
72
73 const actor = await getOrCreateActorAndServerAndModel(actorUrl)
74
75 const verified = isHTTPSignatureVerified(parsed, actor)
76 if (verified !== true) {
77 res.sendStatus(403)
78 return false
79 }
80
81 res.locals.signature = { actor }
82
83 return true
84}
85
86async function checkJsonLDSignature (req: Request, res: Response) {
87 const signatureObject: ActivityPubSignature = req.body.signature
88
df66d815 89 if (!signatureObject || !signatureObject.creator) {
41f2ebae
C
90 res.sendStatus(403)
91 return false
92 }
93
94 const [ creator ] = signatureObject.creator.split('#')
95
96 logger.debug('Checking JsonLD signature of actor %s...', creator)
97
98 const actor = await getOrCreateActorAndServerAndModel(creator)
99 const verified = await isJsonLDSignatureVerified(actor, req.body)
100
101 if (verified !== true) {
102 res.sendStatus(403)
103 return false
104 }
105
106 res.locals.signature = { actor }
107
108 return true
109}