Files
SnowLuma/packages/core/tests/webui-login.test.ts
T
DrSmoothl 5c2f620c54 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
2026-08-13 01:33:57 +08:00

89 lines
2.7 KiB
TypeScript

import { describe, expect, it } from 'vitest';
import { confirmTotpEnrollment } from '../src/webui/totp';
import { completeWebuiLogin, invalidateOtherSessions, type WebuiLoginAuth } from '../src/webui/webui-login';
const RFC_SECRET = 'GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ';
const T59_MS = 59_000;
const T59_CODE = '287082';
const PASSWORD = 'Correct-Horse-1!';
function authWithoutTotp(): WebuiLoginAuth {
return {
verify: (password) => password === PASSWORD,
totpEnabled: () => false,
totpState: () => undefined,
mustChangePassword: () => false,
};
}
describe('completeWebuiLogin', () => {
it('rejects a wrong password without mentioning TOTP', () => {
expect(completeWebuiLogin(authWithoutTotp(), { password: 'nope' }, T59_MS)).toEqual({
kind: 'bad-password',
});
});
it('logs in with only a password when 2FA is off', () => {
expect(completeWebuiLogin(authWithoutTotp(), { password: PASSWORD }, T59_MS)).toEqual({
kind: 'ok',
mustChangePassword: false,
});
});
it('asks for TOTP after a correct password when 2FA is on', () => {
const enabled = confirmTotpEnrollment({
password: PASSWORD,
secret: RFC_SECRET,
code: T59_CODE,
label: 'SnowLuma (testhost)',
atMs: T59_MS,
});
const auth: WebuiLoginAuth = {
verify: (password) => password === PASSWORD,
totpEnabled: () => true,
totpState: () => enabled.state,
mustChangePassword: () => false,
};
expect(completeWebuiLogin(auth, { password: PASSWORD }, T59_MS)).toEqual({
kind: 'needs-totp',
});
});
it('accepts password plus TOTP and returns the updated totp state', () => {
const enabled = confirmTotpEnrollment({
password: PASSWORD,
secret: RFC_SECRET,
code: T59_CODE,
label: 'SnowLuma (testhost)',
atMs: T59_MS,
});
const auth: WebuiLoginAuth = {
verify: (password) => password === PASSWORD,
totpEnabled: () => true,
totpState: () => enabled.state,
mustChangePassword: () => true,
};
const result = completeWebuiLogin(
auth,
{ password: PASSWORD, totp: '081804' },
1_111_111_109_000,
);
expect(result.kind).toBe('ok');
if (result.kind !== 'ok') return;
expect(result.mustChangePassword).toBe(true);
expect(result.totpState?.lastUsedStep).toBeGreaterThan(enabled.state.lastUsedStep);
});
});
describe('invalidateOtherSessions', () => {
it('keeps the current session and drops the rest', () => {
const sessions = new Map([
['keep', { n: 1 }],
['drop-a', { n: 2 }],
['drop-b', { n: 3 }],
]);
invalidateOtherSessions(sessions, 'keep');
expect([...sessions.keys()]).toEqual(['keep']);
});
});