PeerTube/middlewares/secure.js

52 lines
1.4 KiB
JavaScript
Raw Normal View History

2015-11-07 11:40:13 -06:00
;(function () {
'use strict'
2016-01-30 10:05:22 -06:00
var logger = require('../helpers/logger')
2016-02-05 11:03:20 -06:00
var peertubeCrypto = require('../helpers/peertubeCrypto')
2016-02-04 14:10:33 -06:00
var Pods = require('../models/pods')
2015-11-07 11:40:13 -06:00
2016-02-04 14:16:27 -06:00
var secureMiddleware = {
2016-01-31 04:23:52 -06:00
decryptBody: decryptBody
}
2015-11-07 11:40:13 -06:00
2016-01-31 04:23:52 -06:00
function decryptBody (req, res, next) {
2016-02-04 14:10:33 -06:00
var url = req.body.signature.url
Pods.findByUrl(url, function (err, pod) {
2015-11-07 11:40:13 -06:00
if (err) {
logger.error('Cannot get signed url in decryptBody.', { error: err })
return res.sendStatus(500)
}
if (pod === null) {
2016-02-04 14:10:33 -06:00
logger.error('Unknown pod %s.', url)
return res.sendStatus(403)
2015-11-07 11:40:13 -06:00
}
2016-02-04 14:10:33 -06:00
logger.debug('Decrypting body from %s.', url)
2015-11-07 11:40:13 -06:00
2016-02-05 11:03:20 -06:00
var signature_ok = peertubeCrypto.checkSignature(pod.publicKey, url, req.body.signature.signature)
2015-11-07 11:40:13 -06:00
if (signature_ok === true) {
2016-02-05 11:03:20 -06:00
peertubeCrypto.decrypt(req.body.key, req.body.data, function (err, decrypted) {
if (err) {
logger.error('Cannot decrypt data.', { error: err })
return res.sendStatus(500)
}
req.body.data = JSON.parse(decrypted)
delete req.body.key
next()
})
2015-11-07 11:40:13 -06:00
} else {
logger.error('Signature is not okay in decryptBody for %s.', req.body.signature.url)
return res.sendStatus(403)
2015-11-07 11:40:13 -06:00
}
})
}
2016-01-31 04:23:52 -06:00
// ---------------------------------------------------------------------------
2016-02-04 14:16:27 -06:00
module.exports = secureMiddleware
2015-11-07 11:40:13 -06:00
})()