import { OAuth2Client } from 'google-auth-library'; import type { Logger, OAuth2TokenStorageProvider } from '../types.js'; /** * Service Account Provider Configuration */ export interface ServiceAccountConfig { /** Path to Google Cloud service account JSON key file */ keyFilePath: string; /** OAuth scopes to request (e.g., ['https://www.googleapis.com/auth/gmail.readonly']) */ scopes: string[]; /** Logger for auth operations */ logger: Logger; } /** * ServiceAccountProvider implements OAuth2TokenStorageProvider using Google Service Accounts * with JWT-based (2-legged OAuth) authentication. * * This provider: * - Loads service account key file from disk * - Generates self-signed JWTs using RS256 algorithm * - Exchanges JWTs for access tokens at Google's token endpoint * - Does NOT store tokens (regenerates on each request) * - Provides single static identity (no account management) * * @example * ```typescript * const provider = new ServiceAccountProvider({ * keyFilePath: '/path/to/service-account-key.json', * scopes: ['https://www.googleapis.com/auth/drive.readonly'], * }); * * // Get authenticated OAuth2Client for googleapis * const auth = provider.toAuth('default'); * const drive = google.drive({ version: 'v3', auth }); * ``` */ export declare class ServiceAccountProvider implements OAuth2TokenStorageProvider { private config; private keyFilePath; private scopes; private keyData?; private cachedToken?; constructor(config: ServiceAccountConfig); /** * Load and parse service account key file from disk * Validates structure and caches for subsequent calls */ private loadKeyFile; /** * Validate service account key file structure * Ensures all required fields are present and correctly typed */ private validateKeyFile; /** * Generate signed JWT (JSON Web Token) for service account authentication * Uses RS256 algorithm with private key from key file */ private generateJWT; /** * Exchange signed JWT for access token at Google OAuth endpoint * POST to https://oauth2.googleapis.com/token with grant_type=jwt-bearer */ private exchangeJWT; /** * Get access token for Google APIs * Generates fresh JWT and exchanges for access token on each call * * Note: accountId parameter is ignored for service accounts (service account is single static identity) */ getAccessToken(_accountId?: string): Promise; /** * Get OAuth2Client with service account credentials for googleapis * This is the CRITICAL method that servers use to get authenticated API clients * * Service account ONLY works with accountId='service-account' (single static identity) * @param accountId - Account identifier (must be 'service-account' or undefined) * @returns OAuth2Client instance with access token credentials set */ toAuth(accountId?: string): OAuth2Client; /** * Get service account email address * Used for account registration and display * * Note: accountId parameter is ignored for service accounts * @returns Service account email from key file (e.g., "service-account@project.iam.gserviceaccount.com") */ getUserEmail(_accountId?: string): Promise; /** * Create middleware wrapper for single-user authentication * This is the CRITICAL method that integrates service account auth into MCP servers * * Middleware wraps tool, resource, and prompt handlers and injects authContext into extra parameter. * Handlers receive OAuth2Client via extra.authContext.auth for API calls. * * @returns Object with withToolAuth, withResourceAuth, withPromptAuth methods * * @example * ```typescript * // Server registration * const authMiddleware = provider.authMiddleware(); * const tools = toolFactories.map(f => f()).map(authMiddleware.withToolAuth); * const resources = resourceFactories.map(f => f()).map(authMiddleware.withResourceAuth); * const prompts = promptFactories.map(f => f()).map(authMiddleware.withPromptAuth); * * // Tool handler receives auth * async function handler({ id }: In, extra: EnrichedExtra) { * // extra.authContext.auth is OAuth2Client (from middleware) * const gmail = google.gmail({ version: 'v1', auth: extra.authContext.auth }); * } * ``` */ authMiddleware(): { withToolAuth: (module: T) => T; withResourceAuth: (module: T) => T; withPromptAuth: (module: T) => T; }; }