fix: Major bug cleanup pass 1
All checks were successful
Queue Release Build / prepare (push) Successful in 19s
Deploy Web Apps / deploy (push) Successful in 8m12s
Queue Release Build / build-windows (push) Successful in 27m44s
Queue Release Build / build-linux (push) Successful in 48m1s
Queue Release Build / build-android (push) Successful in 22m7s
Queue Release Build / finalize (push) Successful in 2m42s
All checks were successful
Queue Release Build / prepare (push) Successful in 19s
Deploy Web Apps / deploy (push) Successful in 8m12s
Queue Release Build / build-windows (push) Successful in 27m44s
Queue Release Build / build-linux (push) Successful in 48m1s
Queue Release Build / build-android (push) Successful in 22m7s
Queue Release Build / finalize (push) Successful in 2m42s
This commit is contained in:
60
electron/api/provision-secret-store.ts
Normal file
60
electron/api/provision-secret-store.ts
Normal file
@@ -0,0 +1,60 @@
|
||||
import { safeStorage } from 'electron';
|
||||
import {
|
||||
mkdir,
|
||||
readFile,
|
||||
writeFile
|
||||
} from 'fs/promises';
|
||||
import path from 'path';
|
||||
import { app } from 'electron';
|
||||
|
||||
const STORAGE_DIR_NAME = 'provision-secrets';
|
||||
|
||||
function getStorageDir(): string {
|
||||
return path.join(app.getPath('userData'), STORAGE_DIR_NAME);
|
||||
}
|
||||
|
||||
function getSecretFilePath(homeUserId: string): string {
|
||||
return path.join(getStorageDir(), `${homeUserId}.bin`);
|
||||
}
|
||||
|
||||
async function ensureStorageDir(): Promise<void> {
|
||||
await mkdir(getStorageDir(), { recursive: true });
|
||||
}
|
||||
|
||||
export async function storeProvisionSecret(homeUserId: string, secret: string): Promise<boolean> {
|
||||
if (!homeUserId.trim() || !secret) {
|
||||
return false;
|
||||
}
|
||||
|
||||
await ensureStorageDir();
|
||||
|
||||
if (!safeStorage.isEncryptionAvailable()) {
|
||||
await writeFile(getSecretFilePath(homeUserId), secret, 'utf8');
|
||||
return true;
|
||||
}
|
||||
|
||||
const encrypted = safeStorage.encryptString(secret);
|
||||
|
||||
await writeFile(getSecretFilePath(homeUserId), encrypted);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
export async function getProvisionSecret(homeUserId: string): Promise<string | null> {
|
||||
if (!homeUserId.trim()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
const filePath = getSecretFilePath(homeUserId);
|
||||
const payload = await readFile(filePath);
|
||||
|
||||
if (!safeStorage.isEncryptionAvailable()) {
|
||||
return payload.toString('utf8');
|
||||
}
|
||||
|
||||
return safeStorage.decryptString(payload);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user