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:
23 files changed
+2676
-153
No files matched your search
@@ -0,0 +1,51 @@
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest';
|
||||
import { createApiClient } from '../src/lib/api/client';
|
||||
|
||||
const tokenStore = {
|
||||
load: () => null as string | null,
|
||||
save: vi.fn(),
|
||||
};
|
||||
|
||||
afterEach(() => {
|
||||
vi.unstubAllGlobals();
|
||||
tokenStore.save.mockReset();
|
||||
});
|
||||
|
||||
describe('login 2FA client', () => {
|
||||
it('returns needsTotp without storing a session token', async () => {
|
||||
vi.stubGlobal('fetch', vi.fn(async () => new Response(JSON.stringify({
|
||||
success: false,
|
||||
needsTotp: true,
|
||||
}), {
|
||||
status: 200,
|
||||
headers: { 'content-type': 'application/json' },
|
||||
})));
|
||||
|
||||
const client = createApiClient({ tokenStore });
|
||||
await expect(client.login('Correct-Horse-1!')).resolves.toEqual({ ok: false, needsTotp: true });
|
||||
expect(tokenStore.save).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('sends the TOTP on the second login request and stores the session', async () => {
|
||||
const fetchMock = vi.fn(async () => new Response(JSON.stringify({
|
||||
success: true,
|
||||
token: 'session-token',
|
||||
mustChangePassword: false,
|
||||
}), {
|
||||
status: 200,
|
||||
headers: { 'content-type': 'application/json' },
|
||||
}));
|
||||
vi.stubGlobal('fetch', fetchMock);
|
||||
|
||||
const client = createApiClient({ tokenStore });
|
||||
await expect(client.login('Correct-Horse-1!', { totp: '287082' })).resolves.toEqual({
|
||||
ok: true,
|
||||
mustChangePassword: false,
|
||||
});
|
||||
expect(fetchMock).toHaveBeenCalledWith('/api/login', expect.objectContaining({
|
||||
method: 'POST',
|
||||
body: JSON.stringify({ password: 'Correct-Horse-1!', totp: '287082' }),
|
||||
}));
|
||||
expect(tokenStore.save).toHaveBeenCalledWith('session-token');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,72 @@
|
||||
import { renderToStaticMarkup } from 'react-dom/server';
|
||||
import { describe, expect, it, vi } from 'vitest';
|
||||
import { LoginPage } from '../src/components/pages/login-page';
|
||||
|
||||
vi.mock('@/contexts/ThemeContext', () => ({
|
||||
useTheme: () => ({
|
||||
appearance: {
|
||||
background: { type: 'none' },
|
||||
reduceMotion: true,
|
||||
disableMotion: true,
|
||||
palette: 'default',
|
||||
},
|
||||
mode: 'light',
|
||||
setMode: () => undefined,
|
||||
}),
|
||||
paletteResolved: () => false,
|
||||
}));
|
||||
|
||||
describe('LoginPage 2FA step', () => {
|
||||
it('keeps the password form on its own screen', () => {
|
||||
const markup = renderToStaticMarkup(
|
||||
<LoginPage onLogin={async () => ({ success: false, error: 'no' })} />,
|
||||
);
|
||||
expect(markup).toContain('输入访问令牌');
|
||||
expect(markup).toContain('进入控制台');
|
||||
expect(markup).toContain('max-w-md');
|
||||
expect(markup).not.toContain('max-w-2xl');
|
||||
expect(markup).not.toContain('character 1 of 6');
|
||||
expect(markup).not.toContain('验证并进入');
|
||||
});
|
||||
|
||||
it('shows a separate TOTP screen after the password factor succeeds', () => {
|
||||
const markup = renderToStaticMarkup(
|
||||
<LoginPage
|
||||
initialPassword="Correct-Horse-1!"
|
||||
initialNeedsTotp
|
||||
onLogin={async () => ({ success: false, needsTotp: true })}
|
||||
/>,
|
||||
);
|
||||
expect(markup).not.toContain('输入访问令牌');
|
||||
expect(markup).toContain('输入 Authenticator 中的 6 位验证码');
|
||||
expect(markup).not.toContain('验证并进入');
|
||||
expect(markup).toContain('character 1 of 6');
|
||||
expect(markup).toContain('可以把完整验证码粘贴到任意一格。');
|
||||
expect(markup).toContain('使用恢复码');
|
||||
expect(markup).toContain('返回');
|
||||
expect(markup).toContain('max-w-md');
|
||||
expect(markup).not.toContain('max-w-2xl');
|
||||
expect(markup).not.toContain('h-[2px] w-3 rounded-full');
|
||||
});
|
||||
|
||||
it('uses the same cell UI for a recovery code instead of a single field', () => {
|
||||
const markup = renderToStaticMarkup(
|
||||
<LoginPage
|
||||
initialPassword="Correct-Horse-1!"
|
||||
initialNeedsTotp
|
||||
initialUseRecovery
|
||||
onLogin={async () => ({ success: false, needsTotp: true })}
|
||||
/>,
|
||||
);
|
||||
expect(markup).toContain('输入保存的一次性恢复码');
|
||||
expect(markup).toContain('character 1 of 8');
|
||||
expect(markup).toContain('character 8 of 8');
|
||||
expect(markup).toContain('可以把完整恢复码粘贴到任意一格');
|
||||
expect(markup).toContain('改用验证码');
|
||||
expect(markup).toContain('max-w-2xl');
|
||||
expect(markup).toContain('h-[2px] w-3 rounded-full');
|
||||
expect(markup).not.toContain('XXXX-XXXX');
|
||||
expect(markup).not.toContain('验证并进入');
|
||||
expect(markup).not.toContain('输入访问令牌');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,31 @@
|
||||
import { renderToStaticMarkup } from 'react-dom/server';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { OtpInput } from '../src/components/interior/otp-input';
|
||||
|
||||
describe('OtpInput official error/hint contract', () => {
|
||||
it('shows the hint while error is false', () => {
|
||||
const markup = renderToStaticMarkup(
|
||||
<OtpInput
|
||||
label="验证码"
|
||||
hint="可以把完整验证码粘贴到任意一格。"
|
||||
error={false}
|
||||
errorMessage="验证码不正确,改错的那一位即可。"
|
||||
/>,
|
||||
);
|
||||
expect(markup).toContain('可以把完整验证码粘贴到任意一格。');
|
||||
expect(markup).not.toContain('验证码不正确,改错的那一位即可。');
|
||||
});
|
||||
|
||||
it('swaps to the error message when error is true', () => {
|
||||
const markup = renderToStaticMarkup(
|
||||
<OtpInput
|
||||
label="验证码"
|
||||
hint="可以把完整验证码粘贴到任意一格。"
|
||||
error
|
||||
errorMessage="验证码不正确,改错的那一位即可。"
|
||||
/>,
|
||||
);
|
||||
expect(markup).toContain('验证码不正确,改错的那一位即可。');
|
||||
expect(markup).toContain('aria-invalid="true"');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,12 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { parseSecondFactor } from '../src/lib/totp-second-factor';
|
||||
|
||||
describe('parseSecondFactor', () => {
|
||||
it('treats a 6-digit code as TOTP, ignoring spaces', () => {
|
||||
expect(parseSecondFactor('287 082')).toEqual({ totp: '287082' });
|
||||
});
|
||||
|
||||
it('treats a recovery code as a recovery code', () => {
|
||||
expect(parseSecondFactor('AB3D-K7MQ')).toEqual({ recoveryCode: 'AB3D-K7MQ' });
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user