-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSessionManager.js
59 lines (49 loc) · 1.96 KB
/
SessionManager.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
const crypto = require('crypto');
class SessionError extends Error {};
function SessionManager (){
// default session length - you might want to
// set this to something small during development
const CookieMaxAgeMs = 600000;
// keeping the session data inside a closure to keep them protected
const sessions = {};
// might be worth thinking about why we create these functions
// as anonymous functions (per each instance) and not as prototype methods
this.createSession = (response, username, maxAge = CookieMaxAgeMs) => {
/* To be implemented */
var token = crypto.randomBytes(30).toString("hex");
var newObj = {
"username": username,
};
sessions[token] = newObj;
response.cookie('cpen400a-session', token, {maxAge: maxAge});
setTimeout(function(){delete sessions[token]}, maxAge);
};
this.deleteSession = (request) => {
/* To be implemented */
delete request.username;
var tempSession = request.session;
delete request.session;
delete sessions[tempSession];
};
this.middleware = (request, response, next) => {
/* To be implemented */
if (request.headers.cookie == null || request.headers.cookie == undefined) {
next(new SessionError('error'));
}else{
var cookie = request.headers.cookie.split(';').find(row => row.startsWith('cpen400a-session')).split('=')[1];
if (cookie in sessions){
request.username = sessions[cookie].username;
request.session = cookie;
next();
}else {
next(new SessionError('error'));
}
}
};
// this function is used by the test script.
// you can use it if you want.
this.getUsername = (token) => ((token in sessions) ? sessions[token].username : null);
};
// SessionError class is available to other modules as "SessionManager.Error"
SessionManager.Error = SessionError;
module.exports = SessionManager;