All files / src/oauth/storage file.ts

97.18% Statements 138/142
92.5% Branches 37/40
92.1% Functions 35/38
98.46% Lines 128/130

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375                          76x 76x   76x           76x 76x                         76x 43x             43x 43x 43x 43x       43x 43x 43x 43x 43x         43x 43x 43x 1x 1x 1x       43x 43x 7x 7x       7x   36x             43x 43x 43x 42x 42x   1x       1x       42x     42x   42x 42x             7x 7x 7x     6x 1x               6x 6x   4x 4x     6x 6x 6x     6x               6x 6x                   1x                 66x   65x 65x   65x                     65x 65x   65x 65x   65x                                   43x     43x 16x       43x 17x 17x 17x           42x       42x 42x           20x 20x       5x       1x       1x       2x 2x 2x       2x 2x 2x       1x         5x 5x       3x       1x       2x 2x 2x         4x 4x       3x       2x 2x 2x         4x 4x       3x       2x 2x 2x         4x 4x       1x       2x 2x 2x         1x 1x         44x 42x 42x   44x 23x 23x       44x     44x   44x       3x         4x 4x 4x   4x 4x      
/**
 * File-Based Session Storage Backend
 *
 * Persists sessions to a JSON file for survival across server restarts.
 * Suitable for single-instance deployments without external database.
 *
 * Features:
 * - Automatic save on changes (debounced)
 * - Periodic auto-save interval
 * - Atomic file writes (write to temp, then rename)
 * - Data version migration support
 */
 
import * as fs from "fs";
import * as path from "path";
import { OAuthSession, DeviceFlowState, AuthCodeFlowState, AuthorizationCode } from "../types";
import {
  SessionStorageBackend,
  SessionStorageStats,
  StorageData,
  STORAGE_DATA_VERSION,
} from "./types";
import { MemoryStorageBackend } from "./memory";
import { logger } from "../../logger";
 
export interface FileStorageOptions {
  /** Path to the storage file */
  filePath: string;
  /** Auto-save interval in milliseconds (default: 30000 = 30 seconds) */
  saveInterval?: number;
  /** Pretty print JSON for debugging (default: false) */
  prettyPrint?: boolean;
  /** Debounce delay for change-triggered saves (default: 1000ms) */
  saveDebounce?: number;
}
 
export class FileStorageBackend implements SessionStorageBackend {
  readonly type = "file" as const;
 
  private memory: MemoryStorageBackend;
  private filePath: string;
  private saveInterval: number;
  private prettyPrint: boolean;
  private saveDebounce: number;
  private saveIntervalId: ReturnType<typeof setInterval> | null = null;
  private saveDebounceId: ReturnType<typeof setTimeout> | null = null;
  private pendingSave = false;
  private initialized = false;
 
  constructor(options: FileStorageOptions) {
    // Use memory backend internally as cache, but suppress its logging
    this.memory = new MemoryStorageBackend({ silent: true });
    this.filePath = options.filePath;
    this.saveInterval = options.saveInterval ?? 30000;
    this.prettyPrint = options.prettyPrint ?? false;
    this.saveDebounce = options.saveDebounce ?? 1000;
  }
 
  async initialize(): Promise<void> {
    // Ensure directory exists
    const dir = path.dirname(this.filePath);
    let dirCreated = false;
    if (!fs.existsSync(dir)) {
      fs.mkdirSync(dir, { recursive: true });
      dirCreated = true;
      logger.info({ dir }, "Created storage directory");
    }
 
    // Load existing data if file exists
    const fileExists = fs.existsSync(this.filePath);
    if (fileExists) {
      const stats = fs.statSync(this.filePath);
      logger.info(
        { filePath: this.filePath, size: stats.size, mtime: stats.mtime.toISOString() },
        "Found existing session file"
      );
      await this.loadFromFile();
    } else {
      logger.info(
        { filePath: this.filePath },
        "No existing session file, will create on first save"
      );
    }
 
    // Verify we can write to the file
    try {
      const testPath = `${this.filePath}.test`;
      fs.writeFileSync(testPath, "test", "utf-8");
      fs.unlinkSync(testPath);
      logger.debug({ filePath: this.filePath }, "Write access verified");
    } catch (error) {
      logger.error(
        { err: error as Error, filePath: this.filePath },
        "Cannot write to storage file path - sessions will NOT persist!"
      );
      throw new Error(`File storage path not writable: ${this.filePath}`);
    }
 
    // Initialize memory backend (starts cleanup interval)
    await this.memory.initialize();
 
    // Start auto-save interval
    this.startSaveInterval();
 
    this.initialized = true;
    logger.info(
      { filePath: this.filePath, dirCreated, fileExisted: fileExists },
      "File storage backend initialized"
    );
  }
 
  private async loadFromFile(): Promise<void> {
    try {
      const content = fs.readFileSync(this.filePath, "utf-8");
      const data = JSON.parse(content) as StorageData;
 
      // Validate version
      if (data.version !== STORAGE_DATA_VERSION) {
        logger.warn(
          { fileVersion: data.version, currentVersion: STORAGE_DATA_VERSION },
          "Storage file version mismatch, migrating data"
        );
        // Future: add migration logic here
      }
 
      // Filter expired data before import
      const now = Date.now();
      const validSessions = data.sessions.filter(s => {
        // Sessions are valid for 7 days
        const maxAge = 7 * 24 * 60 * 60 * 1000;
        return s.createdAt + maxAge > now;
      });
 
      const validDeviceFlows = data.deviceFlows.filter(d => d.flow.expiresAt > now);
      const validAuthCodeFlows = data.authCodeFlows.filter(a => a.flow.expiresAt > now);
      const validAuthCodes = data.authCodes.filter(a => a.expiresAt > now);
 
      // Import into memory
      this.memory.importData({
        sessions: validSessions,
        deviceFlows: validDeviceFlows,
        authCodeFlows: validAuthCodeFlows,
        authCodes: validAuthCodes,
        mcpSessionMappings: data.mcpSessionMappings,
      });
 
      const stats = await this.memory.getStats();
      logger.info(
        {
          loadedSessions: stats.sessions,
          expiredSessions: data.sessions.length - validSessions.length,
          loadedDeviceFlows: stats.deviceFlows,
          loadedAuthCodes: stats.authCodes,
        },
        "Loaded sessions from file"
      );
    } catch (error) {
      logger.error(
        { err: error as Error, filePath: this.filePath },
        "Failed to load sessions from file"
      );
      // Start fresh on load error
    }
  }
 
  private async saveToFile(): Promise<void> {
    if (!this.initialized) return;
 
    try {
      const exportedData = this.memory.exportData();
 
      const data: StorageData = {
        version: STORAGE_DATA_VERSION,
        exportedAt: Date.now(),
        sessions: exportedData.sessions,
        deviceFlows: exportedData.deviceFlows,
        authCodeFlows: exportedData.authCodeFlows,
        authCodes: exportedData.authCodes,
        mcpSessionMappings: exportedData.mcpSessionMappings,
      };
 
      // Atomic write: write to temp file, then rename
      const tempPath = `${this.filePath}.tmp`;
      const content = this.prettyPrint ? JSON.stringify(data, null, 2) : JSON.stringify(data);
 
      fs.writeFileSync(tempPath, content, "utf-8");
      fs.renameSync(tempPath, this.filePath);
 
      logger.debug(
        {
          sessions: data.sessions.length,
          deviceFlows: data.deviceFlows.length,
          authCodes: data.authCodes.length,
        },
        "Saved sessions to file"
      );
    } catch (error) {
      logger.error(
        { err: error as Error, filePath: this.filePath },
        "Failed to save sessions to file"
      );
    }
  }
 
  private scheduleSave(): void {
    // Mark as pending
    this.pendingSave = true;
 
    // Debounce: cancel previous scheduled save
    if (this.saveDebounceId) {
      clearTimeout(this.saveDebounceId);
    }
 
    // Schedule save after debounce delay
    this.saveDebounceId = setTimeout(() => {
      Eif (this.pendingSave) {
        this.pendingSave = false;
        this.saveToFile().catch(err => logger.error({ err }, "Failed to save to file"));
      }
    }, this.saveDebounce);
  }
 
  private startSaveInterval(): void {
    this.saveIntervalId = setInterval(() => {
      this.saveToFile().catch(err => logger.error({ err }, "Failed to save to file"));
    }, this.saveInterval);
 
    Eif (this.saveIntervalId.unref) {
      this.saveIntervalId.unref();
    }
  }
 
  // Session operations - delegate to memory with save scheduling
  async createSession(session: OAuthSession): Promise<void> {
    await this.memory.createSession(session);
    this.scheduleSave();
  }
 
  async getSession(sessionId: string): Promise<OAuthSession | undefined> {
    return this.memory.getSession(sessionId);
  }
 
  async getSessionByToken(token: string): Promise<OAuthSession | undefined> {
    return this.memory.getSessionByToken(token);
  }
 
  async getSessionByRefreshToken(refreshToken: string): Promise<OAuthSession | undefined> {
    return this.memory.getSessionByRefreshToken(refreshToken);
  }
 
  async updateSession(sessionId: string, updates: Partial<OAuthSession>): Promise<boolean> {
    const result = await this.memory.updateSession(sessionId, updates);
    if (result) this.scheduleSave();
    return result;
  }
 
  async deleteSession(sessionId: string): Promise<boolean> {
    const result = await this.memory.deleteSession(sessionId);
    if (result) this.scheduleSave();
    return result;
  }
 
  async getAllSessions(): Promise<OAuthSession[]> {
    return this.memory.getAllSessions();
  }
 
  // Device flow operations
  async storeDeviceFlow(state: string, flow: DeviceFlowState): Promise<void> {
    await this.memory.storeDeviceFlow(state, flow);
    this.scheduleSave();
  }
 
  async getDeviceFlow(state: string): Promise<DeviceFlowState | undefined> {
    return this.memory.getDeviceFlow(state);
  }
 
  async getDeviceFlowByDeviceCode(deviceCode: string): Promise<DeviceFlowState | undefined> {
    return this.memory.getDeviceFlowByDeviceCode(deviceCode);
  }
 
  async deleteDeviceFlow(state: string): Promise<boolean> {
    const result = await this.memory.deleteDeviceFlow(state);
    if (result) this.scheduleSave();
    return result;
  }
 
  // Auth code flow operations
  async storeAuthCodeFlow(internalState: string, flow: AuthCodeFlowState): Promise<void> {
    await this.memory.storeAuthCodeFlow(internalState, flow);
    this.scheduleSave();
  }
 
  async getAuthCodeFlow(internalState: string): Promise<AuthCodeFlowState | undefined> {
    return this.memory.getAuthCodeFlow(internalState);
  }
 
  async deleteAuthCodeFlow(internalState: string): Promise<boolean> {
    const result = await this.memory.deleteAuthCodeFlow(internalState);
    if (result) this.scheduleSave();
    return result;
  }
 
  // Authorization code operations
  async storeAuthCode(code: AuthorizationCode): Promise<void> {
    await this.memory.storeAuthCode(code);
    this.scheduleSave();
  }
 
  async getAuthCode(code: string): Promise<AuthorizationCode | undefined> {
    return this.memory.getAuthCode(code);
  }
 
  async deleteAuthCode(code: string): Promise<boolean> {
    const result = await this.memory.deleteAuthCode(code);
    if (result) this.scheduleSave();
    return result;
  }
 
  // MCP session mapping
  async associateMcpSession(mcpSessionId: string, oauthSessionId: string): Promise<void> {
    await this.memory.associateMcpSession(mcpSessionId, oauthSessionId);
    this.scheduleSave();
  }
 
  async getSessionByMcpSessionId(mcpSessionId: string): Promise<OAuthSession | undefined> {
    return this.memory.getSessionByMcpSessionId(mcpSessionId);
  }
 
  async removeMcpSessionAssociation(mcpSessionId: string): Promise<boolean> {
    const result = await this.memory.removeMcpSessionAssociation(mcpSessionId);
    if (result) this.scheduleSave();
    return result;
  }
 
  // Cleanup
  async cleanup(): Promise<void> {
    await this.memory.cleanup();
    await this.saveToFile();
  }
 
  async close(): Promise<void> {
    // Stop intervals
    if (this.saveIntervalId) {
      clearInterval(this.saveIntervalId);
      this.saveIntervalId = null;
    }
    if (this.saveDebounceId) {
      clearTimeout(this.saveDebounceId);
      this.saveDebounceId = null;
    }
 
    // Final save
    await this.saveToFile();
 
    // Close memory backend
    await this.memory.close();
 
    logger.info("File storage backend closed");
  }
 
  async getStats(): Promise<SessionStorageStats> {
    return this.memory.getStats();
  }
 
  /** Force immediate save (for graceful shutdown) */
  async forceSave(): Promise<void> {
    Eif (this.saveDebounceId) {
      clearTimeout(this.saveDebounceId);
      this.saveDebounceId = null;
    }
    this.pendingSave = false;
    await this.saveToFile();
  }
}