aboutsummaryrefslogtreecommitdiffhomepage
path: root/middlewares/misc.js
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2016-02-04 21:16:27 +0100
committerChocobozzz <florian.bigard@gmail.com>2016-02-04 21:23:06 +0100
commita5fa04b0cce851f680a45055f57ed2c3d22f1c82 (patch)
tree0749c4d6889d7cf250110f9ff59a8cd22ee8a480 /middlewares/misc.js
parentc173e56520b0fe4206b9ea8049b6add40bfeabcd (diff)
downloadPeerTube-a5fa04b0cce851f680a45055f57ed2c3d22f1c82.tar.gz
PeerTube-a5fa04b0cce851f680a45055f57ed2c3d22f1c82.tar.zst
PeerTube-a5fa04b0cce851f680a45055f57ed2c3d22f1c82.zip
Split misc middleware
Diffstat (limited to 'middlewares/misc.js')
-rw-r--r--middlewares/misc.js65
1 files changed, 0 insertions, 65 deletions
diff --git a/middlewares/misc.js b/middlewares/misc.js
deleted file mode 100644
index cc4e2e8a4..000000000
--- a/middlewares/misc.js
+++ /dev/null
@@ -1,65 +0,0 @@
1;(function () {
2 'use strict'
3
4 var fs = require('fs')
5 var ursa = require('ursa')
6
7 var logger = require('../helpers/logger')
8 var Pods = require('../models/pods')
9 var utils = require('../helpers/utils')
10
11 var miscMiddleware = {
12 cache: cache,
13 decryptBody: decryptBody
14 }
15
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')
22 } else {
23 res.setHeader('Cache-Control', 'no-cache, no-store, max-age=0, must-revalidate')
24 }
25
26 next()
27 }
28 }
29
30 function decryptBody (req, res, next) {
31 var url = req.body.signature.url
32 Pods.findByUrl(url, function (err, pod) {
33 if (err) {
34 logger.error('Cannot get signed url in decryptBody.', { error: err })
35 return res.sendStatus(500)
36 }
37
38 if (pod === null) {
39 logger.error('Unknown pod %s.', url)
40 return res.sendStatus(403)
41 }
42
43 logger.debug('Decrypting body from %s.', url)
44
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')
47
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))
52 delete req.body.key
53 } else {
54 logger.error('Signature is not okay in decryptBody for %s.', req.body.signature.url)
55 return res.sendStatus(403)
56 }
57
58 next()
59 })
60 }
61
62 // ---------------------------------------------------------------------------
63
64 module.exports = miscMiddleware
65})()