2023-07-24 14:40:17 -07:00
|
|
|
import { IsNull, Not } from 'typeorm';
|
2023-01-27 02:19:47 -08:00
|
|
|
import validator from 'validator';
|
|
|
|
import { Get, Post, RestController } from '@/decorators';
|
|
|
|
import {
|
|
|
|
BadRequestError,
|
|
|
|
InternalServerError,
|
|
|
|
NotFoundError,
|
2023-03-30 03:44:53 -07:00
|
|
|
UnauthorizedError,
|
2023-01-27 02:19:47 -08:00
|
|
|
UnprocessableRequestError,
|
|
|
|
} from '@/ResponseHelper';
|
|
|
|
import {
|
|
|
|
getInstanceBaseUrl,
|
|
|
|
hashPassword,
|
|
|
|
validatePassword,
|
|
|
|
} from '@/UserManagement/UserManagementHelper';
|
2023-08-25 04:23:22 -07:00
|
|
|
import { UserManagementMailer } from '@/UserManagement/email';
|
2023-01-27 02:19:47 -08:00
|
|
|
|
2023-01-27 05:56:56 -08:00
|
|
|
import { Response } from 'express';
|
|
|
|
import { PasswordResetRequest } from '@/requests';
|
2023-08-25 04:23:22 -07:00
|
|
|
import { IExternalHooksClass, IInternalHooksClass } from '@/Interfaces';
|
2023-01-27 02:19:47 -08:00
|
|
|
import { issueCookie } from '@/auth/jwt';
|
|
|
|
import { isLdapEnabled } from '@/Ldap/helpers';
|
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';
|
|
|
|
import { Container } from 'typedi';
|
|
|
|
import { RESPONSE_ERROR_MESSAGES } from '@/constants';
|
2023-07-24 14:40:17 -07:00
|
|
|
import { TokenExpiredError } from 'jsonwebtoken';
|
2023-08-22 06:58:05 -07:00
|
|
|
import type { JwtPayload } from '@/services/jwt.service';
|
|
|
|
import { JwtService } from '@/services/jwt.service';
|
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-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-08-25 04:23:22 -07:00
|
|
|
private readonly externalHooks: IExternalHooksClass,
|
|
|
|
private readonly internalHooks: IInternalHooksClass,
|
|
|
|
private readonly mailer: UserManagementMailer,
|
|
|
|
private readonly userService: UserService,
|
|
|
|
private readonly jwtService: JwtService,
|
|
|
|
private readonly mfaService: MfaService,
|
|
|
|
) {}
|
2023-01-27 02:19:47 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Send a password reset email.
|
|
|
|
*/
|
|
|
|
@Post('/forgot-password')
|
|
|
|
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
|
2023-08-22 06:58:05 -07:00
|
|
|
const user = await this.userService.findOne({
|
2023-01-27 02:19:47 -08:00
|
|
|
where: {
|
|
|
|
email,
|
|
|
|
password: Not(IsNull()),
|
|
|
|
},
|
2023-03-30 03:44:53 -07:00
|
|
|
relations: ['authIdentities', 'globalRole'],
|
2023-01-27 02:19:47 -08:00
|
|
|
});
|
|
|
|
|
2023-07-12 05:11:46 -07:00
|
|
|
if (!user?.isOwner && !Container.get(License).isWithinUsersLimit()) {
|
|
|
|
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() &&
|
|
|
|
!(user?.globalRole.name === 'owner' || user?.settings?.allowSSOManualLogin === true)
|
|
|
|
) {
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isLdapEnabled() && ldapIdentity) {
|
|
|
|
throw new UnprocessableRequestError('forgotPassword.ldapUserPasswordResetUnavailable');
|
|
|
|
}
|
|
|
|
|
|
|
|
const baseUrl = getInstanceBaseUrl();
|
2023-05-30 03:52:02 -07:00
|
|
|
const { id, firstName, lastName } = user;
|
2023-07-24 14:40:17 -07:00
|
|
|
|
|
|
|
const resetPasswordToken = this.jwtService.signData(
|
|
|
|
{ sub: id },
|
|
|
|
{
|
2023-11-03 04:32:08 -07:00
|
|
|
expiresIn: '20m',
|
2023-07-24 14:40:17 -07:00
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2023-08-23 19:59:16 -07:00
|
|
|
const url = this.userService.generatePasswordResetUrl(
|
|
|
|
baseUrl,
|
|
|
|
resetPasswordToken,
|
|
|
|
user.mfaEnabled,
|
|
|
|
);
|
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-01-27 02:19:47 -08:00
|
|
|
domain: baseUrl,
|
|
|
|
});
|
|
|
|
} 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.
|
|
|
|
*/
|
|
|
|
@Get('/resolve-password-token')
|
|
|
|
async resolvePasswordToken(req: PasswordResetRequest.Credentials) {
|
2023-07-24 14:40:17 -07:00
|
|
|
const { token: resetPasswordToken } = req.query;
|
2023-01-27 02:19:47 -08:00
|
|
|
|
2023-07-24 14:40:17 -07:00
|
|
|
if (!resetPasswordToken) {
|
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('');
|
|
|
|
}
|
|
|
|
|
2023-07-24 14:40:17 -07:00
|
|
|
const decodedToken = this.verifyResetPasswordToken(resetPasswordToken);
|
2023-01-27 02:19:47 -08:00
|
|
|
|
2023-08-22 06:58:05 -07:00
|
|
|
const user = await this.userService.findOne({
|
|
|
|
where: { id: decodedToken.sub },
|
2023-07-12 05:11:46 -07:00
|
|
|
relations: ['globalRole'],
|
2023-01-27 02:19:47 -08:00
|
|
|
});
|
2023-07-24 14:40:17 -07:00
|
|
|
|
2023-07-12 05:11:46 -07:00
|
|
|
if (!user?.isOwner && !Container.get(License).isWithinUsersLimit()) {
|
|
|
|
this.logger.debug(
|
|
|
|
'Request to resolve password token failed because the user limit was reached',
|
2023-07-24 14:40:17 -07:00
|
|
|
{ userId: decodedToken.sub },
|
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
|
|
|
|
2023-01-27 02:19:47 -08:00
|
|
|
if (!user) {
|
|
|
|
this.logger.debug(
|
2023-07-24 14:40:17 -07:00
|
|
|
'Request to resolve password token failed because no user was found for the provided user ID',
|
2023-01-27 02:19:47 -08:00
|
|
|
{
|
2023-07-24 14:40:17 -07:00
|
|
|
userId: decodedToken.sub,
|
2023-01-27 02:19:47 -08:00
|
|
|
resetPasswordToken,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
throw new NotFoundError('');
|
|
|
|
}
|
|
|
|
|
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
|
|
|
*/
|
|
|
|
@Post('/change-password')
|
|
|
|
async changePassword(req: PasswordResetRequest.NewPassword, res: Response) {
|
2023-08-23 19:59:16 -07:00
|
|
|
const { token: resetPasswordToken, password, mfaToken } = req.body;
|
2023-01-27 02:19:47 -08:00
|
|
|
|
2023-07-24 14:40:17 -07:00
|
|
|
if (!resetPasswordToken || !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');
|
|
|
|
}
|
|
|
|
|
|
|
|
const validPassword = validatePassword(password);
|
|
|
|
|
2023-07-24 14:40:17 -07:00
|
|
|
const decodedToken = this.verifyResetPasswordToken(resetPasswordToken);
|
2023-01-27 02:19:47 -08:00
|
|
|
|
2023-08-22 06:58:05 -07:00
|
|
|
const user = await this.userService.findOne({
|
2023-07-24 14:40:17 -07:00
|
|
|
where: { id: decodedToken.sub },
|
2023-09-21 02:56:40 -07:00
|
|
|
relations: ['authIdentities', 'globalRole'],
|
2023-01-27 02:19:47 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
if (!user) {
|
|
|
|
this.logger.debug(
|
2023-07-24 14:40:17 -07:00
|
|
|
'Request to resolve password token failed because no user was found for the provided user ID',
|
2023-01-27 02:19:47 -08:00
|
|
|
{
|
|
|
|
resetPasswordToken,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
throw new NotFoundError('');
|
|
|
|
}
|
|
|
|
|
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-03-30 07:44:39 -07:00
|
|
|
const passwordHash = await hashPassword(validPassword);
|
|
|
|
|
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
|
|
|
|
|
|
|
await issueCookie(res, user);
|
|
|
|
|
|
|
|
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
|
|
|
}
|
2023-07-24 14:40:17 -07:00
|
|
|
|
|
|
|
private verifyResetPasswordToken(resetPasswordToken: string) {
|
|
|
|
let decodedToken: JwtPayload;
|
|
|
|
try {
|
|
|
|
decodedToken = this.jwtService.verifyToken(resetPasswordToken);
|
|
|
|
return decodedToken;
|
|
|
|
} catch (e) {
|
|
|
|
if (e instanceof TokenExpiredError) {
|
|
|
|
this.logger.debug('Reset password token expired', {
|
|
|
|
resetPasswordToken,
|
|
|
|
});
|
|
|
|
throw new NotFoundError('');
|
|
|
|
}
|
|
|
|
this.logger.debug('Error verifying token', {
|
|
|
|
resetPasswordToken,
|
|
|
|
});
|
|
|
|
throw new BadRequestError('');
|
|
|
|
}
|
|
|
|
}
|
2023-01-27 02:19:47 -08:00
|
|
|
}
|