feat: two-factor sign-in, required for sensitive permissions (#12)
Phase 3d (docs/design.md): two-factor sign-in, required for sensitive permissions. - **Sign-in:** Better Auth's `twoFactor` plugin, using an authenticator app (TOTP) with 10 backup codes. Once it's on, signing in asks for a code from the app or a backup code, and each backup code works once. - **Account page:** a new Two-Factor Sign-In card. - To turn it on: enter your password, scan the QR code (the key is also shown as text), keep the backup codes, and confirm with a code. - Once it's on, you can make new backup codes or turn it off; both ask for the password. - If you hold sensitive permissions, the card lists the ones that need it. - **Enforcement:** the sensitive permissions are `product:remove`, `person:ban`, `person:role`, `audit:read-sensitive`, `catalog:transfer`, `catalog:suspend`, `import:grant-on-behalf` and `settings:edit` (`permissions.ts` `sensitive`). Without two-factor on, they answer 403 `two_factor_required`, and API tokens get the same check through their owner. - Routes guarded only by the moderator check now also check the permission: removing products, ban and unban, roles, and settings. - **Console:** a reminder on every console page while two-factor is needed. The People list notes staff without two-factor, and each person's page shows "two-factor on". - **Closed Better Auth admin endpoints:** `/api/auth/admin/*` now answers 404. Admins could have used these endpoints to ban people or change roles without the check or the audit log. - **Tests:** a new end-to-end test, `test/two-factor.test.ts`. Moderation tests turn two-factor on for their accounts directly (`test/two-factor.ts`). All 57 API tests pass, and type checks pass. - **Checked on the scratch hub through the website:** - turning it on with a real code - the console reminder appearing and disappearing - signing in again needing a code - a Ban form while two-factor is off showing "Turn on two-factor sign-in on your account page first." - **Not checked in a browser:** the QR and sign-in code screens. - **Dependency:** `uqr` draws the QR code; it has no dependencies of its own. Phase 3d (docs/design.md): two-factor sign-in, required for sensitive permissions. - **Sign-in:** Better Auth's `twoFactor` plugin, using an authenticator app (TOTP) with 10 backup codes. Once it's on, signing in asks for a code from the app or a backup code, and each backup code works once. - **Account page:** a new Two-Factor Sign-In card. - To turn it on: enter your password, scan the QR code (the key is also shown as text), keep the backup codes, and confirm with a code. - Once it's on, you can make new backup codes or turn it off; both ask for the password. - If you hold sensitive permissions, the card lists the ones that need it. - **Enforcement:** the sensitive permissions are `product:remove`, `person:ban`, `person:role`, `audit:read-sensitive`, `catalog:transfer`, `catalog:suspend`, `import:grant-on-behalf` and `settings:edit` (`permissions.ts` `sensitive`). Without two-factor on, they answer 403 `two_factor_required`, and API tokens get the same check through their owner. - Routes guarded only by the moderator check now also check the permission: removing products, ban and unban, roles, and settings. - **Console:** a reminder on every console page while two-factor is needed. The People list notes staff without two-factor, and each person's page shows "two-factor on". - **Closed Better Auth admin endpoints:** `/api/auth/admin/*` now answers 404. Admins could have used these endpoints to ban people or change roles without the check or the audit log. - **Tests:** a new end-to-end test, `test/two-factor.test.ts`. Moderation tests turn two-factor on for their accounts directly (`test/two-factor.ts`). All 57 API tests pass, and type checks pass. - **Checked on the scratch hub through the website:** - turning it on with a real code - the console reminder appearing and disappearing - signing in again needing a code - a Ban form while two-factor is off showing "Turn on two-factor sign-in on your account page first." - **Not checked in a browser:** the QR and sign-in code screens. - **Dependency:** `uqr` draws the QR code; it has no dependencies of its own. Later: auditing when two-factor is turned on or off, and an optional "trust this browser". 🤖 Generated with [Claude Code](https://claude.com/claude-code) Later: auditing when two-factor is turned on or off, and an optional "trust this browser". 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: dhkatz <[email protected]>
This commit was merged in pull request #12.
This commit is contained in:
1 parent
69c55a72fb
commit
eca1884918
28 files changed
+4365
-62
No files matched your search
@@ -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
@@ -106,6 +106,13 @@
|
||||
"when": 1793918993367,
|
||||
"tag": "0014_api_tokens",
|
||||
"breakpoints": true
|
||||
},
|
||||
{
|
||||
"idx": 15,
|
||||
"version": "7",
|
||||
"when": 1793934143730,
|
||||
"tag": "0015_two_factor",
|
||||
"breakpoints": true
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -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(),
|
||||
|
||||
Reference in new issue
Block a user