aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/middlewares/secure.ts
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-05-15 22:22:03 +0200
committerChocobozzz <florian.bigard@gmail.com>2017-05-20 09:57:40 +0200
commit65fcc3119c334b75dd13bcfdebf186afdc580a8f (patch)
tree4f2158c61a9b7c3f47cfa233d01413b946ee53c0 /server/middlewares/secure.ts
parentd5f345ed4cfac4e1fa84dcb4fce1cda4d32f9c73 (diff)
downloadPeerTube-65fcc3119c334b75dd13bcfdebf186afdc580a8f.tar.gz
PeerTube-65fcc3119c334b75dd13bcfdebf186afdc580a8f.tar.zst
PeerTube-65fcc3119c334b75dd13bcfdebf186afdc580a8f.zip
First typescript iteration
Diffstat (limited to 'server/middlewares/secure.ts')
-rw-r--r--server/middlewares/secure.ts48
1 files changed, 48 insertions, 0 deletions
diff --git a/server/middlewares/secure.ts b/server/middlewares/secure.ts
new file mode 100644
index 000000000..ee8545028
--- /dev/null
+++ b/server/middlewares/secure.ts
@@ -0,0 +1,48 @@
1const db = require('../initializers/database')
2const logger = require('../helpers/logger')
3const peertubeCrypto = require('../helpers/peertube-crypto')
4
5function checkSignature (req, res, next) {
6 const host = req.body.signature.host
7 db.Pod.loadByHost(host, function (err, pod) {
8 if (err) {
9 logger.error('Cannot get signed host in body.', { error: err })
10 return res.sendStatus(500)
11 }
12
13 if (pod === null) {
14 logger.error('Unknown pod %s.', host)
15 return res.sendStatus(403)
16 }
17
18 logger.debug('Checking signature from %s.', host)
19
20 let signatureShouldBe
21 // If there is data in the body the sender used it for its signature
22 // If there is no data we just use its host as signature
23 if (req.body.data) {
24 signatureShouldBe = req.body.data
25 } else {
26 signatureShouldBe = host
27 }
28
29 const signatureOk = peertubeCrypto.checkSignature(pod.publicKey, signatureShouldBe, req.body.signature.signature)
30
31 if (signatureOk === true) {
32 res.locals.secure = {
33 pod
34 }
35
36 return next()
37 }
38
39 logger.error('Signature is not okay in body for %s.', req.body.signature.host)
40 return res.sendStatus(403)
41 })
42}
43
44// ---------------------------------------------------------------------------
45
46export {
47 checkSignature
48}