]>
git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - middlewares/misc.js
4 var ursa
= require('ursa')
7 var logger
= require('../src/logger')
8 var utils
= require('../src/utils')
9 var PodsDB
= require('../src/database').PodsDB
13 misc
.cache = function (cache
) {
14 return function (req
, res
, next
) {
15 // If we want explicitly a cache
16 // Or if we don't specify if we want a cache or no and we are in production
17 if (cache
=== true || (cache
!== false && process
.env
.NODE_ENV
=== 'production')) {
18 res
.setHeader('Cache-Control', 'public')
20 res
.setHeader('Cache-Control', 'no-cache, no-store, max-age=0, must-revalidate')
27 misc
.decryptBody = function (req
, res
, next
) {
28 logger
.debug('Decrypting body.')
30 PodsDB
.findOne({ url: req
.body
.signature
.url
}, function (err
, pod
) {
32 logger
.error('Cannot get signed url in decryptBody.', { error: err
})
36 logger
.debug('Found one pod which could send the message.', { pod: pod
.publicKey
, url: req
.body
.signature
.url
})
38 var crt
= ursa
.createPublicKey(pod
.publicKey
)
39 var signature_ok
= crt
.hashAndVerify('sha256', new Buffer(req
.body
.signature
.url
).toString('hex'), req
.body
.signature
.signature
, 'hex')
41 if (signature_ok
=== true) {
42 var myKey
= ursa
.createPrivateKey(fs
.readFileSync(utils
.certDir
+ 'peertube.key.pem'))
43 var decryptedKey
= myKey
.decrypt(req
.body
.key
, 'hex', 'utf8')
44 logger
.debug(decryptedKey
)
45 req
.body
.data
= JSON
.parse(utils
.symetricDecrypt(req
.body
.data
, decryptedKey
))
46 logger
.debug('Decrypted.', { body: req
.body
})
48 logger
.error('Signature is not okay in decryptBody for %s.', req
.body
.signature
.url
)