Remove o-auth account creation

Accounts can only be created with Github or email
This commit is contained in:
Berkeley Martinez
2016-04-21 20:35:19 -07:00
parent 428cf8135e
commit 8166bfbcd8
4 changed files with 86 additions and 96 deletions

View File

@ -12,18 +12,6 @@ const { defaultProfileImage } = require('../utils/constantStrings.json');
const githubRegex = (/github/i); const githubRegex = (/github/i);
const debug = debugFactory('fcc:models:userIdent'); const debug = debugFactory('fcc:models:userIdent');
function createAccessToken(user, ttl, cb) {
if (arguments.length === 2 && typeof ttl === 'function') {
cb = ttl;
ttl = 0;
}
user.accessTokens.create({
created: new Date(),
ttl: Math.min(ttl || user.constructor.settings.ttl,
user.constructor.settings.maxTTL)
}, cb);
}
export default function(UserIdent) { export default function(UserIdent) {
// original source // original source
// github.com/strongloop/loopback-component-passport // github.com/strongloop/loopback-component-passport
@ -40,88 +28,89 @@ export default function(UserIdent) {
cb = options; cb = options;
options = {}; options = {};
} }
const autoLogin = options.autoLogin || !options.autoLogin;
const userIdentityModel = UserIdent; const userIdentityModel = UserIdent;
profile.id = profile.id || profile.openid; profile.id = profile.id || profile.openid;
return userIdentityModel.findOne({ const filter = {
where: { where: {
provider: getSocialProvider(provider), provider: getSocialProvider(provider),
externalId: profile.id externalId: profile.id
} }
}) };
.then(identity => { return userIdentityModel.findOne(filter)
if (identity) { .then(identity => {
identity.credentials = credentials; // identity already exists
return identity.updateAttributes({ // find user and log them in
profile: profile, if (identity) {
credentials: credentials, identity.credentials = credentials;
modified: new Date() const options = {
}) profile: profile,
.then(function() { credentials: credentials,
// Find the user for the given identity modified: new Date()
return identity.user(function(err, user) { };
// Create access token if the autoLogin flag is set to true return identity.updateAttributes(options)
if (!err && user && autoLogin) { // grab user associated with identity
return (options.createAccessToken || createAccessToken)( .then(() => identity.user())
user, .then(user => {
function(err, token) { // Create access token for user
cb(err, user, identity, token); const options = {
} created: new Date(),
); ttl: user.constructor.settings.ttl
} };
return cb(err, user, identity); return user.accessTokens.create(options)
}); .then(token => ({ user, token }));
}); })
} .then(({ token, user })=> {
// Find the user model cb(null, user, identity, token);
const userModel = userIdentityModel.relations.user && })
userIdentityModel.relations.user.modelTo || .catch(err => cb(err));
loopback.getModelByType(loopback.User);
const userObj = options.profileToUser(provider, profile, options);
if (!userObj.email && !options.emailOptional) {
process.nextTick(function() {
return cb('email is missing from the user profile');
});
}
const query;
if (userObj.email) {
query = { or: [
{ username: userObj.username },
{ email: userObj.email }
]};
} else {
query = { username: userObj.username };
}
return userModel.findOrCreate({ where: query }, userObj, (err, user) => {
if (err) {
return cb(err);
} }
const date = new Date(); // Find the user model
return userIdentityModel.create({ const userModel = userIdentityModel.relations.user &&
provider: getSocialProvider(provider), userIdentityModel.relations.user.modelTo ||
externalId: profile.id, loopback.getModelByType(loopback.User);
authScheme: authScheme,
profile: profile, const userObj = options.profileToUser(provider, profile, options);
credentials: credentials, if (getSocialProvider(provider) !== 'github') {
userId: user.id, return process.nextTick(() => cb(
created: date, new Error(
modified: date 'accounts can only be created using Github or though email'
}, function(err, identity) { )
if (!err && user && autoLogin) { ));
return (options.createAccessToken || createAccessToken)( }
user,
function(err, token) { let query;
cb(err, user, identity, token); if (userObj.email) {
} query = { or: [
); { username: userObj.username },
} { email: userObj.email }
return cb(err, user, identity); ]};
}); } else {
query = { username: userObj.username };
}
return userModel.findOrCreate({ where: query }, userObj)
.then(([ user ]) => {
const promises = [
userIdentityModel.create({
provider: getSocialProvider(provider),
externalId: profile.id,
authScheme: authScheme,
profile: profile,
credentials: credentials,
userId: user.id,
created: new Date(),
modified: new Date()
}),
user.accessTokens.create({
created: new Date(),
ttl: user.constructor.settings.ttl
})
];
return Promise.all(promises)
.then(([ identity, token ]) => ({ user, identity, token }));
})
.then(({ user, token, identity }) => cb(null, user, identity, token))
.catch(err => cb(err));
}); });
});
}; };
UserIdent.observe('before save', function(ctx, next) { UserIdent.observe('before save', function(ctx, next) {

View File

@ -41,6 +41,8 @@ module.exports = function(User) {
User.definition.properties.rand.default = function() { User.definition.properties.rand.default = function() {
return Math.random(); return Math.random();
}; };
// increase user accessToken ttl to 900 days
User.settings.ttl = 900 * 24 * 60 * 60 * 1000;
// username should not be in blacklist // username should not be in blacklist
User.validatesExclusionOf('username', { User.validatesExclusionOf('username', {

View File

@ -13,18 +13,18 @@ import {
const passportOptions = { const passportOptions = {
emailOptional: true, emailOptional: true,
profileToUser(provider, profile) { profileToUser(provider, profile) {
var emails = profile.emails; const emails = profile.emails;
// NOTE(berks): get email or set to null. // NOTE(berks): get email or set to null.
// MongoDB indexs email but can be sparse(blank) // MongoDB indexs email but can be sparse(blank)
var email = emails && emails[0] && emails[0].value ? const email = emails && emails[0] && emails[0].value ?
emails[0].value : emails[0].value :
null; null;
// create random username // create random username
// username will be assigned when camper signups for Github // username will be assigned when camper signups for Github
var username = 'fcc' + uuid.v4().slice(0, 8); const username = 'fcc' + uuid.v4().slice(0, 8);
var password = generateKey('password'); const password = generateKey('password');
var userObj = { let userObj = {
username: username, username: username,
password: password password: password
}; };
@ -41,7 +41,7 @@ const passportOptions = {
} }
if (/github/.test(provider)) { if (/github/.test(provider)) {
setProfileFromGithub(userObj, profile, profile._json); userObj = setProfileFromGithub(userObj, profile, profile._json);
} }
return userObj; return userObj;
} }

View File

@ -1,5 +1,3 @@
import assign from 'object.assign';
const providerHash = { const providerHash = {
facebook: ({ id }) => id, facebook: ({ id }) => id,
twitter: ({ username }) => username, twitter: ({ username }) => username,
@ -32,15 +30,16 @@ export function setProfileFromGithub(
name name
} }
) { ) {
return assign( return Object.assign(
user, user,
{ isGithubCool: true, isMigrationGrandfathered: false },
{ {
name, name,
email: user.email || githubEmail,
username: username.toLowerCase(), username: username.toLowerCase(),
location, location,
joinedGithubOn, joinedGithubOn,
website, website,
isGithubCool: true,
picture, picture,
githubId, githubId,
githubURL, githubURL,