-
Notifications
You must be signed in to change notification settings - Fork 0
/
SessionManager.js
65 lines (55 loc) · 2.07 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
60
61
62
63
64
65
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 result = crypto.randomBytes(32).toString("hex");
var obj = {
username:username,
timestamp:Date.now(),
expire_timestamp : (Date.now + maxAge)
}
sessions[result]=obj;
response.cookie("cpen322-session", result, {maxAge : maxAge});
setTimeout(()=>{
delete(sessions[result]);
}, maxAge);
};
this.deleteSession = (request) => {
/* To be implemented */
delete sessions[request.session];
delete request.username
delete request.session;
};
this.middleware = (request, response, next) => {
/* To be implemented */
var cookie_string = request.headers.cookie
if(cookie_string == null){
next(new SessionError("not in header"))
return
}
cookie_string = cookie_string.split(';').map(s => s.split('=').pop().trim()).shift();
if(sessions[cookie_string]){
let cookie_in_session = sessions[cookie_string];
request.username = cookie_in_session.username;
request.session = cookie_string;
next();
return
}
next(new SessionError("not in session"));
return
};
// 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;