Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 1x 1x 1x 1x 3x 1x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | const Boom = require('@hapi/boom');
const jwt = require('jsonwebtoken');
const helpers = {
formatMessage: function(message) {
return typeof message === 'string' ? message : JSON.stringify(message);
},
getTokens(strs, separators) {
if (!separators.length) {
return {key: strs.shift().trim(), value: strs.shift().trim()};
}
const separator = separators.shift();
return strs
.map((s) => (helpers.getTokens(s.split(separator), separators)))
.reduce((accum, c) => {
if (!c.key) {
return Object.assign(accum, c);
}
accum[c.key] = c.value;
return accum;
}, {});
},
jwtXsrfCheck({query, cookie, hashKey, verifyOptions}) {
return new Promise((resolve, reject) => {
// return unauthorized if something is wrong with xsrf get query param or with cookie itself
Iif (query.xsrf === '' || !cookie) return reject(Boom.unauthorized());
jwt.verify(cookie, hashKey, verifyOptions, (err, decoded) => { // verify cookie
// if wild error appears, mark this request as unauthorized
Iif (err) return reject(Boom.unauthorized(err.name));
// if xsrf get param is not the same as xsrfToken from the cookie, mark this request as unauthorized
Iif (decoded.xsrfToken !== query.xsrf) return reject(Boom.unauthorized('Xsrf mismatch'));
// yeah we are done, on later stage will check for correct permissions
resolve(decoded);
});
});
},
permissionVerify(socket, appId) {
const actions = ['%'];
Iif (appId) actions.push(appId);
const objects = ['%', socket.fingerprint];
Eif (Array.isArray(socket.auth.scopes)) {
for (let i = 0, n = socket.auth.scopes.length; i < n; i += 1) {
const {actionId, objectId} = socket.auth.scopes[i];
Eif (actions.includes(actionId) && objects.includes(objectId)) {
return true;
}
}
}
throw Boom.forbidden();
}
};
module.exports = helpers;
|