- 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).
70 lines
2.7 KiB
TypeScript
70 lines
2.7 KiB
TypeScript
// Signed session cookie: base64url(JSON).base64url(HMAC-SHA256). Server-only.
|
|
|
|
import { b64url, constantTimeEqual, hmacSha256 } from "./crypto.ts";
|
|
|
|
export const SESSION_COOKIE = "ry_session";
|
|
export const SESSION_TTL_SECONDS = 7 * 24 * 3600;
|
|
|
|
/**
|
|
* First-touch signup attribution (console cross-sell bridge, RYP-X1): a short
|
|
* lived cookie set when a sign-in link carries `?src=<slug>` (for example the
|
|
* Insights dashboard banner linking to `/auth/start?email=&src=insights`).
|
|
* Read once at the moment a *new* org is created, written to its
|
|
* `signup_source` column, then discarded; it never changes which org an
|
|
* existing user signs into.
|
|
*/
|
|
export const SRC_COOKIE = "ry_src";
|
|
export const SRC_COOKIE_TTL_SECONDS = 1800;
|
|
|
|
/** A bounded, lower-case attribution slug (e.g. "insights"), or null. */
|
|
export function normalizeSrc(raw: string | null | undefined): string | null {
|
|
if (typeof raw !== "string") return null;
|
|
const src = raw.trim().toLowerCase();
|
|
return /^[a-z0-9_-]{1,40}$/.test(src) ? src : null;
|
|
}
|
|
|
|
/**
|
|
* OIDC provider (backlog item 5): holds the original, already-validated
|
|
* `/oidc/authorize` query string across a sign-in detour so a visitor who
|
|
* was not signed in yet lands back on `/oidc/authorize` (and gets their
|
|
* authorization code) right after completing email or OAuth sign-in,
|
|
* instead of at the default `/apps`. Re-validated on return -- this cookie
|
|
* only ever carries a query string this same handler already checked.
|
|
*/
|
|
export const OIDC_RETURN_COOKIE = "ry_oidc_return";
|
|
export const OIDC_RETURN_TTL_SECONDS = 600;
|
|
|
|
export interface Session {
|
|
uid: string;
|
|
email: string;
|
|
org: string;
|
|
exp: number;
|
|
}
|
|
|
|
const enc = new TextEncoder();
|
|
const dec = new TextDecoder();
|
|
|
|
function fromB64url(s: string): string {
|
|
const pad = s.replace(/-/g, "+").replace(/_/g, "/");
|
|
return dec.decode(Uint8Array.from(atob(pad + "===".slice((pad.length + 3) % 4)), (c) => c.charCodeAt(0)));
|
|
}
|
|
|
|
export async function sealSession(secret: string, s: Omit<Session, "exp">, now: Date): Promise<string> {
|
|
const body = b64url(enc.encode(JSON.stringify({ ...s, exp: Math.floor(now.getTime() / 1000) + SESSION_TTL_SECONDS })));
|
|
return `${body}.${b64url(await hmacSha256(`${secret}:session`, body))}`;
|
|
}
|
|
|
|
export async function openSession(secret: string, cookie: string | undefined | null, now: Date): Promise<Session | null> {
|
|
if (!cookie) return null;
|
|
const [body, mac] = cookie.split(".");
|
|
if (!body || !mac) return null;
|
|
if (!constantTimeEqual(mac, b64url(await hmacSha256(`${secret}:session`, body)))) return null;
|
|
try {
|
|
const s = JSON.parse(fromB64url(body)) as Session;
|
|
if (typeof s.uid !== "string" || typeof s.org !== "string" || s.exp < now.getTime() / 1000) return null;
|
|
return s;
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|