forked from jedireza/drywall
-
Notifications
You must be signed in to change notification settings - Fork 0
/
passport.js
137 lines (123 loc) · 3.73 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
'use strict';
exports = module.exports = function(app, passport) {
var LocalStrategy = require('passport-local').Strategy,
TwitterStrategy = require('passport-twitter').Strategy,
GitHubStrategy = require('passport-github').Strategy,
FacebookStrategy = require('passport-facebook').Strategy,
GoogleStrategy = require('passport-google-oauth').OAuth2Strategy,
TumblrStrategy = require('passport-tumblr').Strategy;
passport.use(new LocalStrategy(
function(username, password, done) {
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' });
}
app.db.models.User.validatePassword(password, user.password, function(err, isValid) {
if (err) {
return done(err);
}
if (!isValid) {
return done(null, false, { message: 'Invalid password' });
}
return done(null, user);
});
});
}
));
if (app.config.oauth.twitter.key) {
passport.use(new TwitterStrategy({
consumerKey: app.config.oauth.twitter.key,
consumerSecret: app.config.oauth.twitter.secret
},
function(token, tokenSecret, profile, done) {
done(null, false, {
token: token,
tokenSecret: tokenSecret,
profile: profile
});
}
));
}
if (app.config.oauth.github.key) {
passport.use(new GitHubStrategy({
clientID: app.config.oauth.github.key,
clientSecret: app.config.oauth.github.secret,
customHeaders: { "User-Agent": app.config.projectName }
},
function(accessToken, refreshToken, profile, done) {
done(null, false, {
accessToken: accessToken,
refreshToken: refreshToken,
profile: profile
});
}
));
}
if (app.config.oauth.facebook.key) {
passport.use(new FacebookStrategy({
clientID: app.config.oauth.facebook.key,
clientSecret: app.config.oauth.facebook.secret
},
function(accessToken, refreshToken, profile, done) {
done(null, false, {
accessToken: accessToken,
refreshToken: refreshToken,
profile: profile
});
}
));
}
if (app.config.oauth.google.key) {
passport.use(new GoogleStrategy({
clientID: app.config.oauth.google.key,
clientSecret: app.config.oauth.google.secret
},
function(accessToken, refreshToken, profile, done) {
done(null, false, {
accessToken: accessToken,
refreshToken: refreshToken,
profile: profile
});
}
));
}
if (app.config.oauth.tumblr.key) {
passport.use(new TumblrStrategy({
consumerKey: app.config.oauth.tumblr.key,
consumerSecret: app.config.oauth.tumblr.secret
},
function(token, tokenSecret, profile, done) {
done(null, false, {
token: token,
tokenSecret: tokenSecret,
profile: profile
});
}
));
}
passport.serializeUser(function(user, done) {
done(null, user._id);
});
passport.deserializeUser(function(id, done) {
app.db.models.User.findOne({ _id: id }).populate('roles.admin').populate('roles.account').exec(function(err, user) {
if (user && user.roles && user.roles.admin) {
user.roles.admin.populate("groups", function(err, admin) {
done(err, user);
});
}
else {
done(err, user);
}
});
});
};