2023-12-01 05:52:16 +05:30
|
|
|
import { prisma } from '@documenso/prisma';
|
2024-01-30 17:31:27 +11:00
|
|
|
import type { User } from '@documenso/prisma/client';
|
|
|
|
|
import { UserSecurityAuditLogType } from '@documenso/prisma/client';
|
2023-12-01 05:52:16 +05:30
|
|
|
|
2024-08-29 01:00:57 +00:00
|
|
|
import { AppError, AppErrorCode } from '../../errors/app-error';
|
2024-01-30 17:31:27 +11:00
|
|
|
import type { RequestMetadata } from '../../universal/extract-request-metadata';
|
2023-12-01 05:52:16 +05:30
|
|
|
import { validateTwoFactorAuthentication } from './validate-2fa';
|
|
|
|
|
|
|
|
|
|
type DisableTwoFactorAuthenticationOptions = {
|
|
|
|
|
user: User;
|
2024-08-29 01:00:57 +00:00
|
|
|
totpCode?: string;
|
|
|
|
|
backupCode?: string;
|
2024-01-30 17:31:27 +11:00
|
|
|
requestMetadata?: RequestMetadata;
|
2023-12-01 05:52:16 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const disableTwoFactorAuthentication = async ({
|
2024-08-29 01:00:57 +00:00
|
|
|
totpCode,
|
|
|
|
|
backupCode,
|
2023-12-01 05:52:16 +05:30
|
|
|
user,
|
2024-01-30 17:31:27 +11:00
|
|
|
requestMetadata,
|
2023-12-01 05:52:16 +05:30
|
|
|
}: DisableTwoFactorAuthenticationOptions) => {
|
2024-08-29 01:00:57 +00:00
|
|
|
let isValid = false;
|
2023-12-01 05:52:16 +05:30
|
|
|
|
2024-08-29 01:00:57 +00:00
|
|
|
if (!totpCode && !backupCode) {
|
|
|
|
|
throw new AppError(AppErrorCode.INVALID_REQUEST);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (totpCode) {
|
|
|
|
|
isValid = await validateTwoFactorAuthentication({ totpCode, user });
|
|
|
|
|
} else if (backupCode) {
|
|
|
|
|
isValid = await validateTwoFactorAuthentication({ backupCode, user });
|
2023-12-01 05:52:16 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!isValid) {
|
2024-03-25 11:34:50 +08:00
|
|
|
throw new AppError('INCORRECT_TWO_FACTOR_CODE');
|
2023-12-01 05:52:16 +05:30
|
|
|
}
|
|
|
|
|
|
2024-01-30 17:31:27 +11:00
|
|
|
await prisma.$transaction(async (tx) => {
|
|
|
|
|
await tx.user.update({
|
|
|
|
|
where: {
|
|
|
|
|
id: user.id,
|
|
|
|
|
},
|
|
|
|
|
data: {
|
|
|
|
|
twoFactorEnabled: false,
|
|
|
|
|
twoFactorBackupCodes: null,
|
|
|
|
|
twoFactorSecret: null,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await tx.userSecurityAuditLog.create({
|
|
|
|
|
data: {
|
|
|
|
|
userId: user.id,
|
|
|
|
|
type: UserSecurityAuditLogType.AUTH_2FA_DISABLE,
|
|
|
|
|
userAgent: requestMetadata?.userAgent,
|
|
|
|
|
ipAddress: requestMetadata?.ipAddress,
|
|
|
|
|
},
|
|
|
|
|
});
|
2023-12-01 05:52:16 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
};
|