import type { OAuthClientInformation, OAuthTokens, OAuthMetadata, } from "@modelcontextprotocol/sdk/shared/auth.js"; import { OAuthClientInformationSchema, OAuthTokensSchema, } from "@modelcontextprotocol/sdk/shared/auth.js"; import type { OAuthStorage } from "./storage.js"; import { type createOAuthStore, type ServerOAuthState } from "./store.js"; import type { IdpSessionState, OAuthClientRegistrationKind, SaveClientInformationOptions, SaveTokensOptions, } from "./storage.js"; type OAuthStoreWithPersist = ReturnType & { persist: { hasHydrated: () => boolean; onFinishHydration: (fn: () => void) => void; }; }; /** * Concrete OAuthStorage implementation parameterized on a Zustand store. * The store carries the storage adapter (sessionStorage, file, remote HTTP, …), * so the same body works for browser, Node, and remote environments. */ export class OAuthStorageBase implements OAuthStorage { private readonly store: OAuthStoreWithPersist; private loadedPromise: Promise | undefined; constructor(store: ReturnType) { this.store = store as OAuthStoreWithPersist; } load(): Promise { if (!this.loadedPromise) { this.loadedPromise = new Promise((resolve) => { if (this.store.persist.hasHydrated()) { resolve(); return; } this.store.persist.onFinishHydration(() => resolve()); }); } return this.loadedPromise; } private async ensureLoaded(): Promise { await this.load(); } async getClientInformation( serverUrl: string, isPreregistered?: boolean, ): Promise { await this.ensureLoaded(); const state = this.store.getState().getServerState(serverUrl); const clientInfo = isPreregistered ? state.preregisteredClientInformation : state.clientInformation; if (!clientInfo) { return undefined; } return await OAuthClientInformationSchema.parseAsync(clientInfo); } getClientRegistrationKind( serverUrl: string, ): OAuthClientRegistrationKind | undefined { return this.store.getState().getServerState(serverUrl) .clientRegistrationKind; } async saveClientInformation( serverUrl: string, clientInformation: OAuthClientInformation, options: SaveClientInformationOptions, ): Promise { await this.ensureLoaded(); this.store.getState().setServerState(serverUrl, { clientInformation, clientRegistrationKind: options.registrationKind, }); } async savePreregisteredClientInformation( serverUrl: string, clientInformation: OAuthClientInformation, ): Promise { await this.ensureLoaded(); this.store.getState().setServerState(serverUrl, { preregisteredClientInformation: clientInformation, clientRegistrationKind: "static", }); } async clearClientInformation( serverUrl: string, isPreregistered?: boolean, ): Promise { await this.ensureLoaded(); const updates: Partial = {}; if (isPreregistered) { updates.preregisteredClientInformation = undefined; } else { updates.clientInformation = undefined; updates.clientRegistrationKind = undefined; } this.store.getState().setServerState(serverUrl, updates); } async getTokens(serverUrl: string): Promise { await this.ensureLoaded(); const state = this.store.getState().getServerState(serverUrl); if (!state.tokens) { return undefined; } return await OAuthTokensSchema.parseAsync(state.tokens); } async saveTokens( serverUrl: string, tokens: OAuthTokens, options?: SaveTokensOptions, ): Promise { await this.ensureLoaded(); this.store.getState().setServerState(serverUrl, { tokens, ...(options?.enterpriseManaged === true && { enterpriseManaged: true }), }); } async clearTokens(serverUrl: string): Promise { await this.ensureLoaded(); this.store.getState().setServerState(serverUrl, { tokens: undefined }); } getCodeVerifier(serverUrl: string): string | undefined { const state = this.store.getState().getServerState(serverUrl); return state.codeVerifier; } async saveCodeVerifier( serverUrl: string, codeVerifier: string, ): Promise { await this.ensureLoaded(); this.store.getState().setServerState(serverUrl, { codeVerifier }); } async clearCodeVerifier(serverUrl: string): Promise { await this.ensureLoaded(); this.store .getState() .setServerState(serverUrl, { codeVerifier: undefined }); } getScope(serverUrl: string): string | undefined { const state = this.store.getState().getServerState(serverUrl); return state.scope; } async saveScope(serverUrl: string, scope: string | undefined): Promise { await this.ensureLoaded(); this.store.getState().setServerState(serverUrl, { scope }); } async clearScope(serverUrl: string): Promise { await this.ensureLoaded(); this.store.getState().setServerState(serverUrl, { scope: undefined }); } getServerMetadata(serverUrl: string): OAuthMetadata | null { const state = this.store.getState().getServerState(serverUrl); return state.serverMetadata || null; } async saveServerMetadata( serverUrl: string, metadata: OAuthMetadata, ): Promise { await this.ensureLoaded(); this.store .getState() .setServerState(serverUrl, { serverMetadata: metadata }); } async clearServerMetadata(serverUrl: string): Promise { await this.ensureLoaded(); this.store .getState() .setServerState(serverUrl, { serverMetadata: undefined }); } async clear(serverUrl: string): Promise { await this.ensureLoaded(); this.store.getState().clearServerState(serverUrl); } async getIdpSession(issuer: string): Promise { await this.ensureLoaded(); const session = this.store.getState().getIdpSession(issuer); if ( !session.idToken && !session.refreshToken && session.idTokenExpiresAt === undefined ) { return undefined; } return session; } async saveIdpSession( issuer: string, session: Partial, ): Promise { await this.ensureLoaded(); this.store.getState().setIdpSession(issuer, session); } async clearIdpSession(issuer: string): Promise { await this.ensureLoaded(); this.store.getState().clearIdpSession(issuer); } async clearEnterpriseManagedResourceServers(): Promise { await this.ensureLoaded(); this.store.getState().clearEnterpriseManagedResourceServers(); } }