]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/secure.ts
Remove ng2-completer
[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
feb4bdfd 12 db.Pod.loadByHost(host, function (err, pod) {
9f10b292 13 if (err) {
38d78e5b 14 logger.error('Cannot get signed host in body.', { error: err })
9f10b292
C
15 return res.sendStatus(500)
16 }
17
18 if (pod === null) {
49abbbbe 19 logger.error('Unknown pod %s.', host)
9f10b292
C
20 return res.sendStatus(403)
21 }
22
38d78e5b 23 logger.debug('Checking signature from %s.', host)
9f10b292 24
bdfbd4f1 25 let signatureShouldBe
5a976a8c
C
26 // If there is data in the body the sender used it for its signature
27 // If there is no data we just use its host as signature
bdfbd4f1
C
28 if (req.body.data) {
29 signatureShouldBe = req.body.data
30 } else {
31 signatureShouldBe = host
32 }
33
e02643f3 34 const signatureOk = peertubeCryptoCheckSignature(pod.publicKey, signatureShouldBe, req.body.signature.signature)
9f10b292 35
bc503c2a 36 if (signatureOk === true) {
4ff0d862
C
37 res.locals.secure = {
38 pod
39 }
40
0eb78d53
C
41 return next()
42 }
43
38d78e5b 44 logger.error('Signature is not okay in body for %s.', req.body.signature.host)
0eb78d53
C
45 return res.sendStatus(403)
46 })
47}
48
9f10b292
C
49// ---------------------------------------------------------------------------
50
65fcc311
C
51export {
52 checkSignature
53}