]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/secure.js
Client: update angular -> 2.0.1
[github/Chocobozzz/PeerTube.git] / server / middlewares / secure.js
CommitLineData
9f10b292
C
1'use strict'
2
f0f5567b 3const logger = require('../helpers/logger')
a3ee6fa2 4const mongoose = require('mongoose')
5f698b82 5const peertubeCrypto = require('../helpers/peertube-crypto')
a3ee6fa2
C
6
7const Pod = mongoose.model('Pod')
9f10b292 8
f0f5567b 9const secureMiddleware = {
0eb78d53 10 checkSignature: checkSignature,
9f10b292
C
11 decryptBody: decryptBody
12}
13
0eb78d53 14function checkSignature (req, res, next) {
f0f5567b 15 const url = req.body.signature.url
a3ee6fa2 16 Pod.loadByUrl(url, function (err, pod) {
9f10b292
C
17 if (err) {
18 logger.error('Cannot get signed url in decryptBody.', { error: err })
19 return res.sendStatus(500)
20 }
21
22 if (pod === null) {
23 logger.error('Unknown pod %s.', url)
24 return res.sendStatus(403)
25 }
26
27 logger.debug('Decrypting body from %s.', url)
28
bc503c2a 29 const signatureOk = peertubeCrypto.checkSignature(pod.publicKey, url, req.body.signature.signature)
9f10b292 30
bc503c2a 31 if (signatureOk === true) {
0eb78d53
C
32 return next()
33 }
34
35 logger.error('Signature is not okay in decryptBody for %s.', req.body.signature.url)
36 return res.sendStatus(403)
37 })
38}
39
40function decryptBody (req, res, next) {
41 peertubeCrypto.decrypt(req.body.key, req.body.data, function (err, decrypted) {
42 if (err) {
43 logger.error('Cannot decrypt data.', { error: err })
44 return res.sendStatus(500)
9f10b292 45 }
0eb78d53
C
46
47 try {
48 req.body.data = JSON.parse(decrypted)
49 delete req.body.key
50 } catch (err) {
51 logger.error('Error in JSON.parse', { error: err })
52 return res.sendStatus(500)
53 }
54
55 next()
9f10b292
C
56 })
57}
58
59// ---------------------------------------------------------------------------
60
61module.exports = secureMiddleware