583 lines
16 KiB
JavaScript
Raw Normal View History

2017-11-14 11:49:19 +01:00
'use strict';
/**
* Auth.js controller
*
* @description: A set of functions called "actions" for managing `Auth`.
*/
/* eslint-disable no-useless-escape */
2017-11-16 18:00:15 +01:00
const crypto = require('crypto');
const _ = require('lodash');
2021-04-29 13:51:12 +02:00
const { sanitizeEntity } = require('@strapi/utils');
2021-07-08 18:15:32 +02:00
const { getService } = require('../utils');
2019-01-18 16:08:15 +01:00
const emailRegExp = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
const formatError = error => [
{ messages: [{ id: error.id, message: error.message, field: error.field }] },
];
2017-11-16 14:12:03 +01:00
2017-11-14 11:49:19 +01:00
module.exports = {
async callback(ctx) {
2017-11-14 11:49:19 +01:00
const provider = ctx.params.provider || 'local';
const params = ctx.request.body;
const store = await strapi.store({ type: 'plugin', name: 'users-permissions' });
2017-11-14 11:49:19 +01:00
if (provider === 'local') {
if (!_.get(await store.get({ key: 'grant' }), 'email.enabled')) {
2018-01-18 16:01:52 +01:00
return ctx.badRequest(null, 'This provider is disabled.');
}
2017-11-14 11:49:19 +01:00
// The identifier is required.
if (!params.identifier) {
return ctx.badRequest(
null,
formatError({
id: 'Auth.form.error.email.provide',
message: 'Please provide your username or your e-mail.',
})
);
2017-11-14 11:49:19 +01:00
}
// The password is required.
if (!params.password) {
return ctx.badRequest(
null,
formatError({
id: 'Auth.form.error.password.provide',
message: 'Please provide your password.',
})
);
2017-11-14 11:49:19 +01:00
}
const query = { provider };
2017-11-14 11:49:19 +01:00
// Check if the provided identifier is an email or not.
const isEmail = emailRegExp.test(params.identifier);
2017-11-14 11:49:19 +01:00
// Set the identifier to the appropriate query field.
if (isEmail) {
query.email = params.identifier.toLowerCase();
2017-11-14 11:49:19 +01:00
} else {
query.username = params.identifier;
}
// Check if the user exists.
2021-08-06 18:09:49 +02:00
const user = await strapi.query('plugin::users-permissions.user').findOne({ where: query });
2017-11-20 16:28:50 +01:00
if (!user) {
return ctx.badRequest(
null,
formatError({
id: 'Auth.form.error.invalid',
message: 'Identifier or password invalid.',
})
);
}
2017-11-20 16:28:50 +01:00
if (
_.get(await store.get({ key: 'advanced' }), 'email_confirmation') &&
user.confirmed !== true
) {
return ctx.badRequest(
null,
formatError({
id: 'Auth.form.error.confirmed',
message: 'Your account email is not confirmed',
})
);
}
if (user.blocked === true) {
return ctx.badRequest(
null,
formatError({
id: 'Auth.form.error.blocked',
message: 'Your account has been blocked by an administrator',
})
);
}
// The user never authenticated with the `local` provider.
2017-11-20 16:28:50 +01:00
if (!user.password) {
return ctx.badRequest(
null,
formatError({
id: 'Auth.form.error.password.local',
message:
2020-01-10 07:40:17 -05:00
'This user never set a local password, please login with the provider used during account creation.',
})
);
2017-11-20 16:28:50 +01:00
}
2021-08-19 22:27:00 +02:00
const validPassword = await getService('user').validatePassword(
params.password,
user.password
);
2017-11-20 16:28:50 +01:00
if (!validPassword) {
return ctx.badRequest(
null,
formatError({
id: 'Auth.form.error.invalid',
message: 'Identifier or password invalid.',
})
);
2017-11-20 16:28:50 +01:00
} else {
ctx.send({
2021-08-19 22:27:00 +02:00
jwt: getService('jwt').issue({
id: user.id,
}),
2021-08-19 22:27:00 +02:00
user: sanitizeEntity(user, {
2021-08-06 18:09:49 +02:00
model: strapi.getModel('plugin::users-permissions.user'),
2019-09-12 10:50:52 +02:00
}),
2017-11-20 16:28:50 +01:00
});
2017-11-14 11:49:19 +01:00
}
} else {
if (!_.get(await store.get({ key: 'grant' }), [provider, 'enabled'])) {
return ctx.badRequest(
null,
formatError({
id: 'provider.disabled',
message: 'This provider is disabled.',
})
);
}
2020-01-10 07:40:17 -05:00
// Connect the user with the third-party provider.
let user;
let error;
try {
2021-08-19 22:27:00 +02:00
[user, error] = await getService('providers').connect(provider, ctx.query);
} catch ([user, error]) {
return ctx.badRequest(null, error === 'array' ? error[0] : error);
}
2018-01-25 15:04:42 +01:00
if (!user) {
return ctx.badRequest(null, error === 'array' ? error[0] : error);
2018-01-25 15:04:42 +01:00
}
2018-01-12 15:20:13 +01:00
ctx.send({
2021-08-19 22:27:00 +02:00
jwt: getService('jwt').issue({ id: user.id }),
user: sanitizeEntity(user, {
2021-08-06 18:09:49 +02:00
model: strapi.getModel('plugin::users-permissions.user'),
2019-09-12 10:50:52 +02:00
}),
2018-01-12 15:20:13 +01:00
});
2017-11-14 11:49:19 +01:00
}
2017-11-16 14:12:03 +01:00
},
async resetPassword(ctx) {
const params = _.assign({}, ctx.request.body, ctx.params);
2017-11-16 14:12:03 +01:00
if (
params.password &&
params.passwordConfirmation &&
params.password === params.passwordConfirmation &&
params.code
) {
const user = await strapi
2021-08-06 18:09:49 +02:00
.query('plugin::users-permissions.user')
2021-07-08 18:15:32 +02:00
.findOne({ where: { resetPasswordToken: `${params.code}` } });
2017-11-16 14:12:03 +01:00
if (!user) {
return ctx.badRequest(
null,
formatError({
id: 'Auth.form.error.code.provide',
message: 'Incorrect code provided.',
})
);
}
2017-11-16 14:12:03 +01:00
2021-07-08 18:15:32 +02:00
const password = await getService('user').hashPassword({ password: params.password });
2017-11-16 14:29:49 +01:00
// Update the user.
await strapi
2021-08-06 18:09:49 +02:00
.query('plugin::users-permissions.user')
2021-07-08 18:15:32 +02:00
.update({ where: { id: user.id }, data: { resetPasswordToken: null, password } });
2017-12-06 11:47:39 +01:00
ctx.send({
2021-08-19 22:27:00 +02:00
jwt: getService('jwt').issue({ id: user.id }),
user: sanitizeEntity(user, {
2021-08-06 18:09:49 +02:00
model: strapi.getModel('plugin::users-permissions.user'),
2019-09-12 10:50:52 +02:00
}),
2017-12-06 11:47:39 +01:00
});
} else if (
params.password &&
params.passwordConfirmation &&
params.password !== params.passwordConfirmation
) {
return ctx.badRequest(
null,
formatError({
id: 'Auth.form.error.password.matching',
message: 'Passwords do not match.',
})
);
} else {
return ctx.badRequest(
null,
formatError({
id: 'Auth.form.error.params.provide',
message: 'Incorrect params provided.',
})
);
2017-12-06 11:47:39 +01:00
}
2017-11-16 18:00:15 +01:00
},
async connect(ctx, next) {
2021-09-03 11:11:37 +02:00
const grant = require('grant-koa');
const grantConfig = await strapi
.store({ type: 'plugin', name: 'users-permissions', key: 'grant' })
.get();
const [requestPath] = ctx.request.url.split('?');
const provider = requestPath.split('/connect/')[1].split('/')[0];
if (!_.get(grantConfig[provider], 'enabled')) {
return ctx.badRequest(null, 'This provider is disabled.');
}
if (!strapi.config.server.url.startsWith('http')) {
strapi.log.warn(
'You are using a third party provider for login. Make sure to set an absolute url in config/server.js. More info here: https://strapi.io/documentation/developer-docs/latest/development/plugins/users-permissions.html#setting-up-the-server-url'
);
}
// Ability to pass OAuth callback dynamically
grantConfig[provider].callback = _.get(ctx, 'query.callback') || grantConfig[provider].callback;
2021-08-19 22:27:00 +02:00
grantConfig[provider].redirect_uri = getService('providers').buildRedirectUri(provider);
return grant(grantConfig)(ctx, next);
},
async forgotPassword(ctx) {
let { email } = ctx.request.body;
// Check if the provided email is valid or not.
const isEmail = emailRegExp.test(email);
if (isEmail) {
email = email.toLowerCase();
} else {
return ctx.badRequest(
null,
formatError({
id: 'Auth.form.error.email.format',
2021-08-24 10:14:42 +02:00
message: 'Please provide a valid email address.',
})
);
}
2019-10-09 17:37:16 +02:00
const pluginStore = await strapi.store({ type: 'plugin', name: 'users-permissions' });
2017-11-16 18:00:15 +01:00
2020-01-10 07:40:17 -05:00
// Find the user by email.
const user = await strapi
2021-08-06 18:09:49 +02:00
.query('plugin::users-permissions.user')
2021-07-08 18:15:32 +02:00
.findOne({ where: { email: email.toLowerCase() } });
2017-11-16 18:00:15 +01:00
// User not found.
if (!user) {
return ctx.badRequest(
null,
formatError({
id: 'Auth.form.error.user.not-exist',
message: 'This email does not exist.',
})
);
2017-11-16 18:00:15 +01:00
}
// User blocked
if (user.blocked) {
2021-08-24 10:14:42 +02:00
return ctx.badRequest(
null,
formatError({
id: 'Auth.form.error.user.blocked',
message: 'This user is disabled.',
})
);
}
2017-11-16 18:00:15 +01:00
// Generate random token.
const resetPasswordToken = crypto.randomBytes(64).toString('hex');
const settings = await pluginStore.get({ key: 'email' }).then(storeEmail => {
try {
return storeEmail['reset_password'].options;
} catch (error) {
return {};
}
});
2019-10-09 17:37:16 +02:00
const advanced = await pluginStore.get({
key: 'advanced',
});
const userInfo = sanitizeEntity(user, {
2021-08-06 18:09:49 +02:00
model: strapi.getModel('plugin::users-permissions.user'),
});
2021-07-08 18:15:32 +02:00
settings.message = await getService('users-permissions').template(settings.message, {
URL: advanced.email_reset_password,
USER: userInfo,
TOKEN: resetPasswordToken,
});
2017-12-04 14:11:00 +01:00
2021-07-08 18:15:32 +02:00
settings.object = await getService('users-permissions').template(settings.object, {
USER: userInfo,
});
2017-12-04 14:00:09 +01:00
try {
// Send an email to the user.
2021-08-19 22:27:00 +02:00
await strapi
.plugin('email')
.service('email')
.send({
to: user.email,
from:
settings.from.email || settings.from.name
? `${settings.from.name} <${settings.from.email}>`
: undefined,
replyTo: settings.response_email,
subject: settings.object,
text: settings.message,
html: settings.message,
});
2017-12-04 14:00:09 +01:00
} catch (err) {
return ctx.badRequest(null, err);
}
2017-11-16 18:00:15 +01:00
// Update the user.
2021-07-08 18:15:32 +02:00
await strapi
2021-08-06 18:09:49 +02:00
.query('plugin::users-permissions.user')
2021-07-08 18:15:32 +02:00
.update({ where: { id: user.id }, data: { resetPasswordToken } });
2017-11-16 18:00:15 +01:00
ctx.send({ ok: true });
2017-11-17 11:41:23 +01:00
},
async register(ctx) {
const pluginStore = await strapi.store({ type: 'plugin', name: 'users-permissions' });
2018-08-23 18:28:13 +02:00
const settings = await pluginStore.get({
key: 'advanced',
2018-08-23 18:28:13 +02:00
});
2018-03-12 16:06:54 +01:00
if (!settings.allow_register) {
return ctx.badRequest(
null,
formatError({
id: 'Auth.advanced.allow_register',
message: 'Register action is currently disabled.',
})
);
}
const params = {
..._.omit(ctx.request.body, ['confirmed', 'confirmationToken', 'resetPasswordToken']),
provider: 'local',
};
2017-11-17 11:41:23 +01:00
// Password is required.
if (!params.password) {
return ctx.badRequest(
null,
formatError({
id: 'Auth.form.error.password.provide',
message: 'Please provide your password.',
})
);
}
2017-11-17 11:41:23 +01:00
// Email is required.
if (!params.email) {
return ctx.badRequest(
null,
formatError({
id: 'Auth.form.error.email.provide',
message: 'Please provide your email.',
})
);
}
// Throw an error if the password selected by the user
// contains more than three times the symbol '$'.
2021-07-08 18:15:32 +02:00
if (getService('user').isHashed(params.password)) {
return ctx.badRequest(
null,
formatError({
id: 'Auth.form.error.password.format',
message: 'Your password cannot contain more than three times the symbol `$`.',
})
);
}
2017-11-17 11:41:23 +01:00
const role = await strapi
2021-08-06 18:09:49 +02:00
.query('plugin::users-permissions.role')
2021-07-08 18:15:32 +02:00
.findOne({ where: { type: settings.default_role } });
if (!role) {
return ctx.badRequest(
null,
formatError({
id: 'Auth.form.error.role.notFound',
message: 'Impossible to find the default role.',
})
);
}
2017-11-17 11:41:23 +01:00
2018-07-13 20:29:41 +05:30
// Check if the provided email is valid or not.
const isEmail = emailRegExp.test(params.email);
if (isEmail) {
2018-07-13 20:29:41 +05:30
params.email = params.email.toLowerCase();
2019-11-03 10:41:21 +11:00
} else {
return ctx.badRequest(
null,
formatError({
id: 'Auth.form.error.email.format',
message: 'Please provide valid email address.',
})
);
}
params.role = role.id;
2021-07-08 18:15:32 +02:00
params.password = await getService('user').hashPassword(params);
2021-08-06 18:09:49 +02:00
const user = await strapi.query('plugin::users-permissions.user').findOne({
2021-07-08 18:15:32 +02:00
where: { email: params.email },
});
if (user && user.provider === params.provider) {
return ctx.badRequest(
null,
formatError({
id: 'Auth.form.error.email.taken',
message: 'Email is already taken.',
})
);
}
2018-09-03 14:19:51 +02:00
if (user && user.provider !== params.provider && settings.unique_email) {
return ctx.badRequest(
null,
formatError({
id: 'Auth.form.error.email.taken',
message: 'Email is already taken.',
})
);
}
try {
2018-08-23 18:28:13 +02:00
if (!settings.email_confirmation) {
params.confirmed = true;
}
2021-08-06 18:09:49 +02:00
const user = await strapi.query('plugin::users-permissions.user').create({ data: params });
2017-11-17 11:41:23 +01:00
const sanitizedUser = sanitizeEntity(user, {
2021-08-06 18:09:49 +02:00
model: strapi.getModel('plugin::users-permissions.user'),
});
2018-08-08 17:57:02 +02:00
if (settings.email_confirmation) {
try {
2021-07-08 18:15:32 +02:00
await getService('user').sendConfirmationEmail(user);
2018-08-08 17:57:02 +02:00
} catch (err) {
return ctx.badRequest(null, err);
}
return ctx.send({ user: sanitizedUser });
2018-08-08 17:57:02 +02:00
}
2021-08-19 22:27:00 +02:00
const jwt = getService('jwt').issue(_.pick(user, ['id']));
return ctx.send({
jwt,
user: sanitizedUser,
2017-11-20 16:28:50 +01:00
});
} catch (err) {
const adminError = _.includes(err.message, 'username')
? {
id: 'Auth.form.error.username.taken',
message: 'Username already taken',
}
: { id: 'Auth.form.error.email.taken', message: 'Email already taken' };
ctx.badRequest(null, formatError(adminError));
2017-11-17 11:41:23 +01:00
}
2018-08-08 17:57:02 +02:00
},
async emailConfirmation(ctx, next, returnUser) {
const { confirmation: confirmationToken } = ctx.query;
2018-08-08 17:57:02 +02:00
2021-08-19 22:27:00 +02:00
const userService = getService('user');
const jwtService = getService('jwt');
2018-08-08 17:57:02 +02:00
if (_.isEmpty(confirmationToken)) {
return ctx.badRequest('token.invalid');
}
const user = await userService.fetch({ confirmationToken }, []);
if (!user) {
return ctx.badRequest('token.invalid');
}
await userService.edit({ id: user.id }, { confirmed: true, confirmationToken: null });
2018-08-08 17:57:02 +02:00
if (returnUser) {
ctx.send({
jwt: jwtService.issue({ id: user.id }),
user: sanitizeEntity(user, {
2021-08-06 18:09:49 +02:00
model: strapi.getModel('plugin::users-permissions.user'),
}),
});
} else {
const settings = await strapi
.store({ type: 'plugin', name: 'users-permissions', key: 'advanced' })
.get();
2018-08-08 17:57:02 +02:00
ctx.redirect(settings.email_confirmation_redirection || '/');
}
},
async sendEmailConfirmation(ctx) {
const params = _.assign(ctx.request.body);
if (!params.email) {
return ctx.badRequest('missing.email');
}
const isEmail = emailRegExp.test(params.email);
if (isEmail) {
params.email = params.email.toLowerCase();
} else {
return ctx.badRequest('wrong.email');
}
2021-08-06 18:09:49 +02:00
const user = await strapi.query('plugin::users-permissions.user').findOne({
2021-07-08 18:15:32 +02:00
where: { email: params.email },
});
if (user.confirmed) {
return ctx.badRequest('already.confirmed');
}
if (user.blocked) {
return ctx.badRequest('blocked.user');
}
try {
2021-07-08 18:15:32 +02:00
await getService('user').sendConfirmationEmail(user);
ctx.send({
email: user.email,
sent: true,
});
} catch (err) {
return ctx.badRequest(null, err);
}
},
2017-11-14 11:49:19 +01:00
};