]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/secure.js
Server: remove encryption when seending requests to other pods
[github/Chocobozzz/PeerTube.git] / server / middlewares / secure.js
CommitLineData
9f10b292
C
1'use strict'
2
f0f5567b 3const logger = require('../helpers/logger')
a3ee6fa2 4const mongoose = require('mongoose')
5f698b82 5const peertubeCrypto = require('../helpers/peertube-crypto')
a3ee6fa2
C
6
7const Pod = mongoose.model('Pod')
9f10b292 8
f0f5567b 9const secureMiddleware = {
38d78e5b 10 checkSignature
9f10b292
C
11}
12
0eb78d53 13function checkSignature (req, res, next) {
49abbbbe
C
14 const host = req.body.signature.host
15 Pod.loadByHost(host, function (err, pod) {
9f10b292 16 if (err) {
38d78e5b 17 logger.error('Cannot get signed host in body.', { error: err })
9f10b292
C
18 return res.sendStatus(500)
19 }
20
21 if (pod === null) {
49abbbbe 22 logger.error('Unknown pod %s.', host)
9f10b292
C
23 return res.sendStatus(403)
24 }
25
38d78e5b 26 logger.debug('Checking signature from %s.', host)
9f10b292 27
49abbbbe 28 const signatureOk = peertubeCrypto.checkSignature(pod.publicKey, host, req.body.signature.signature)
9f10b292 29
bc503c2a 30 if (signatureOk === true) {
0eb78d53
C
31 return next()
32 }
33
38d78e5b 34 logger.error('Signature is not okay in body for %s.', req.body.signature.host)
0eb78d53
C
35 return res.sendStatus(403)
36 })
37}
38
9f10b292
C
39// ---------------------------------------------------------------------------
40
41module.exports = secureMiddleware