fix(auth): OIDC callback evicts a cached client id the provider rejects
The redirect back with ?error=invalid_client is the only place a stale dynamic client id is ever rejected; the callback now resolves the issuer from the SDK's stored mx_oidc_<state> entry and invalidates the cache so the next attempt re-registers. Degrades to a no-op if the state entry is gone. Unit-tested. Fixes #102 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PPmy3tPq869XDW4njjVaKA
This commit is contained in:
1 parent
2137c37c8b
commit
2ca59be9fc
3 files changed
+124
-6
No files matched your search
@@ -45,14 +45,16 @@ export const invalidateCachedClient = (issuer: string): void => {
|
||||
const STALE_CLIENT_ERROR_CODES = new Set(['invalid_client', 'unauthorized_client']);
|
||||
|
||||
/**
|
||||
* #67 — pure: does a `startOidcLogin` failure indicate the CACHED client id is
|
||||
* #67 / #102 — pure: does an OIDC 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.
|
||||
* oidc-client-ts `ErrorResponse`, OR as echoed back on the callback redirect's
|
||||
* `?error=` query param via {@link OidcCallbackParams}), 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;
|
||||
@@ -69,7 +71,7 @@ export const isStaleClientError = (e: unknown): boolean => {
|
||||
/** 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: 'error'; error: string; errorDescription?: string; state?: string }
|
||||
| { kind: 'invalid' };
|
||||
|
||||
/** Pure: classify the callback query string into success / error / invalid. */
|
||||
@@ -77,10 +79,41 @@ 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 };
|
||||
return {
|
||||
kind: 'error',
|
||||
error,
|
||||
errorDescription: params.get('error_description') ?? undefined,
|
||||
// OAuth error redirects echo back the `state` we sent, same as a
|
||||
// success redirect would — needed to look up the pending sign-in's
|
||||
// issuer (see readStoredOidcIssuer) when the client id itself was
|
||||
// rejected.
|
||||
state: params.get('state') ?? undefined,
|
||||
};
|
||||
}
|
||||
const code = params.get('code');
|
||||
const state = params.get('state');
|
||||
if (code && state) return { kind: 'success', code, state };
|
||||
return { kind: 'invalid' };
|
||||
};
|
||||
|
||||
/**
|
||||
* #102 — the issuer for a pending sign-in isn't available on the callback's
|
||||
* error branch (we bail out before `completeAuthorizationCodeGrant`, which is
|
||||
* what would otherwise surface it). oidc-client-ts's `generateOidcAuthorizationUrl`
|
||||
* persists the pending sign-in's `SigninState` (including `authority` and
|
||||
* `client_id`) into sessionStorage before redirecting, keyed by
|
||||
* `mx_oidc_<state>` (WebStorageStateStore prefix `mx_oidc_`, keyed by the same
|
||||
* `state` value the provider echoes back on redirect). Read it directly so a
|
||||
* genuinely stale cached client id (`invalid_client`) can be evicted even when
|
||||
* the callback errors before a token exchange is attempted.
|
||||
*/
|
||||
export const readStoredOidcIssuer = (state: string): string | undefined => {
|
||||
try {
|
||||
const raw = sessionStorage.getItem(`mx_oidc_${state}`);
|
||||
if (!raw) return undefined;
|
||||
const { authority } = JSON.parse(raw) as { authority?: unknown };
|
||||
return typeof authority === 'string' ? authority : undefined;
|
||||
} catch {
|
||||
return undefined;
|
||||
}
|
||||
};
|
||||
Reference in new issue
Block a user