picoclaw/web/frontend/src/api/system.ts
zeed zhao 6ea364e67d
feat(web): protect launcher dashboard with token and SPA login (#1953)
Add token-based authentication for the Launcher's embedded Web Dashboard.

- Ephemeral token generated in-memory each run (or via PICOCLAW_LAUNCHER_TOKEN env var)
- HMAC-SHA256 session cookie (HttpOnly, SameSite=Lax, Secure when HTTPS)
- Bearer token support for API/script access
- Rate limiting on login (10 attempts/IP/min)
- Referrer-Policy: no-referrer on all responses
- POST-only logout with JSON content-type (CSRF-safe)
- System tray "Copy dashboard token" action
- Login page shows contextual help (console/tray/log file path)
- Path traversal protection via path.Clean
- X-Forwarded-Host/Port/Proto support for reverse proxy deployments
- Full i18n support (English, Chinese)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-29 13:11:43 +08:00

64 lines
1.7 KiB
TypeScript

import { launcherFetch } from "@/api/http"
export interface AutoStartStatus {
enabled: boolean
supported: boolean
platform: string
message?: string
}
export interface LauncherConfig {
port: number
public: boolean
allowed_cidrs: string[]
}
async function request<T>(path: string, options?: RequestInit): Promise<T> {
const res = await launcherFetch(path, options)
if (!res.ok) {
let message = `API error: ${res.status} ${res.statusText}`
try {
const body = (await res.json()) as {
error?: string
errors?: string[]
}
if (Array.isArray(body.errors) && body.errors.length > 0) {
message = body.errors.join("; ")
} else if (typeof body.error === "string" && body.error.trim() !== "") {
message = body.error
}
} catch {
// Keep fallback error message when response body is not JSON.
}
throw new Error(message)
}
return res.json() as Promise<T>
}
export async function getAutoStartStatus(): Promise<AutoStartStatus> {
return request<AutoStartStatus>("/api/system/autostart")
}
export async function setAutoStartEnabled(
enabled: boolean,
): Promise<AutoStartStatus> {
return request<AutoStartStatus>("/api/system/autostart", {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ enabled }),
})
}
export async function getLauncherConfig(): Promise<LauncherConfig> {
return request<LauncherConfig>("/api/system/launcher-config")
}
export async function setLauncherConfig(
payload: LauncherConfig,
): Promise<LauncherConfig> {
return request<LauncherConfig>("/api/system/launcher-config", {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(payload),
})
}