-
Notifications
You must be signed in to change notification settings - Fork 2
/
auth.js
264 lines (224 loc) · 9.9 KB
/
auth.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
/**
Copyright 2014 Andrew P. Sillers
This file is part of Lords of the Fey.
Lords of the Fey is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Lords of the Fey is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with Lords of the Fey. If not, see <http://www.gnu.org/licenses/>.
*/
/** @module auth */
var config = require("./config"),
FacebookStrategy = require('passport-facebook').Strategy,
TwitterStrategy = require('passport-twitter').Strategy,
GoogleStrategy = require('passport-google-oauth').OAuth2Strategy;
/**
Decide whether the owner of the given socket can act in the given game
@param socket - Socket.io socket
@param game - game object
@param {Boolean} allowAdvancement - the operation being attempted is a unit level-up (without this, when a level-up choice is pending, the action will fail)
@returns {Boolean}
*/
exports.socketOwnerCanAct = function(socket, game, allowAdvancement) {
var user = socket.request.user;
if(!user) { return false; }
var player = game.players.filter(function(p) { return p.username == user.username })[0];
if(!player) { return false; }
// if the player must resolve a branching level-up
// and this is not an attempt to resolve that
if(player.advancingUnit && !allowAdvancement) { return false; }
return player.team == game.activeTeam;
}
/** Activate passport for th eapp and mongo instance */
exports.initAuth = function(app, mongo, collections) {
var passport = require("passport");
var passwordHash = require("password-hash");
var LocalStrategy = require("passport-local").Strategy;
passport.use(new LocalStrategy(function(username, password, done){
collections.users.findOne({ username : username},function(err,user){
if(err) { return done(err); }
if(!user){
return done(null, false, { message: 'Incorrect username.' });
}
if (passwordHash.verify(password, user.hash)) return done(null, user);
done(null, false, { message: 'Incorrect password.' });
});
}));
app.get('/login.html', function(req, res) {
if(req.user && req.user.username) { res.redirect("/"); return; }
var error = ({
"fail":"Incorrect username or password."
})[req.query.error];
res.render("login.hbs", { config: config, error: error });
});
app.get('/signup.html', function(req, res) {
if(req.user && req.user.username) { res.redirect("/"); return; }
var error = ({
"mismatch":"Password fields did not match.",
"taken": "The username you entered is already taken."
})[req.query.error];
res.render("signup.hbs", { config: config, error: error });
});
app.post('/login',
passport.authenticate('local', { failureRedirect: '/login.html?error=fail' }),
function(req, res) {
res.redirect('/');
});
app.get('/logout', function(req, res){
req.logout();
res.redirect('/');
});
app.post('/changeusername', function(req, res) {
var newName = req.body.newname;
if(newName.replace(/\s/g, "") == "") {
res.redirect('/changeusername.html?error=invalid')
}
if(req.user && req.user.username) {
collections.users.findOne({ username: newName }, function (err, userWithName) {
if(!userWithName) {
collections.users.findOne({ username: req.user.username }, function (err, userRecord) {
userRecord.username = newName;
delete userRecord.unchangedName;
collections.users.save(userRecord, { safe: true }, function(err) {
req.login(userRecord, function(err) {
if(err) { console.log("Error in chaging user name: ", err); }
res.redirect('/')
});
});
});
} else {
res.redirect('/changeusername.html?error=taken')
}
});
} else {
res.redirect("/login.html");
}
});
app.get('/changeusername.html', function(req, res) {
if(req.user && req.user.username) {
res.render("changeusername.hbs", { username: req.user.username });
} else {
res.redirect("/login.html")
}
});
app.post('/signup', function(req, res) {
var username = req.body.username;
var password = req.body.password;
var passConfirm = req.body.passconfirm
collections.users.findOne({ username: username }, function (err, user) {
if (!user) {
if(password == passConfirm) {
var hashedPassword = passwordHash.generate(password);
collections.users.save({ username: username, hash: hashedPassword }, { safe: true }, function(err) {
passport.authenticate('local')(req, res, function () {
res.redirect('/');
});
});
} else {
res.redirect("/signup.html?error=mismatch");
}
} else {
res.redirect("/signup.html?error=taken");
}
});
});
app.get("/onoauthlogin", function(req, res) {
if(req.user && req.user.unchangedName) {
res.redirect("/changeusername.html");
} else {
res.redirect("/");
}
});
/* Facebook auth */
if(config.facebook && config.facebook.enabled) {
passport.use(new FacebookStrategy({
clientID: config.facebook.app_id,
clientSecret: config.facebook.app_secret,
callbackURL: config.origin + "/auth/facebook/callback"
},
function(accessToken, refreshToken, profile, done) {
collections.users.findOne({ fbProfileId : profile.id },function(err,user){
if (err) { return done(err); }
if(!user) {
user = { fbProfileId: profile.id, username: "facebook-"+profile.id, unchangedName:true };
collections.users.save(user, { safe: true }, function(err) {
done(null, user);
});
} else {
done(null, user);
}
});
}));
app.get('/login/facebook', passport.authenticate('facebook'));
app.get('/auth/facebook/callback',
passport.authenticate('facebook', { successRedirect: '/onoauthlogin',
failureRedirect: '/login' }));
}
/* Twitter auth */
if(config.twitter && config.twitter.enabled) {
passport.use(new TwitterStrategy({
consumerKey: config.twitter.consumer_key,
consumerSecret: config.twitter.consumer_secret,
callbackURL: config.origin + "/auth/twitter/callback"
},
function(token, tokenSecret, profile, done) {
collections.users.findOne({ twProfileId : profile.id },function(err,user){
if (err) { return done(err); }
if(!user) {
user = { twProfileId: profile.id, username: "twitter-"+profile.id, unchangedName:true };
collections.users.save(user, { safe: true }, function(err) {
done(null, user);
});
} else {
done(null, user);
}
});
}));
app.get('/login/twitter', passport.authenticate('twitter'));
app.get('/auth/twitter/callback',
passport.authenticate('twitter', { successRedirect: '/onoauthlogin',
failureRedirect: '/login' }));
}
/* Google auth */
if(config.google && config.google.enabled) {
passport.use(new GoogleStrategy({
clientID: config.google.clientID,
clientSecret: config.google.clientSecret,
callbackURL: config.origin + "/auth/google/callback"
},
function(accessToken, refreshToken, profile, done) {
collections.users.findOne({ googProfileId : profile.id },function(err,user){
if (err) { return done(err); }
if(!user) {
user = { googProfileId: profile.id, username: "google-"+profile.id, unchangedName:true };
collections.users.save(user, { safe: true }, function(err) {
done(null, user);
});
} else {
done(null, user);
}
})
}));
app.get('/auth/google',
passport.authenticate('google', { scope: 'https://www.googleapis.com/auth/plus.login' }));
app.get('/auth/google/callback',
passport.authenticate('google', { failureRedirect: '/login' }),
function(req, res) {
// Successful authentication, redirect
res.redirect('/onoauthlogin');
});
}
passport.serializeUser(function(user, done) {
done(null, user.username);
});
passport.deserializeUser(function(username, done) {
collections.users.findOne({username: username}, function (err, user) {
done(err, user);
});
});
}