store emails in lowercase + add /api as prefix

This commit is contained in:
Pierre Noël 2021-09-29 12:04:42 +02:00
parent c0c6c2aeab
commit 32eb442cd5
7 changed files with 36 additions and 28 deletions

View File

@ -26,7 +26,6 @@ module.exports = async ({ strapi }) => {
await getService('users-permissions').initialize(); await getService('users-permissions').initialize();
// TODO: adapt with new extension system
if (!strapi.config.get('plugin.users-permissions.jwtSecret')) { if (!strapi.config.get('plugin.users-permissions.jwtSecret')) {
const jwtSecret = uuid(); const jwtSecret = uuid();
strapi.config.set('plugin.users-permissions.jwtSecret', jwtSecret); strapi.config.set('plugin.users-permissions.jwtSecret', jwtSecret);
@ -39,6 +38,9 @@ module.exports = async ({ strapi }) => {
const initGrant = async pluginStore => { const initGrant = async pluginStore => {
const grantConfig = { const grantConfig = {
defaults: {
prefix: '/api/connect',
},
email: { email: {
enabled: true, enabled: true,
icon: 'envelope', icon: 'envelope',
@ -48,7 +50,7 @@ const initGrant = async pluginStore => {
icon: 'discord', icon: 'discord',
key: '', key: '',
secret: '', secret: '',
callback: `${strapi.config.server.url}/auth/discord/callback`, callback: `${strapi.config.server.url}/api/auth/discord/callback`,
scope: ['identify', 'email'], scope: ['identify', 'email'],
}, },
facebook: { facebook: {
@ -56,7 +58,7 @@ const initGrant = async pluginStore => {
icon: 'facebook-square', icon: 'facebook-square',
key: '', key: '',
secret: '', secret: '',
callback: `${strapi.config.server.url}/auth/facebook/callback`, callback: `${strapi.config.server.url}/api/auth/facebook/callback`,
scope: ['email'], scope: ['email'],
}, },
google: { google: {
@ -64,7 +66,7 @@ const initGrant = async pluginStore => {
icon: 'google', icon: 'google',
key: '', key: '',
secret: '', secret: '',
callback: `${strapi.config.server.url}/auth/google/callback`, callback: `${strapi.config.server.url}/api/auth/google/callback`,
scope: ['email'], scope: ['email'],
}, },
github: { github: {
@ -72,7 +74,7 @@ const initGrant = async pluginStore => {
icon: 'github', icon: 'github',
key: '', key: '',
secret: '', secret: '',
callback: `${strapi.config.server.url}/auth/github/callback`, callback: `${strapi.config.server.url}/api/auth/github/callback`,
scope: ['user', 'user:email'], scope: ['user', 'user:email'],
}, },
microsoft: { microsoft: {
@ -80,7 +82,7 @@ const initGrant = async pluginStore => {
icon: 'windows', icon: 'windows',
key: '', key: '',
secret: '', secret: '',
callback: `${strapi.config.server.url}/auth/microsoft/callback`, callback: `${strapi.config.server.url}/api/auth/microsoft/callback`,
scope: ['user.read'], scope: ['user.read'],
}, },
twitter: { twitter: {
@ -88,14 +90,14 @@ const initGrant = async pluginStore => {
icon: 'twitter', icon: 'twitter',
key: '', key: '',
secret: '', secret: '',
callback: `${strapi.config.server.url}/auth/twitter/callback`, callback: `${strapi.config.server.url}/api/auth/twitter/callback`,
}, },
instagram: { instagram: {
enabled: false, enabled: false,
icon: 'instagram', icon: 'instagram',
key: '', key: '',
secret: '', secret: '',
callback: `${strapi.config.server.url}/auth/instagram/callback`, callback: `${strapi.config.server.url}/api/auth/instagram/callback`,
scope: ['user_profile'], scope: ['user_profile'],
}, },
vk: { vk: {
@ -103,7 +105,7 @@ const initGrant = async pluginStore => {
icon: 'vk', icon: 'vk',
key: '', key: '',
secret: '', secret: '',
callback: `${strapi.config.server.url}/auth/vk/callback`, callback: `${strapi.config.server.url}/api/auth/vk/callback`,
scope: ['email'], scope: ['email'],
}, },
twitch: { twitch: {
@ -111,7 +113,7 @@ const initGrant = async pluginStore => {
icon: 'twitch', icon: 'twitch',
key: '', key: '',
secret: '', secret: '',
callback: `${strapi.config.server.url}/auth/twitch/callback`, callback: `${strapi.config.server.url}/api/auth/twitch/callback`,
scope: ['user:read:email'], scope: ['user:read:email'],
}, },
linkedin: { linkedin: {
@ -119,7 +121,7 @@ const initGrant = async pluginStore => {
icon: 'linkedin', icon: 'linkedin',
key: '', key: '',
secret: '', secret: '',
callback: `${strapi.config.server.url}/auth/linkedin/callback`, callback: `${strapi.config.server.url}/api/auth/linkedin/callback`,
scope: ['r_liteprofile', 'r_emailaddress'], scope: ['r_liteprofile', 'r_emailaddress'],
}, },
cognito: { cognito: {
@ -128,7 +130,7 @@ const initGrant = async pluginStore => {
key: '', key: '',
secret: '', secret: '',
subdomain: 'my.subdomain.com', subdomain: 'my.subdomain.com',
callback: `${strapi.config.server.url}/auth/cognito/callback`, callback: `${strapi.config.server.url}/api/auth/cognito/callback`,
scope: ['email', 'openid', 'profile'], scope: ['email', 'openid', 'profile'],
}, },
reddit: { reddit: {
@ -137,7 +139,7 @@ const initGrant = async pluginStore => {
key: '', key: '',
secret: '', secret: '',
state: true, state: true,
callback: `${strapi.config.server.url}/auth/reddit/callback`, callback: `${strapi.config.server.url}/api/auth/reddit/callback`,
scope: ['identity'], scope: ['identity'],
}, },
auth0: { auth0: {
@ -146,7 +148,7 @@ const initGrant = async pluginStore => {
key: '', key: '',
secret: '', secret: '',
subdomain: 'my-tenant.eu', subdomain: 'my-tenant.eu',
callback: `${strapi.config.server.url}/auth/auth0/callback`, callback: `${strapi.config.server.url}/api/auth/auth0/callback`,
scope: ['openid', 'email', 'profile'], scope: ['openid', 'email', 'profile'],
}, },
cas: { cas: {
@ -154,7 +156,7 @@ const initGrant = async pluginStore => {
icon: 'book', icon: 'book',
key: '', key: '',
secret: '', secret: '',
callback: `${strapi.config.server.url}/auth/cas/callback`, callback: `${strapi.config.server.url}/api/auth/cas/callback`,
scope: ['openid email'], // scopes should be space delimited scope: ['openid email'], // scopes should be space delimited
subdomain: 'my.subdomain.com/cas', subdomain: 'my.subdomain.com/cas',
}, },

View File

@ -234,7 +234,7 @@ module.exports = {
.get(); .get();
const [requestPath] = ctx.request.url.split('?'); const [requestPath] = ctx.request.url.split('?');
const provider = requestPath.split('/')[2]; const provider = requestPath.split('/connect/')[1].split('/')[0];
if (!_.get(grantConfig[provider], 'enabled')) { if (!_.get(grantConfig[provider], 'enabled')) {
return ctx.badRequest(null, 'This provider is disabled.'); return ctx.badRequest(null, 'This provider is disabled.');

View File

@ -3,7 +3,7 @@
const register = require('./register'); const register = require('./register');
const bootstrap = require('./bootstrap'); const bootstrap = require('./bootstrap');
const contentTypes = require('./content-types'); const contentTypes = require('./content-types');
const policies = require('./policies'); const middlewares = require('./middlewares');
const services = require('./services'); const services = require('./services');
const routes = require('./routes'); const routes = require('./routes');
const controllers = require('./controllers'); const controllers = require('./controllers');
@ -16,6 +16,6 @@ module.exports = () => ({
routes, routes,
controllers, controllers,
contentTypes, contentTypes,
policies, middlewares,
services, services,
}); });

View File

@ -1,12 +1,14 @@
'use strict'; 'use strict';
const { rateLimit } = require('../../middlewares');
module.exports = [ module.exports = [
{ {
method: 'GET', method: 'GET',
path: '/connect/(.*)', path: '/connect/(.*)',
handler: 'auth.connect', handler: 'auth.connect',
config: { config: {
policies: ['plugin::users-permissions.rateLimit'], middlewares: [rateLimit],
prefix: '', prefix: '',
}, },
}, },
@ -15,7 +17,7 @@ module.exports = [
path: '/auth/local', path: '/auth/local',
handler: 'auth.callback', handler: 'auth.callback',
config: { config: {
policies: ['plugin::users-permissions.rateLimit'], middlewares: [rateLimit],
prefix: '', prefix: '',
}, },
}, },
@ -24,7 +26,7 @@ module.exports = [
path: '/auth/local/register', path: '/auth/local/register',
handler: 'auth.register', handler: 'auth.register',
config: { config: {
policies: ['plugin::users-permissions.rateLimit'], middlewares: [rateLimit],
prefix: '', prefix: '',
}, },
}, },
@ -41,7 +43,7 @@ module.exports = [
path: '/auth/forgot-password', path: '/auth/forgot-password',
handler: 'auth.forgotPassword', handler: 'auth.forgotPassword',
config: { config: {
policies: ['plugin::users-permissions.rateLimit'], middlewares: [rateLimit],
prefix: '', prefix: '',
}, },
}, },
@ -50,7 +52,7 @@ module.exports = [
path: '/auth/reset-password', path: '/auth/reset-password',
handler: 'auth.resetPassword', handler: 'auth.resetPassword',
config: { config: {
policies: ['plugin::users-permissions.rateLimit'], middlewares: [rateLimit],
prefix: '', prefix: '',
}, },
}, },

View File

@ -519,14 +519,16 @@ module.exports = ({ strapi }) => {
return reject([null, err]); return reject([null, err]);
} }
const email = _.toLower(profile.email);
// We need at least the mail. // We need at least the mail.
if (!profile.email) { if (!email) {
return reject([null, { message: 'Email was not available.' }]); return reject([null, { message: 'Email was not available.' }]);
} }
try { try {
const users = await strapi.query('plugin::users-permissions.user').findMany({ const users = await strapi.query('plugin::users-permissions.user').findMany({
where: { email: profile.email }, where: { email },
}); });
const advanced = await strapi const advanced = await strapi
@ -564,11 +566,13 @@ module.exports = ({ strapi }) => {
.findOne({ where: { type: advanced.default_role } }); .findOne({ where: { type: advanced.default_role } });
// Create the new user. // Create the new user.
const params = _.assign(profile, { const params = {
...profile,
email, // overwrite with lowercased email
provider, provider,
role: defaultRole.id, role: defaultRole.id,
confirmed: true, confirmed: true,
}); };
const createdUser = await strapi const createdUser = await strapi
.query('plugin::users-permissions.user') .query('plugin::users-permissions.user')
@ -583,7 +587,7 @@ module.exports = ({ strapi }) => {
}; };
const buildRedirectUri = (provider = '') => const buildRedirectUri = (provider = '') =>
`${getAbsoluteServerUrl(strapi.config)}/connect/${provider}/callback`; `${getAbsoluteServerUrl(strapi.config)}/api/connect/${provider}/callback`;
return { return {
connect, connect,