railyard/apps/console/db/migrations/0013_oidc_codes.sql
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

36 lines
1.6 KiB
SQL

-- Console as an OIDC provider (backlog item 5, closes apps/gitea's
-- TODO(railyard-sso)): single-use authorization codes for the authorization
-- code grant served at /oidc/authorize + /oidc/token. Idempotent; not
-- applied by agents to any hosted database (owner step), agents only run it
-- against a scratch Postgres.
create table if not exists railyard.oidc_codes (
code_hash text primary key,
client_id text not null check (length(client_id) between 1 and 100),
redirect_uri text not null check (redirect_uri ~ '^https://' and length(redirect_uri) <= 500),
org_id uuid not null references railyard.orgs (id) on delete cascade,
user_id text not null check (length(user_id) between 1 and 200),
email text not null check (length(email) between 3 and 254),
nonce text check (nonce is null or length(nonce) <= 200),
scope text not null check (length(scope) between 1 and 200),
created_at timestamptz not null default now(),
expires_at timestamptz not null,
consumed_at timestamptz
);
create index if not exists oidc_codes_expires_idx on railyard.oidc_codes (expires_at);
alter table railyard.oidc_codes enable row level security;
do $$
begin
if exists (select 1 from pg_roles where rolname = 'anon') then
execute 'revoke all on railyard.oidc_codes from anon';
end if;
if exists (select 1 from pg_roles where rolname = 'authenticated') then
execute 'revoke all on railyard.oidc_codes from authenticated';
end if;
if exists (select 1 from pg_roles where rolname = 'service_role') then
execute 'grant all on railyard.oidc_codes to service_role';
end if;
end $$;