add test + prettier
This commit is contained in:
parent
35eab580ef
commit
cc49358f5a
7 changed files with 139 additions and 23 deletions
|
|
@ -122,7 +122,8 @@ const App = () => {
|
||||||
const [isAuthDebuggerVisible, setIsAuthDebuggerVisible] = useState(false);
|
const [isAuthDebuggerVisible, setIsAuthDebuggerVisible] = useState(false);
|
||||||
|
|
||||||
// Auth debugger state
|
// Auth debugger state
|
||||||
const [authState, setAuthState] = useState<AuthDebuggerState>(EMPTY_DEBUGGER_STATE);
|
const [authState, setAuthState] =
|
||||||
|
useState<AuthDebuggerState>(EMPTY_DEBUGGER_STATE);
|
||||||
|
|
||||||
// Helper function to update specific auth state properties
|
// Helper function to update specific auth state properties
|
||||||
const updateAuthState = (updates: Partial<AuthDebuggerState>) => {
|
const updateAuthState = (updates: Partial<AuthDebuggerState>) => {
|
||||||
|
|
@ -268,7 +269,10 @@ const App = () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
// Continue stepping through the OAuth flow from where we left off
|
// Continue stepping through the OAuth flow from where we left off
|
||||||
while (currentState.oauthStep !== "complete" && currentState.oauthStep !== "authorization_code") {
|
while (
|
||||||
|
currentState.oauthStep !== "complete" &&
|
||||||
|
currentState.oauthStep !== "authorization_code"
|
||||||
|
) {
|
||||||
await stateMachine.executeStep(currentState);
|
await stateMachine.executeStep(currentState);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -286,7 +290,8 @@ const App = () => {
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("OAuth continuation error:", error);
|
console.error("OAuth continuation error:", error);
|
||||||
updateAuthState({
|
updateAuthState({
|
||||||
latestError: error instanceof Error ? error : new Error(String(error)),
|
latestError:
|
||||||
|
error instanceof Error ? error : new Error(String(error)),
|
||||||
statusMessage: {
|
statusMessage: {
|
||||||
type: "error",
|
type: "error",
|
||||||
message: `Failed to complete OAuth flow: ${error instanceof Error ? error.message : String(error)}`,
|
message: `Failed to complete OAuth flow: ${error instanceof Error ? error.message : String(error)}`,
|
||||||
|
|
|
||||||
|
|
@ -162,7 +162,7 @@ const AuthDebugger = ({
|
||||||
// Store the current auth state before redirecting
|
// Store the current auth state before redirecting
|
||||||
sessionStorage.setItem(
|
sessionStorage.setItem(
|
||||||
SESSION_KEYS.AUTH_DEBUGGER_STATE,
|
SESSION_KEYS.AUTH_DEBUGGER_STATE,
|
||||||
JSON.stringify(currentState)
|
JSON.stringify(currentState),
|
||||||
);
|
);
|
||||||
// Open the authorization URL automatically
|
// Open the authorization URL automatically
|
||||||
window.location.href = currentState.authorizationUrl;
|
window.location.href = currentState.authorizationUrl;
|
||||||
|
|
|
||||||
|
|
@ -38,7 +38,9 @@ const OAuthDebugCallback = ({ onConnect }: OAuthCallbackProps) => {
|
||||||
const serverUrl = sessionStorage.getItem(SESSION_KEYS.SERVER_URL);
|
const serverUrl = sessionStorage.getItem(SESSION_KEYS.SERVER_URL);
|
||||||
|
|
||||||
// Try to restore the auth state
|
// Try to restore the auth state
|
||||||
const storedState = sessionStorage.getItem(SESSION_KEYS.AUTH_DEBUGGER_STATE);
|
const storedState = sessionStorage.getItem(
|
||||||
|
SESSION_KEYS.AUTH_DEBUGGER_STATE,
|
||||||
|
);
|
||||||
let restoredState = null;
|
let restoredState = null;
|
||||||
if (storedState) {
|
if (storedState) {
|
||||||
try {
|
try {
|
||||||
|
|
|
||||||
|
|
@ -139,7 +139,13 @@ export const OAuthFlowProgress = ({
|
||||||
<div className="mt-2">
|
<div className="mt-2">
|
||||||
<p className="font-medium">Resource Metadata:</p>
|
<p className="font-medium">Resource Metadata:</p>
|
||||||
<p className="text-xs text-muted-foreground">
|
<p className="text-xs text-muted-foreground">
|
||||||
From {new URL('/.well-known/oauth-protected-resource', serverUrl).href}
|
From{" "}
|
||||||
|
{
|
||||||
|
new URL(
|
||||||
|
"/.well-known/oauth-protected-resource",
|
||||||
|
serverUrl,
|
||||||
|
).href
|
||||||
|
}
|
||||||
</p>
|
</p>
|
||||||
<pre className="mt-2 p-2 bg-muted rounded-md overflow-auto max-h-[300px]">
|
<pre className="mt-2 p-2 bg-muted rounded-md overflow-auto max-h-[300px]">
|
||||||
{JSON.stringify(authState.resourceMetadata, null, 2)}
|
{JSON.stringify(authState.resourceMetadata, null, 2)}
|
||||||
|
|
@ -150,17 +156,35 @@ export const OAuthFlowProgress = ({
|
||||||
{authState.resourceMetadataError && (
|
{authState.resourceMetadataError && (
|
||||||
<div className="mt-2 p-3 border border-blue-300 bg-blue-50 rounded-md">
|
<div className="mt-2 p-3 border border-blue-300 bg-blue-50 rounded-md">
|
||||||
<p className="text-sm font-medium text-blue-700">
|
<p className="text-sm font-medium text-blue-700">
|
||||||
ℹ️ No resource metadata available from {' '}
|
ℹ️ No resource metadata available from{" "}
|
||||||
<a href={new URL('/.well-known/oauth-protected-resource', serverUrl).href} target="_blank" rel="noopener noreferrer" className="text-blue-500 hover:text-blue-700">
|
<a
|
||||||
{new URL('/.well-known/oauth-protected-resource', serverUrl).href}
|
href={
|
||||||
|
new URL(
|
||||||
|
"/.well-known/oauth-protected-resource",
|
||||||
|
serverUrl,
|
||||||
|
).href
|
||||||
|
}
|
||||||
|
target="_blank"
|
||||||
|
rel="noopener noreferrer"
|
||||||
|
className="text-blue-500 hover:text-blue-700"
|
||||||
|
>
|
||||||
|
{
|
||||||
|
new URL(
|
||||||
|
"/.well-known/oauth-protected-resource",
|
||||||
|
serverUrl,
|
||||||
|
).href
|
||||||
|
}
|
||||||
</a>
|
</a>
|
||||||
</p>
|
</p>
|
||||||
<p className="text-xs text-blue-600 mt-1">
|
<p className="text-xs text-blue-600 mt-1">
|
||||||
Resource metadata was added in the <a href="https://modelcontextprotocol.io/specification/draft/basic/authorization#2-3-1-authorization-server-location">2025-DRAFT-v2 specification update</a>
|
Resource metadata was added in the{" "}
|
||||||
|
<a href="https://modelcontextprotocol.io/specification/draft/basic/authorization#2-3-1-authorization-server-location">
|
||||||
|
2025-DRAFT-v2 specification update
|
||||||
|
</a>
|
||||||
<br />
|
<br />
|
||||||
{authState.resourceMetadataError.message}
|
{authState.resourceMetadataError.message}
|
||||||
{authState.resourceMetadataError instanceof TypeError
|
{authState.resourceMetadataError instanceof TypeError &&
|
||||||
&& " (This could indicate the endpoint doesn't exist or does not have CORS configured)"}
|
" (This could indicate the endpoint doesn't exist or does not have CORS configured)"}
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
|
@ -168,9 +192,17 @@ export const OAuthFlowProgress = ({
|
||||||
{authState.oauthMetadata && (
|
{authState.oauthMetadata && (
|
||||||
<div className="mt-2">
|
<div className="mt-2">
|
||||||
<p className="font-medium">Authorization Server Metadata:</p>
|
<p className="font-medium">Authorization Server Metadata:</p>
|
||||||
{authState.authServerUrl && <p className="text-xs text-muted-foreground">
|
{authState.authServerUrl && (
|
||||||
From {new URL('/.well-known/oauth-authorization-server', authState.authServerUrl).href}
|
<p className="text-xs text-muted-foreground">
|
||||||
</p>}
|
From{" "}
|
||||||
|
{
|
||||||
|
new URL(
|
||||||
|
"/.well-known/oauth-authorization-server",
|
||||||
|
authState.authServerUrl,
|
||||||
|
).href
|
||||||
|
}
|
||||||
|
</p>
|
||||||
|
)}
|
||||||
<pre className="mt-2 p-2 bg-muted rounded-md overflow-auto max-h-[300px]">
|
<pre className="mt-2 p-2 bg-muted rounded-md overflow-auto max-h-[300px]">
|
||||||
{JSON.stringify(authState.oauthMetadata, null, 2)}
|
{JSON.stringify(authState.oauthMetadata, null, 2)}
|
||||||
</pre>
|
</pre>
|
||||||
|
|
|
||||||
|
|
@ -40,6 +40,7 @@ jest.mock("@modelcontextprotocol/sdk/client/auth.js", () => ({
|
||||||
registerClient: jest.fn(),
|
registerClient: jest.fn(),
|
||||||
startAuthorization: jest.fn(),
|
startAuthorization: jest.fn(),
|
||||||
exchangeAuthorization: jest.fn(),
|
exchangeAuthorization: jest.fn(),
|
||||||
|
discoverOAuthProtectedResourceMetadata: jest.fn(),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
// Import the functions to get their types
|
// Import the functions to get their types
|
||||||
|
|
@ -49,6 +50,7 @@ import {
|
||||||
startAuthorization,
|
startAuthorization,
|
||||||
exchangeAuthorization,
|
exchangeAuthorization,
|
||||||
auth,
|
auth,
|
||||||
|
discoverOAuthProtectedResourceMetadata,
|
||||||
} from "@modelcontextprotocol/sdk/client/auth.js";
|
} from "@modelcontextprotocol/sdk/client/auth.js";
|
||||||
import { OAuthMetadata } from "@modelcontextprotocol/sdk/shared/auth.js";
|
import { OAuthMetadata } from "@modelcontextprotocol/sdk/shared/auth.js";
|
||||||
import { EMPTY_DEBUGGER_STATE } from "@/lib/auth-types";
|
import { EMPTY_DEBUGGER_STATE } from "@/lib/auth-types";
|
||||||
|
|
@ -67,6 +69,10 @@ const mockExchangeAuthorization = exchangeAuthorization as jest.MockedFunction<
|
||||||
typeof exchangeAuthorization
|
typeof exchangeAuthorization
|
||||||
>;
|
>;
|
||||||
const mockAuth = auth as jest.MockedFunction<typeof auth>;
|
const mockAuth = auth as jest.MockedFunction<typeof auth>;
|
||||||
|
const mockDiscoverOAuthProtectedResourceMetadata =
|
||||||
|
discoverOAuthProtectedResourceMetadata as jest.MockedFunction<
|
||||||
|
typeof discoverOAuthProtectedResourceMetadata
|
||||||
|
>;
|
||||||
|
|
||||||
const sessionStorageMock = {
|
const sessionStorageMock = {
|
||||||
getItem: jest.fn(),
|
getItem: jest.fn(),
|
||||||
|
|
@ -100,6 +106,7 @@ describe("AuthDebugger", () => {
|
||||||
|
|
||||||
mockDiscoverOAuthMetadata.mockResolvedValue(mockOAuthMetadata);
|
mockDiscoverOAuthMetadata.mockResolvedValue(mockOAuthMetadata);
|
||||||
mockRegisterClient.mockResolvedValue(mockOAuthClientInfo);
|
mockRegisterClient.mockResolvedValue(mockOAuthClientInfo);
|
||||||
|
mockDiscoverOAuthProtectedResourceMetadata.mockResolvedValue(null);
|
||||||
mockStartAuthorization.mockImplementation(async (_sseUrl, options) => {
|
mockStartAuthorization.mockImplementation(async (_sseUrl, options) => {
|
||||||
const authUrl = new URL("https://oauth.example.com/authorize");
|
const authUrl = new URL("https://oauth.example.com/authorize");
|
||||||
|
|
||||||
|
|
@ -421,4 +428,63 @@ describe("AuthDebugger", () => {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("OAuth State Persistence", () => {
|
||||||
|
it("should store auth state to sessionStorage before redirect in Quick OAuth Flow", async () => {
|
||||||
|
const updateAuthState = jest.fn();
|
||||||
|
|
||||||
|
// Mock window.location.href setter
|
||||||
|
delete (window as any).location;
|
||||||
|
window.location = { href: "" } as any;
|
||||||
|
|
||||||
|
// Setup mocks for OAuth flow
|
||||||
|
mockStartAuthorization.mockResolvedValue({
|
||||||
|
authorizationUrl: new URL(
|
||||||
|
"https://oauth.example.com/authorize?client_id=test_client_id&redirect_uri=http%3A%2F%2Flocalhost%3A3000%2Foauth%2Fcallback%2Fdebug",
|
||||||
|
),
|
||||||
|
codeVerifier: "test_verifier",
|
||||||
|
});
|
||||||
|
|
||||||
|
await act(async () => {
|
||||||
|
renderAuthDebugger({
|
||||||
|
updateAuthState,
|
||||||
|
authState: { ...defaultAuthState, loading: false },
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// Click Quick OAuth Flow
|
||||||
|
await act(async () => {
|
||||||
|
fireEvent.click(screen.getByText("Quick OAuth Flow"));
|
||||||
|
});
|
||||||
|
|
||||||
|
// Wait for the flow to reach the authorization step
|
||||||
|
await waitFor(() => {
|
||||||
|
expect(sessionStorage.setItem).toHaveBeenCalledWith(
|
||||||
|
SESSION_KEYS.AUTH_DEBUGGER_STATE,
|
||||||
|
expect.stringContaining('"oauthStep":"authorization_code"'),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
// Verify the stored state includes all the accumulated data
|
||||||
|
const storedStateCall = (
|
||||||
|
sessionStorage.setItem as jest.Mock
|
||||||
|
).mock.calls.find((call) => call[0] === SESSION_KEYS.AUTH_DEBUGGER_STATE);
|
||||||
|
|
||||||
|
expect(storedStateCall).toBeDefined();
|
||||||
|
const storedState = JSON.parse(storedStateCall![1]);
|
||||||
|
|
||||||
|
expect(storedState).toMatchObject({
|
||||||
|
oauthStep: "authorization_code",
|
||||||
|
authorizationUrl: expect.stringMatching(
|
||||||
|
/^https:\/\/oauth\.example\.com\/authorize/,
|
||||||
|
),
|
||||||
|
oauthMetadata: expect.objectContaining({
|
||||||
|
token_endpoint: "https://oauth.example.com/token",
|
||||||
|
}),
|
||||||
|
oauthClientInfo: expect.objectContaining({
|
||||||
|
client_id: "test_client_id",
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -30,7 +30,10 @@ export interface AuthDebuggerState {
|
||||||
loading: boolean;
|
loading: boolean;
|
||||||
oauthStep: OAuthStep;
|
oauthStep: OAuthStep;
|
||||||
resourceMetadata: OAuthProtectedResourceMetadata | null;
|
resourceMetadata: OAuthProtectedResourceMetadata | null;
|
||||||
resourceMetadataError: Error | { status: number; statusText: string; message: string } | null;
|
resourceMetadataError:
|
||||||
|
| Error
|
||||||
|
| { status: number; statusText: string; message: string }
|
||||||
|
| null;
|
||||||
authServerUrl: URL | null;
|
authServerUrl: URL | null;
|
||||||
oauthMetadata: OAuthMetadata | null;
|
oauthMetadata: OAuthMetadata | null;
|
||||||
oauthClientInfo: OAuthClientInformationFull | OAuthClientInformation | null;
|
oauthClientInfo: OAuthClientInformationFull | OAuthClientInformation | null;
|
||||||
|
|
@ -56,4 +59,4 @@ export const EMPTY_DEBUGGER_STATE: AuthDebuggerState = {
|
||||||
latestError: null,
|
latestError: null,
|
||||||
statusMessage: null,
|
statusMessage: null,
|
||||||
validationError: null,
|
validationError: null,
|
||||||
}
|
};
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,10 @@ import {
|
||||||
exchangeAuthorization,
|
exchangeAuthorization,
|
||||||
discoverOAuthProtectedResourceMetadata,
|
discoverOAuthProtectedResourceMetadata,
|
||||||
} from "@modelcontextprotocol/sdk/client/auth.js";
|
} from "@modelcontextprotocol/sdk/client/auth.js";
|
||||||
import { OAuthMetadataSchema, OAuthProtectedResourceMetadata } from "@modelcontextprotocol/sdk/shared/auth.js";
|
import {
|
||||||
|
OAuthMetadataSchema,
|
||||||
|
OAuthProtectedResourceMetadata,
|
||||||
|
} from "@modelcontextprotocol/sdk/shared/auth.js";
|
||||||
|
|
||||||
export interface StateMachineContext {
|
export interface StateMachineContext {
|
||||||
state: AuthDebuggerState;
|
state: AuthDebuggerState;
|
||||||
|
|
@ -30,8 +33,13 @@ export const oauthTransitions: Record<OAuthStep, StateTransition> = {
|
||||||
let resourceMetadata: OAuthProtectedResourceMetadata | null = null;
|
let resourceMetadata: OAuthProtectedResourceMetadata | null = null;
|
||||||
let resourceMetadataError: Error | null = null;
|
let resourceMetadataError: Error | null = null;
|
||||||
try {
|
try {
|
||||||
resourceMetadata = await discoverOAuthProtectedResourceMetadata(context.serverUrl);
|
resourceMetadata = await discoverOAuthProtectedResourceMetadata(
|
||||||
if (resourceMetadata && resourceMetadata.authorization_servers?.length) {
|
context.serverUrl,
|
||||||
|
);
|
||||||
|
if (
|
||||||
|
resourceMetadata &&
|
||||||
|
resourceMetadata.authorization_servers?.length
|
||||||
|
) {
|
||||||
authServerUrl = new URL(resourceMetadata.authorization_servers[0]);
|
authServerUrl = new URL(resourceMetadata.authorization_servers[0]);
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue