-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsecure.js
79 lines (68 loc) · 2 KB
/
secure.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
const _ = require('lodash');
// function setCookie(req, res) {
// var tokenCookie = req.signedCookies && req.signedCookies.access_token;
// if (tokenCookie === undefined) {
// res.cookie('access_token', req.accessToken.id, {
// maxAge: 900000,
// signed: true
// });
// console.log('TOKEN cookie created successfully: ', req.accessToken.id);
// } else {
// // yes, cookie was already present
// console.log('cookie exists', tokenCookie);
// }
// }
function redRoutes(params) {
paths = params.paths || []
//principalType = params.principalType || "USER";
return function (req, res, next) {
var matched = paths.some(function(securePath){
var regEx = new RegExp('^'+securePath)
return req.path.match(regEx)
});
//var matched = req.path.match(/^\/ping/)
//continue if path doesnt match any route
if(!matched) {
next();
return;
}
if (!req.accessToken) {
res.status(401).send('USER UNAUTHENTICATED!')
return;
}
const app = req.app;
const userId = req.accessToken.userId;
const RoleModel = app.models[params.roleModel] || app.models.Role;
RoleModel.getRoles({'principalId': userId}, {'returnOnlyRoleNames': true}, function(err, roles){
console.log("### ROLES : ", roles)
if(err) {
next(err)
return;
}
const allowed = _.intersection(params.roles, roles).length > 0;
if(!allowed) {
res.status(401).send('USER UNAUTHORIZED!')
return;
}
next();
})
}
}
function setLoginCookie(context, accessToken, next) {
var res = context.res;
var req = context.req;
if (accessToken != null) {
if (accessToken.id != null) {
res.cookie('access_token', accessToken.id, {
signed: true,
maxAge: 1000 * accessToken.ttl
});
return next()//res.redirect('/');
}
}
return next();
}
module.exports = {
red: redRoutes,
setLoginCookie: setLoginCookie
}