/** * MCP Auth Storage Module * * Handles secure storage of OAuth credentials, tokens, client information, * and PKCE state for MCP servers. * * Standalone file storage location: $MCP_OAUTH_DIR/sha256-/tokens.json when set, * otherwise /mcp-oauth/sha256-/tokens.json */ import { createHash } from 'crypto'; import { mkdirSync, readFileSync, writeFileSync, existsSync, rmSync } from 'fs'; import { join } from 'path'; import { getAgentPath } from './agent-dir.ts'; /** OAuth token storage format */ export interface StoredTokens { accessToken: string; refreshToken?: string; expiresAt?: number; // Unix timestamp in seconds scope?: string; } /** OAuth client information from dynamic or static registration */ export interface StoredClientInfo { clientId: string; clientSecret?: string; clientIdIssuedAt?: number; clientSecretExpiresAt?: number; redirectUris?: string[]; } /** Complete auth entry for a server */ export interface AuthEntry { tokens?: StoredTokens; clientInfo?: StoredClientInfo; codeVerifier?: string; oauthState?: string; serverUrl?: string; // Track the URL these credentials are for } // Base directory for auth storage - can be overridden via env var for testing function getAuthBaseDir(): string { const override = process.env.MCP_OAUTH_DIR?.trim(); return override ? override : getAgentPath('mcp-oauth'); } /** * Get the server-specific directory path. */ function getServerDir(serverName: string): string { if (typeof serverName !== 'string') { throw new Error(`Invalid MCP server name: ${JSON.stringify(serverName)}`); } const storageKey = createHash('sha256').update(serverName, 'utf8').digest('hex'); return join(getAuthBaseDir(), `sha256-${storageKey}`); } /** * Get the tokens file path for a server. */ export function getAuthEntryFilePath(serverName: string): string { return join(getServerDir(serverName), 'tokens.json'); } /** * Ensure the server directory exists with secure permissions. */ function ensureServerDir(serverName: string): void { const dir = getServerDir(serverName); if (!existsSync(dir)) { mkdirSync(dir, { recursive: true, mode: 0o700 }); } } /** * Read the auth entry for a server from disk. * Returns undefined if file doesn't exist. */ function readAuthEntry(serverName: string): AuthEntry | undefined { const filePath = getAuthEntryFilePath(serverName); try { if (!existsSync(filePath)) { return undefined; } const data = readFileSync(filePath, 'utf-8'); return JSON.parse(data) as AuthEntry; } catch (error) { console.error(`Failed to read auth entry for ${serverName}:`, error); return undefined; } } /** * Write the auth entry for a server to disk with secure permissions. */ function writeAuthEntry(serverName: string, entry: AuthEntry): void { ensureServerDir(serverName); const filePath = getAuthEntryFilePath(serverName); writeFileSync(filePath, JSON.stringify(entry, null, 2), { mode: 0o600 }); } function removeFileAuthEntry(serverName: string): void { try { const filePath = getAuthEntryFilePath(serverName); if (existsSync(filePath)) { writeFileSync(filePath, '{}', { mode: 0o600 }); } // Try to remove the directory const dir = getServerDir(serverName); if (existsSync(dir)) { try { rmSync(dir, { recursive: true }); } catch { // Directory may not be empty, ignore } } } catch (error) { console.error(`Failed to remove auth entry for ${serverName}:`, error); } } /** Explicit synchronous credential authority. Transactions must reject async callbacks. * read returns a detached value. No method initializes or selects another backend. */ export interface McpAuthStorage { read(serverName: string): AuthEntry | undefined; write(serverName: string, entry: AuthEntry): void; remove(serverName: string): void; transaction(operation: () => T): T; } /** Standalone file backend. Construction does not read or create files. */ export function createFileMcpAuthStorage(): McpAuthStorage { return { read: readAuthEntry, write: writeAuthEntry, remove: removeFileAuthEntry, transaction(operation) { if (operation.constructor.name === "AsyncFunction") throw new Error("OAuth storage transactions must be synchronous"); const result = operation(); if (result != null && typeof (result as any).then === "function") { void Promise.resolve(result).catch(() => {}); throw new Error("OAuth storage transactions must be synchronous"); } return result; }, }; } export const fileMcpAuthStorage = createFileMcpAuthStorage(); /** Bind all credential operations to exactly one explicitly supplied authority. */ export function createMcpAuth(storage: McpAuthStorage) { /** * Get auth entry for a server. */ function getAuthEntry(serverName: string): AuthEntry | undefined { return storage.read(serverName); } /** * Get auth entry and validate it's for the correct URL. * Returns undefined if URL has changed (credentials are invalid). */ function getAuthForUrl(serverName: string, serverUrl: string): AuthEntry | undefined { const entry = getAuthEntry(serverName); if (!entry) return undefined; // If no serverUrl is stored, this is from an old version - consider it invalid if (!entry.serverUrl) return undefined; // If URL has changed, credentials are invalid if (entry.serverUrl !== serverUrl) return undefined; return entry; } /** * Save auth entry for a server. */ function saveAuthEntry(serverName: string, entry: AuthEntry, serverUrl?: string): void { // Always update serverUrl if provided if (serverUrl) { entry.serverUrl = serverUrl; } storage.write(serverName, entry); } /** * Remove auth entry for a server. * Backend errors propagate to the caller. */ function removeAuthEntry(serverName: string): void { storage.remove(serverName); } /** * Update tokens for a server. */ function updateTokens( serverName: string, tokens: StoredTokens, serverUrl?: string ): void { storage.transaction(() => { const entry = getAuthEntry(serverName) ?? {}; if (serverUrl && entry.serverUrl !== serverUrl) { delete entry.clientInfo; delete entry.codeVerifier; delete entry.oauthState; } entry.tokens = tokens; saveAuthEntry(serverName, entry, serverUrl); }); } /** * Update client info for a server. */ function updateClientInfo( serverName: string, clientInfo: StoredClientInfo, serverUrl?: string ): void { storage.transaction(() => { const entry = getAuthEntry(serverName) ?? {}; if (serverUrl && entry.serverUrl !== serverUrl) { delete entry.tokens; delete entry.codeVerifier; delete entry.oauthState; } entry.clientInfo = clientInfo; saveAuthEntry(serverName, entry, serverUrl); }); } /** * Update code verifier for a server. */ function updateCodeVerifier(serverName: string, codeVerifier: string, serverUrl?: string): void { storage.transaction(() => { const entry = getAuthEntry(serverName) ?? {}; if (serverUrl && entry.serverUrl !== serverUrl) { delete entry.tokens; delete entry.clientInfo; delete entry.oauthState; } entry.codeVerifier = codeVerifier; saveAuthEntry(serverName, entry, serverUrl); }); } /** * Clear code verifier for a server. */ function clearCodeVerifier(serverName: string): void { storage.transaction(() => { const entry = getAuthEntry(serverName); if (entry) { delete entry.codeVerifier; saveAuthEntry(serverName, entry); } }); } /** * Update OAuth state for a server. */ function updateOAuthState(serverName: string, state: string, serverUrl?: string): void { storage.transaction(() => { const entry = getAuthEntry(serverName) ?? {}; if (serverUrl && entry.serverUrl !== serverUrl) { delete entry.tokens; delete entry.clientInfo; delete entry.codeVerifier; } entry.oauthState = state; saveAuthEntry(serverName, entry, serverUrl); }); } /** * Get OAuth state for a server. */ function getOAuthState(serverName: string): string | undefined { const entry = getAuthEntry(serverName); return entry?.oauthState; } /** * Clear OAuth state for a server. */ function clearOAuthState(serverName: string): void { storage.transaction(() => { const entry = getAuthEntry(serverName); if (entry) { delete entry.oauthState; saveAuthEntry(serverName, entry); } }); } /** * Check if stored tokens are expired. * Returns null if no tokens exist, false if no expiry or not expired, true if expired. */ function isTokenExpired(serverName: string): boolean | null { const entry = getAuthEntry(serverName); if (!entry?.tokens) return null; if (!entry.tokens.expiresAt) return false; return entry.tokens.expiresAt < Date.now() / 1000; } /** * Check if a server has stored tokens. */ function hasStoredTokens(serverName: string): boolean { const entry = getAuthEntry(serverName); return !!entry?.tokens; } /** * Clear all credentials for a server. */ function clearAllCredentials(serverName: string): void { removeAuthEntry(serverName); } /** Clear dynamic registration and its tokens/PKCE/state in one credential update. */ function resetRegistration(serverName: string): void { storage.transaction(() => { const entry = getAuthEntry(serverName); if (!entry) return; delete entry.clientInfo; delete entry.tokens; delete entry.codeVerifier; delete entry.oauthState; saveAuthEntry(serverName, entry); }); } /** * Clear only client info for a server. */ function clearClientInfo(serverName: string): void { storage.transaction(() => { const entry = getAuthEntry(serverName); if (entry) { delete entry.clientInfo; saveAuthEntry(serverName, entry); } }); } /** * Clear only tokens for a server. */ function clearTokens(serverName: string): void { storage.transaction(() => { const entry = getAuthEntry(serverName); if (entry) { delete entry.tokens; saveAuthEntry(serverName, entry); } }); } return { getAuthEntry, getAuthForUrl, saveAuthEntry, removeAuthEntry, updateTokens, updateClientInfo, updateCodeVerifier, clearCodeVerifier, updateOAuthState, getOAuthState, clearOAuthState, isTokenExpired, hasStoredTokens, clearAllCredentials, clearClientInfo, clearTokens, resetRegistration }; } export type McpAuth = ReturnType; export const { getAuthEntry, getAuthForUrl, saveAuthEntry, removeAuthEntry, updateTokens, updateClientInfo, updateCodeVerifier, clearCodeVerifier, updateOAuthState, getOAuthState, clearOAuthState, isTokenExpired, hasStoredTokens, clearAllCredentials, clearClientInfo, clearTokens, resetRegistration } = createMcpAuth(fileMcpAuthStorage);