railyard/apps/console/scripts/gen-oidc-key.mjs
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

26 lines
1.1 KiB
JavaScript

#!/usr/bin/env node
// One-time key generation for Console's OIDC provider (backlog item 5).
// Run once, then set the printed value as the RAILYARD_OIDC_SIGNING_KEY
// secret (never commit it). No dependency: Node's built-in WebCrypto.
//
// node apps/console/scripts/gen-oidc-key.mjs > /tmp/oidc-key.json
// # paste the single-line JSON into RAILYARD_OIDC_SIGNING_KEY
//
// The same WebCrypto JWK import (crypto.subtle.importKey("jwk", ...)) the
// console's own OIDC routes use at request time (lib/oidc.ts) reads this
// back; the public n/e/kid are re-derived from it for /oidc/jwks, so only
// this one private value needs to be stored.
import { webcrypto as crypto } from "node:crypto";
const pair = await crypto.subtle.generateKey(
{ name: "RSASSA-PKCS1-v1_5", modulusLength: 2048, publicExponent: new Uint8Array([1, 0, 1]), hash: "SHA-256" },
true,
["sign", "verify"],
);
const jwk = await crypto.subtle.exportKey("jwk", pair.privateKey);
jwk.kid = `railyard-oidc-${Date.now().toString(36)}`;
jwk.alg = "RS256";
jwk.use = "sig";
process.stdout.write(`${JSON.stringify(jwk)}\n`);