fix: read the SDK's own discovery state instead of a marker of our own

Copilot review (#2287), two findings — and taken together they said the marker
was the wrong mechanism, so this removes it rather than patching it again:

- A transient failure in our CIMD preflight recorded no marker, so a *successful*
  SDK fallback discovery that advertised CIMD was still stored as `dcr`.
- `existing` and `existingKind` could describe different registrations, because
  `clientInformation()` answers preregistered-first while `clientRegistrationKind()`
  prefers the issuer's dynamic slot.

SDK v2 `auth()` persists the authorization-server metadata via
`saveDiscoveryState` *before* it reads or writes client information, and it takes
its URL-based-client-ID branch — rather than `registerClient` — exactly when that
metadata advertises `client_id_metadata_document_supported` and a
`clientMetadataUrl` is configured. So the branch is not inferred at all now: it is
read back from the state the SDK itself just wrote.

`resolveSdkRegistrationKind` becomes two cases. A save for an issuer that already
has a registration under this `client_id` is a back-stamp, answered by that
registration's recorded kind — so an existing DCR on the metadata URL stays
`dcr`. A save for an issuer with none is a new registration, answered by the
discovery state, guarded on the metadata describing that same issuer.

This drops `cimdClientMetadataUrl` and its storage accessors entirely, and
restores `ensureCimdClientRegistration` to a plain issuer-bound pre-registration.
Invalid-client recovery, second-issuer CIMD, second-issuer DCR minting the
metadata URL, and preflight failure are all consequences of the two cases rather
than separate mechanisms.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Lmj6X2epWpPE7ScdSKQmtA
Signed-off-by: cliffhall <cliff@futurescale.com>
This commit is contained in:
cliffhall 2026-09-07 02:03:12 -04:00
commit 6273790077

View file

@ -68,50 +68,19 @@ export async function ensureCimdClientRegistration(params: {
}
const issuer = metadata?.issuer;
const supportsCimd = metadata?.client_id_metadata_document_supported === true;
/**
* The marker records that *this* AS accepts this URL as a `client_id` **and**
* that the registration standing for it got there through CIMD. It is written
* only where both are established, and actively withdrawn otherwise, so it
* cannot go stale: discovery runs on every connect.
*/
const setMarker = async (url: string | undefined) => {
if (issuer) await params.provider.saveCimdClientMetadataUrl(issuer, url);
};
if (!supportsCimd) {
// Withdrawn, not merely left alone — an AS that stops advertising CIMD
// stops being treated as one.
await setMarker(undefined);
return;
}
if (!metadata?.client_id_metadata_document_supported) return;
// ⚠️ Keyed by the issuer just resolved, not read ctx-less. A ctx-less read
// resolves through the *active* issuer, so it early-returns for every
// subsequent issuer and leaves them with no CIMD record at all. It answers the
// static case first, since `clientInformation` checks the preregistered slot
// before any issuer slot.
// subsequent issuer and leaves them with no CIMD registration at all. It
// answers the static case first, since `clientInformation` checks the
// preregistered slot before any issuer slot — and a static client, like any
// existing registration, means there is nothing to pre-register.
const existing = await params.provider.clientInformation(
issuer ? { issuer } : undefined,
);
if (existing?.client_id) {
// Something is already registered for this AS, so this call establishes
// nothing — and AS support for CIMD is not on its own evidence that *that*
// registration is a CIMD one. RFC 7591 §3.2 leaves a dynamically issued
// `client_id` opaque, so an existing DCR may carry this very URL; marking it
// would relabel a real dynamic registration (Copilot). Reaffirm the marker
// only for a registration already recorded as `cimd` under this exact URL,
// and withdraw it otherwise — which also covers a static client.
const existingKind = await params.provider.clientRegistrationKind(issuer);
const isCimdRegistration =
existingKind === "cimd" && existing.client_id === clientMetadataUrl;
await setMarker(isCimdRegistration ? clientMetadataUrl : undefined);
return;
}
if (existing?.client_id) return;
// From here this call *is* the CIMD registration, so the marker is earned.
await setMarker(clientMetadataUrl);
await params.provider.saveClientInformation(
{ client_id: clientMetadataUrl },
{