fix(auth): carry resource_metadata into first-time legacy authorization

The challenge-driven plumbing did not reach the default CLI/TUI flow. With
no stored token in the legacy era, InspectorClient deliberately builds the
transport with no authProvider (so the SDK cannot open a browser before the
callback server is listening), which also leaves challenge interception off;
the 401 then surfaces as the SDK's headerless UnauthorizedError and the
runner calls authenticate() with no challenge in hand. So a server serving
metadata only at the advertised path still failed there — including for the
legacy showcase the README instructs.

Add a passive counterpart to the challenge interceptor: an observer fetch
that reports every 401/403 without touching control flow or the body. The
OAuthManager records the advertised URL from it and uses it in
authenticate() and its CIMD pre-registration. Latest-wins, including
clearing when a later challenge advertises none.

Also: the fixture helper dropped an explicitly supplied empty
resourceMetadataPath by truthiness, bypassing validation and silently
reverting to the well-known route.

Covered by a direct-client integration test that drives connect ->
authenticate() against the real server and asserts the advertised path was
requested and the well-known one never was; verified failing without the
fix.

Signed-off-by: cliffhall <cliff@futurescale.com>
This commit is contained in:
cliffhall 2026-08-24 21:04:34 -04:00
commit 597e1bd25f

View file

@ -82,6 +82,12 @@ export class OAuthManager {
private oauthFlowState: OAuthFlowState | null = null;
/** SEP-2350 union scope pending until interactive step-up completes. */
private pendingAuthorizationScope: string | undefined;
/**
* The RFC 9728 metadata URL from the last 401/403 the transport observed.
* See {@link noteObservedAuthChallenge}.
*/
private observedResourceMetadataUrl: URL | undefined;
private authChallengeMutex: Promise<void> = Promise.resolve();
constructor(params: OAuthManagerParams) {
@ -263,6 +269,20 @@ export class OAuthManager {
return authorizationUrl;
}
/**
* Record a challenge the transport saw but did not raise as one.
*
* The legacy first-authorization path connects with no `authProvider` and no
* challenge interception, so the client calls {@link authenticate} off a
* headerless SDK `UnauthorizedError` — this is what carries the advertised
* RFC 9728 metadata URL across that gap (#2071). Latest-wins: every
* challenge comes from the same server, so the most recent one is what that
* server currently says, including when it says nothing.
*/
noteObservedAuthChallenge(challenge: AuthChallenge): void {
this.observedResourceMetadataUrl = challengeResourceMetadataUrl(challenge);
}
async authenticate(): Promise<URL | undefined> {
if (this.isEnterpriseManaged()) {
return this.authenticateEnterpriseManaged();
@ -273,16 +293,23 @@ export class OAuthManager {
provider.clearCapturedAuthUrl();
// No challenge is passed to this entry point — it is reached from a plain
// unauthorized error — so the last one the transport observed is the only
// source for the advertised metadata URL.
const resourceMetadataUrl = this.observedResourceMetadataUrl;
await ensureCimdClientRegistration({
serverUrl,
provider,
fetchFn: this.params.effectiveAuthFetch,
resourceMetadataUrl,
});
const result = await mcpAuth(provider, {
serverUrl,
scope: provider.scope,
fetchFn: this.params.effectiveAuthFetch,
resourceMetadataUrl,
});
if (result === "AUTHORIZED") {