mirror of
https://github.com/documenso/documenso.git
synced 2026-10-02 17:54:28 +10:00
Selecting password auth failed with a generic "Unauthorized" for users who signed up via OAuth or passkey, with no way to set one. Detect the missing password and email the existing reset link from the signing dialog and security settings. Require a 2FA code and rate limit update-password.
36 lines
1020 B
TypeScript
36 lines
1020 B
TypeScript
import type { User } from '@prisma/client';
|
|
|
|
import { AppError, AppErrorCode } from '../../errors/app-error';
|
|
import { verifyTwoFactorAuthenticationToken } from './verify-2fa-token';
|
|
import { verifyBackupCode } from './verify-backup-code';
|
|
|
|
type ValidateTwoFactorAuthenticationOptions = {
|
|
totpCode?: string;
|
|
backupCode?: string;
|
|
user: Pick<User, 'id' | 'email' | 'twoFactorEnabled' | 'twoFactorSecret' | 'twoFactorBackupCodes'>;
|
|
};
|
|
|
|
export const validateTwoFactorAuthentication = async ({
|
|
backupCode,
|
|
totpCode,
|
|
user,
|
|
}: ValidateTwoFactorAuthenticationOptions) => {
|
|
if (!user.twoFactorEnabled) {
|
|
throw new AppError(AppErrorCode.TWO_FACTOR_SETUP_REQUIRED);
|
|
}
|
|
|
|
if (!user.twoFactorSecret) {
|
|
throw new AppError(AppErrorCode.TWO_FACTOR_MISSING_SECRET);
|
|
}
|
|
|
|
if (totpCode) {
|
|
return await verifyTwoFactorAuthenticationToken({ user, totpCode });
|
|
}
|
|
|
|
if (backupCode) {
|
|
return verifyBackupCode({ user, backupCode });
|
|
}
|
|
|
|
throw new AppError(AppErrorCode.TWO_FACTOR_MISSING_CREDENTIALS);
|
|
};
|