-
Notifications
You must be signed in to change notification settings - Fork 0
/
security.js
49 lines (43 loc) · 1.47 KB
/
security.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
let ReverseProxyStrategy = require('passport-reverseproxy');
const Constants = require('./Constants');
const ipaddr = require('ipaddr.js');
const localhostIP = ipaddr.process('127.0.0.1');
const shibbolethAuthentication = (app, passport) => {
passport.use(new ReverseProxyStrategy({
headers: {
'eppn': {alias: 'eppn', required: true},
'preferredlanguage': {alias: 'preferredLanguage', required: false},
'hyGroupCn': {alias: 'hyGroupCn', required: false},
'displayName': {alias: 'displayName', required: false}
},
whitelist: localhostIP
})
);
app.use(passport.initialize());
app.use((req, res, next) => {
passport.authenticate('reverseproxy', {session: false})(req, res, next);
});
};
const isAdminOrWriter = (req, res, next) => {
if (!req.user.roles || !req.user.roles.includes(Constants.ROLE_ADMIN) && !req.user.roles.includes(Constants.ROLE_WRITER)) {
return res.status(403).json({
status: 403,
message: 'FORBIDDEN'
})
}
next();
}
const isAdmin = (req, res, next) => {
if (!req.user.roles || !req.user.roles.includes(Constants.ROLE_ADMIN)) {
return res.status(403).json({
status: 403,
message: 'FORBIDDEN'
})
}
next();
};
module.exports = {
shibbolethAuthentication : shibbolethAuthentication,
isAdmin: isAdmin,
isAdminOrWriter: isAdminOrWriter
};