-
Notifications
You must be signed in to change notification settings - Fork 0
/
user_management.js
193 lines (146 loc) · 5.53 KB
/
user_management.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
const crypto = require('crypto')
DB = undefined;
function changePassword ( username = "", newPassword = "" ) {
if (!validPassword(password)) {
return "BAD_PASSWORD"
}
user = DB.prepare("SELECT * FROM users WHERE username = ?").get(username)
if (user == undefined) {
return "UNKNOWN_USERNAME"
}
salt = crypto.randomBytes(128).toString('base64')
hash = crypto.pbkdf2Sync(password, salt, 10000, 64, 'sha512').toString('hex')
DB.prepare("UPDATE users SET password_hash = ?, salt = ? WHERE username = ?").run(hash, salt, username)
return "OK"
}
function checkPassword ( username = "", password = "" ) {
user = DB.prepare("SELECT * FROM users WHERE username = ?").get(username)
if (user == undefined) {
return "UNKNOWN_USERNAME"
}
passwordAttemptHash = crypto.pbkdf2Sync(password, user.salt, 10000, 64, 'sha512').toString('hex');
if (passwordAttemptHash != user.password_hash) {
return "INCORRECT_PASSWORD"
}
return "OK"
}
function validPassword ( password = "" ) {
// ^.* : Start
// (?=.{8,60}) : between 8 and 60 characters
// (?=.*[a-zA-Z]) : Letters
// (?=.*\d) : Digits
// .*$ : End
return /^.*(?=.{8,60})(?=.*[a-zA-Z])(?=.*\d).*$/.test(password)
}
/**
* Logs a user in
* @param {Express.Request} req The express request object
* @param {String} username
* @param {String} password
* @param {String} ip The login request source's IP address
* @param {String} ua The login request source's User Agent
* @returns {String} The new SID of the logged in user
*/
function login ( username = "", password = "", ip = "", ua = "") {
attempt = checkPassword(username, password)
if (attempt != "OK") return attempt;
newSID = crypto.randomBytes(32).toString('base64url')
newSIDhash = crypto.createHash('sha512').update(newSID).digest('hex');
DB.prepare("INSERT INTO active_sessions (hashed_sid, ip_addr, user_agent, username, invalid_after) VALUES (?, ?, ?, ?, ?)").run(newSIDhash, ip, ua, username, Date.now() + 6 * 60 * 60 * 1000)
DB.prepare("UPDATE users SET last_logged_in = ? WHERE username = ?").run(Date.now(), username)
return newSID
}
/**
* Logs a user out
* @param {String} sid The SID of the session to invalidate
*/
function logout ( sid = "" ) {
SIDhash = crypto.createHash('sha512').update(sid).digest('hex');
DB.prepare("DELETE FROM active_sessions WHERE hashed_sid = ?").run(SIDhash)
}
function removeUserSessions( username = "" ) {
DB.prepare("DELETE FROM active_sessions WHERE username = ?").run(username)
}
function createUser( username = "", password = "" ) {
if (!validPassword(password)) {
return "BAD_PASSWORD"
}
if (DB.prepare("SELECT * FROM users WHERE username = ?").get(username)) {
return "USER_ALREADY_EXISTS"
}
salt = crypto.randomBytes(128).toString('base64')
hash = crypto.pbkdf2Sync(password, salt, 10000, 64, 'sha512').toString('hex')
DB.prepare("INSERT INTO users (username, password_hash, salt, date_created, last_logged_in, permission_level) VALUES (?, ?, ?, ?, ?, ?)").run(username, hash, salt, Date.now(), Date.now(), "Admin")
return "OK"
}
function deleteUser( username = "" ) {
DB.prepare("DELETE FROM users WHERE username = ?").run(username)
}
function userList() {
output = DB.prepare("SELECT * FROM users").all()
cleanedOutput = []
for (user in output) {
cleanedOutput.push({
username: output[user].username,
permissionLevel: output[user].permission_level,
joinDate: output[user].date_created,
lastLoggedIn: output[user].last_logged_in
})
}
return cleanedOutput
}
function cleanSessions() {
DB.prepare("DELETE FROM active_sessions WHERE invalid_after < ?").run(Date.now())
}
function validSession ( req ) {
sid = req.parseCookies()["SID"]
ua = (req.headers['user-agent'] || "")
ip = req.ip
if (!sid) {
return false
}
SIDhash = crypto.createHash('sha512').update(sid).digest('hex')
row = DB.prepare("SELECT * FROM active_sessions WHERE hashed_sid = ? AND ip_addr = ? AND user_agent = ?").get(SIDhash, ip, ua)
// Session is not found
if (!row) {
return undefined
}
// If session has expired, remove
// else renew session
if (row["invalid_after"] < Date.now()) {
DB.prepare("DELETE FROM active_sessions WHERE hashed_sid = ?").run(SIDhash)
return undefined
} else {
DB.prepare("UPDATE active_sessions SET invalid_after = ? WHERE hashed_sid = ?").run(Date.now() + 6 * 60 * 60 * 1000, SIDhash)
}
return { user: row["username"], permissionLevel: row["permission_level"] }
}
function sessionUsername( req ) {
sid = req.parseCookies()["SID"]
if (!sid) {
return false
}
SIDhash = crypto.createHash('sha512').update(sid).digest('hex')
row = DB.prepare("SELECT * FROM active_sessions WHERE hashed_sid = ?").get(SIDhash)
if (!row) {
return undefined
}
return row["username"]
}
module.exports = function(database) {
DB = database
cleanSessions()
var module = {
login,
logout,
validSession,
removeUserSessions,
sessionUsername,
checkPassword,
changePassword,
createUser,
deleteUser,
userList,
};
return module;
};