Files
explore/lib/oidc.ts
T
zeekayandHanzo Dev 6f80c0ce4e fix(explore): the explorer's own token sheet is the umbrella's
styles/tokens.css was a 494-line FORK of @luxfi/ui/tokens.css, which is how it
drifted its own browns: --color-status-warn #B45309 / #FBBF7A and the whole
Chakra orange + yellow badge ramp (#FEEBCB #FFFAF0 #DD6B20 #FFFFF0 #D69E2E
#FEFCBF #B7791F #C05621 #7B341E #744210 #FAF089). It imports the umbrella now
and adds nothing of its own.

next.config.js hand-listed SEVEN of the engine's 55 packages in
transpilePackages and deduped none of them, which is how this app's store holds
seven physical copies of @hanzogui/popper (3.0.0, 3.0.1, 3.0.2 x3, 7.3.0 x2). A
context published at module scope is per-copy, so PopperAnchor read the empty
default and called refs.setReference on undefined once per tooltip trigger. It
uses withLuxUi now — the one place that wiring lives.

47 files reached for Tailwind's chromatic ramps directly (108 occurrences).
They resolve through tokens now, under short semantic names registered in the
@theme block: text-good / text-bad / text-warn, where warn is ACHROMATIC —
attention in Lux is the loudest neutral, not a hue. The Quasar certificate
tooltip stops naming colours it no longer paints ("BLS (green)", "Corona
(blue)", "ML-DSA (purple)") — copy that describes a palette has to change with
it.

Measured on the rendered pages: warm 0%, tinted-neutral 0.0003%, mean chroma
0.004 (/) and 0.003 (/txs) at 1440x900, 0.000 at 390x844, both modes; zero
uncaught setReference errors.

Two pre-existing defects surfaced because lint-staged now lints these files and
are fixed here rather than skipped: `Box` was used but never imported in
AddressEntity.pw.tsx, and eight lines already exceeded the 160-column limit.

Co-authored-by: Hanzo Dev <[email protected]>
2026-07-27 19:14:51 -07:00

115 lines
3.7 KiB
TypeScript

import config from 'configs/app';
/**
* The one place that knows where the IdP's OAuth endpoints live.
*
* IAM serves OAuth under `/v1/iam/oauth/*` and nothing else. The bare
* `/oauth/authorize` and `/oauth/token` that pages used to build by hand are
* not routes at all — the IdP's SPA catch-all answers them with `200
* text/html`, so a hand-built authorize silently renders the wrong page and a
* hand-built token exchange passes `response.ok` and then dies parsing HTML as
* JSON. Measured on lux.id:
*
* /oauth/authorize 200 text/html (SPA shell — silent failure)
* /v1/iam/oauth/authorize 400 text/plain (real endpoint)
* /oauth/token 200 text/html (SPA shell — silent failure)
* /v1/iam/oauth/token 401 application/json (real endpoint)
*
* Never reconstruct these paths at a call site. Import from here.
*/
const OAUTH_BASE = '/v1/iam/oauth';
const STATE_KEY = 'oidc_state';
interface Oidc {
serverUrl: string;
clientId: string;
}
/** The configured OIDC provider, or null when account auth is off. */
export function getOidc(): Oidc | null {
const feature = config.features.account;
if (!feature.isEnabled || feature.authProvider !== 'oidc' || !feature.oidc) {
return null;
}
return feature.oidc;
}
/** Absolute URL of an IAM OAuth endpoint on the configured issuer. */
function endpoint(serverUrl: string, name: 'authorize' | 'token'): string {
return `${ serverUrl.replace(/\/$/, '') }${ OAUTH_BASE }/${ name }`;
}
/**
* Authorization-code login URL. Stores the CSRF state in sessionStorage so
* `exchangeCode` can verify it on the way back. Returns '' when OIDC is off.
*/
export function buildLoginUrl(redirectPath: string): string {
const oidc = getOidc();
if (!oidc) {
return '';
}
const state = crypto.randomUUID();
sessionStorage.setItem(STATE_KEY, state);
const params = new URLSearchParams({
response_type: 'code',
client_id: oidc.clientId,
redirect_uri: `${ window.location.origin }${ redirectPath }`,
scope: 'openid profile email',
state,
});
return `${ endpoint(oidc.serverUrl, 'authorize') }?${ params.toString() }`;
}
/** True when `state` matches the nonce stored by `buildLoginUrl`. */
export function consumeState(state: string | undefined): boolean {
const saved = sessionStorage.getItem(STATE_KEY);
if (!saved || state !== saved) {
return false;
}
sessionStorage.removeItem(STATE_KEY);
return true;
}
export interface TokenResponse {
access_token: string;
id_token?: string;
refresh_token?: string;
expires_in?: number;
}
/** Exchange an authorization code for tokens at IAM's token endpoint. */
export async function exchangeCode(code: string, redirectPath: string): Promise<TokenResponse> {
const oidc = getOidc();
if (!oidc) {
throw new Error('OIDC authentication is not configured');
}
const response = await fetch(endpoint(oidc.serverUrl, 'token'), {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: new URLSearchParams({
grant_type: 'authorization_code',
code,
redirect_uri: `${ window.location.origin }${ redirectPath }`,
client_id: oidc.clientId,
}),
});
if (!response.ok) {
throw new Error(`Token exchange failed: ${ await response.text() }`);
}
// A 200 that is not JSON means we hit the IdP's SPA shell, not its token
// endpoint. Say so, instead of dying inside JSON.parse.
const contentType = response.headers.get('content-type') ?? '';
if (!contentType.includes('application/json')) {
throw new Error(`Token endpoint returned ${ contentType || 'no content-type' }, not JSON`);
}
return await response.json() as TokenResponse;
}