Repository navigation
Expand file tree
/
Copy pathsecurity.js
More file actions
111 lines (101 loc) · 3.95 KB
/
Copy pathsecurity.js
File metadata and controls
111 lines (101 loc) · 3.95 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
const ReverseProxyStrategy = require('passport-reverseproxy');
const ipaddr = require('ipaddr.js');
const constants = require('./utils/constants');
const utf8 = require('utf8');
const crypto = require('crypto');
const SECRET_KEY = process.env.URL_SIGNER_KEY;
const concatenateArray = (data) => Array.prototype.concat([], data);
const decodeUser = (user) => {
const eppn = utf8.decode(user.eppn);
const eduPersonAffiliation = concatenateArray(
utf8.decode(user.eduPersonAffiliation).split(';'),
);
const hyGroupCn = concatenateArray(utf8.decode(user.hyGroupCn).split(';'));
const preferredLanguage = utf8.decode(user.preferredLanguage);
const displayName = utf8.decode(user.displayName);
return {
eppn: eppn,
eduPersonAffiliation: eduPersonAffiliation,
hyGroupCn: hyGroupCn,
preferredLanguage: preferredLanguage,
displayName: displayName,
};
};
/**
* Contains the IP address of the local host.
*
* The variable `localhostIP` is instantiated with the processed
* IP address string '127.0.0.1' using the `ipaddr.process()` method.
*/
const localhostIP = ipaddr.process('127.0.0.1');
/**
* Configures Shibboleth authentication for the given Express application.
*
* @param {Object} app - The Express application instance.
* @param {Object} passport - The Passport.js instance.
*
* This function sets up Passport.js to use the Reverse Proxy Strategy which handles the
* authentication headers. These headers include 'eppn', 'eduPersonAffiliation', 'preferredLanguage',
* 'hyGroupCn', and 'displayName'. The 'eppn' header is required, while the others are optional.
*
* The authentication middleware is applied to the Express application to ensure that
* requests are authenticated based on the configured strategy. If authentication fails,
* a 401 Unauthorized response is sent.
*/
const shibbolethAuthentication = (app, passport) => {
passport.use(
new ReverseProxyStrategy({
headers: {
eppn: { alias: 'eppn', required: true },
eduPersonAffiliation: { alias: 'eduPersonAffiliation', required: false },
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 }, (err, user, info) => {
if (err || !user) {
return res.status(401).send('Not Authorized');
}
req.user = decodeUser(user);
next();
})(req, res, next);
});
};
/**
* Middleware function to confirm if the logged-in user has a teacher role.
*
* This function checks if the current user has the role of a teacher based on their
* affiliations. If the user is not a teacher, it responds with a 403 status and
* an appropriate message. If the user is a teacher, it proceeds to the next middleware
* or route handler.
*
* @param {Object} req - The request object, containing user information.
* @param {Object} res - The response object used to send back the desired HTTP response.
* @param {Function} next - The next middleware function in the stack.
*/
const teacherConfirmation = (req, res, next) => {
const user = req.user;
const isTeacher = user.eduPersonAffiliation.includes(constants.ROLE_TEACHER);
if (!isTeacher) {
res.status(403).json('User is not a teacher.');
} else {
next();
}
};
const generateSignature = (data) => {
return crypto.createHmac('sha256', SECRET_KEY).update(data).digest('base64url');
};
const generateSignedUrl = (courseId) => {
const data = courseId.toString();
return generateSignature(data);
};
module.exports = {
shibbolethAuthentication,
teacherConfirmation,
generateSignedUrl,
};