Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions packages/api/src/models/otp.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { randomBytes } from "node:crypto";
import { verify as argonVerify, hash } from "argon2";
import { and, eq } from "drizzle-orm";
import { and, eq, isNull } from "drizzle-orm";
import { otpBackupCodes, otpConfigs, users } from "../db/schema.ts";
import { ValidationError } from "../errors.ts";
import { getSetting } from "../services/settings.ts";
Expand Down Expand Up @@ -184,7 +184,13 @@ export async function verifyOtpCode(
const backupCodes = await context.db
.select({ id: otpBackupCodes.id, codeHash: otpBackupCodes.codeHash })
.from(otpBackupCodes)
.where(and(eq(otpBackupCodes.cohort, cohort), eq(otpBackupCodes.subjectId, subjectId)));
.where(
and(
eq(otpBackupCodes.cohort, cohort),
eq(otpBackupCodes.subjectId, subjectId),
isNull(otpBackupCodes.usedAt)
)
);
for (const backupCode of backupCodes) {
const isMatch = await argonVerify(backupCode.codeHash, code).catch(() => false);
if (isMatch) {
Expand All @@ -200,7 +206,7 @@ export async function verifyOtpCode(
await context.db
.update(otpBackupCodes)
.set({ usedAt: new Date() })
.where(eq(otpBackupCodes.id, backupCode.id));
.where(and(eq(otpBackupCodes.id, backupCode.id), isNull(otpBackupCodes.usedAt)));
return { success: true };
}
}
Expand Down
Loading