-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.js
121 lines (112 loc) · 3.26 KB
/
session.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
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
112
113
114
115
116
117
118
119
120
121
const hash = require("./hash.js");
const id = require("nanoid");
const db = require("./db.js");
const heap = require("./heap.js");
const session = new Map();
const sessionHeap = new heap.MinHeap();
exports.removeInactive = function () {
console.log(
"Start cleaning session store.\n Total sessions before cleaning: " +
session.size +
"\nHeap size: " +
sessionHeap.size()
);
while (
sessionHeap.getMin().sessionData.timeStamp + 60 * 60 * 1000 <
Date.now()
) {
session.delete(sessionHeap.pop().sessionData.id);
}
console.log(
"Total sessions after cleaning: " +
session.size +
"\nHeap size: " +
sessionHeap.size()
);
};
exports.addSession = function (id, sessionData) {
delete sessionData.user.pwd;
sessionHeap.push(sessionData);
session.set(id, sessionData);
console.log(
"Start session: " +
sessionData.user.email +
"\nTotal Sessions: " +
session.size +
"\nHeap size: " +
sessionHeap.size()
);
};
exports.isSession = function (cookies) {
for (cookie of cookies) {
let cookiePair = cookie.split("=");
if (cookiePair[0].trim() === "session") {
return session.get(cookiePair[1].trim());
}
}
return null;
};
exports.deleteSession = async function (cookies) {
for (cookie of cookies) {
let cookiePair = cookie.split("=");
if (cookiePair[0].trim() === "session") {
let sessionData = session.get(cookiePair[1].trim());
let email = sessionData.user.email;
let sessionIndex = sessionData.index;
console.log(
"End Session: " +
email +
" Status: " +
session.delete(cookiePair[1].trim()) +
" " +
sessionHeap.remove(sessionIndex) +
"\nTotal Sessions: " +
session.size +
"\nHeap size: " +
sessionHeap.size()
);
} else if (cookiePair[0].trim().length === 21)
await db.deleteSession(cookiePair[0].trim());
}
};
exports.authCookie = async function (cookies) {
let data;
let cookiePair;
await db.connect();
for (cookie of cookies) {
cookiePair = cookie.split("=");
//nanoid key length
if (cookiePair[0].trim().length === 21) {
return await db.sessionLookUp(cookiePair[0].trim()).then(async (data) => {
if (data && data !== -1) {
return await hash
.encode(data.user._id, cookiePair[1].trim())
.then((hashedKey) => {
if (Buffer.compare(hashedKey, data.key.buffer) === 0) {
console.log("Authenticated user: " + data.user.email);
return { user: data.user, lookUp: data.lookUp };
} else return false;
});
} else return null;
});
}
}
};
exports.authPwd = async function (user, pwd) {
return await hash.encode(user._id, pwd).then((hashedKey) => {
if (Buffer.compare(hashedKey, user.pwd.buffer) === 0) return true;
else return false;
});
};
exports.createId = () => {
return id.nanoid();
};
exports.createToken = async function (user, oldLookUp) {
let lookUp;
if (oldLookUp) lookUp = oldLookUp;
else lookUp = id.nanoid();
let key = id.nanoid();
return await hash.encode(user._id, key).then((hashedKey) => {
return { lookUp: lookUp, key: key, hashedKey: hashedKey };
});
};