caelum0x 977520fc00 feat(console): Mint billing accounts, OIDC SSO for Rithub, Relay vs. OpenRouter page
- Apps tab: one-click Mint ledger account per app (POST /v1/accounts
  already existed on Mint; the gap was the UI), db/migrations/0012.
- Console is now a real OIDC provider for Rithub (Gitea) SSO: RS256
  id_token + self-contained access_token over the authorization-code
  grant, single-use codes (db/migrations/0013), discovery/jwks/
  authorize/token/userinfo routes. A visitor who isn't signed in yet
  is bounced through existing sign-in and lands back on /oidc/authorize
  automatically. Gated on RAILYARD_OIDC_SIGNING_KEY/RAILYARD_OIDC_CLIENTS
  (scripts/gen-oidc-key.mjs generates the former); unset, every /oidc/*
  route is a clean 503.
- Public, numbers-backed /compare/relay-vs-openrouter page with a
  spend-tier pricing table and a Console-signup CTA using the existing
  cross-sell attribution bridge.

Verified: tsc clean, full `npm test` (167) and `npm run test:bundle`
(3) green; a scratch script exercised the OIDC code/token/userinfo
flow end to end against real generated RSA keys (found and fixed a
missing email_verified claim); `next dev` used to curl every new
route live (discovery, jwks, authorize's 400s and sign-in detour,
token validation, userinfo auth, the comparison page, the mint-account
API).
2026-10-23 16:52:45 +03:00

51 lines
2.0 KiB
TypeScript

// Client for Mint (apps/mint)'s ledger API, used to provision a per-app
// billing account from the Apps tab (backlog item 11: "no UI, out-of-band"
// gap) instead of an operator curling `POST /v1/accounts` with the master
// key by hand. Server-only: the API key never reaches a client component.
export interface MintAccount {
accountId: string;
}
export interface MintBalance {
account_id: string;
available_micros: number;
held_micros: number;
}
export interface MintClient {
/** `POST /v1/accounts` with kind "org", owner_ref = this app's id. */
createAccount(ownerRef: string): Promise<MintAccount>;
/** `GET /v1/accounts/:id/balance`. */
balance(accountId: string): Promise<MintBalance>;
}
class HttpMintClient implements MintClient {
constructor(private base: string, private apiKey: string, private fetchImpl: typeof fetch = fetch) {}
private async req(path: string, init?: RequestInit): Promise<Response> {
return this.fetchImpl(`${this.base}${path}`, {
...init,
headers: { "content-type": "application/json", "x-api-key": this.apiKey, ...(init?.headers ?? {}) },
});
}
async createAccount(ownerRef: string): Promise<MintAccount> {
const res = await this.req("/v1/accounts", { method: "POST", body: JSON.stringify({ kind: "org", owner_ref: ownerRef }) });
if (!res.ok) throw new Error(`mint account create failed with ${res.status}`);
const body = (await res.json()) as { account_id?: string };
if (!body.account_id) throw new Error("mint returned no account_id");
return { accountId: body.account_id };
}
async balance(accountId: string): Promise<MintBalance> {
const res = await this.req(`/v1/accounts/${encodeURIComponent(accountId)}/balance`);
if (!res.ok) throw new Error(`mint balance lookup failed with ${res.status}`);
return (await res.json()) as MintBalance;
}
}
export function mintClient(opts: { url: string; apiKey: string; fetchImpl?: typeof fetch }): MintClient {
return new HttpMintClient(opts.url.replace(/\/$/, ""), opts.apiKey, opts.fetchImpl);
}