-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.js
60 lines (51 loc) · 2.19 KB
/
middleware.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
const _ = require('lodash');
const crypto = require('crypto');
const RouterSignature = require('./signature');
const config = require('./config');
module.exports = {
/**
* Create middleware that validates `X-Router-Signature`
*
* @param {string} appDomain - Expected app domain of the JWT to pass validation (usually the domain the request is sent o)
* @param {object} additionalVerifyOpts - additional options to pass verifier
* @param {string} [routerUrl=config.ROUTER_URL] Optional override for router url
* @return {expressMiddleware} Returns a new middleware to be used in express
*/
validateSignature(appDomain, additionalVerifyOpts = {}, routerUrl = config.ROUTER_URL) {
if (!appDomain) throw new Error('appDomain arg is required for safe signature validation');
const signatureValidator = new RouterSignature(Object.assign({ audience: appDomain }, additionalVerifyOpts), routerUrl);
return (req, res, next) => {
signatureValidator.assertValidJwt(req.get('X-Router-Signature'))
.then((token) => {
req.routerSignature = token;
next();
}).catch(() => {
res.status(401).send({ message: 'JWT failed validation' });
});
};
},
/**
* Middleware that will validate signature hash against the `req.body`
* This will make sure that nothing has changed since the router has sent the payload
* MUST be run after the `validateSignature` middleware
* @return {expressMiddleware} Returns a middleware that validates `req.routerSignaure.hash` against the body
*/
validateBodyHash() {
return (req, res, next) => {
if (!req.routerSignature || !req.routerSignature.hash) {
return res.status(401).send({ message: 'Unable to validate body against missing signature' });
}
let payload = req.body || '';
if (_.isObjectLike(payload)) {
payload = JSON.stringify(payload);
}
const sha256 = crypto.createHash('sha256');
sha256.update(payload);
const hash = sha256.digest('base64');
if (hash === req.routerSignature.hash) {
return next();
}
return res.status(401).send({ message: 'Failed to validate signature hash' });
};
},
};