Skip to content

fix: store resource in session storage #632

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
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
25 changes: 25 additions & 0 deletions client/src/lib/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,35 @@ export class DebugInspectorOAuthClientProvider extends InspectorOAuthClientProvi
return JSON.parse(metadata);
}

/**
* Saves the resource URL to session storage to persist it across the redirect.
*/
saveResource(resource: URL): void {
const key = getServerSpecificKey(SESSION_KEYS.RESOURCE_URL, this.serverUrl);
sessionStorage.setItem(key, resource.toString());
}

/**
* Retrieves the persisted resource URL from session storage.
*/
getResource(): URL | undefined {
const key = getServerSpecificKey(SESSION_KEYS.RESOURCE_URL, this.serverUrl);
const urlString = sessionStorage.getItem(key);
if (!urlString) {
return undefined;
}
return new URL(urlString);
}

clear() {
super.clear();
sessionStorage.removeItem(
getServerSpecificKey(SESSION_KEYS.SERVER_METADATA, this.serverUrl),
);

// Also clear the resource URL
sessionStorage.removeItem(
getServerSpecificKey(SESSION_KEYS.RESOURCE_URL, this.serverUrl),
);
}
}
1 change: 1 addition & 0 deletions client/src/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const SESSION_KEYS = {
PREREGISTERED_CLIENT_INFORMATION: "mcp_preregistered_client_information",
SERVER_METADATA: "mcp_server_metadata",
AUTH_DEBUGGER_STATE: "mcp_auth_debugger_state",
RESOURCE_URL: "mcp_resource_url",
} as const;

// Generate server-specific session storage keys
Expand Down
10 changes: 9 additions & 1 deletion client/src/lib/oauth-state-machine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ export const oauthTransitions: Record<OAuthStep, StateTransition> = {
resourceMetadata ?? undefined,
);

// Persist the resource URL so it survives the redirect.
if (resource) {
context.provider.saveResource(resource);
}

const metadata = await discoverAuthorizationServerMetadata(authServerUrl);
if (!metadata) {
throw new Error("Failed to discover OAuth metadata");
Expand Down Expand Up @@ -171,13 +176,16 @@ export const oauthTransitions: Record<OAuthStep, StateTransition> = {
const metadata = context.provider.getServerMetadata()!;
const clientInformation = (await context.provider.clientInformation())!;

// Retrieve the resource from persistent storage, not volatile state.
const resource = context.provider.getResource();

const tokens = await exchangeAuthorization(context.serverUrl, {
metadata,
clientInformation,
authorizationCode: context.state.authorizationCode,
codeVerifier,
redirectUri: context.provider.redirectUrl,
resource: context.state.resource ?? undefined,
resource: resource ?? undefined,
});

context.provider.saveTokens(tokens);
Expand Down