feat(api): accept recovery codes as second factor and let users regenerate them

Enabling TOTP returns ten codes that are shown once. Disabling TOTP and
regenerating codes require the password plus a current code.

Closes #40

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
This commit is contained in:
Sebastian UnterschützandClaude Opus 5.5 committed 2026-11-22 00:35:20 +01:00
1 parent dbea42e7a5
commit 7b063d2cd8
12 files changed
+510 -123

No files matched your search

+37 -8
View File
@@ -13,6 +13,8 @@ type Me struct {
ActiveOrg string
Orgs []Org
TOTPMissing bool
// RecoveryCodesLeft counts unused TOTP recovery codes.
RecoveryCodesLeft int
}
// Org is one entry of the organization switcher.
@@ -81,13 +83,29 @@ templ Security(p Page, me Me, identities []Identity, canLink bool) {
<section class="max-w-md">
<h2 class="mb-1 text-xs font-medium uppercase tracking-wider text-muted">{ i18n.T(ctx, "security.totp.heading") }</h2>
if me.TOTP {
<p class="mb-6 text-sm"><span class="text-up">●</span> { i18n.T(ctx, "security.totp.active") }</p>
<form data-api="/me/totp/disable" data-next="/settings/security">
@formError()
@field(i18n.T(ctx, "auth.field.password"), "password", "password", "current-password", true)
@field(i18n.T(ctx, "security.totp.current_code"), "code", "text", "one-time-code", true)
@submit(i18n.T(ctx, "security.totp.disable"))
</form>
<p class="mb-1 text-sm"><span class="text-up">●</span> { i18n.T(ctx, "security.totp.active") }</p>
<p class={ "mb-6 text-sm", templ.KV("text-down", me.RecoveryCodesLeft < 3), templ.KV("text-muted", me.RecoveryCodesLeft >= 3) }>
{ i18n.T(ctx, "security.recovery.left", "Count", me.RecoveryCodesLeft) }
</p>
<details class="mb-6">
<summary class="cursor-pointer text-sm text-muted hover:text-ink">{ i18n.T(ctx, "security.recovery.regenerate") }</summary>
<form data-api="/me/totp/recovery-codes" data-show-codes class="mt-4">
@formError()
@field(i18n.T(ctx, "auth.field.password"), "password", "password", "current-password", true)
@field(i18n.T(ctx, "security.totp.current_code"), "code", "text", "one-time-code", true)
@submit(i18n.T(ctx, "security.recovery.regenerate"))
@recoveryOutput()
</form>
</details>
<details>
<summary class="cursor-pointer text-sm text-muted hover:text-ink">{ i18n.T(ctx, "security.totp.disable") }</summary>
<form data-api="/me/totp/disable" data-next="/settings/security" class="mt-4">
@formError()
@field(i18n.T(ctx, "auth.field.password"), "password", "password", "current-password", true)
@field(i18n.T(ctx, "security.totp.current_code"), "code", "text", "one-time-code", true)
@submit(i18n.T(ctx, "security.totp.disable"))
</form>
</details>
} else {
<p class="mb-6 text-sm"><span class="text-unknown">●</span> { i18n.T(ctx, "security.totp.inactive") }</p>
<div data-totp-setup>
@@ -95,10 +113,11 @@ templ Security(p Page, me Me, identities []Identity, canLink bool) {
<div data-totp-panel hidden>
<img data-totp-qr alt={ i18n.T(ctx, "security.totp.qr_alt") } class="mb-3 h-48 w-48 border border-rule bg-white p-2"/>
<p class="mb-6 break-all font-mono text-xs text-muted" data-totp-secret></p>
<form data-api="/me/totp/enable" data-next="/settings/security">
<form data-api="/me/totp/enable" data-show-codes>
@formError()
@field(i18n.T(ctx, "security.totp.app_code"), "code", "text", "one-time-code", true)
@submit(i18n.T(ctx, "security.totp.confirm"))
@recoveryOutput()
</form>
</div>
</div>
@@ -121,3 +140,13 @@ templ Security(p Page, me Me, identities []Identity, canLink bool) {
</section>
}
}
// recoveryOutput is filled by app.js with codes returned by the API; they
// are shown once and never rendered by the server.
templ recoveryOutput() {
<div data-recovery-output hidden class="mt-6 border border-ink p-4">
<p class="mb-3 text-sm">{ i18n.T(ctx, "security.recovery.save_now") }</p>
<ol data-recovery-list class="mb-4 grid grid-cols-2 gap-x-6 gap-y-1 font-mono text-sm"></ol>
<a href="/settings/security" class="text-sm underline underline-offset-2">{ i18n.T(ctx, "security.recovery.saved") }</a>
</div>
}