|
| 1 | +import { app } from 'electron/main' |
| 2 | +import crypto from 'crypto' |
| 3 | +import { join } from 'path' |
| 4 | +import { existsSync, readFileSync } from 'fs' |
| 5 | +import { config } from 'dotenv' |
| 6 | + |
| 7 | +config() |
| 8 | + |
| 9 | +type Secrets = Record<string, string> |
| 10 | + |
| 11 | +interface AppConfig { |
| 12 | + secrets: Secrets |
| 13 | + meta?: { |
| 14 | + hasSecrets?: boolean |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +let configuration: AppConfig | null = null |
| 19 | +let decryptedSecrets: Secrets = {} |
| 20 | + |
| 21 | +function createDecryptionKey(): Buffer { |
| 22 | + // Must match the salt and keyMaterial from electron-builder.env.js |
| 23 | + const keyMaterial = `${app.getVersion()}-deskthing-secrets` |
| 24 | + return crypto.scryptSync(keyMaterial, 'dt-salt-v1', 32) |
| 25 | +} |
| 26 | + |
| 27 | +function decrypt(encryptedData: string): string { |
| 28 | + if (!encryptedData) return '' |
| 29 | + try { |
| 30 | + const key = createDecryptionKey() |
| 31 | + const [ivHex, encrypted] = encryptedData.split(':') |
| 32 | + const iv = Buffer.from(ivHex, 'hex') |
| 33 | + const decipher = crypto.createDecipheriv('aes-256-cbc', key, iv) |
| 34 | + let decrypted = decipher.update(encrypted, 'hex', 'utf8') |
| 35 | + decrypted += decipher.final('utf8') |
| 36 | + return decrypted |
| 37 | + } catch (error) { |
| 38 | + console.error('Failed to decrypt secret:', error) |
| 39 | + return '' |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +function addSecretsToEnv(): void { |
| 44 | + if (!configuration || !configuration.secrets) return |
| 45 | + |
| 46 | + if (!decryptedSecrets) { |
| 47 | + decryptSecrets() |
| 48 | + } |
| 49 | + |
| 50 | + for (const [key, value] of Object.entries(decryptedSecrets)) { |
| 51 | + process.env[key] = value |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +function loadConfig(): void { |
| 56 | + try { |
| 57 | + const configPath = join(process.resourcesPath, 'config.json') |
| 58 | + if (existsSync(configPath)) { |
| 59 | + const configData = readFileSync(configPath, 'utf8') |
| 60 | + configuration = JSON.parse(configData) as AppConfig |
| 61 | + decryptSecrets() |
| 62 | + addSecretsToEnv() |
| 63 | + } else { |
| 64 | + console.warn('No config file found, using empty secrets') |
| 65 | + configuration = { secrets: {} } |
| 66 | + decryptedSecrets = {} |
| 67 | + } |
| 68 | + } catch (error) { |
| 69 | + console.error('Failed to load config:', error) |
| 70 | + configuration = { secrets: {} } |
| 71 | + decryptedSecrets = {} |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +function decryptSecrets(): void { |
| 76 | + if (!configuration) return |
| 77 | + decryptedSecrets = {} |
| 78 | + for (const [key, value] of Object.entries(configuration.secrets)) { |
| 79 | + decryptedSecrets[key] = decrypt(value) |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +function getSecret(key: string): string { |
| 84 | + return decryptedSecrets[key] || '' |
| 85 | +} |
| 86 | + |
| 87 | +function hasSecret(key: string): boolean { |
| 88 | + return Boolean(decryptedSecrets[key]) |
| 89 | +} |
| 90 | + |
| 91 | +function getConfig(): AppConfig | null { |
| 92 | + return configuration |
| 93 | +} |
| 94 | + |
| 95 | +// Immediately load config on import |
| 96 | +loadConfig() |
| 97 | + |
| 98 | +export { getSecret, hasSecret, getConfig, loadConfig } |
0 commit comments