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,38 +28,41 @@ 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
} }
}) };
return userIdentityModel.findOne(filter)
.then(identity => { .then(identity => {
// identity already exists
// find user and log them in
if (identity) { if (identity) {
identity.credentials = credentials; identity.credentials = credentials;
return identity.updateAttributes({ const options = {
profile: profile, profile: profile,
credentials: credentials, credentials: credentials,
modified: new Date() modified: new Date()
};
return identity.updateAttributes(options)
// grab user associated with identity
.then(() => identity.user())
.then(user => {
// Create access token for user
const options = {
created: new Date(),
ttl: user.constructor.settings.ttl
};
return user.accessTokens.create(options)
.then(token => ({ user, token }));
}) })
.then(function() { .then(({ token, user })=> {
// Find the user for the given identity cb(null, user, identity, token);
return identity.user(function(err, user) { })
// Create access token if the autoLogin flag is set to true .catch(err => cb(err));
if (!err && user && autoLogin) {
return (options.createAccessToken || createAccessToken)(
user,
function(err, token) {
cb(err, user, identity, token);
}
);
}
return cb(err, user, identity);
});
});
} }
// Find the user model // Find the user model
const userModel = userIdentityModel.relations.user && const userModel = userIdentityModel.relations.user &&
@ -79,14 +70,15 @@ export default function(UserIdent) {
loopback.getModelByType(loopback.User); loopback.getModelByType(loopback.User);
const userObj = options.profileToUser(provider, profile, options); const userObj = options.profileToUser(provider, profile, options);
if (getSocialProvider(provider) !== 'github') {
if (!userObj.email && !options.emailOptional) { return process.nextTick(() => cb(
process.nextTick(function() { new Error(
return cb('email is missing from the user profile'); 'accounts can only be created using Github or though email'
}); )
));
} }
const query; let query;
if (userObj.email) { if (userObj.email) {
query = { or: [ query = { or: [
{ username: userObj.username }, { username: userObj.username },
@ -95,32 +87,29 @@ export default function(UserIdent) {
} else { } else {
query = { username: userObj.username }; query = { username: userObj.username };
} }
return userModel.findOrCreate({ where: query }, userObj, (err, user) => { return userModel.findOrCreate({ where: query }, userObj)
if (err) { .then(([ user ]) => {
return cb(err); const promises = [
} userIdentityModel.create({
const date = new Date();
return userIdentityModel.create({
provider: getSocialProvider(provider), provider: getSocialProvider(provider),
externalId: profile.id, externalId: profile.id,
authScheme: authScheme, authScheme: authScheme,
profile: profile, profile: profile,
credentials: credentials, credentials: credentials,
userId: user.id, userId: user.id,
created: date, created: new Date(),
modified: date modified: new Date()
}, function(err, identity) { }),
if (!err && user && autoLogin) { user.accessTokens.create({
return (options.createAccessToken || createAccessToken)( created: new Date(),
user, ttl: user.constructor.settings.ttl
function(err, token) { })
cb(err, user, identity, token); ];
} return Promise.all(promises)
); .then(([ identity, token ]) => ({ user, identity, token }));
} })
return cb(err, user, identity); .then(({ user, token, identity }) => cb(null, user, identity, token))
}); .catch(err => cb(err));
});
}); });
}; };

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,