manifest, one table, a router and two panels. Discovery is by convention, so
this is the point at which the directory stops being a directory and becomes
something Officer can install.
The manifest holds only what a listing cannot say. One permission, grantable,
with ownerOnly on /_config — reading and editing the site is exactly what a
member might be brought in for, but the token is unrestricted write authority
over everything the public sees, so handing it over is the owner's.
db/schema.ts is ONE table and should stay that way. fofos owns its own Postgres
and its own media store; this holds a URL and a token. That is what makes the
strongest thing about this plugin true: no operation on it can destroy the
client's work, checkbox ticked or not, because none of it was ever here. A cache
would be the thin end of that.
The router mostly forwards. fofos's /api/admin already answers in the shape a
panel wants, so re-wrapping it would be a second place for the contract to drift.
Two things in it are load-bearing rather than style:
registerProxiedPrefix('/api/fofos-admin/media'). bodyParser calls parseBody on
multipart across the whole protected router, and Hono then caches the parsed
FormData — so asking for the bytes afterwards re-serialises them under a NEW
boundary while the ORIGINAL content-type header is what gets forwarded. Header
and body disagree and every upload fails with "Unexpected end of form".
Narrow to the one route: everything else is JSON that is fine to parse.
The catch-all is LAST in the file. Hono runs matching handlers in registration
order, so /* above /_config would swallow it and the settings form would
forward itself to fofos, which has no such endpoint — a 404 from a plugin that
is working perfectly.
Panels are a nav and a detail pane that never speak to each other: the nav writes
?section=, the detail pane reads it. Real <Link>s rather than the buttons
proxy-manager uses — AGENTS.md says nav items are links, and the file documenting
that pattern says not to copy the older one. <Link> and not <NavLink> because
active state here is a query parameter and NavLink compares paths, which would
light every row at once.
Which component renders which section is a MAP, not a conditional on `built`.
The first version read `built ? <MessagesPanel/> : <Planned/>`, which is correct
only while exactly one section exists — the second one finished would have
rendered Messages under its own name.
Messages is the only section built, chosen because it is one read-only route: it
proves the path from an Officer panel through this proxy to fofos and back with
nothing else able to be wrong at the same time. The other eight are in the nav,
dimmed and labelled, saying what they will do and that the old admin still edits
them. A nav that hides what is unfinished gives no sense of what the thing is for.
Typechecked against the platform — and the typechecker was verified to actually
cover plugins/ by planting an error and watching it fail, because a clean run
that does not include your files is worse than no run.
NOT yet: installed, or run against a live fofos. Nothing here has been through
the permission gate or a real request.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
108 lines
3.4 KiB
TypeScript
108 lines
3.4 KiB
TypeScript
import { db } from 'officerdb/db';
|
|
import { encryptSecret, decryptSecret } from 'officerdb/crypto';
|
|
import { eq } from 'drizzle-orm';
|
|
import { fofosAdminConnection, type FofosAdminConnection } from '../db/schema';
|
|
import { normaliseUrl, type FofosCredentials } from './fofos-client';
|
|
|
|
/**
|
|
* Reading and writing the one connection row.
|
|
*
|
|
* The token is encrypted under this plugin's own purpose key, so a leak of that key opens this column
|
|
* and nothing else — the per-purpose split the platform's secret store already decided on.
|
|
*
|
|
* **The token never leaves this module in plaintext except into `fofosRequest`.** No route returns
|
|
* it, at any permission level, to anybody, including the owner. A form that wants to show "a token is
|
|
* set" gets a boolean.
|
|
*/
|
|
|
|
const PURPOSE = 'fofos-admin';
|
|
|
|
export type ConnectionView = {
|
|
configured: boolean;
|
|
url: string | null;
|
|
/** Never the token itself. Only whether one is stored. */
|
|
hasToken: boolean;
|
|
sections: string[] | null;
|
|
lastProbeAt: string | null;
|
|
lastProbeOk: boolean | null;
|
|
};
|
|
|
|
const EMPTY: ConnectionView = {
|
|
configured: false,
|
|
url: null,
|
|
hasToken: false,
|
|
sections: null,
|
|
lastProbeAt: null,
|
|
lastProbeOk: null,
|
|
};
|
|
|
|
const row = async (): Promise<FofosAdminConnection | null> => {
|
|
const rows = await db.select().from(fofosAdminConnection).limit(1);
|
|
return rows[0] ?? null;
|
|
};
|
|
|
|
export async function readConnection(): Promise<ConnectionView> {
|
|
const found = await row();
|
|
if (!found) return EMPTY;
|
|
|
|
return {
|
|
configured: true,
|
|
url: found.url,
|
|
hasToken: found.token.length > 0,
|
|
sections: found.sections ?? null,
|
|
lastProbeAt: found.lastProbeAt ? found.lastProbeAt.toISOString() : null,
|
|
lastProbeOk: found.lastProbeOk === null ? null : found.lastProbeOk === 1,
|
|
};
|
|
}
|
|
|
|
/** Decrypted, for the client only. Never serialise the result of this. */
|
|
export async function readCredentials(): Promise<FofosCredentials | null> {
|
|
const found = await row();
|
|
if (!found) return null;
|
|
return { url: found.url, token: decryptSecret(PURPOSE, found.token) };
|
|
}
|
|
|
|
export type SaveParams = { url: string; token?: string };
|
|
|
|
/**
|
|
* `token` omitted means "keep the stored one".
|
|
*
|
|
* So a typo in the URL can be corrected without going to find the token again, and a settings form
|
|
* never has to round-trip a secret in order to send it back.
|
|
*/
|
|
export async function saveConnection(params: SaveParams): Promise<ConnectionView> {
|
|
const url = normaliseUrl(params.url);
|
|
const existing = await row();
|
|
|
|
if (!existing && !params.token) {
|
|
throw new Error('A token is required the first time the connection is set.');
|
|
}
|
|
|
|
const token = params.token ? encryptSecret(PURPOSE, params.token) : (existing?.token as string);
|
|
|
|
if (existing) {
|
|
await db
|
|
.update(fofosAdminConnection)
|
|
.set({ url, token, updatedAt: new Date() })
|
|
.where(eq(fofosAdminConnection.id, existing.id));
|
|
} else {
|
|
await db.insert(fofosAdminConnection).values({ url, token });
|
|
}
|
|
|
|
return readConnection();
|
|
}
|
|
|
|
export async function deleteConnection(): Promise<void> {
|
|
await db.delete(fofosAdminConnection);
|
|
}
|
|
|
|
export async function recordProbe(sections: string[] | null, ok: boolean): Promise<void> {
|
|
const existing = await row();
|
|
if (!existing) return;
|
|
|
|
await db
|
|
.update(fofosAdminConnection)
|
|
.set({ sections, lastProbeAt: new Date(), lastProbeOk: ok ? 1 : 0 })
|
|
.where(eq(fofosAdminConnection.id, existing.id));
|
|
}
|