1
0
Fork 0
peertube/middlewares/secure.js

52 lines
1.4 KiB
JavaScript
Raw Normal View History

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