]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/secure.ts
Async signature and various fixes
[github/Chocobozzz/PeerTube.git] / server / middlewares / secure.ts
CommitLineData
69818c93
C
1import 'express-validator'
2import * as express from 'express'
3
e02643f3
C
4import { database as db } from '../initializers'
5import {
6 logger,
7 checkSignature as peertubeCryptoCheckSignature
8} from '../helpers'
a3ee6fa2 9
69818c93 10function checkSignature (req: express.Request, res: express.Response, next: express.NextFunction) {
49abbbbe 11 const host = req.body.signature.host
6fcd19ba
C
12 db.Pod.loadByHost(host)
13 .then(pod => {
14 if (pod === null) {
15 logger.error('Unknown pod %s.', host)
16 return res.sendStatus(403)
17 }
9f10b292 18
6fcd19ba 19 logger.debug('Checking signature from %s.', host)
9f10b292 20
6fcd19ba
C
21 let signatureShouldBe
22 // If there is data in the body the sender used it for its signature
23 // If there is no data we just use its host as signature
24 if (req.body.data) {
25 signatureShouldBe = req.body.data
26 } else {
27 signatureShouldBe = host
28 }
9f10b292 29
6fcd19ba 30 const signatureOk = peertubeCryptoCheckSignature(pod.publicKey, signatureShouldBe, req.body.signature.signature)
bdfbd4f1 31
6fcd19ba
C
32 if (signatureOk === true) {
33 res.locals.secure = {
34 pod
35 }
9f10b292 36
6fcd19ba 37 return next()
4ff0d862
C
38 }
39
6fcd19ba
C
40 logger.error('Signature is not okay in body for %s.', req.body.signature.host)
41 return res.sendStatus(403)
42 })
43 .catch(err => {
709756b8 44 logger.error('Cannot get signed host in body.', { error: err.stack, signature: req.body.signature.signature })
6fcd19ba
C
45 return res.sendStatus(500)
46 })
0eb78d53
C
47}
48
9f10b292
C
49// ---------------------------------------------------------------------------
50
65fcc311
C
51export {
52 checkSignature
53}