|
| 1 | +import { Injectable } from "@angular/core"; |
| 2 | +import { IpcService } from "@batch-flask/electron"; |
| 3 | +import { IpcEvent } from "common/constants"; |
| 4 | + |
| 5 | +/** |
| 6 | + * Service wrapping Electron's safeStorage API for secure credential storage. |
| 7 | + * This service is for use in the renderer process and communicates with the main process via IPC. |
| 8 | + */ |
| 9 | +@Injectable({ providedIn: "root" }) |
| 10 | +export class SafeStorageService { |
| 11 | + constructor(private ipc: IpcService) {} |
| 12 | + |
| 13 | + /** |
| 14 | + * Store a password securely using Electron's safeStorage API |
| 15 | + * @param service Service name (equivalent to keytar service) |
| 16 | + * @param account Account name (equivalent to keytar account) |
| 17 | + * @param password Password to store |
| 18 | + */ |
| 19 | + public async setPassword(service: string, account: string, password: string): Promise<void> { |
| 20 | + const key = `${service}:${account}`; |
| 21 | + return this.ipc.send(IpcEvent.safeStorage.setPassword, { key, password }); |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * Retrieve a password securely using Electron's safeStorage API |
| 26 | + * @param service Service name (equivalent to keytar service) |
| 27 | + * @param account Account name (equivalent to keytar account) |
| 28 | + * @returns Promise resolving to password or null if not found |
| 29 | + */ |
| 30 | + public async getPassword(service: string, account: string): Promise<string | null> { |
| 31 | + const key = `${service}:${account}`; |
| 32 | + return this.ipc.send(IpcEvent.safeStorage.getPassword, { key }); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Delete a password from secure storage |
| 37 | + * @param service Service name |
| 38 | + * @param account Account name |
| 39 | + */ |
| 40 | + public async deletePassword(service: string, account: string): Promise<boolean> { |
| 41 | + const key = `${service}:${account}`; |
| 42 | + return this.ipc.send(IpcEvent.safeStorage.deletePassword, { key }); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Check if encryption is available |
| 47 | + * Important on Linux where safeStorage may fall back to unencrypted storage |
| 48 | + */ |
| 49 | + public async isEncryptionAvailable(): Promise<boolean> { |
| 50 | + return this.ipc.send(IpcEvent.safeStorage.isEncryptionAvailable); |
| 51 | + } |
| 52 | +} |
0 commit comments