WIP Events 2.3.0: TOTP util, schema and migration for authenticator-app 2FA

Not wired up yet: twoFactor.js, login, enable/confirm/disable, recovery
codes endpoints and the frontend still to do.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-17 13:37:52 +02:00
co-authored by Claude Opus 5.5
parent 53e15fdd69
commit b207d17db8
3 changed files with 117 additions and 0 deletions
@@ -0,0 +1,16 @@
-- 2.3.0: two-factor with an authenticator app (TOTP), and recovery codes.
ALTER TYPE "TwoFactorMethod" ADD VALUE 'authenticator';
ALTER TABLE "User" ADD COLUMN "totpSecret" TEXT;
ALTER TABLE "User" ADD COLUMN "totpLastStep" INTEGER;
ALTER TABLE "TwoFactorChallenge" ADD COLUMN "pendingSecret" TEXT;
CREATE TABLE "RecoveryCode" (
"id" TEXT NOT NULL,
"userId" TEXT NOT NULL,
"codeHash" TEXT NOT NULL,
"usedAt" TIMESTAMP(3),
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "RecoveryCode_pkey" PRIMARY KEY ("id")
);
CREATE UNIQUE INDEX "RecoveryCode_codeHash_key" ON "RecoveryCode"("codeHash");
CREATE INDEX "RecoveryCode_userId_idx" ON "RecoveryCode"("userId");
ALTER TABLE "RecoveryCode" ADD CONSTRAINT "RecoveryCode_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;
+21
View File
@@ -57,6 +57,8 @@ enum SecurityEventType {
enum TwoFactorMethod {
email
whatsapp
// 2.3.0: codes from an authenticator app (TOTP); nothing is sent.
authenticator
}
model User {
@@ -75,6 +77,11 @@ model User {
notificationPreference NotificationPreference @default(email)
twoFactorEnabled Boolean @default(false)
twoFactorMethod TwoFactorMethod?
// 2.3.0: the authenticator app's secret (encrypted, utils/encryption.js) and the last 30-second
// step a code was used for, so a code can't be used twice.
totpSecret String?
totpLastStep Int?
recoveryCodes RecoveryCode[]
// POPIA: opted in to "News & announcements" (marketing) bulk messages, per channel.
// Event updates to registrants don't depend on these. History in CommunicationConsent.
newsEmail Boolean @default(false)
@@ -713,6 +720,8 @@ model TwoFactorChallenge {
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
purpose String // 'login' | 'enable'
method TwoFactorMethod
// 2.3.0: an authenticator app being set up: its secret (encrypted) until the first code confirms it.
pendingSecret String?
codeHash String
attempts Int @default(0)
lastSentAt DateTime @default(now())
@@ -2353,3 +2362,15 @@ model AiCall {
@@index([createdAt])
@@index([userId, createdAt])
}
// 2.3.0: single-use codes for when the authenticator app (or phone) is lost. Only an HMAC is kept.
model RecoveryCode {
id String @id @default(uuid())
userId String
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
codeHash String @unique
usedAt DateTime?
createdAt DateTime @default(now())
@@index([userId])
}