-
Notifications
You must be signed in to change notification settings - Fork 0
/
users.js
143 lines (111 loc) · 3.14 KB
/
users.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
var Util = require('./util');
var Db = require('mongodb').Db,
MongoClient = require('mongodb').MongoClient,
ObjectID = require('mongodb').ObjectID,
Server = require('mongodb').Server,
crypto = require('crypto'),
Zip = require('node-zip'),
log4js = require('log4js'),
logger = log4js.getLogger();
// See docs @ http://mongodb.github.io/node-mongodb-native/
var Users = Util.Class({
init: function(db) {
this._db = db;
},
getUser: function(username, callback) {
var users = this._db.collection('users');
if (!callback) return;
if (!username) {
callback(new Error('No token specified for authentication'));
return;
}
users.findOne({username: username}, callback);
},
checkUser: function(username, password, callback) {
var users = this._db.collection('users'),
hashes = this._db.collection('hashes'),
thisUser = null;
if (!callback) return;
if (!username || !password) {
callback(new Error('No token specified for authentication'));
return;
}
users.findOne({username: username}, function(err, user) {
if (err || !user) {
logger.error(err);
callback(new Error('Could not retrieve user'));
return;
}
thisUser = user;
if (!user.salt) {
logger.error('No salt set for user - not authenticated');
callback(new Error('No salt set for user - not authenticated'));
return;
}
var sha512 = crypto.createHash('sha512');
sha512.update(user.salt + password);
hashes.findOne({hash: sha512.digest('hex')}, onHashFound);
});
var onHashFound = function(err, item) {
if (err || !item) {
logger.error(err);
callback(new Error('Could not validate token'));
return;
}
callback(null, true);
}.bind(this)
},
updateUser: function(login, email, name, password, callback) {
var users = this._db.collection('users');
users.findOne({username: login}, function(err, item) {
if (err || !item) {
users.insert({
username: login,
email: email,
name: name
}, {w: 1}, function(err, result) {
if (err || !result || !result.length) {
logger.error(err);
resp.end(500, "Error creating user");
return;
}
this._doUpdateUser(result[0], password, callback);
}.bind(this));
return;
}
this._doUpdateUser(item, password, callback);
}.bind(this));
},
_doUpdateUser: function(user, password, callback) {
var users = this._db.collection('users'),
hashes = this._db.collection('hashes');
user.salt = crypto.randomBytes(256);
users.save(user);
var sha512 = crypto.createHash('sha512');
sha512.update(user.salt + password);
hashes.insert({
hash: sha512.digest('hex')
}, {w: 1}, function(err, result) {
if (err) {
logger.error(err);
resp.end(500, "Error creating user");
return;
}
if (callback) callback(err, true);
});
},
clearUser: function(username, callback) {
var users = this._db.collection('users');
users.findOne({username: username}, function(err, user) {
if (err || !user) {
log.error(err);
if (callback) callback(err);
return;
}
user.salt = null;
users.save(user);
if (callback) callback(null, true);
}.bind(this));
}
});
module.exports = Users;