]>
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 Pods
= require('../models/pods')
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 var url
= req
.body
.signature
.url
32 Pods
.findByUrl(url
, function (err
, pod
) {
34 logger
.error('Cannot get signed url in decryptBody.', { error: err
})
35 return res
.sendStatus(500)
39 logger
.error('Unknown pod %s.', url
)
40 return res
.sendStatus(403)
43 logger
.debug('Decrypting body from %s.', url
)
45 var crt
= ursa
.createPublicKey(pod
.publicKey
)
46 var signature_ok
= crt
.hashAndVerify('sha256', new Buffer(req
.body
.signature
.url
).toString('hex'), req
.body
.signature
.signature
, 'hex')
48 if (signature_ok
=== true) {
49 var myKey
= ursa
.createPrivateKey(fs
.readFileSync(utils
.getCertDir() + 'peertube.key.pem'))
50 var decryptedKey
= myKey
.decrypt(req
.body
.key
, 'hex', 'utf8')
51 req
.body
.data
= JSON
.parse(utils
.symetricDecrypt(req
.body
.data
, decryptedKey
))
54 logger
.error('Signature is not okay in decryptBody for %s.', req
.body
.signature
.url
)
55 return res
.sendStatus(403)
62 // ---------------------------------------------------------------------------
64 module
.exports
= miscMiddleware