mirror of
https://github.com/Such-Software/smirk-backend-core.git
synced 2026-11-18 22:35:15 +01:00
feat(admin): serve Operator Console SPA at /admin (rust-embed)
Preact + Vite standalone admin-ui bundled into the server binary via rust-embed. Login is NIP-07 (Smirk wallet signs the NIP-98 admin challenge); tabs cover instance status, admin keys, invite codes, and the effective-config editor (live vs restart-required per field). admin-ui/dist is committed because rust-embed reads it at compile time; node_modules is gitignored (npm ci reproduces it). A lib test asserts index.html and the /admin/ asset base are embedded so an unbuilt console fails the build instead of an operator. The console is served on the admin plane only (loopback bind), behind the existing NIP-98 admin_guard + MAC allowlist.
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
// Fetch layer for the /admin JSON API. Same-origin (the SPA is served from the admin
|
||||
// plane), bearer access token held in memory only (never localStorage). Admin routes
|
||||
// are outside the public OpenAPI, so this is hand-written.
|
||||
|
||||
let accessToken: string | null = null;
|
||||
|
||||
export function setToken(t: string | null): void {
|
||||
accessToken = t;
|
||||
}
|
||||
export function hasToken(): boolean {
|
||||
return accessToken !== null;
|
||||
}
|
||||
|
||||
async function req(method: string, path: string, body?: unknown): Promise<unknown> {
|
||||
const headers: Record<string, string> = {};
|
||||
if (accessToken) headers['Authorization'] = `Bearer ${accessToken}`;
|
||||
if (body !== undefined) headers['Content-Type'] = 'application/json';
|
||||
const res = await fetch(path, {
|
||||
method,
|
||||
headers,
|
||||
body: body !== undefined ? JSON.stringify(body) : undefined,
|
||||
});
|
||||
const text = await res.text();
|
||||
const data: unknown = text ? JSON.parse(text) : null;
|
||||
if (!res.ok) {
|
||||
const msg =
|
||||
(data as { error?: string; message?: string })?.error ??
|
||||
(data as { message?: string })?.message ??
|
||||
`${res.status} ${res.statusText}`;
|
||||
throw new Error(msg);
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
export interface ChallengeResp {
|
||||
challenge: string;
|
||||
url: string;
|
||||
instance_id: string;
|
||||
expires_in: number;
|
||||
}
|
||||
export interface TokenResp {
|
||||
access_token: string;
|
||||
refresh_token?: string;
|
||||
expires_in: number;
|
||||
}
|
||||
export interface KeyRow {
|
||||
id: string;
|
||||
pubkey: string;
|
||||
status: string;
|
||||
}
|
||||
export interface InviteView {
|
||||
code_prefix: string;
|
||||
label: string | null;
|
||||
created_at: string;
|
||||
status: string;
|
||||
}
|
||||
|
||||
export const api = {
|
||||
challenge: () => req('GET', '/admin/auth/challenge') as Promise<ChallengeResp>,
|
||||
verify: (admin_token: string, challenge: string) =>
|
||||
req('POST', '/admin/auth/verify', { admin_token, challenge }) as Promise<TokenResp>,
|
||||
logout: () => req('POST', '/admin/auth/logout', {}),
|
||||
me: () => req('GET', '/admin/me'),
|
||||
features: () => req('GET', '/admin/features'),
|
||||
keys: () => req('GET', '/admin/keys') as Promise<KeyRow[]>,
|
||||
addKey: (pubkey: string, label?: string) =>
|
||||
req('POST', '/admin/keys', label ? { pubkey, label } : { pubkey }),
|
||||
revokeKey: (id: string) => req('DELETE', `/admin/keys/${id}`),
|
||||
config: () => req('GET', '/admin/config'),
|
||||
putConfig: (patch: unknown, expected_versions: Record<string, number>) =>
|
||||
req('PUT', '/admin/config', { patch, expected_versions }),
|
||||
invites: () =>
|
||||
req('GET', '/admin/invites') as Promise<{ invites: InviteView[]; unused_count: number }>,
|
||||
mintInvites: (count: number, label?: string) =>
|
||||
req('POST', '/admin/invites', label ? { count, label } : { count }) as Promise<{
|
||||
codes: string[];
|
||||
}>,
|
||||
};
|
||||
Reference in New Issue
Block a user