Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions client/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,9 @@ const App = () => {
});

const updateAuthState = (updates: Partial<AuthDebuggerState>) => {
if (updates.oauthClientInfo?.client_id) {
setOauthClientId(updates.oauthClientInfo.client_id);
}
setAuthState((prev) => ({ ...prev, ...updates }));
};

Expand Down
53 changes: 51 additions & 2 deletions client/src/__tests__/App.routing.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { render, waitFor } from "@testing-library/react";
import { fireEvent, render, screen, waitFor } from "@testing-library/react";
import "@testing-library/jest-dom";
import App from "../App";
import { useConnection } from "../lib/hooks/useConnection";
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
Expand Down Expand Up @@ -79,7 +80,35 @@ jest.mock("../lib/hooks/useDraggablePane", () => ({

jest.mock("../components/Sidebar", () => ({
__esModule: true,
default: () => <div>Sidebar</div>,
default: ({ oauthClientId }: { oauthClientId: string }) => (
<div>
<div data-testid="sidebar-oauth-client-id">{oauthClientId}</div>
</div>
),
}));

jest.mock("../components/AuthDebugger", () => ({
__esModule: true,
default: ({
updateAuthState,
}: {
updateAuthState: (
updates: Partial<import("../lib/auth-types").AuthDebuggerState>,
) => void;
}) => (
<button
onClick={() =>
updateAuthState({
oauthClientInfo: {
client_id: "dcr_client_id",
redirect_uris: ["http://localhost:3000/oauth/callback/debug"],
},
})
}
>
simulate dcr client
</button>
),
}));

// Mock fetch
Expand All @@ -95,6 +124,8 @@ describe("App - URL Fragment Routing", () => {

beforeEach(() => {
jest.restoreAllMocks();
localStorage.clear();
window.location.hash = "";

// Inspector starts disconnected.
mockUseConnection.mockReturnValue(disconnectedConnectionState);
Expand Down Expand Up @@ -158,4 +189,22 @@ describe("App - URL Fragment Routing", () => {
expect(window.location.hash).toBe("");
});
});

test("syncs dynamically registered OAuth client ID into the sidebar field", async () => {
render(<App />);

fireEvent.click(
screen.getByRole("button", { name: /open auth settings/i }),
);
fireEvent.click(
screen.getByRole("button", { name: /simulate dcr client/i }),
);

await waitFor(() => {
expect(screen.getByTestId("sidebar-oauth-client-id")).toHaveTextContent(
"dcr_client_id",
);
expect(localStorage.getItem("lastOauthClientId")).toBe("dcr_client_id");
});
});
});
40 changes: 40 additions & 0 deletions client/src/components/__tests__/AuthDebugger.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,46 @@ describe("AuthDebugger", () => {
});
});

describe("Token Request behavior", () => {
it("uses OAuth client information restored with auth state when storage is empty", async () => {
sessionStorageMock.getItem.mockImplementation(() => null);
mockExchangeAuthorization.mockResolvedValueOnce(mockOAuthTokens);

const updateAuthState = jest.fn();

await act(async () => {
renderAuthDebugger({
updateAuthState,
authState: {
...defaultAuthState,
isInitiatingAuth: false,
oauthStep: "token_request",
oauthMetadata: mockOAuthMetadata as unknown as OAuthMetadata,
oauthClientInfo: mockOAuthClientInfo,
authorizationCode: "test_authorization_code",
},
});
});

await act(async () => {
fireEvent.click(screen.getByText("Continue"));
});

expect(mockExchangeAuthorization).toHaveBeenCalledWith(
defaultProps.serverUrl,
expect.objectContaining({
metadata: mockOAuthMetadata,
clientInformation: mockOAuthClientInfo,
authorizationCode: "test_authorization_code",
}),
);
expect(updateAuthState).toHaveBeenCalledWith({
oauthTokens: mockOAuthTokens,
oauthStep: "complete",
});
});
});

describe("OAuth State Persistence", () => {
it("should store auth state to sessionStorage before redirect in Quick OAuth Flow", async () => {
const updateAuthState = jest.fn();
Expand Down
21 changes: 16 additions & 5 deletions client/src/lib/oauth-state-machine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,16 +175,27 @@ export const oauthTransitions: Record<OAuthStep, StateTransition> = {

token_request: {
canTransition: async (context) => {
const metadata =
context.provider.getServerMetadata() ?? context.state.oauthMetadata;
const clientInformation =
(await context.provider.clientInformation()) ??
context.state.oauthClientInfo;

return (
!!context.state.authorizationCode &&
!!context.provider.getServerMetadata() &&
!!(await context.provider.clientInformation())
!!context.state.authorizationCode && !!metadata && !!clientInformation
);
},
execute: async (context) => {
const codeVerifier = context.provider.codeVerifier();
const metadata = context.provider.getServerMetadata()!;
const clientInformation = (await context.provider.clientInformation())!;
const metadata =
context.provider.getServerMetadata() ?? context.state.oauthMetadata;
const clientInformation =
(await context.provider.clientInformation()) ??
context.state.oauthClientInfo;

if (!metadata || !clientInformation) {
throw new Error("Missing OAuth metadata or client information");
}

const tokens = await exchangeAuthorization(context.serverUrl, {
metadata,
Expand Down