]>
Commit | Line | Data |
---|---|---|
9f10b292 C |
1 | 'use strict' |
2 | ||
f0f5567b | 3 | const logger = require('../helpers/logger') |
a3ee6fa2 | 4 | const mongoose = require('mongoose') |
5f698b82 | 5 | const peertubeCrypto = require('../helpers/peertube-crypto') |
a3ee6fa2 C |
6 | |
7 | const Pod = mongoose.model('Pod') | |
9f10b292 | 8 | |
f0f5567b | 9 | const secureMiddleware = { |
9f10b292 C |
10 | decryptBody: decryptBody |
11 | } | |
12 | ||
13 | function decryptBody (req, res, next) { | |
f0f5567b | 14 | const url = req.body.signature.url |
a3ee6fa2 | 15 | Pod.loadByUrl(url, function (err, pod) { |
9f10b292 C |
16 | if (err) { |
17 | logger.error('Cannot get signed url in decryptBody.', { error: err }) | |
18 | return res.sendStatus(500) | |
19 | } | |
20 | ||
21 | if (pod === null) { | |
22 | logger.error('Unknown pod %s.', url) | |
23 | return res.sendStatus(403) | |
24 | } | |
25 | ||
26 | logger.debug('Decrypting body from %s.', url) | |
27 | ||
bc503c2a | 28 | const signatureOk = peertubeCrypto.checkSignature(pod.publicKey, url, req.body.signature.signature) |
9f10b292 | 29 | |
bc503c2a | 30 | if (signatureOk === true) { |
9f10b292 C |
31 | peertubeCrypto.decrypt(req.body.key, req.body.data, function (err, decrypted) { |
32 | if (err) { | |
33 | logger.error('Cannot decrypt data.', { error: err }) | |
34 | return res.sendStatus(500) | |
35 | } | |
36 | ||
37 | req.body.data = JSON.parse(decrypted) | |
38 | delete req.body.key | |
39 | ||
40 | next() | |
41 | }) | |
42 | } else { | |
43 | logger.error('Signature is not okay in decryptBody for %s.', req.body.signature.url) | |
44 | return res.sendStatus(403) | |
45 | } | |
46 | }) | |
47 | } | |
48 | ||
49 | // --------------------------------------------------------------------------- | |
50 | ||
51 | module.exports = secureMiddleware |