2018-01-12 15:20:13 +01:00
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Module dependencies.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
// Public node modules.
|
|
|
|
|
const _ = require('lodash');
|
2018-01-23 09:30:25 +01:00
|
|
|
|
const request = require('request');
|
2018-01-12 15:20:13 +01:00
|
|
|
|
|
|
|
|
|
// Purest strategies.
|
|
|
|
|
const Purest = require('purest');
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Connect thanks to a third-party provider.
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @param {String} provider
|
|
|
|
|
* @param {String} access_token
|
|
|
|
|
*
|
|
|
|
|
* @return {*}
|
|
|
|
|
*/
|
|
|
|
|
|
2018-01-23 09:30:25 +01:00
|
|
|
|
exports.connect = (provider, query) => {
|
|
|
|
|
const access_token = query.access_token || query.code || query.oauth_token;
|
|
|
|
|
|
2018-01-12 15:20:13 +01:00
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
if (!access_token) {
|
2018-01-25 11:53:22 +01:00
|
|
|
|
return reject(null, {
|
2018-01-12 15:20:13 +01:00
|
|
|
|
message: 'No access_token.'
|
|
|
|
|
});
|
|
|
|
|
}
|
2018-01-25 11:53:22 +01:00
|
|
|
|
|
|
|
|
|
// Get the profile.
|
|
|
|
|
getProfile(provider, query, async (err, profile) => {
|
|
|
|
|
if (err) {
|
|
|
|
|
return reject(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// We need at least the mail.
|
|
|
|
|
if (!profile.email) {
|
2018-01-25 12:26:09 +01:00
|
|
|
|
return reject([{
|
2018-01-25 11:53:22 +01:00
|
|
|
|
message: 'Email was not available.'
|
|
|
|
|
}, null]);
|
2018-01-25 13:43:07 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-01-25 12:26:09 +01:00
|
|
|
|
try {
|
2018-01-29 17:12:49 +01:00
|
|
|
|
const users = await strapi.query('user', 'users-permissions').find({
|
|
|
|
|
email: profile.email
|
|
|
|
|
});
|
2018-01-25 11:53:22 +01:00
|
|
|
|
|
2018-02-06 13:10:43 +01:00
|
|
|
|
const advanced = await strapi.store({
|
2018-02-13 15:04:21 +01:00
|
|
|
|
environment: '',
|
2018-02-06 13:10:43 +01:00
|
|
|
|
type: 'plugin',
|
|
|
|
|
name: 'users-permissions',
|
|
|
|
|
key: 'advanced'
|
|
|
|
|
}).get();
|
2018-02-01 18:12:38 +01:00
|
|
|
|
|
|
|
|
|
if (_.isEmpty(_.find(users, {provider})) && !advanced.allow_register) {
|
2018-01-25 12:26:09 +01:00
|
|
|
|
return resolve([null, [{ messages: [{ id: 'Auth.advanced.allow_register' }] }], 'Register action is actualy not available.']);
|
|
|
|
|
}
|
2018-01-25 11:53:22 +01:00
|
|
|
|
|
2018-02-14 17:02:44 +01:00
|
|
|
|
const user = _.find(users, {provider});
|
|
|
|
|
|
|
|
|
|
if (!_.isEmpty(user)) {
|
2018-01-25 15:48:56 +01:00
|
|
|
|
return resolve([user, null]);
|
2018-01-25 12:26:09 +01:00
|
|
|
|
}
|
2018-01-25 11:53:22 +01:00
|
|
|
|
|
2018-02-01 18:12:38 +01:00
|
|
|
|
if (!_.isEmpty(_.find(users, user => user.provider !== provider)) && advanced.unique_email) {
|
2018-01-25 12:26:09 +01:00
|
|
|
|
return resolve([null, [{ messages: [{ id: 'Auth.form.error.email.taken' }] }], 'Email is already taken.']);
|
|
|
|
|
}
|
2018-01-25 11:53:22 +01:00
|
|
|
|
|
2018-03-12 16:37:20 +01:00
|
|
|
|
// Retrieve role `public`.
|
|
|
|
|
const publicRole = await strapi.query('role', 'users-permissions').findOne({ type: 'public' }, []);
|
2018-01-25 16:37:35 +01:00
|
|
|
|
|
2018-01-29 17:12:49 +01:00
|
|
|
|
// Create the new user.
|
|
|
|
|
const params = _.assign(profile, {
|
|
|
|
|
provider: provider,
|
2018-03-12 16:37:20 +01:00
|
|
|
|
role: publicRole._id || publicRole.id
|
2018-01-29 17:12:49 +01:00
|
|
|
|
});
|
2018-01-25 11:53:22 +01:00
|
|
|
|
|
2018-01-29 17:12:49 +01:00
|
|
|
|
const createdUser = await strapi.query('user', 'users-permissions').create(params);
|
2018-01-25 11:53:22 +01:00
|
|
|
|
|
2018-01-29 17:12:49 +01:00
|
|
|
|
return resolve([createdUser, null]);
|
2018-01-25 12:26:09 +01:00
|
|
|
|
} catch (err) {
|
|
|
|
|
reject([null, err]);
|
2018-01-25 11:53:22 +01:00
|
|
|
|
}
|
|
|
|
|
});
|
2018-01-12 15:20:13 +01:00
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Helper to get profiles
|
|
|
|
|
*
|
|
|
|
|
* @param {String} provider
|
|
|
|
|
* @param {Function} callback
|
|
|
|
|
*/
|
|
|
|
|
|
2018-02-01 18:12:38 +01:00
|
|
|
|
const getProfile = async (provider, query, callback) => {
|
2018-01-23 09:30:25 +01:00
|
|
|
|
const access_token = query.access_token || query.code || query.oauth_token;
|
|
|
|
|
|
2018-02-06 13:10:43 +01:00
|
|
|
|
const grant = await strapi.store({
|
2018-02-13 15:04:21 +01:00
|
|
|
|
environment: '',
|
2018-02-06 13:10:43 +01:00
|
|
|
|
type: 'plugin',
|
|
|
|
|
name: 'users-permissions',
|
|
|
|
|
key: 'grant'
|
|
|
|
|
}).get();
|
2018-02-01 18:12:38 +01:00
|
|
|
|
|
2018-01-12 15:20:13 +01:00
|
|
|
|
switch (provider) {
|
|
|
|
|
case 'facebook':
|
2018-01-25 15:04:42 +01:00
|
|
|
|
const facebook = new Purest({
|
|
|
|
|
provider: 'facebook'
|
|
|
|
|
});
|
|
|
|
|
|
2018-01-12 15:20:13 +01:00
|
|
|
|
facebook.query().get('me?fields=name,email').auth(access_token).request((err, res, body) => {
|
|
|
|
|
if (err) {
|
|
|
|
|
callback(err);
|
|
|
|
|
} else {
|
|
|
|
|
callback(null, {
|
|
|
|
|
username: body.name,
|
|
|
|
|
email: body.email
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
break;
|
|
|
|
|
case 'google':
|
2018-01-25 15:04:42 +01:00
|
|
|
|
const google = new Purest({
|
|
|
|
|
provider: 'google'
|
|
|
|
|
});
|
|
|
|
|
|
2018-01-12 15:20:13 +01:00
|
|
|
|
google.query('plus').get('people/me').auth(access_token).request((err, res, body) => {
|
|
|
|
|
if (err) {
|
|
|
|
|
callback(err);
|
|
|
|
|
} else {
|
|
|
|
|
callback(null, {
|
2018-01-25 15:30:57 +01:00
|
|
|
|
username: body.displayName || body.emails[0].value,
|
2018-01-12 15:20:13 +01:00
|
|
|
|
email: body.emails[0].value
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
break;
|
|
|
|
|
case 'github':
|
2018-01-25 15:04:42 +01:00
|
|
|
|
const github = new Purest({
|
|
|
|
|
provider: 'github',
|
|
|
|
|
defaults: {
|
|
|
|
|
headers: {
|
|
|
|
|
'user-agent': 'strapi'
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2018-01-23 09:30:25 +01:00
|
|
|
|
request.post({
|
|
|
|
|
url: 'https://github.com/login/oauth/access_token',
|
|
|
|
|
form: {
|
2018-02-01 18:12:38 +01:00
|
|
|
|
client_id: grant.github.key,
|
|
|
|
|
client_secret: grant.github.secret,
|
2018-01-23 09:30:25 +01:00
|
|
|
|
code: access_token
|
2018-01-12 15:20:13 +01:00
|
|
|
|
}
|
2018-01-23 09:30:25 +01:00
|
|
|
|
}, (err, res, body) => {
|
|
|
|
|
github.query().get('user').auth(body.split('&')[0].split('=')[1]).request((err, res, body) => {
|
|
|
|
|
if (err) {
|
|
|
|
|
callback(err);
|
|
|
|
|
} else {
|
|
|
|
|
callback(null, {
|
|
|
|
|
username: body.login,
|
|
|
|
|
email: body.email
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
2018-01-12 15:20:13 +01:00
|
|
|
|
});
|
|
|
|
|
break;
|
2018-01-23 09:30:25 +01:00
|
|
|
|
case 'twitter':
|
2018-01-25 15:04:42 +01:00
|
|
|
|
const twitter = new Purest({
|
|
|
|
|
provider: 'twitter',
|
2018-02-01 18:12:38 +01:00
|
|
|
|
key: grant.twitter.key,
|
|
|
|
|
secret: grant.twitter.secret
|
2018-01-25 15:04:42 +01:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
twitter.query().get('account/verify_credentials').auth(access_token, query.access_secret).qs({screen_name: query['raw[screen_name]'], include_email: 'true'}).request((err, res, body) => {
|
2018-01-12 15:20:13 +01:00
|
|
|
|
if (err) {
|
|
|
|
|
callback(err);
|
|
|
|
|
} else {
|
|
|
|
|
callback(null, {
|
2018-01-23 09:30:25 +01:00
|
|
|
|
username: body.screen_name,
|
|
|
|
|
email: body.email
|
2018-01-12 15:20:13 +01:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
callback({
|
|
|
|
|
message: 'Unknown provider.'
|
|
|
|
|
});
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|