-
Notifications
You must be signed in to change notification settings - Fork 3
/
passport.js
305 lines (270 loc) · 11 KB
/
passport.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
exports = module.exports = function(app, passport) {
var passportLocalStrategy = require('passport-local').Strategy,
config = require('./config.json'),
passportTwitterStrategy = require('passport-twitter').Strategy,
passportGitHubStrategy = require('passport-github').Strategy;
// passport use local strategy
passport.use(new passportLocalStrategy(
function(username, password, done) {
//lookup conditions
var conditions = {
isActive: 'yes'
};
if (username.indexOf('@') === -1) {
conditions.username = username;
}
else {
conditions.email = username;
}
app.db.models.User.findOne(conditions, function(err, user) {
if (err) return done(err);
if (!user) return done(null, false, {
message: 'Unknown user'
});
//validate password
var encryptedPassword = app.db.models.User.encryptPassword(password);
if (user.password != encryptedPassword) {
return done(null, false, {
message: 'Invalid password'
});
}
//we're good
return done(null, user);
});
}));
// passport use github strategy
if (config.auth.github.consumerkey.length) {
passport.use(new passportGitHubStrategy({
clientID: config.auth.github.consumerkey,
clientSecret: config.auth.github.consumersecret,
callbackURL: config.auth.github.callback,
passReqToCallback: true
},
function(req, token, refreshToken, profile, done) {
if (!req.user) {
app.db.models.User.findOne({
'github.id': profile.id
}, function(err, user) {
if (err) {
return done(err)
}
if (!user) {
console.log('github auth: - user not found');
var gh_username = '';
var fieldsToSet = {};
//TODO: sanitize profile.username / check for allowed characters
//if yes check in dbs - create function to check for existing username in 3rd party passport use
app.db.models.User.findOne({
'username': profile.username
}, function(err, testuser) {
if (err) {
return done(err)
}
if (!testuser) {
if (/^[a-zA-Z0-9\-\_]+$/.test(profile.username)) {
gh_username = profile.username;
fieldsToSet = {
isActive: 'no',
username: gh_username,
email: profile.emails[0].value,
github: profile._json
};
app.db.models.User.create(fieldsToSet, function(err, user) {
if (err) {
return done(err)
}
return done(err, user);
});
}
}
else { //username already in dbs
fieldsToSet = {
isActive: 'no',
username: gh_username,
email: profile.emails[0].value,
github: profile._json
};
app.db.models.User.create(fieldsToSet, function(err, user) {
if (err) {
return done(err)
}
return done(err, user);
});
}
});
}
else {
return done(err, user);
}
});
}//!req.user
else { // is authenticated user we add 3rd party to the user profile
app.db.models.User.findOne({
_id: req.user._id
}, function(err, user) {
if (err) {
return done(err)
}
if (!user) {
console.log('passport deserialize: - user not found');
return done(err, req.user);
}
else {
//TODO: check if profile.id already in db
user.github = profile._json;
user.save(function(err, user) {
if (err) {
return done(err);
}
return done(err, user);
});
}
});
return done(null, req.user);
}// else - is authenticated user we add 3rd party to the user profile
}));
}
// passport use twitter strategy
if (config.auth.twitter.consumerkey.length) {
passport.use(new passportTwitterStrategy({
consumerKey: config.auth.twitter.consumerkey,
consumerSecret: config.auth.twitter.consumersecret,
callbackURL: config.auth.twitter.callback,
passReqToCallback: true
},
function(req, token, tokenSecret, profile, done) {
if (!req.user) {
app.db.models.User.findOne({
'twitter.id': profile.id
}, function(err, user) {
if (err) {
return done(err)
}
if (!user) {
console.log('twitter auth: - user not found');
var tw_username = '';
var fieldsToSet = {};
//TODO: sanitize profile.username / check for allowed characters
//if yes check in dbs
app.db.models.User.findOne({
'username': profile.username
}, function(err, testuser) {
if (err) {
return done(err)
}
if (!testuser) {
if (/^[a-zA-Z0-9\-\_]+$/.test(profile.username)) {
tw_username = profile.username;
fieldsToSet = {
isActive: 'no',
username: tw_username,
twitter: profile._json
};
app.db.models.User.create(fieldsToSet, function(err, user) {
if (err) {
return done(err)
}
return done(err, user);
});
}
}
else { //username already in dbs
fieldsToSet = {
isActive: 'no',
username: tw_username,
twitter: profile._json
};
app.db.models.User.create(fieldsToSet, function(err, user) {
if (err) {
return done(err)
}
return done(err, user);
});
}
});
}
else {
return done(err, user);
}
});
}//!req.user
else {
// console.log('twitter strategy (account): ' + JSON.stringify(req.user));
// console.log('twitter strategy (account): ' + JSON.stringify(profile));
app.db.models.User.findOne({
_id: req.user._id
}, function(err, user) {
if (err) {
return done(err)
}
if (!user) {
console.log('passport deserialize: - user not found');
return done(err, req.user);
}
else {
//TODO: check if profile.id already in db
user.twitter = profile._json;
user.save(function(err, user) {
if (err) {
return done(err);
}
return done(err, user);
});
}
});
return done(null, req.user);
}
}));
}
//--------------------------------------------------------------
passport.serializeUser(function(user, done) {
done(null, user._id);
});
passport.deserializeUser(function(id, done) {
app.db.models.User.findOne({
_id: id
}, function(err, user) {
if (err) {
return done(err)
}
if (!user) {
console.log('passport deserialize: - user not found');
return done(err, user);
}
else {
if (user.isActive == 'no') { //user not active -> has no account in dbs e.g. created from 3rd party auth provider
console.log('passport deserialize: - user without account');
return done(err, user);
}
else { //user is active -> has account
app.db.models.User.findOne({
_id: id
}).populate('roles.admin').populate('roles.account').exec(function(err, user) {
/*
* TODO:
* when mongoose supports calling populate on embedded documents,
* we can change this code and stop using the '_groups' hack since
* assigning direcly to 'groups' doesn't stick right now
* https://github.com/LearnBoost/mongoose/issues/601
*
*/
if (user.roles && user.roles.admin && user.roles.admin.groups) {
app.db.models.AdminGroup.find({
_id: {
$in: user.roles.admin.groups
}
}).exec(function(err, groups) {
user.roles.admin._groups = groups;
done(err, user);
});
}
else {
done(err, user);
//done(null, id);
}
});
}
}
});
});
};