-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpassport_init.js
71 lines (66 loc) · 2.13 KB
/
passport_init.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
const GoogleStrategy = require('passport-google-oauth').OAuth2Strategy;
const passport = require('passport');
const core = require('./api/internal/core');
let db = require('./db/database_mongo');
const WHITELIST_DOMAINS = ['gpmail.org'];
const WHITELIST_EMAILS = [
const getDomain = (email) => {
if (email !== undefined) {
const parts = email.split(`@`);
if (parts.length === 2) {
return parts[1];
}
}
core.log(`email does not have domain: ${email}`);
};
passport.use(new GoogleStrategy({
clientID: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_SECRET,
callbackURL: '/auth/google/callback'
},
function (token, tokenSecret, profile, cb) {
if (profile) {
const email = profile._json.email || profile.emails[0].value;
const domain = profile._json.hd || getDomain(email);
const user = { googleId: profile.id,
name: profile.displayName,
givenName: profile.name.givenName,
familyName: profile.name.familyName,
imageUrl: profile._json.picture,
email: email
};
core.log(`domain: ${domain}`);
core.log(`whitelist domain: ${WHITELIST_DOMAINS.indexOf(domain)}`);
core.log(profile._json);
if (WHITELIST_DOMAINS.indexOf(domain) === -1 && WHITELIST_EMAILS.indexOf(email) === -1) {
return cb(new Error(`Invalid Domain: ${domain} and Invalid Email: ${email}`));
}
db.createUserByGoogleId({ user_data: user });
return cb(null, user);
}
return cb(new Error(`Could not create or find user with profile: ${profile}`));
}));
passport.serializeUser(function (user, done) {
done(null, user.googleId);
});
passport.deserializeUser(function (id, done) {
db.getUser(id).then(user => {
done(null, user);
});
});