]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/secure.js
Server: migration script that add admin role to root user
[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 = {
9f10b292
C
10 decryptBody: decryptBody
11}
12
13function 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
39f87cb2
C
37 try {
38 req.body.data = JSON.parse(decrypted)
39 delete req.body.key
40 } catch (err) {
41 logger.error('Error in JSON.parse', { error: err })
42 return res.sendStatus(500)
43 }
9f10b292
C
44
45 next()
46 })
47 } else {
48 logger.error('Signature is not okay in decryptBody for %s.', req.body.signature.url)
49 return res.sendStatus(403)
50 }
51 })
52}
53
54// ---------------------------------------------------------------------------
55
56module.exports = secureMiddleware