Lint / Lint (Node/Svelte) (push) Successful in 24s
Lint / Lint (Go) (push) Successful in 28s
Lint / Lint (Python) (push) Failing after 14s
Tests / Test (Node/Svelte) (push) Successful in 18s
Tests / Test (Go) (push) Successful in 25s
Tests / Test (Python) (push) Successful in 12s
- Added TOTP support for two-factor authentication in the auth module. - Introduced new types and interfaces for TOTP setup and token actions in the web UI. - Created backend logic for generating and verifying TOTP secrets and tokens. - Implemented user management features including role assignments and user CRUD operations. - Enhanced UI components for managing users and TOTP settings. - Added styles for optional labels and authentication reset links. - Implemented reset code generation and validation for admin accounts.
168 lines
4.4 KiB
Svelte
168 lines
4.4 KiB
Svelte
<script lang="ts">
|
|
import type {
|
|
TotpSetupRequest,
|
|
TotpStatusRequest,
|
|
TotpTokenAction,
|
|
TotpSetupResponse
|
|
} from '../types';
|
|
import Button from './Button.svelte';
|
|
|
|
let {
|
|
enabled = false,
|
|
onDisable,
|
|
onEnable,
|
|
onSetup,
|
|
onStatus,
|
|
open = false
|
|
}: {
|
|
enabled?: boolean;
|
|
onDisable?: TotpTokenAction;
|
|
onEnable?: TotpTokenAction;
|
|
onSetup?: TotpSetupRequest;
|
|
onStatus?: TotpStatusRequest;
|
|
open?: boolean;
|
|
} = $props();
|
|
|
|
let currentEnabled = $state(false);
|
|
let setup = $state<TotpSetupResponse | null>(null);
|
|
let token = $state('');
|
|
let error = $state('');
|
|
let busy = $state(false);
|
|
|
|
$effect(() => {
|
|
currentEnabled = enabled;
|
|
});
|
|
|
|
$effect(() => {
|
|
if (!open || !onStatus) return;
|
|
void refreshStatus();
|
|
});
|
|
|
|
async function refreshStatus(): Promise<void> {
|
|
try {
|
|
currentEnabled = await onStatus?.() ?? currentEnabled;
|
|
} catch {
|
|
// The explicit setup controls remain usable if status is unavailable.
|
|
}
|
|
}
|
|
|
|
async function beginSetup(): Promise<void> {
|
|
if (!onSetup) return;
|
|
error = '';
|
|
busy = true;
|
|
try {
|
|
setup = await onSetup();
|
|
token = '';
|
|
} catch (caught) {
|
|
error = caught instanceof Error ? caught.message : 'Unable to start two-factor setup.';
|
|
} finally {
|
|
busy = false;
|
|
}
|
|
}
|
|
|
|
async function confirmEnable(): Promise<void> {
|
|
const normalized = token.trim();
|
|
if (!/^\d{6}$/.test(normalized)) {
|
|
error = 'Enter the six-digit code from your authenticator app.';
|
|
return;
|
|
}
|
|
if (!onEnable) return;
|
|
error = '';
|
|
busy = true;
|
|
try {
|
|
await onEnable(normalized);
|
|
currentEnabled = true;
|
|
setup = null;
|
|
token = '';
|
|
} catch (caught) {
|
|
error = caught instanceof Error ? caught.message : 'Unable to enable two-factor authentication.';
|
|
} finally {
|
|
busy = false;
|
|
}
|
|
}
|
|
|
|
async function disable(): Promise<void> {
|
|
const normalized = token.trim();
|
|
if (!/^\d{6}$/.test(normalized)) {
|
|
error = 'Enter the current six-digit authenticator code.';
|
|
return;
|
|
}
|
|
if (!onDisable) return;
|
|
error = '';
|
|
busy = true;
|
|
try {
|
|
await onDisable(normalized);
|
|
currentEnabled = false;
|
|
token = '';
|
|
} catch (caught) {
|
|
error = caught instanceof Error ? caught.message : 'Unable to disable two-factor authentication.';
|
|
} finally {
|
|
busy = false;
|
|
}
|
|
}
|
|
</script>
|
|
|
|
<section class="sc-totp-settings" aria-labelledby="sc-totp-title">
|
|
<div class="sc-totp-heading">
|
|
<div>
|
|
<h3 id="sc-totp-title">Two-factor authentication</h3>
|
|
<p class="sc-muted">Protect sign-in with a time-based authenticator code.</p>
|
|
</div>
|
|
<span class:sc-totp-enabled={currentEnabled} class="sc-totp-status">
|
|
{currentEnabled ? 'Enabled' : 'Optional'}
|
|
</span>
|
|
</div>
|
|
|
|
{#if currentEnabled}
|
|
<label class="sc-field">
|
|
<span class="sc-label">Current authenticator code</span>
|
|
<input
|
|
autocomplete="one-time-code"
|
|
class="sc-input"
|
|
inputmode="numeric"
|
|
maxlength="6"
|
|
pattern="[0-9]{6}"
|
|
placeholder="123456"
|
|
type="text"
|
|
bind:value={token}
|
|
/>
|
|
</label>
|
|
<Button disabled={busy} variant="danger" onclick={() => void disable()}>
|
|
{busy ? 'Disabling…' : 'Disable two-factor authentication'}
|
|
</Button>
|
|
{:else if setup}
|
|
<div class="sc-totp-secret">
|
|
<p>Scan this address in your authenticator app, or enter the secret manually.</p>
|
|
<code>{setup.otpauthUrl}</code>
|
|
<p><span class="sc-label">Manual secret:</span> <code>{setup.secret}</code></p>
|
|
</div>
|
|
<label class="sc-field">
|
|
<span class="sc-label">Authenticator code</span>
|
|
<input
|
|
autocomplete="one-time-code"
|
|
class="sc-input"
|
|
inputmode="numeric"
|
|
maxlength="6"
|
|
pattern="[0-9]{6}"
|
|
placeholder="123456"
|
|
type="text"
|
|
bind:value={token}
|
|
/>
|
|
</label>
|
|
<div class="sc-form-actions">
|
|
<Button disabled={busy} variant="secondary" onclick={() => (setup = null)}>Cancel</Button>
|
|
<Button disabled={busy} onclick={() => void confirmEnable()}>
|
|
{busy ? 'Enabling…' : 'Enable two-factor authentication'}
|
|
</Button>
|
|
</div>
|
|
{:else}
|
|
<Button disabled={busy} onclick={() => void beginSetup()}>
|
|
{busy ? 'Preparing…' : 'Set up authenticator'}
|
|
</Button>
|
|
{/if}
|
|
|
|
{#if error}
|
|
<p aria-live="polite" class="sc-error">{error}</p>
|
|
{/if}
|
|
</section>
|