-
Notifications
You must be signed in to change notification settings - Fork 1
/
auth.js
222 lines (184 loc) · 6.01 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
"use strict";
/**
* @desc Passport Integration for Oauth2 login
* supported providers: GitHub
*/
module.exports = function(app, mongo, express){
/* OAuth Key-File */
var config = require('./config.js');
var oauth_keys = require('./oauth_keys.js');
var session = require('express-session');
app.use(session({
secret: oauth_keys.session_secret,
resave: false,
saveUninitialized: false
}));
/* Passport & Login Strategies */
var passport = require('passport');
var GitHubStrategy = require('passport-github2').Strategy;
var GoogleStrategy = require('passport-google-oauth').OAuth2Strategy;
var LinkedInStrategy = require('passport-linkedin-oauth2').Strategy;
app.use(passport.initialize());
app.use(passport.session());
/** GITHUB STRATEGY **/
passport.use(new GitHubStrategy({
clientID: oauth_keys.GITHUB_CLIENT_ID,
clientSecret: oauth_keys.GITHUB_CLIENT_SECRET,
callbackURL: 'https://' + config.hostname + ':' + config.httpsPort + '/auth/github/callback/'
},
function(accessToken, refreshToken, profile, done) {
//First we need to check if the user logs in for the first time
mongo.models.users.findOne({
'providerID': profile.id,
'provider': 'github'
}, function(err, user) {
if (err) return done(err);
if (!user) {
// no user existent --> new user --> create a new one
user = new mongo.models.users({
name: profile.displayName,
email: profile.emails[0].value,
username: profile.username,
provider: 'github',
providerID: profile.id
});
user.save(function(err) {
if (err) console.log(err);
return done(err, user);
});
} else {
//user found. return it.
return done(err, user);
}
});
}));
/** GOOGLE STRATEGY **/
passport.use(new GoogleStrategy({
clientID: oauth_keys.GOOGLE_CLIENT_ID,
clientSecret: oauth_keys.GOOGLE_CLIENT_SECRET,
callbackURL: 'https://' + config.hostname + ':' + config.httpsPort + '/auth/google/callback/'
},
function(accessToken, refreshToken, profile, done) {
mongo.models.users.findOne({
'providerID': profile.id,
'provider': 'google'
}, function(err, user) {
if (err) return done(err);
if (!user) {
// no user existent --> new user --> create a new one
// Fix if a user has a Google Account but no real name
var userName = 'Unbekannter Benutzer via Google';
if(profile.displayName) userName = profile.displayName;
user = new mongo.models.users({
name: userName,
email: profile.emails[0].value,
provider: 'google',
providerID: profile.id
});
user.save(function(err) {
if (err) console.log(err);
return done(err, user);
});
} else {
//user found. return it.
return done(err, user);
}
});
}));
/** LINKEDIN STRATEGY **/
passport.use(new LinkedInStrategy({
clientID: oauth_keys.LINKEDIN_KEY,
clientSecret: oauth_keys.LINKEDIN_SECRET,
callbackURL: 'https://' + config.hostname + ':' + config.httpsPort + '/auth/linkedin/callback/',
scope: ['r_emailaddress', 'r_basicprofile']
}, function(accessToken, refreshToken, profile, done) {
mongo.models.users.findOne({
'providerID': profile.id,
'provider': 'linkedin'
}, function(err, user) {
if (err) {
return done(err);
}
if (!user) {
// no user existent --> new user --> create a new one
user = new mongo.models.users({
name: profile.displayName,
email: profile.emails[0].value,
provider: 'linkedin',
providerID: profile.id
});
user.save(function(err) {
if (err) console.log(err);
return done(err, user);
});
} else {
//user found. return it.
return done(err, user);
}
});
}));
/** GITHUB ROUTES **/
// GET /auth/github
// Use passport.authenticate() as route middleware to authenticate the
// request. The first step in GitHub authentication will involve redirecting
// the user to github.com. After authorization, GitHub will redirect the user
// back to this application at /auth/github/callback
app.get('/auth/github',
passport.authenticate('github', { scope: [ 'user:email' ] }), // scope = what data we want access to
function(req, res){
// The request will be redirected to GitHub for authentication, so this
// function will not be called.
});
// GET /auth/github/callback
// Use passport.authenticate() as route middleware to authenticate the
// request. If authentication fails, the user will be redirected back to the
// login page. Otherwise, the primary route function will be called,
// which, in this example, will redirect the user to the home page.
app.get('/auth/github/callback',
passport.authenticate('github', { failureRedirect: '/login' }),
function(req, res) {
res.redirect('/');
});
/** GOOGLE ROUTES **/
// GET /auth/google
app.get('/auth/google', passport.authenticate('google', { scope: [ 'https://www.googleapis.com/auth/plus.login', 'https://www.googleapis.com/auth/plus.profile.emails.read' ] }));
// GET /auth/google/callback
app.get('/auth/google/callback',
passport.authenticate('google', { failureRedirect: '/login' }),
function(req, res) {
// Successful authentication, redirect home.
res.redirect('/');
});
/** LINKEDIN ROUTES **/
// GET /auth/linkedin
app.get('/auth/linkedin',
passport.authenticate('linkedin', { state: oauth_keys.LINKEDIN_SECRET }),
function(req, res){
});
// GET /auth/linkedin/callback
app.get('/auth/linkedin/callback', passport.authenticate('linkedin', {
successRedirect: '/',
failureRedirect: '/login'
}));
/** Additional Passport Routes **/
// Can be used to log a user out.
app.get('/logout', function(req, res){
req.logout();
res.redirect('/');
});
// Can be used to check for the Login status of the current user
app.get('/getAuthStatus', function(req,res){
if(req.user){
res.send('Auth successful');
}else{
res.send('Auth unsuccessful');
}
});
/* passport serialization functions */
passport.serializeUser(function(user, done) {
done(null, user);
});
passport.deserializeUser(function(user, done) {
done(null, user);
});
};