|
| 1 | +const { ipcMain } = require('electron'); |
| 2 | +const crypto = require('crypto'); |
| 3 | + |
| 4 | +// Per-window AES trust map (mirrors Tauri's WindowAesTrust) |
| 5 | +// Uses webContents.id which persists across page reloads but changes when window is destroyed |
| 6 | +const windowTrustMap = new Map(); // webContentsId -> { key, iv } |
| 7 | + |
| 8 | +// Keytar for system keychain (libsecret on Linux, Keychain on macOS, Credential Vault on Windows) |
| 9 | +let keytar = null; |
| 10 | +try { |
| 11 | + keytar = require('keytar'); |
| 12 | +} catch (e) { |
| 13 | + console.warn('keytar not available, credential storage will not work'); |
| 14 | +} |
| 15 | + |
| 16 | +const PHOENIX_CRED_PREFIX = 'phcode_electron_'; |
| 17 | + |
| 18 | +function registerCredIpcHandlers() { |
| 19 | + // Trust window AES key - can only be called once per window |
| 20 | + ipcMain.handle('trust-window-aes-key', (event, key, iv) => { |
| 21 | + const webContentsId = event.sender.id; |
| 22 | + |
| 23 | + if (windowTrustMap.has(webContentsId)) { |
| 24 | + throw new Error('Trust has already been established for this window.'); |
| 25 | + } |
| 26 | + |
| 27 | + // Validate key (64 hex chars = 32 bytes for AES-256) |
| 28 | + if (!/^[0-9a-fA-F]{64}$/.test(key)) { |
| 29 | + throw new Error('Invalid AES key. Must be 64 hex characters.'); |
| 30 | + } |
| 31 | + // Validate IV (24 hex chars = 12 bytes for AES-GCM) |
| 32 | + if (!/^[0-9a-fA-F]{24}$/.test(iv)) { |
| 33 | + throw new Error('Invalid IV. Must be 24 hex characters.'); |
| 34 | + } |
| 35 | + |
| 36 | + windowTrustMap.set(webContentsId, { key, iv }); |
| 37 | + console.log(`AES trust established for webContents: ${webContentsId}`); |
| 38 | + }); |
| 39 | + |
| 40 | + // Remove trust - requires matching key/iv |
| 41 | + ipcMain.handle('remove-trust-window-aes-key', (event, key, iv) => { |
| 42 | + const webContentsId = event.sender.id; |
| 43 | + const stored = windowTrustMap.get(webContentsId); |
| 44 | + |
| 45 | + if (!stored) { |
| 46 | + throw new Error('No trust established for this window.'); |
| 47 | + } |
| 48 | + if (stored.key !== key || stored.iv !== iv) { |
| 49 | + throw new Error('Provided key and IV do not match.'); |
| 50 | + } |
| 51 | + |
| 52 | + windowTrustMap.delete(webContentsId); |
| 53 | + console.log(`AES trust removed for webContents: ${webContentsId}`); |
| 54 | + }); |
| 55 | + |
| 56 | + // Store credential in system keychain |
| 57 | + ipcMain.handle('store-credential', async (event, scopeName, secretVal) => { |
| 58 | + if (!keytar) { |
| 59 | + throw new Error('keytar module not available.'); |
| 60 | + } |
| 61 | + const service = PHOENIX_CRED_PREFIX + scopeName; |
| 62 | + await keytar.setPassword(service, process.env.USER || 'user', secretVal); |
| 63 | + }); |
| 64 | + |
| 65 | + // Get credential (encrypted with window's AES key) |
| 66 | + ipcMain.handle('get-credential', async (event, scopeName) => { |
| 67 | + if (!keytar) { |
| 68 | + throw new Error('keytar module not available.'); |
| 69 | + } |
| 70 | + |
| 71 | + const webContentsId = event.sender.id; |
| 72 | + const trustData = windowTrustMap.get(webContentsId); |
| 73 | + if (!trustData) { |
| 74 | + throw new Error('Trust needs to be established first.'); |
| 75 | + } |
| 76 | + |
| 77 | + const service = PHOENIX_CRED_PREFIX + scopeName; |
| 78 | + const credential = await keytar.getPassword(service, process.env.USER || 'user'); |
| 79 | + if (!credential) { |
| 80 | + return null; |
| 81 | + } |
| 82 | + |
| 83 | + // Encrypt with AES-256-GCM (same as Tauri) |
| 84 | + const keyBytes = Buffer.from(trustData.key, 'hex'); |
| 85 | + const ivBytes = Buffer.from(trustData.iv, 'hex'); |
| 86 | + const cipher = crypto.createCipheriv('aes-256-gcm', keyBytes, ivBytes); |
| 87 | + let encrypted = cipher.update(credential, 'utf8', 'hex'); |
| 88 | + encrypted += cipher.final('hex'); |
| 89 | + const authTag = cipher.getAuthTag().toString('hex'); |
| 90 | + |
| 91 | + return encrypted + authTag; // Return ciphertext + authTag as hex string |
| 92 | + }); |
| 93 | + |
| 94 | + // Delete credential from system keychain |
| 95 | + ipcMain.handle('delete-credential', async (event, scopeName) => { |
| 96 | + if (!keytar) { |
| 97 | + throw new Error('keytar module not available.'); |
| 98 | + } |
| 99 | + const service = PHOENIX_CRED_PREFIX + scopeName; |
| 100 | + await keytar.deletePassword(service, process.env.USER || 'user'); |
| 101 | + }); |
| 102 | +} |
| 103 | + |
| 104 | +// Clean up trust when window closes |
| 105 | +function cleanupWindowTrust(webContentsId) { |
| 106 | + if (windowTrustMap.has(webContentsId)) { |
| 107 | + windowTrustMap.delete(webContentsId); |
| 108 | + console.log(`AES trust auto-removed for closed webContents: ${webContentsId}`); |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +module.exports = { registerCredIpcHandlers, cleanupWindowTrust }; |
0 commit comments