Merge branch 'v2/main' into v2/feat/2068-refresh-token-toggle
This commit is contained in:
commit
6968738f24
4 changed files with 1427 additions and 1310 deletions
|
|
@ -1,6 +1,7 @@
|
|||
import { describe, it, expect } from "vitest";
|
||||
import {
|
||||
AuthChallengeError,
|
||||
challengeResourceMetadataUrl,
|
||||
AuthRecoveryRequiredError,
|
||||
findNestedAuthError,
|
||||
isAuthChallengeError,
|
||||
|
|
@ -286,6 +287,58 @@ describe("parseAuthChallengeFromError", () => {
|
|||
});
|
||||
});
|
||||
|
||||
describe("resource_metadata (RFC 9728) preservation — #2071", () => {
|
||||
const METADATA_URL = "http://127.0.0.1:3001/custom/protected-resource";
|
||||
const HEADER = `Bearer resource_metadata="${METADATA_URL}"`;
|
||||
|
||||
it("keeps the advertised URL when parsing a response challenge", () => {
|
||||
const response = new Response(null, {
|
||||
status: 401,
|
||||
headers: { "WWW-Authenticate": HEADER },
|
||||
});
|
||||
|
||||
expect(parseAuthChallengeFromResponse(response)).toEqual({
|
||||
reason: "token_expired",
|
||||
resourceMetadataUrl: METADATA_URL,
|
||||
raw: { httpStatus: 401, wwwAuthenticate: HEADER },
|
||||
});
|
||||
});
|
||||
|
||||
it("keeps the advertised URL when parsing an error challenge", () => {
|
||||
expect(
|
||||
parseAuthChallengeFromError({ status: 401, wwwAuthenticate: HEADER }),
|
||||
).toMatchObject({ resourceMetadataUrl: METADATA_URL });
|
||||
});
|
||||
|
||||
it("omits the field entirely when the challenge does not advertise one", () => {
|
||||
const response = new Response(null, {
|
||||
status: 401,
|
||||
headers: { "WWW-Authenticate": "Bearer" },
|
||||
});
|
||||
|
||||
expect(parseAuthChallengeFromResponse(response)).not.toHaveProperty(
|
||||
"resourceMetadataUrl",
|
||||
);
|
||||
});
|
||||
|
||||
it("converts the stored string to a URL at the OAuth boundary", () => {
|
||||
expect(
|
||||
challengeResourceMetadataUrl({ resourceMetadataUrl: METADATA_URL })?.href,
|
||||
).toBe(METADATA_URL);
|
||||
});
|
||||
|
||||
it.each([
|
||||
["absent", undefined],
|
||||
["empty", ""],
|
||||
["whitespace", " "],
|
||||
["not a URL", "not-a-url"],
|
||||
])("ignores a %s resource_metadata value", (_label, value) => {
|
||||
expect(
|
||||
challengeResourceMetadataUrl({ resourceMetadataUrl: value }),
|
||||
).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe("isAuthChallengeError", () => {
|
||||
it("detects AuthChallengeError instances", () => {
|
||||
const err = new AuthChallengeError({ reason: "token_expired" }, 401);
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
import { describe, it, expect, vi, beforeEach } from "vitest";
|
||||
import { describe, it, expect, vi, afterEach, beforeEach } from "vitest";
|
||||
import type { OAuthStorage } from "@inspector/core/auth/storage.js";
|
||||
import { BaseOAuthClientProvider } from "@inspector/core/auth/providers.js";
|
||||
import { ensureCimdClientRegistration } from "@inspector/core/auth/cimd.js";
|
||||
|
|
@ -31,6 +31,10 @@ describe("ensureCimdClientRegistration", () => {
|
|||
} as unknown as OAuthStorage;
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.unstubAllGlobals();
|
||||
});
|
||||
|
||||
it("pre-registers URL-based client id when AS supports CIMD", async () => {
|
||||
const fetchFn = vi.fn(async (input: RequestInfo | URL) => {
|
||||
const url = String(input);
|
||||
|
|
@ -97,6 +101,63 @@ describe("ensureCimdClientRegistration", () => {
|
|||
expect(storage.saveClientInformation).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("discovers protected-resource metadata at the challenge-advertised URL (#2071)", async () => {
|
||||
const metadataUrl = new URL(
|
||||
"http://127.0.0.1:9999/custom/protected-resource",
|
||||
);
|
||||
// Both discovery legs must run through the configured fetch — on web that
|
||||
// is the backend proxy, so a leg that fell back to the global would fail
|
||||
// on CORS and be swallowed.
|
||||
const fetchFn = vi.fn(async (input: RequestInfo | URL) => {
|
||||
const url = String(input);
|
||||
if (url === metadataUrl.href) {
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
resource: SERVER_URL,
|
||||
authorization_servers: ["http://127.0.0.1:9999"],
|
||||
}),
|
||||
);
|
||||
}
|
||||
if (url.includes("/.well-known/oauth-authorization-server")) {
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
issuer: "http://127.0.0.1:9999",
|
||||
authorization_endpoint: "http://127.0.0.1:9999/oauth/authorize",
|
||||
token_endpoint: "http://127.0.0.1:9999/oauth/token",
|
||||
response_types_supported: ["code"],
|
||||
client_id_metadata_document_supported: true,
|
||||
}),
|
||||
);
|
||||
}
|
||||
return new Response(null, { status: 404 });
|
||||
});
|
||||
// Nothing may reach the network directly; a call here means a leg fell
|
||||
// back to the global fetch.
|
||||
const globalFetch = vi.fn();
|
||||
vi.stubGlobal("fetch", globalFetch);
|
||||
|
||||
await ensureCimdClientRegistration({
|
||||
serverUrl: SERVER_URL,
|
||||
provider: createProvider(storage),
|
||||
fetchFn,
|
||||
resourceMetadataUrl: metadataUrl,
|
||||
});
|
||||
|
||||
expect(globalFetch).not.toHaveBeenCalled();
|
||||
const requested = fetchFn.mock.calls.map(([input]) => String(input));
|
||||
expect(requested).toContain(metadataUrl.href);
|
||||
expect(
|
||||
requested.filter((url) =>
|
||||
url.includes("/.well-known/oauth-protected-resource"),
|
||||
),
|
||||
).toEqual([]);
|
||||
expect(storage.saveClientInformation).toHaveBeenCalledWith(
|
||||
SERVER_URL,
|
||||
{ client_id: METADATA_URL },
|
||||
{ registrationKind: "cimd" },
|
||||
);
|
||||
});
|
||||
|
||||
it("no-ops when client information is already stored", async () => {
|
||||
storage.getClientInformation = vi.fn(async () => ({
|
||||
client_id: "existing-client",
|
||||
|
|
|
|||
|
|
@ -1,5 +1,8 @@
|
|||
import { describe, it, expect, vi } from "vitest";
|
||||
import { createAuthChallengeInterceptFetch } from "@inspector/core/mcp/node/authChallengeFetch.js";
|
||||
import {
|
||||
createAuthChallengeInterceptFetch,
|
||||
createAuthChallengeObserverFetch,
|
||||
} from "@inspector/core/mcp/node/authChallengeFetch.js";
|
||||
import { AuthChallengeError } from "@inspector/core/auth/challenge.js";
|
||||
|
||||
describe("createAuthChallengeInterceptFetch", () => {
|
||||
|
|
@ -84,3 +87,63 @@ describe("createAuthChallengeInterceptFetch", () => {
|
|||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("createAuthChallengeObserverFetch", () => {
|
||||
const METADATA_URL = "http://127.0.0.1:3001/custom/protected-resource";
|
||||
|
||||
it("reports a challenge without altering the response or its body", async () => {
|
||||
const baseFetch = vi.fn(
|
||||
async () =>
|
||||
new Response("payload", {
|
||||
status: 401,
|
||||
headers: {
|
||||
"WWW-Authenticate": `Bearer resource_metadata="${METADATA_URL}"`,
|
||||
},
|
||||
}),
|
||||
);
|
||||
const onChallenge = vi.fn();
|
||||
const fetchFn = createAuthChallengeObserverFetch(baseFetch, onChallenge);
|
||||
|
||||
const res = await fetchFn("https://example.com/mcp");
|
||||
|
||||
expect(res.status).toBe(401);
|
||||
// The body must survive for the transport (or the interceptor above).
|
||||
expect(await res.text()).toBe("payload");
|
||||
expect(onChallenge).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ resourceMetadataUrl: METADATA_URL }),
|
||||
);
|
||||
});
|
||||
|
||||
it("reports a 403 challenge too", async () => {
|
||||
const baseFetch = vi.fn(
|
||||
async () =>
|
||||
new Response(null, {
|
||||
status: 403,
|
||||
headers: { "WWW-Authenticate": 'Bearer error="insufficient_scope"' },
|
||||
}),
|
||||
);
|
||||
const onChallenge = vi.fn();
|
||||
|
||||
await createAuthChallengeObserverFetch(
|
||||
baseFetch,
|
||||
onChallenge,
|
||||
)("https://example.com/mcp");
|
||||
|
||||
expect(onChallenge).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ reason: "insufficient_scope" }),
|
||||
);
|
||||
});
|
||||
|
||||
it("stays silent on a non-challenge response", async () => {
|
||||
const baseFetch = vi.fn(async () => new Response("ok", { status: 200 }));
|
||||
const onChallenge = vi.fn();
|
||||
|
||||
const res = await createAuthChallengeObserverFetch(
|
||||
baseFetch,
|
||||
onChallenge,
|
||||
)("https://example.com/mcp");
|
||||
|
||||
expect(res.status).toBe(200);
|
||||
expect(onChallenge).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1816,6 +1816,153 @@ describe("OAuthManager", () => {
|
|||
});
|
||||
});
|
||||
|
||||
describe("resource_metadata forwarding (RFC 9728) — #2071", () => {
|
||||
const METADATA_URL = "http://127.0.0.1:3001/custom/protected-resource";
|
||||
|
||||
it("forwards the advertised metadata URL to auth() as a URL", async () => {
|
||||
mockedMcpAuth.mockResolvedValue("AUTHORIZED");
|
||||
const manager = new OAuthManager(createMockParams());
|
||||
|
||||
await manager.handleAuthChallenge({
|
||||
reason: "token_expired",
|
||||
resourceMetadataUrl: METADATA_URL,
|
||||
});
|
||||
|
||||
expect(mockedMcpAuth).toHaveBeenCalledWith(
|
||||
expect.anything(),
|
||||
expect.objectContaining({
|
||||
resourceMetadataUrl: new URL(METADATA_URL),
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it("leaves the metadata URL undefined when the challenge advertises none", async () => {
|
||||
mockedMcpAuth.mockResolvedValue("AUTHORIZED");
|
||||
const manager = new OAuthManager(createMockParams());
|
||||
|
||||
await manager.handleAuthChallenge({ reason: "token_expired" });
|
||||
|
||||
expect(mockedMcpAuth).toHaveBeenCalledWith(
|
||||
expect.anything(),
|
||||
expect.objectContaining({ resourceMetadataUrl: undefined }),
|
||||
);
|
||||
});
|
||||
|
||||
it("ignores a malformed metadata URL rather than failing authorization", async () => {
|
||||
mockedMcpAuth.mockResolvedValue("AUTHORIZED");
|
||||
const manager = new OAuthManager(createMockParams());
|
||||
|
||||
const outcome = await manager.handleAuthChallenge({
|
||||
reason: "token_expired",
|
||||
resourceMetadataUrl: "not-a-url",
|
||||
});
|
||||
|
||||
expect(outcome).toEqual({ kind: "satisfied" });
|
||||
expect(mockedMcpAuth).toHaveBeenCalledWith(
|
||||
expect.anything(),
|
||||
expect.objectContaining({ resourceMetadataUrl: undefined }),
|
||||
);
|
||||
});
|
||||
|
||||
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();
|
||||
const noScopeTokens = {
|
||||
access_token: "a",
|
||||
token_type: "Bearer",
|
||||
scope: "",
|
||||
};
|
||||
const midScopeTokens = {
|
||||
access_token: "b",
|
||||
token_type: "Bearer",
|
||||
scope: "newscope",
|
||||
};
|
||||
const grantedTokens = {
|
||||
access_token: "d",
|
||||
token_type: "Bearer",
|
||||
scope: "granted:scope",
|
||||
};
|
||||
storageOf(params)
|
||||
.getTokens.mockResolvedValueOnce(undefined)
|
||||
.mockResolvedValueOnce(undefined)
|
||||
.mockResolvedValueOnce(noScopeTokens)
|
||||
.mockResolvedValueOnce(noScopeTokens)
|
||||
.mockResolvedValueOnce(midScopeTokens)
|
||||
.mockResolvedValueOnce(grantedTokens);
|
||||
const manager = new OAuthManager(params);
|
||||
|
||||
await manager.handleAuthChallenge({
|
||||
reason: "insufficient_scope",
|
||||
resourceMetadataUrl: METADATA_URL,
|
||||
});
|
||||
|
||||
expect(mockedMcpAuth).toHaveBeenCalledTimes(2);
|
||||
expect(mockedMcpAuth).toHaveBeenNthCalledWith(
|
||||
2,
|
||||
expect.anything(),
|
||||
expect.objectContaining({
|
||||
forceReauthorization: true,
|
||||
resourceMetadataUrl: new URL(METADATA_URL),
|
||||
}),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("handleAuthChallenge (additional branch coverage)", () => {
|
||||
it("resolves via the second satisfaction check inside the mutex", async () => {
|
||||
const params = createMockParams();
|
||||
|
|
|
|||
Loading…
Reference in a new issue