forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbyokStore.ts
More file actions
115 lines (102 loc) · 3.48 KB
/
Copy pathbyokStore.ts
File metadata and controls
115 lines (102 loc) · 3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Persist BYOK provider keys encrypted at rest via Electron safeStorage
// (DPAPI on Windows). Mirrors the pattern in `integrations/tokenStore.ts`:
// one JSON file under userData, per-provider base64 ciphertext, synchronous
// file I/O. Key material is NEVER logged.
//
// Storage shape: { openai?: <base64 ciphertext>, anthropic?: ..., ... }
// Each value is `safeStorage.encryptString(rawKey).toString('base64')`.
import { app, safeStorage } from 'electron'
import { existsSync, readFileSync, writeFileSync, rmSync } from 'fs'
import { join } from 'path'
import { BYOK_PROVIDERS, isByokActive, type ByokKeys, type ByokProvider } from '../../shared/byok'
/** On-disk shape: provider → base64-encoded safeStorage ciphertext. */
type StoredFile = Partial<Record<ByokProvider, string>>
/**
* Encrypted-at-rest store for the four BYOK provider keys. Reads/writes are
* synchronous, matching `tokenStore`. Construct with no args for the real
* userData path, or pass an explicit path in tests.
*/
export class ByokKeyStore {
private readonly filePath: string
constructor(filePath?: string) {
this.filePath = filePath ?? join(app.getPath('userData'), 'byok-keys.json')
}
private requireEncryption(): void {
if (!safeStorage.isEncryptionAvailable()) {
throw new Error('Secure storage is unavailable on this system')
}
}
private readFile(): StoredFile {
if (!existsSync(this.filePath)) return {}
try {
const raw = JSON.parse(readFileSync(this.filePath, 'utf8')) as StoredFile
return raw && typeof raw === 'object' ? raw : {}
} catch {
return {}
}
}
private writeFile(data: StoredFile): void {
writeFileSync(this.filePath, JSON.stringify(data), 'utf8')
}
/** Decrypt and return one provider's key, or null if unset/undecryptable. */
getKey(provider: ByokProvider): string | null {
const enc = this.readFile()[provider]
if (!enc) return null
try {
this.requireEncryption()
return safeStorage.decryptString(Buffer.from(enc, 'base64'))
} catch {
return null
}
}
/** Decrypt and return every stored provider key. */
getAllKeys(): ByokKeys {
const stored = this.readFile()
const out: ByokKeys = {}
for (const provider of BYOK_PROVIDERS) {
const enc = stored[provider]
if (!enc) continue
try {
this.requireEncryption()
out[provider] = safeStorage.decryptString(Buffer.from(enc, 'base64'))
} catch {
/* skip undecryptable entries */
}
}
return out
}
/**
* Encrypt and persist one provider's key. A blank (whitespace-only) key
* clears that provider instead of storing an empty value.
*/
setKey(provider: ByokProvider, key: string): void {
const trimmed = key.trim()
if (!trimmed) {
this.clearKey(provider)
return
}
this.requireEncryption()
const data = this.readFile()
data[provider] = safeStorage.encryptString(trimmed).toString('base64')
this.writeFile(data)
}
/** Remove one provider's key. */
clearKey(provider: ByokProvider): void {
const data = this.readFile()
if (!(provider in data)) return
delete data[provider]
this.writeFile(data)
}
/** Remove all stored keys (deletes the backing file). */
clearAll(): void {
try {
rmSync(this.filePath, { force: true })
} catch {
/* best-effort */
}
}
/** True when all four providers have a stored key (backend all-or-nothing). */
isActive(): boolean {
return isByokActive(this.getAllKeys())
}
}