-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmongo.js
348 lines (313 loc) · 9.92 KB
/
mongo.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
var Q = require('q');
var _ = require('lodash');
var crypto = require('crypto');
var errorHelper = rootRequire('./helpers/error');
var winston = require('winston');
var passwordHelper = rootRequire('./helpers/password');
var models = require('./models');
var User = models.user;
var Authentication = models.authentications;
var OauthSession = models.oauthSession;
//API
module.exports = {
addUser: addUser,
checkAccessTokenScope: checkAccessTokenScope,
createAccessToken: createAccessToken,
createCode: createCode,
createForceLogin: createForceLogin,
createOAuthAuthentication: createOAuthAuthentication,
getAuthorization: getAuthorization,
getForceLogin: getForceLogin,
getOAuthAuthentication: getOAuthAuthentication,
getUser: getUser,
updateAccessToken: updateAccessToken,
updateUser: updateUser,
updateUserPasswordForgot: updateUserPasswordForgot,
updateUserResetHash: updateUserResetHash
};
function updateUserPasswordForgot(email, hash, newPassword) {
return User.findOneAndUpdate({
email: email,
passwordResetHash: hash
}, {
passwordResetHash: '',
password: passwordHelper.generate_password(newPassword)
}).exec()
.then(function(user) {
if (!user) {
throw new Error('Invalid password reset code');
}
return user;
})
.then(null, function(err) {
winston.error('', {
error: err.message,
stack: err.stack,
email: email
});
throw err;
});
}
//update user authentication with new accessToken
function updateAccessToken(userId, access_token) {
return Authentication.findOneAndUpdate({
_id: userId
}, {
access_token: access_token
}).exec()
.then(null, function(err) {
winston.error('Error updating authentication', {
error: err.message,
stack: err.stack,
userId: userId,
access_token: access_token
});
throw err;
});
}
//update user authentication with new accessToken
function getOAuthAuthentication(query) {
return Authentication.findOne({
provider: query.provider,
uid: query.uid
}).exec()
.then(function(auth) {
if (!auth) {
winston.warn("Authentication entry doesn't exist for that query", {
query: query
});
return;
}
return auth;
})
.then(null, function(err) {
winston('Error getting OauthAuthenticaion', {
error: err.message,
stack: err.stack,
query: query ? query : null
});
throw err;
});
}
function createOAuthAuthentication(authentication) {
return Q.ninvoke(new Authentication(authentication), 'save')
.spread(function(auth) {
if (!auth) {
throw new Error('Error saving new authentication to mongo');
}
return auth;
}).then(null, function(err) {
winston.error('Error creating new authentication', {
error: err.message,
stack: err.stack,
authentication: authentication ? authentication : null
});
throw err;
});
}
function addUser(user) {
//check the user exists
return User.findOne({
username: user.username,
clientId: user.clientId
}).exec()
.then(function(existingUser) {
if (existingUser) {
throw new Error('User already Exists');
}
return Q.ninvoke(new User(user), 'save')
.spread(function(user) {
if (!user) {
throw new Error('Error creating user');
}
return user;
});
})
.then(null, function(err) {
switch (err.message) {
case 'User already Exists':
winston.warn('User already exists', {
warning: err.message,
stack: err.stack,
user: JSON.stringify(user)
});
break;
default:
winston.error('Mongoose Error: Creating new user', {
error: err.message,
stack: err.stack,
user: JSON.stringify(user)
});
}
throw err;
});
}
function getUser(query) {
return User.findOne(query).exec()
.then(function(user) {
return user;
})
.then(null, function(err) {
winston.warn('Mongoose Error: Error Retrieving user', {
error: err.message,
stack: err.stack,
query: query
});
throw err;
});
}
function updateUser(query, update) {
return User.findOneAndUpdate(query, update).exec()
.then(function(user) {
if (!user) {
throw new Error('No user associtated with that query');
}
return user;
})
.then(null, function(err) {
winston.error('Error updating user', {
error: err.message,
stack: err.stack
});
throw err;
});
}
function updateUserResetHash(email, hash) {
return User.findOneAndUpdate({
username: email
}, {
passwordResetHash: hash
}).exec()
.then(function(user) {
if (!user) {
throw new Error('That email is not associated with an account');
}
return user;
}).then(null, function(err) {
winston.error('Error updating user', {
error: err.message,
stack: err.stack,
email: email,
hash: hash
});
throw err;
});
}
function getAuthorization(query) {
return OauthSession.findOne(query)
.exec()
.then(function(auth) {
if (!auth) {
throw new Error('Invalid query');
}
return auth;
})
.then(null, function(err) {
winston.error('Error finding auth for that query', {
error: err.message,
stack: err.stack,
query: query
});
throw err;
});
}
function createCode(data) {
return OauthSession.findOneAndUpdate({
userId: data.userId
}, data, {
upsert: true,
new: true
}).exec()
.then(function(session) {
if (!session) {
throw new Error('No session updated or created');
}
return session;
})
.then(null, function(err) {
winston.error('Error creating code', {
error: err.message,
stack: err.stack,
data: data
});
throw err;
});
}
function createAccessToken(data) {
return Q.nfcall(crypto.randomBytes, 48)
.then(function(buf) {
if (!buf) {
throw new Error('Error generating access_token');
}
return buf.toString('hex');
})
.then(function(accessToken) {
return OauthSession.create(_.assign(data, {
access_token: accessToken
}));
});
}
function createForceLogin(userId, callback) {
var code = crypto.randomBytes(24)
.toString('hex');
models.forceLogin.create({
userId: userId,
code: code,
createdAt: new Date()
}).then(function() {
callback(null, code);
}).then(null, function(err) {
winston.error("Error creating force login", {
error: err.message,
stack: err.stack,
userId: userId
});
return errorHelper.databaseError(err, 'Error inserting force_login') && callback(err);
});
}
function getForceLogin(code, callback) {
models.forceLogin.findOne({
code: code
}).exec().then(function(force_login) {
if (!force_login) {
throw new Error('no login found for this code');
}
if (force_login.createdAt.getTime() + 10 * 60 * 1000 < new Date().getTime()) {
throw new Error('login has expired');
}
return callback(null, force_login.userId);
}).then(null, function(err) {
winston.error("Error getting force login", {
error: err.message,
stack: err.stack,
code: code
});
return errorHelper.databaseError(err, 'Error getting force_login') && callback(err);
});
}
/**
* callback: function(err:Error, has_scope:boolean, session_expired:boolen)
*/
function checkAccessTokenScope(access_token, scope, callback) {
OauthSession.findOne({
access_token: access_token
}).exec()
.then(function(session) {
if (!session) {
throw new Error('No scope associated with that session'); //doesn't have scope and is expired
}
if (!session.expiryDate) {
//shouldn't happen so considered expired if it doesn't have an expiry date
throw new Error('No expiryDate set on that oauth session');
}
return callback(null, session.scopes.indexOf(scope) > -1, session.expiryDate < new Date());
}).then(null, function(err) {
winston.error('Error during checkAccessTokenScope', {
error: err.message,
stack: err.stack,
access_token: access_token,
scope: scope
});
return callback(null, false, true);
});
}