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

@ -1777,6 +1777,61 @@ describe("OAuthManager", () => {
);
});
it("uses the last observed challenge on the plain authenticate() path", async () => {
// The legacy first-authorization path: no challenge is passed in, so the
// one the transport observed is the only source (#2071).
mockedMcpAuth.mockResolvedValue("REDIRECT");
const params = createMockParams();
const manager = new OAuthManager(params);
const captureSpy = vi
.spyOn(
(await import("@inspector/core/auth/providers.js"))
.BaseOAuthClientProvider.prototype,
"getCapturedAuthUrl",
)
.mockReturnValue(new URL("https://auth.example.com/authorize?state=x"));
manager.noteObservedAuthChallenge({
reason: "unauthorized",
resourceMetadataUrl: METADATA_URL,
});
await manager.authenticate();
expect(mockedMcpAuth).toHaveBeenCalledWith(
expect.anything(),
expect.objectContaining({
resourceMetadataUrl: new URL(METADATA_URL),
}),
);
captureSpy.mockRestore();
});
it("clears the observed URL when a later challenge advertises none", async () => {
mockedMcpAuth.mockResolvedValue("REDIRECT");
const params = createMockParams();
const manager = new OAuthManager(params);
const captureSpy = vi
.spyOn(
(await import("@inspector/core/auth/providers.js"))
.BaseOAuthClientProvider.prototype,
"getCapturedAuthUrl",
)
.mockReturnValue(new URL("https://auth.example.com/authorize?state=x"));
manager.noteObservedAuthChallenge({
reason: "unauthorized",
resourceMetadataUrl: METADATA_URL,
});
manager.noteObservedAuthChallenge({ reason: "unauthorized" });
await manager.authenticate();
expect(mockedMcpAuth).toHaveBeenCalledWith(
expect.anything(),
expect.objectContaining({ resourceMetadataUrl: undefined }),
);
captureSpy.mockRestore();
});
it("forwards it on the forced step-up reauthorization leg too", async () => {
mockedMcpAuth.mockResolvedValue("AUTHORIZED");
const params = createMockParams();