feat(webui): add optional TOTP 2FA for operator login (#351)

WebUI access was a single shared password. This adds optional
Authenticator TOTP as a second factor, with one-time recovery codes
and SSH as the last-resort disable path.

Login stays two HTTP posts with no pending token: a correct password
and enabled 2FA without a second factor returns { needsTotp: true };
the client then resubmits { password, totp } or { password, recoveryCode }.
The TOTP secret is wrapped with a password-derived AES-256-GCM key.
Eight XXXX-XXXX recovery codes are stored as SHA-256 hashes and each
code is consumed once. Enabling or disabling 2FA invalidates other
sessions; disabling still requires password plus TOTP or a recovery
code. SNOWLUMA_DEV_MODE hides the feature.

The login page uses a dedicated second-factor screen (Interior OTP
cells for the 6-digit code; the same cell UI for recovery codes,
grouped as XXXX-XXXX). Settings expose enroll/confirm/disable and
one-time recovery-code display.

Fixes #351
This commit is contained in:
DrSmoothl committed 2026-08-13 01:33:57 +08:00
1 parent 679f3b5b9f
commit 5c2f620c54
23 files changed
+2676 -153

No files matched your search

@@ -0,0 +1,74 @@
import { useState } from 'react';
import { Check, Copy } from 'lucide-react';
import { Button } from '@/components/ui/button';
async function copyText(value: string): Promise<boolean> {
try {
await navigator.clipboard.writeText(value);
return true;
} catch {
try {
const ta = document.createElement('textarea');
ta.value = value;
ta.style.position = 'fixed';
ta.style.opacity = '0';
document.body.appendChild(ta);
ta.select();
const ok = document.execCommand('copy');
document.body.removeChild(ta);
return ok;
} catch {
return false;
}
}
}
export function TotpRecoveryCodes({
codes,
onConfirm,
}: {
codes: string[];
onConfirm: () => void;
}) {
const [copied, setCopied] = useState(false);
const [saved, setSaved] = useState(false);
return (
<div className="flex flex-col gap-4">
<p className="text-sm text-muted-foreground">
每个恢复码只能用一次。请立刻抄写或保存到离线位置,关闭后将无法再查看。
</p>
<ol className="grid grid-cols-2 gap-2 font-mono text-sm">
{codes.map((code) => (
<li key={code} className="rounded-md border bg-muted/40 px-3 py-2 text-center tracking-wide">
{code}
</li>
))}
</ol>
<Button
type="button"
variant="outline"
size="sm"
className="self-start"
onClick={async () => {
if (await copyText(codes.join('\n'))) setCopied(true);
}}
>
{copied ? <Check className="size-4" /> : <Copy className="size-4" />}
{copied ? '已复制' : '复制全部'}
</Button>
<label className="flex items-start gap-2 text-sm">
<input
type="checkbox"
checked={saved}
onChange={(e) => setSaved(e.target.checked)}
className="mt-0.5"
/>
我已把恢复码保存到安全的地方
</label>
<Button type="button" disabled={!saved} onClick={onConfirm}>
完成
</Button>
</div>
);
}