feat: two-factor sign-in, required for sensitive permissions
Pull request / title (pull_request) Successful in 2s
CI / test (pull_request) Successful in 1m11s
CI / image (apps/api/Dockerfile, t5de-hub-api) (pull_request) Skipped
CI / image (apps/web/Dockerfile, t5de-hub-web) (pull_request) Skipped

Better Auth's twoFactor plugin: an authenticator app (TOTP) with 10 backup codes.
Turning it on, making new backup codes and turning it off are on the account page,
and signing in asks for a code once it's on. Sensitive permissions (product:remove,
person:ban, person:role, settings:edit, catalog:suspend, ...) answer 403
two_factor_required without it, API tokens included; the console reminds people
who need it. Better Auth's own admin endpoints are closed (404).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
dhkatzandClaude Opus 5.5 committed 2026-11-05 19:17:46 -08:00
1 parent 69c55a72fb
commit d5bc792190
28 files changed
+4365 -62

No files matched your search

+14
View File
@@ -0,0 +1,14 @@
CREATE TABLE "two_factor" (
"id" text PRIMARY KEY NOT NULL,
"user_id" text NOT NULL,
"secret" text NOT NULL,
"backup_codes" text NOT NULL,
"verified" boolean DEFAULT true NOT NULL,
"failed_verification_count" integer DEFAULT 0 NOT NULL,
"locked_until" timestamp with time zone
);
--> statement-breakpoint
ALTER TABLE "user" ADD COLUMN "two_factor_enabled" boolean DEFAULT false NOT NULL;--> statement-breakpoint
ALTER TABLE "two_factor" ADD CONSTRAINT "two_factor_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
CREATE INDEX "two_factor_user" ON "two_factor" USING btree ("user_id");--> statement-breakpoint
CREATE INDEX "two_factor_secret" ON "two_factor" USING btree ("secret");
File diff suppressed because it is too large. Load diff
+7
View File
@@ -106,6 +106,13 @@
"when": 1793918993367,
"tag": "0014_api_tokens",
"breakpoints": true
},
{
"idx": 15,
"version": "7",
"when": 1793934143730,
"tag": "0015_two_factor",
"breakpoints": true
}
]
}
+19
View File
@@ -45,6 +45,8 @@ export const user = pgTable(
linkCap: integer("link_cap").notNull().default(10),
// False while a new account waits for a moderator's approval (registration closed); it can only sign in.
approved: boolean("approved").notNull().default(true),
// Two-factor sign-in (an authenticator app), required for sensitive permissions (docs/console.md).
twoFactorEnabled: boolean("two_factor_enabled").notNull().default(false),
},
(t) => [uniqueIndex("user_username_lower").on(sql`lower(${t.username})`)],
);
@@ -112,6 +114,23 @@ export const jwks = pgTable("jwks", {
crv: text("crv"),
});
// Two-factor sign-in: the person's authenticator secret and backup codes (both encrypted by Better Auth).
export const twoFactor = pgTable(
"two_factor",
{
id: text("id").primaryKey(),
userId: text("user_id")
.notNull()
.references(() => user.id, { onDelete: "cascade" }),
secret: text("secret").notNull(),
backupCodes: text("backup_codes").notNull(),
verified: boolean("verified").notNull().default(true),
failedVerificationCount: integer("failed_verification_count").notNull().default(0),
lockedUntil: timestamp("locked_until", { withTimezone: true }),
},
(t) => [index("two_factor_user").on(t.userId), index("two_factor_secret").on(t.secret)],
);
// A T5DE proxy signing in: it shows userCode, the person approves it on the website.
export const deviceCode = pgTable("device_code", {
id: text("id").primaryKey(),