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

32 lines
1.7 KiB
TypeScript

import { deps } from "../../../lib/deps.ts";
import { cookie, readCookie } from "../../../lib/http.ts";
import { finishSignIn, PKCE_COOKIE } from "../../../lib/scribase-auth.ts";
import { normalizeSrc, OIDC_RETURN_COOKIE, SESSION_COOKIE, SESSION_TTL_SECONDS, SRC_COOKIE } from "../../../lib/session.ts";
import { completeSignIn } from "../../../lib/signin.ts";
import { postSignInLocation } from "../../../lib/post-signin.ts";
export const dynamic = "force-dynamic";
export async function GET(req: Request) {
const { cfg, repo } = deps();
const code = new URL(req.url).searchParams.get("code");
const verifier = readCookie(req, PKCE_COOKIE);
const src = normalizeSrc(readCookie(req, SRC_COOKIE));
const back = (q: string) => new Response(null, { status: 302, headers: { location: `${cfg.consoleUrl}/?${q}`, "set-cookie": cookie(PKCE_COOKIE, "", 0) } });
if (!code || !verifier) return back("signin=expired");
try {
const user = await finishSignIn({ scribaseUrl: cfg.scribaseUrl, anonKey: cfg.scribaseAnonKey, code, verifier });
const { cookie: session } = await completeSignIn(repo, cfg.sessionSecret, user, new Date(), src);
const { location, clearCookie } = postSignInLocation(cfg.consoleUrl, readCookie(req, OIDC_RETURN_COOKIE));
const headers = new Headers({ location });
headers.append("set-cookie", cookie(SESSION_COOKIE, session, SESSION_TTL_SECONDS));
headers.append("set-cookie", cookie(PKCE_COOKIE, "", 0));
headers.append("set-cookie", cookie(SRC_COOKIE, "", 0));
headers.append("set-cookie", clearCookie);
return new Response(null, { status: 302, headers });
} catch (err) {
console.error(`sign-in failed: ${(err as Error).message}`);
return back("signin=failed");
}
}