diff options
Diffstat (limited to 'middlewares/misc.js')
-rw-r--r-- | middlewares/misc.js | 65 |
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 | })() | ||