-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauthmgmt.js
More file actions
178 lines (129 loc) · 3.63 KB
/
authmgmt.js
File metadata and controls
178 lines (129 loc) · 3.63 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
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
var mongoose = require('mongoose');
var bcrypt = require('bcryptjs');
var crypto = require('crypto');
var db;
var currentSessions = [];
var userSchema = mongoose.Schema({
username: {type: String, lowercase: true},
passHash: String,
});
var sessionSchema = mongoose.Schema({
username: {type: String, lowercase: true},
id: String,
exp: Number
});
var User = mongoose.model('User', userSchema);
var Session = mongoose.model('Session', sessionSchema);
//connect to mongodb
mongoose.connect('mongodb://localhost/users');
db = mongoose.connection;
function addCredentials(username, password, callback) {
var salt, hash, i, user;
//check if username exist
User.find({username: username}, function(error, userData){
//if it doesn't exist then add it
if(userData.length === 0) {
user = new User();
user.username = username;
//generate a salt
bcrypt.genSalt(10, function(err, salt) {
//when salt is done hash the password
bcrypt.hash(password, salt, function(err, hash) {
//when hash is done store new credentials in db
user.passHash = hash;
user.save(function (err) {
if(!err) {
callback('user added');
return;
}
});
});
});
}
//otherwise user already exist
else {
callback('user taken');
return;
}
});
}
function checkCredentials(username, password, callback) {
var user;
//get the user information
User.find({username: username}, function(error, userData) {
//does the user exist
if(userData.length === 0) {
callback('no such user');
return;
}
else {
user = userData[0];
//check the password
bcrypt.compare(password, user.passHash, function(err, res) {
if(res === true) {
//respond with the session information
callback('user validated', user.username);
return;
}
else {
callback("invalid password", user.username);
return;
}
});
}
});
}
function makeSession(username, callback) {
var session = new Session();
session.username = username;
//generate a sessionId
crypto.randomBytes(32, function(err, buf) {
session.id = buf.toString('base64');
//generate a session experation
//an hour from the current time
session.exp = (Date.now() + 3600000);
//put session in db
session.save();
//put session in cashed sessions
currentSessions.push(session);
callback(session);
});
}
function checkSession(sessionId) {
var i;
for(i=0; i < currentSessions.length; i++) {
if(currentSessions[i].id === sessionId) {
break;
}
}
if(i >= currentSessions.length) {
return 'session not found';
}
else if(currentSessions[i].exp < Date.now()) {
return 'session is expired';
}
else {
return 'session is valid';
}
}
function loadCurrentSessions() {
Session.find( {exp: {$gt: Date.now()} }, function(err, sessions) {
currentSessions = sessions;
});
}
//when loading the module go ahead and load
//any old sessions that have not expired
loadCurrentSessions();
/*
checkCredentials('gandalf', 'xyzzy', function(status, username) {
console.log('checking ' + username + ': ' + status);
makeSession(username, function(session) {
console.log(session);
});
});
*/
exports.addCredentials = addCredentials;
exports.checkCredentials = checkCredentials;
exports.makeSession = makeSession;
exports.checkSession = checkSession;
exports.loadCurrentSessions = loadCurrentSessions;