2023-11-07 06:35:43 -08:00
|
|
|
import { Response } from 'express';
|
2023-01-27 02:19:47 -08:00
|
|
|
import validator from 'validator';
|
2023-11-07 06:35:43 -08:00
|
|
|
|
2024-02-28 04:12:28 -08:00
|
|
|
import { AuthService } from '@/auth/auth.service';
|
2023-01-27 02:19:47 -08:00
|
|
|
import { Get, Post, RestController } from '@/decorators';
|
2023-12-11 09:23:42 -08:00
|
|
|
import { PasswordUtility } from '@/services/password.utility';
|
2023-08-25 04:23:22 -07:00
|
|
|
import { UserManagementMailer } from '@/UserManagement/email';
|
2023-01-27 05:56:56 -08:00
|
|
|
import { PasswordResetRequest } from '@/requests';
|
2023-07-12 05:11:46 -07:00
|
|
|
import { isSamlCurrentAuthenticationMethod } from '@/sso/ssoHelpers';
|
2023-08-22 06:58:05 -07:00
|
|
|
import { UserService } from '@/services/user.service';
|
2023-07-12 05:11:46 -07:00
|
|
|
import { License } from '@/License';
|
2024-04-03 03:08:54 -07:00
|
|
|
import { RESPONSE_ERROR_MESSAGES } from '@/constants';
|
2023-08-25 04:23:22 -07:00
|
|
|
import { MfaService } from '@/Mfa/mfa.service';
|
2023-10-25 07:35:22 -07:00
|
|
|
import { Logger } from '@/Logger';
|
2023-11-07 06:35:43 -08:00
|
|
|
import { ExternalHooks } from '@/ExternalHooks';
|
|
|
|
import { InternalHooks } from '@/InternalHooks';
|
2023-12-22 06:19:50 -08:00
|
|
|
import { UrlService } from '@/services/url.service';
|
2023-11-28 01:19:27 -08:00
|
|
|
import { InternalServerError } from '@/errors/response-errors/internal-server.error';
|
|
|
|
import { BadRequestError } from '@/errors/response-errors/bad-request.error';
|
|
|
|
import { UnauthorizedError } from '@/errors/response-errors/unauthorized.error';
|
|
|
|
import { NotFoundError } from '@/errors/response-errors/not-found.error';
|
|
|
|
import { UnprocessableRequestError } from '@/errors/response-errors/unprocessable.error';
|
2024-01-02 08:53:24 -08:00
|
|
|
import { UserRepository } from '@/databases/repositories/user.repository';
|
2023-11-03 10:44:12 -07:00
|
|
|
|
2023-01-27 02:19:47 -08:00
|
|
|
@RestController()
|
|
|
|
export class PasswordResetController {
|
2023-08-25 04:23:22 -07:00
|
|
|
constructor(
|
2023-10-25 07:35:22 -07:00
|
|
|
private readonly logger: Logger,
|
2023-11-07 06:35:43 -08:00
|
|
|
private readonly externalHooks: ExternalHooks,
|
|
|
|
private readonly internalHooks: InternalHooks,
|
2023-08-25 04:23:22 -07:00
|
|
|
private readonly mailer: UserManagementMailer,
|
2024-02-28 04:12:28 -08:00
|
|
|
private readonly authService: AuthService,
|
2023-08-25 04:23:22 -07:00
|
|
|
private readonly userService: UserService,
|
|
|
|
private readonly mfaService: MfaService,
|
2023-12-22 06:19:50 -08:00
|
|
|
private readonly urlService: UrlService,
|
2023-11-07 06:35:43 -08:00
|
|
|
private readonly license: License,
|
2023-12-11 09:23:42 -08:00
|
|
|
private readonly passwordUtility: PasswordUtility,
|
2024-01-02 08:53:24 -08:00
|
|
|
private readonly userRepository: UserRepository,
|
2023-08-25 04:23:22 -07:00
|
|
|
) {}
|
2023-01-27 02:19:47 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Send a password reset email.
|
|
|
|
*/
|
2024-04-03 03:08:54 -07:00
|
|
|
@Post('/forgot-password', { skipAuth: true, rateLimit: true })
|
2023-01-27 02:19:47 -08:00
|
|
|
async forgotPassword(req: PasswordResetRequest.Email) {
|
2023-10-19 04:58:06 -07:00
|
|
|
if (!this.mailer.isEmailSetUp) {
|
2023-01-27 02:19:47 -08:00
|
|
|
this.logger.debug(
|
|
|
|
'Request to send password reset email failed because emailing was not set up',
|
|
|
|
);
|
|
|
|
throw new InternalServerError(
|
|
|
|
'Email sending must be set up in order to request a password reset email',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const { email } = req.body;
|
|
|
|
if (!email) {
|
|
|
|
this.logger.debug(
|
|
|
|
'Request to send password reset email failed because of missing email in payload',
|
|
|
|
{ payload: req.body },
|
|
|
|
);
|
|
|
|
throw new BadRequestError('Email is mandatory');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!validator.isEmail(email)) {
|
|
|
|
this.logger.debug(
|
|
|
|
'Request to send password reset email failed because of invalid email in payload',
|
|
|
|
{ invalidEmail: email },
|
|
|
|
);
|
|
|
|
throw new BadRequestError('Invalid email address');
|
|
|
|
}
|
|
|
|
|
|
|
|
// User should just be able to reset password if one is already present
|
2024-01-02 08:53:24 -08:00
|
|
|
const user = await this.userRepository.findNonShellUser(email);
|
2023-01-27 02:19:47 -08:00
|
|
|
|
2023-11-07 06:35:43 -08:00
|
|
|
if (!user?.isOwner && !this.license.isWithinUsersLimit()) {
|
2023-07-12 05:11:46 -07:00
|
|
|
this.logger.debug(
|
|
|
|
'Request to send password reset email failed because the user limit was reached',
|
|
|
|
);
|
|
|
|
throw new UnauthorizedError(RESPONSE_ERROR_MESSAGES.USERS_QUOTA_REACHED);
|
|
|
|
}
|
2023-05-30 03:52:02 -07:00
|
|
|
if (
|
|
|
|
isSamlCurrentAuthenticationMethod() &&
|
2023-11-29 06:48:36 -08:00
|
|
|
!(
|
2024-05-02 03:52:15 -07:00
|
|
|
user?.hasGlobalScope('user:resetPassword') === true ||
|
2023-11-29 06:48:36 -08:00
|
|
|
user?.settings?.allowSSOManualLogin === true
|
|
|
|
)
|
2023-05-30 03:52:02 -07:00
|
|
|
) {
|
2023-03-30 03:44:53 -07:00
|
|
|
this.logger.debug(
|
|
|
|
'Request to send password reset email failed because login is handled by SAML',
|
|
|
|
);
|
|
|
|
throw new UnauthorizedError(
|
|
|
|
'Login is handled by SAML. Please contact your Identity Provider to reset your password.',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-01-27 02:19:47 -08:00
|
|
|
const ldapIdentity = user?.authIdentities?.find((i) => i.providerType === 'ldap');
|
|
|
|
if (!user?.password || (ldapIdentity && user.disabled)) {
|
|
|
|
this.logger.debug(
|
|
|
|
'Request to send password reset email failed because no user was found for the provided email',
|
|
|
|
{ invalidEmail: email },
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-01-15 06:01:48 -08:00
|
|
|
if (this.license.isLdapEnabled() && ldapIdentity) {
|
2023-01-27 02:19:47 -08:00
|
|
|
throw new UnprocessableRequestError('forgotPassword.ldapUserPasswordResetUnavailable');
|
|
|
|
}
|
|
|
|
|
2024-02-28 04:12:28 -08:00
|
|
|
const url = this.authService.generatePasswordResetUrl(user);
|
2023-01-27 02:19:47 -08:00
|
|
|
|
2023-11-07 06:35:43 -08:00
|
|
|
const { id, firstName, lastName } = user;
|
2023-01-27 02:19:47 -08:00
|
|
|
try {
|
2023-03-16 07:34:13 -07:00
|
|
|
await this.mailer.passwordReset({
|
2023-01-27 02:19:47 -08:00
|
|
|
email,
|
|
|
|
firstName,
|
|
|
|
lastName,
|
2023-06-17 01:23:22 -07:00
|
|
|
passwordResetUrl: url,
|
2023-12-22 06:19:50 -08:00
|
|
|
domain: this.urlService.getInstanceBaseUrl(),
|
2023-01-27 02:19:47 -08:00
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
void this.internalHooks.onEmailFailed({
|
|
|
|
user,
|
|
|
|
message_type: 'Reset password',
|
|
|
|
public_api: false,
|
|
|
|
});
|
|
|
|
if (error instanceof Error) {
|
|
|
|
throw new InternalServerError(`Please contact your administrator: ${error.message}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.logger.info('Sent password reset email successfully', { userId: user.id, email });
|
|
|
|
void this.internalHooks.onUserTransactionalEmail({
|
|
|
|
user_id: id,
|
|
|
|
message_type: 'Reset password',
|
|
|
|
public_api: false,
|
|
|
|
});
|
|
|
|
|
|
|
|
void this.internalHooks.onUserPasswordResetRequestClick({ user });
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Verify password reset token and user ID.
|
|
|
|
*/
|
2024-02-28 08:02:18 -08:00
|
|
|
@Get('/resolve-password-token', { skipAuth: true })
|
2023-01-27 02:19:47 -08:00
|
|
|
async resolvePasswordToken(req: PasswordResetRequest.Credentials) {
|
2023-11-07 06:35:43 -08:00
|
|
|
const { token } = req.query;
|
2023-01-27 02:19:47 -08:00
|
|
|
|
2023-11-07 06:35:43 -08:00
|
|
|
if (!token) {
|
2023-01-27 02:19:47 -08:00
|
|
|
this.logger.debug(
|
2023-07-24 14:40:17 -07:00
|
|
|
'Request to resolve password token failed because of missing password reset token',
|
2023-01-27 02:19:47 -08:00
|
|
|
{
|
|
|
|
queryString: req.query,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
throw new BadRequestError('');
|
|
|
|
}
|
|
|
|
|
2024-02-28 04:12:28 -08:00
|
|
|
const user = await this.authService.resolvePasswordResetToken(token);
|
2023-11-07 06:35:43 -08:00
|
|
|
if (!user) throw new NotFoundError('');
|
2023-01-27 02:19:47 -08:00
|
|
|
|
2023-11-07 06:35:43 -08:00
|
|
|
if (!user?.isOwner && !this.license.isWithinUsersLimit()) {
|
2023-07-12 05:11:46 -07:00
|
|
|
this.logger.debug(
|
|
|
|
'Request to resolve password token failed because the user limit was reached',
|
2023-11-07 06:35:43 -08:00
|
|
|
{ userId: user.id },
|
2023-07-12 05:11:46 -07:00
|
|
|
);
|
|
|
|
throw new UnauthorizedError(RESPONSE_ERROR_MESSAGES.USERS_QUOTA_REACHED);
|
|
|
|
}
|
2023-07-24 14:40:17 -07:00
|
|
|
|
|
|
|
this.logger.info('Reset-password token resolved successfully', { userId: user.id });
|
2023-01-27 02:19:47 -08:00
|
|
|
void this.internalHooks.onUserPasswordResetEmailClick({ user });
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-07-24 14:40:17 -07:00
|
|
|
* Verify password reset token and update password.
|
2023-01-27 02:19:47 -08:00
|
|
|
*/
|
2024-02-28 08:02:18 -08:00
|
|
|
@Post('/change-password', { skipAuth: true })
|
2023-01-27 02:19:47 -08:00
|
|
|
async changePassword(req: PasswordResetRequest.NewPassword, res: Response) {
|
2023-11-07 06:35:43 -08:00
|
|
|
const { token, password, mfaToken } = req.body;
|
2023-01-27 02:19:47 -08:00
|
|
|
|
2023-11-07 06:35:43 -08:00
|
|
|
if (!token || !password) {
|
2023-01-27 02:19:47 -08:00
|
|
|
this.logger.debug(
|
|
|
|
'Request to change password failed because of missing user ID or password or reset password token in payload',
|
|
|
|
{
|
|
|
|
payload: req.body,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
throw new BadRequestError('Missing user ID or password or reset password token');
|
|
|
|
}
|
|
|
|
|
2023-12-11 09:23:42 -08:00
|
|
|
const validPassword = this.passwordUtility.validate(password);
|
2023-01-27 02:19:47 -08:00
|
|
|
|
2024-02-28 04:12:28 -08:00
|
|
|
const user = await this.authService.resolvePasswordResetToken(token);
|
2023-11-07 06:35:43 -08:00
|
|
|
if (!user) throw new NotFoundError('');
|
2023-01-27 02:19:47 -08:00
|
|
|
|
2023-08-23 19:59:16 -07:00
|
|
|
if (user.mfaEnabled) {
|
|
|
|
if (!mfaToken) throw new BadRequestError('If MFA enabled, mfaToken is required.');
|
|
|
|
|
|
|
|
const { decryptedSecret: secret } = await this.mfaService.getSecretAndRecoveryCodes(user.id);
|
|
|
|
|
|
|
|
const validToken = this.mfaService.totp.verifySecret({ secret, token: mfaToken });
|
|
|
|
|
|
|
|
if (!validToken) throw new BadRequestError('Invalid MFA token.');
|
|
|
|
}
|
|
|
|
|
2023-12-11 09:23:42 -08:00
|
|
|
const passwordHash = await this.passwordUtility.hash(validPassword);
|
2023-03-30 07:44:39 -07:00
|
|
|
|
2023-08-22 06:58:05 -07:00
|
|
|
await this.userService.update(user.id, { password: passwordHash });
|
2023-01-27 02:19:47 -08:00
|
|
|
|
2023-07-24 14:40:17 -07:00
|
|
|
this.logger.info('User password updated successfully', { userId: user.id });
|
2023-01-27 02:19:47 -08:00
|
|
|
|
2024-04-09 02:20:35 -07:00
|
|
|
this.authService.issueCookie(res, user, req.browserId);
|
2023-01-27 02:19:47 -08:00
|
|
|
|
|
|
|
void this.internalHooks.onUserUpdate({
|
|
|
|
user,
|
|
|
|
fields_changed: ['password'],
|
|
|
|
});
|
|
|
|
|
|
|
|
// if this user used to be an LDAP users
|
|
|
|
const ldapIdentity = user?.authIdentities?.find((i) => i.providerType === 'ldap');
|
|
|
|
if (ldapIdentity) {
|
|
|
|
void this.internalHooks.onUserSignup(user, {
|
|
|
|
user_type: 'email',
|
|
|
|
was_disabled_ldap_user: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-03-30 07:44:39 -07:00
|
|
|
await this.externalHooks.run('user.password.update', [user.email, passwordHash]);
|
2023-01-27 02:19:47 -08:00
|
|
|
}
|
|
|
|
}
|