diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2015-11-07 18:40:13 +0100 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2015-11-07 18:40:13 +0100 |
commit | b60035d8e86b712e586550f762c26caae353dc0e (patch) | |
tree | a6dc088e5e48f93eb129e40672a7bea0d230a97c /middlewares/index.js | |
parent | e85782f7da44267e11714a63567feb7e0127126e (diff) | |
download | PeerTube-b60035d8e86b712e586550f762c26caae353dc0e.tar.gz PeerTube-b60035d8e86b712e586550f762c26caae353dc0e.tar.zst PeerTube-b60035d8e86b712e586550f762c26caae353dc0e.zip |
Middleware refractoring
Diffstat (limited to 'middlewares/index.js')
-rw-r--r-- | middlewares/index.js | 53 |
1 files changed, 3 insertions, 50 deletions
diff --git a/middlewares/index.js b/middlewares/index.js index 6368d791a..e727202ba 100644 --- a/middlewares/index.js +++ b/middlewares/index.js | |||
@@ -1,56 +1,9 @@ | |||
1 | ;(function () { | 1 | ;(function () { |
2 | 'use strict' | 2 | 'use strict' |
3 | 3 | ||
4 | var ursa = require('ursa') | 4 | var middleware = { |
5 | var fs = require('fs') | 5 | reqValidators: require('./reqValidators'), |
6 | 6 | misc: require('./misc') | |
7 | var logger = require('../src/logger') | ||
8 | var utils = require('../src/utils') | ||
9 | var PodsDB = require('../src/database').PodsDB | ||
10 | |||
11 | var middleware = {} | ||
12 | |||
13 | middleware.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') | ||
19 | } else { | ||
20 | res.setHeader('Cache-Control', 'no-cache, no-store, max-age=0, must-revalidate') | ||
21 | } | ||
22 | |||
23 | next() | ||
24 | } | ||
25 | } | ||
26 | |||
27 | middleware.decryptBody = function (req, res, next) { | ||
28 | logger.debug('Decrypting body.') | ||
29 | |||
30 | PodsDB.findOne({ url: req.body.signature.url }, function (err, pod) { | ||
31 | if (err) { | ||
32 | logger.error('Cannot get signed url in decryptBody.', { error: err }) | ||
33 | res.sendStatus(500) | ||
34 | } | ||
35 | |||
36 | logger.debug('Found one pod which could send the message.', { pod: pod.publicKey, url: req.body.signature.url }) | ||
37 | |||
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') | ||
40 | |||
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 }) | ||
47 | } else { | ||
48 | logger.error('Signature is not okay in decryptBody for %s.', req.body.signature.url) | ||
49 | res.sendStatus(500) | ||
50 | } | ||
51 | |||
52 | next() | ||
53 | }) | ||
54 | } | 7 | } |
55 | 8 | ||
56 | module.exports = middleware | 9 | module.exports = middleware |