2019-05-01 19:38:11 +00:00
|
|
|
import slugify from 'slugify';
|
|
|
|
import friendlyWords from 'friendly-words';
|
2018-11-06 16:28:17 +00:00
|
|
|
import lodash from 'lodash';
|
2017-02-22 19:29:35 +00:00
|
|
|
|
2018-11-06 16:28:17 +00:00
|
|
|
import passport from 'passport';
|
|
|
|
import GitHubStrategy from 'passport-github';
|
|
|
|
import LocalStrategy from 'passport-local';
|
|
|
|
import GoogleStrategy from 'passport-google-oauth20';
|
|
|
|
import { BasicStrategy } from 'passport-http';
|
2018-04-18 20:38:02 +00:00
|
|
|
|
2018-11-06 16:28:17 +00:00
|
|
|
import User from '../models/user';
|
2016-05-17 19:50:37 +00:00
|
|
|
|
|
|
|
passport.serializeUser((user, done) => {
|
|
|
|
done(null, user.id);
|
|
|
|
});
|
|
|
|
|
|
|
|
passport.deserializeUser((id, done) => {
|
|
|
|
User.findById(id, (err, user) => {
|
|
|
|
done(err, user);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
2017-01-06 23:14:42 +00:00
|
|
|
* Sign in using Email/Username and Password.
|
2016-05-17 19:50:37 +00:00
|
|
|
*/
|
|
|
|
passport.use(new LocalStrategy({ usernameField: 'email' }, (email, password, done) => {
|
2020-04-06 23:01:37 +00:00
|
|
|
User.findByMailOrName(email)
|
2018-05-05 00:22:39 +00:00
|
|
|
.then((user) => { // eslint-disable-line consistent-return
|
|
|
|
if (!user) {
|
|
|
|
return done(null, false, { msg: `Email ${email} not found.` });
|
2016-05-17 19:50:37 +00:00
|
|
|
}
|
2018-05-05 00:22:39 +00:00
|
|
|
user.comparePassword(password, (innerErr, isMatch) => {
|
|
|
|
if (isMatch) {
|
|
|
|
return done(null, user);
|
|
|
|
}
|
|
|
|
return done(null, false, { msg: 'Invalid email or password.' });
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(err => done(null, false, { msg: err }));
|
2016-05-17 19:50:37 +00:00
|
|
|
}));
|
|
|
|
|
2018-11-06 16:28:17 +00:00
|
|
|
/**
|
|
|
|
* Authentificate using Basic Auth (Username + Api Key)
|
|
|
|
*/
|
|
|
|
passport.use(new BasicStrategy((userid, key, done) => {
|
|
|
|
User.findOne({ username: userid }, (err, user) => { // eslint-disable-line consistent-return
|
|
|
|
if (err) { return done(err); }
|
|
|
|
if (!user) { return done(null, false); }
|
2019-05-15 12:07:46 +00:00
|
|
|
user.findMatchingKey(key, (innerErr, isMatch, keyDocument) => {
|
2018-11-06 16:28:17 +00:00
|
|
|
if (isMatch) {
|
2019-05-15 12:07:46 +00:00
|
|
|
keyDocument.lastUsedAt = Date.now();
|
|
|
|
user.save();
|
2018-11-06 16:28:17 +00:00
|
|
|
return done(null, user);
|
|
|
|
}
|
|
|
|
return done(null, false, { msg: 'Invalid username or API key' });
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
2017-04-13 16:04:10 +00:00
|
|
|
/*
|
|
|
|
Input:
|
|
|
|
[
|
|
|
|
{ value: 'email@example.com', primary: false, verified: true },
|
|
|
|
{ value: 'unverified@example.com', primary: false, verified: false }
|
|
|
|
]
|
|
|
|
|
|
|
|
Output:
|
|
|
|
['email@example.com']
|
|
|
|
*/
|
|
|
|
const getVerifiedEmails = githubEmails => (
|
|
|
|
(githubEmails || [])
|
|
|
|
.filter(item => item.verified === true)
|
|
|
|
.map(item => item.value)
|
|
|
|
);
|
|
|
|
|
|
|
|
const getPrimaryEmail = githubEmails => (
|
|
|
|
(
|
|
|
|
lodash.find(githubEmails, { primary: true }) || {}
|
|
|
|
).value
|
|
|
|
);
|
|
|
|
|
2016-05-17 19:50:37 +00:00
|
|
|
/**
|
|
|
|
* Sign in with GitHub.
|
|
|
|
*/
|
2016-08-31 16:28:06 +00:00
|
|
|
passport.use(new GitHubStrategy({
|
|
|
|
clientID: process.env.GITHUB_ID,
|
|
|
|
clientSecret: process.env.GITHUB_SECRET,
|
|
|
|
callbackURL: '/auth/github/callback',
|
2017-04-13 16:04:10 +00:00
|
|
|
passReqToCallback: true,
|
|
|
|
scope: ['user:email'],
|
2016-08-31 16:28:06 +00:00
|
|
|
}, (req, accessToken, refreshToken, profile, done) => {
|
2017-02-22 19:29:35 +00:00
|
|
|
User.findOne({ github: profile.id }, (findByGithubErr, existingUser) => {
|
2016-08-31 19:30:37 +00:00
|
|
|
if (existingUser) {
|
2017-02-22 19:29:35 +00:00
|
|
|
done(null, existingUser);
|
|
|
|
return;
|
2016-08-31 19:30:37 +00:00
|
|
|
}
|
2017-04-13 16:04:10 +00:00
|
|
|
|
|
|
|
const emails = getVerifiedEmails(profile.emails);
|
|
|
|
const primaryEmail = getPrimaryEmail(profile.emails);
|
|
|
|
|
|
|
|
User.findOne({
|
|
|
|
email: { $in: emails },
|
|
|
|
}, (findByEmailErr, existingEmailUser) => {
|
2016-08-31 19:30:37 +00:00
|
|
|
if (existingEmailUser) {
|
2017-04-13 16:04:10 +00:00
|
|
|
existingEmailUser.email = existingEmailUser.email || primaryEmail;
|
2016-08-31 19:30:37 +00:00
|
|
|
existingEmailUser.github = profile.id;
|
|
|
|
existingEmailUser.username = existingEmailUser.username || profile.username;
|
|
|
|
existingEmailUser.tokens.push({ kind: 'github', accessToken });
|
|
|
|
existingEmailUser.name = existingEmailUser.name || profile.displayName;
|
2017-06-26 16:48:28 +00:00
|
|
|
existingEmailUser.verified = User.EmailConfirmation.Verified;
|
2017-02-22 19:29:35 +00:00
|
|
|
existingEmailUser.save(saveErr => done(null, existingEmailUser));
|
2016-08-31 16:28:06 +00:00
|
|
|
} else {
|
2016-08-31 19:30:37 +00:00
|
|
|
const user = new User();
|
2017-04-13 16:04:10 +00:00
|
|
|
user.email = primaryEmail;
|
2016-08-31 19:30:37 +00:00
|
|
|
user.github = profile.id;
|
|
|
|
user.username = profile.username;
|
|
|
|
user.tokens.push({ kind: 'github', accessToken });
|
|
|
|
user.name = profile.displayName;
|
2017-06-26 16:48:28 +00:00
|
|
|
user.verified = User.EmailConfirmation.Verified;
|
2017-02-22 19:29:35 +00:00
|
|
|
user.save(saveErr => done(null, user));
|
2016-08-31 16:28:06 +00:00
|
|
|
}
|
|
|
|
});
|
2016-08-31 19:30:37 +00:00
|
|
|
});
|
2016-08-31 16:28:06 +00:00
|
|
|
}));
|
2018-04-18 20:38:02 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sign in with Google.
|
|
|
|
*/
|
|
|
|
passport.use(new GoogleStrategy({
|
|
|
|
clientID: process.env.GOOGLE_ID,
|
|
|
|
clientSecret: process.env.GOOGLE_SECRET,
|
|
|
|
callbackURL: '/auth/google/callback',
|
|
|
|
passReqToCallback: true,
|
2019-05-01 19:38:11 +00:00
|
|
|
scope: ['openid email'],
|
2018-04-18 20:38:02 +00:00
|
|
|
}, (req, accessToken, refreshToken, profile, done) => {
|
|
|
|
User.findOne({ google: profile._json.emails[0].value }, (findByGoogleErr, existingUser) => {
|
|
|
|
if (existingUser) {
|
|
|
|
done(null, existingUser);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const primaryEmail = profile._json.emails[0].value;
|
|
|
|
|
|
|
|
User.findOne({
|
|
|
|
email: primaryEmail,
|
|
|
|
}, (findByEmailErr, existingEmailUser) => {
|
2019-05-01 19:38:11 +00:00
|
|
|
let username = profile._json.emails[0].value.split('@')[0];
|
|
|
|
User.findOne({ username }, (findByUsernameErr, existingUsernameUser) => {
|
|
|
|
if (existingUsernameUser) {
|
|
|
|
const adj = friendlyWords.predicates[Math.floor(Math.random() * friendlyWords.predicates.length)];
|
|
|
|
username = slugify(`${username} ${adj}`);
|
|
|
|
}
|
|
|
|
// what if a username is already taken from the display name too?
|
|
|
|
// then, append a random friendly word?
|
|
|
|
if (existingEmailUser) {
|
|
|
|
existingEmailUser.email = existingEmailUser.email || primaryEmail;
|
|
|
|
existingEmailUser.google = profile._json.emails[0].value;
|
|
|
|
existingEmailUser.username = existingEmailUser.username || username;
|
|
|
|
existingEmailUser.tokens.push({ kind: 'google', accessToken });
|
|
|
|
existingEmailUser.name = existingEmailUser.name || profile._json.displayName;
|
|
|
|
existingEmailUser.verified = User.EmailConfirmation.Verified;
|
|
|
|
existingEmailUser.save((saveErr) => {
|
|
|
|
if (saveErr) {
|
|
|
|
console.log(saveErr);
|
|
|
|
}
|
|
|
|
done(null, existingEmailUser);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
const user = new User();
|
|
|
|
user.email = primaryEmail;
|
|
|
|
user.google = profile._json.emails[0].value;
|
|
|
|
user.username = username;
|
|
|
|
user.tokens.push({ kind: 'google', accessToken });
|
|
|
|
user.name = profile._json.displayName;
|
|
|
|
user.verified = User.EmailConfirmation.Verified;
|
|
|
|
user.save((saveErr) => {
|
|
|
|
if (saveErr) {
|
|
|
|
console.log(saveErr);
|
|
|
|
}
|
|
|
|
done(null, user);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2018-04-18 20:38:02 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}));
|