2026-06-30 16:01:35 -04:00
|
|
|
// Pure OIDC-login state helpers (no imports) so they're unit-testable under the
|
|
|
|
|
// tsx + node:test harness. The SDK-driven flow lives in oidcLoginUtil.ts.
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Dynamic-registration cache. MAS issues a `client_id` per dynamic registration;
|
|
|
|
|
* caching by issuer (scoped to the current redirectUri) avoids re-registering on
|
|
|
|
|
* every login. Invalidated when the redirectUri changes or the provider later
|
|
|
|
|
* rejects the cached id (`invalid_client`).
|
|
|
|
|
*/
|
|
|
|
|
const REG_CACHE_KEY = 'cinny_oidc_dynamic_clients';
|
|
|
|
|
type RegEntry = { clientId: string; redirectUri: string };
|
|
|
|
|
type RegCache = Record<string, RegEntry>;
|
|
|
|
|
|
|
|
|
|
const readRegCache = (): RegCache => {
|
|
|
|
|
try {
|
|
|
|
|
const raw = localStorage.getItem(REG_CACHE_KEY);
|
|
|
|
|
return raw ? (JSON.parse(raw) as RegCache) : {};
|
|
|
|
|
} catch {
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
const writeRegCache = (cache: RegCache): void => {
|
|
|
|
|
localStorage.setItem(REG_CACHE_KEY, JSON.stringify(cache));
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const getCachedClientId = (issuer: string, redirectUri: string): string | undefined => {
|
|
|
|
|
const entry = readRegCache()[issuer];
|
|
|
|
|
return entry && entry.redirectUri === redirectUri ? entry.clientId : undefined;
|
|
|
|
|
};
|
|
|
|
|
export const cacheClientId = (issuer: string, clientId: string, redirectUri: string): void => {
|
|
|
|
|
const cache = readRegCache();
|
|
|
|
|
cache[issuer] = { clientId, redirectUri };
|
|
|
|
|
writeRegCache(cache);
|
|
|
|
|
};
|
|
|
|
|
export const invalidateCachedClient = (issuer: string): void => {
|
|
|
|
|
const cache = readRegCache();
|
|
|
|
|
if (cache[issuer]) {
|
|
|
|
|
delete cache[issuer];
|
|
|
|
|
writeRegCache(cache);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2026-09-12 19:46:05 -04:00
|
|
|
// OAuth error codes that mean the provider no longer recognises our client id
|
|
|
|
|
// (deleted/expired dynamic registration, or one issued by a different deployment).
|
|
|
|
|
const STALE_CLIENT_ERROR_CODES = new Set(['invalid_client', 'unauthorized_client']);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* #67 — pure: does a `startOidcLogin` failure indicate the CACHED client id is
|
|
|
|
|
* bad? Only then is dropping the registration cache justified; a transient
|
|
|
|
|
* network error, an offline discovery fetch, or a local (`crypto.randomUUID`)
|
|
|
|
|
* failure must leave it alone, otherwise every retry performs a fresh dynamic
|
|
|
|
|
* registration and piles throwaway clients onto the provider. Recognised
|
|
|
|
|
* shapes: an OAuth error response (`{ error: 'invalid_client' }`, as thrown by
|
|
|
|
|
* oidc-client-ts `ErrorResponse`), or an HTTP 400/401 carried as
|
|
|
|
|
* `httpStatus`/`status` (MatrixError-style) from the registration/authorize step.
|
|
|
|
|
*/
|
|
|
|
|
export const isStaleClientError = (e: unknown): boolean => {
|
|
|
|
|
if (!e || typeof e !== 'object') return false;
|
|
|
|
|
const { error, httpStatus, status } = e as {
|
|
|
|
|
error?: unknown;
|
|
|
|
|
httpStatus?: unknown;
|
|
|
|
|
status?: unknown;
|
|
|
|
|
};
|
|
|
|
|
if (typeof error === 'string' && STALE_CLIENT_ERROR_CODES.has(error)) return true;
|
|
|
|
|
const code = typeof httpStatus === 'number' ? httpStatus : status;
|
|
|
|
|
return code === 400 || code === 401;
|
|
|
|
|
};
|
|
|
|
|
|
2026-06-30 16:01:35 -04:00
|
|
|
/** Parsed shape of the provider's redirect back to our callback URL. */
|
|
|
|
|
export type OidcCallbackParams =
|
|
|
|
|
| { kind: 'success'; code: string; state: string }
|
|
|
|
|
| { kind: 'error'; error: string; errorDescription?: string }
|
|
|
|
|
| { kind: 'invalid' };
|
|
|
|
|
|
|
|
|
|
/** Pure: classify the callback query string into success / error / invalid. */
|
|
|
|
|
export const parseOidcCallbackParams = (search: string): OidcCallbackParams => {
|
|
|
|
|
const params = new URLSearchParams(search);
|
|
|
|
|
const error = params.get('error');
|
|
|
|
|
if (error) {
|
|
|
|
|
return { kind: 'error', error, errorDescription: params.get('error_description') ?? undefined };
|
|
|
|
|
}
|
|
|
|
|
const code = params.get('code');
|
|
|
|
|
const state = params.get('state');
|
|
|
|
|
if (code && state) return { kind: 'success', code, state };
|
|
|
|
|
return { kind: 'invalid' };
|
|
|
|
|
};
|