]>
git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - middlewares/misc.js
5 var ursa
= require('ursa')
7 var logger
= require('../helpers/logger')
8 var PodsDB
= require('../initializers/database').PodsDB
9 var utils
= require('../helpers/utils')
11 var miscMiddleware
= {
13 decryptBody: decryptBody
16 function cache (cache
) {
17 return function (req
, res
, next
) {
18 // If we want explicitly a cache
19 // Or if we don't specify if we want a cache or no and we are in production
20 if (cache
=== true || (cache
!== false && process
.env
.NODE_ENV
=== 'production')) {
21 res
.setHeader('Cache-Control', 'public')
23 res
.setHeader('Cache-Control', 'no-cache, no-store, max-age=0, must-revalidate')
30 function decryptBody (req
, res
, next
) {
31 PodsDB
.findOne({ url: req
.body
.signature
.url
}, function (err
, pod
) {
33 logger
.error('Cannot get signed url in decryptBody.', { error: err
})
34 return res
.sendStatus(500)
38 logger
.error('Unknown pod %s.', req
.body
.signature
.url
)
39 return res
.sendStatus(403)
42 logger
.debug('Decrypting body from %s.', req
.body
.signature
.url
)
44 var crt
= ursa
.createPublicKey(pod
.publicKey
)
45 var signature_ok
= crt
.hashAndVerify('sha256', new Buffer(req
.body
.signature
.url
).toString('hex'), req
.body
.signature
.signature
, 'hex')
47 if (signature_ok
=== true) {
48 var myKey
= ursa
.createPrivateKey(fs
.readFileSync(utils
.getCertDir() + 'peertube.key.pem'))
49 var decryptedKey
= myKey
.decrypt(req
.body
.key
, 'hex', 'utf8')
50 req
.body
.data
= JSON
.parse(utils
.symetricDecrypt(req
.body
.data
, decryptedKey
))
53 logger
.error('Signature is not okay in decryptBody for %s.', req
.body
.signature
.url
)
54 return res
.sendStatus(403)
61 // ---------------------------------------------------------------------------
63 module
.exports
= miscMiddleware