()
| 23 | * Uses centralized electronUserData methods for path validation. |
| 24 | */ |
| 25 | export function ensureApiKey(): string { |
| 26 | try { |
| 27 | if (electronUserDataExists(API_KEY_FILENAME)) { |
| 28 | const key = electronUserDataReadFileSync(API_KEY_FILENAME).trim(); |
| 29 | if (key) { |
| 30 | state.apiKey = key; |
| 31 | logger.info('Loaded existing API key'); |
| 32 | return state.apiKey; |
| 33 | } |
| 34 | } |
| 35 | } catch (error) { |
| 36 | logger.warn('Error reading API key:', error); |
| 37 | } |
| 38 | |
| 39 | // Generate new key |
| 40 | state.apiKey = crypto.randomUUID(); |
| 41 | try { |
| 42 | electronUserDataWriteFileSync(API_KEY_FILENAME, state.apiKey, { |
| 43 | encoding: 'utf-8', |
| 44 | mode: 0o600, |
| 45 | }); |
| 46 | logger.info('Generated new API key'); |
| 47 | } catch (error) { |
| 48 | logger.error('Failed to save API key:', error); |
| 49 | } |
| 50 | return state.apiKey; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Get the current API key |
no test coverage detected