-
Notifications
You must be signed in to change notification settings - Fork 54
/
server_middleware.js
79 lines (69 loc) · 2.01 KB
/
server_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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
const axios = require('axios');
const https = require('https');
const { OPENSHIFT_HOST } = process.env;
const insecureAgent = new https.Agent({
rejectUnauthorized: false
});
function getApiHost() {
if (process.env.OPENSHIFT_VERSION === '4') {
return `https://${process.env.OPENSHIFT_API}/apis/user.openshift.io/v1/users/~`;
}
return `https://${OPENSHIFT_HOST}/oapi/v1/users/~`;
}
function checkRoles(memberGroups, requestedGroups) {
// If no groups were requested the request is always authorized
if (!requestedGroups) {
return true;
}
// If groups were requested but the user is not a member of any group the request is
// always unauthorized
if (!memberGroups) {
return false;
}
// Require that at least one of the requested groups are part of the members group array
return requestedGroups.map(group => {
console.log(`checking against ${group}`);
return memberGroups.indexOf(group) >= 0;
}).reduce((acc, current) => {
return acc || current;
}, false);
}
exports.requireRoles = config => {
return function(req, res, next) {
// Not much we can do if the openshift host is not set. This probably
// means we are running in a mock environment
if (!OPENSHIFT_HOST) {
return next();
}
const token = req.get('X-Forwarded-Access-Token');
if (!token) {
return res.sendStatus(401);
}
return axios({
httpsAgent: insecureAgent,
url: getApiHost(),
headers: {
authorization: `Bearer ${token}`
}
})
.then(response => {
const {
kind,
groups,
metadata: { name }
} = response.data;
if (kind === 'User') {
if (checkRoles(groups, config)) {
console.error(`Access granted to user ${name}`);
return next();
}
console.error(`Access denied to user ${name}`);
}
return res.sendStatus(403);
})
.catch(err => {
console.error(err);
return res.sendStatus(401);
});
};
};