* Load authentication data (decrypts if encrypted)
()
| 67 | * Load authentication data (decrypts if encrypted) |
| 68 | */ |
| 69 | load(): AuthData | null { |
| 70 | try { |
| 71 | // Try encrypted file first |
| 72 | if (existsSync(this.filePath) && this.isEncryptionAvailable()) { |
| 73 | const encrypted = readFileSync(this.filePath) |
| 74 | const decrypted = safeStorage.decryptString(encrypted) |
| 75 | return JSON.parse(decrypted) |
| 76 | } |
| 77 | |
| 78 | // Fallback: try unencrypted file (for migration or when encryption unavailable) |
| 79 | const fallbackPath = this.filePath + ".json" |
| 80 | if (existsSync(fallbackPath)) { |
| 81 | const content = readFileSync(fallbackPath, "utf-8") |
| 82 | const data = JSON.parse(content) |
| 83 | |
| 84 | // Migrate to encrypted storage if now available |
| 85 | if (this.isEncryptionAvailable()) { |
| 86 | this.save(data) |
| 87 | unlinkSync(fallbackPath) // Remove unencrypted file after migration |
| 88 | } |
| 89 | |
| 90 | return data |
| 91 | } |
| 92 | |
| 93 | // Legacy: check for old auth.json file and migrate |
| 94 | const legacyPath = join(dirname(this.filePath), "auth.json") |
| 95 | if (existsSync(legacyPath)) { |
| 96 | const content = readFileSync(legacyPath, "utf-8") |
| 97 | const data = JSON.parse(content) |
| 98 | |
| 99 | // Migrate to encrypted storage |
| 100 | this.save(data) |
| 101 | unlinkSync(legacyPath) // Remove legacy unencrypted file |
| 102 | console.log("Migrated auth data from plaintext to encrypted storage") |
| 103 | |
| 104 | return data |
| 105 | } |
| 106 | |
| 107 | return null |
| 108 | } catch { |
| 109 | console.error("Failed to load auth data") |
| 110 | return null |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Clear all stored authentication data (both encrypted and fallback files) |
no test coverage detected