This commit is contained in:
Manav Rathi 2024-04-10 11:03:36 +05:30
parent 18606b2358
commit 8bcf77b7f7
No known key found for this signature in database
7 changed files with 35 additions and 26 deletions

View file

@ -28,8 +28,8 @@ import {
} from "../services/imageProcessor";
import {
clearStores,
getEncryptionKey,
setEncryptionKey,
encryptionKey,
saveEncryptionKey,
} from "../services/store";
import {
getElectronFilesFromGoogleZip,
@ -100,11 +100,11 @@ export const attachIPCHandlers = () => {
ipcMain.on("clearStores", () => clearStores());
ipcMain.handle("setEncryptionKey", (_, encryptionKey) =>
setEncryptionKey(encryptionKey),
ipcMain.handle("saveEncryptionKey", (_, encryptionKey) =>
saveEncryptionKey(encryptionKey),
);
ipcMain.handle("getEncryptionKey", () => getEncryptionKey());
ipcMain.handle("encryptionKey", () => encryptionKey());
// - App update

View file

@ -63,11 +63,11 @@ const openLogDirectory = (): Promise<void> =>
const clearStores = () => ipcRenderer.send("clearStores");
const setEncryptionKey = (encryptionKey: string): Promise<void> =>
ipcRenderer.invoke("setEncryptionKey", encryptionKey);
const encryptionKey = (): Promise<string | undefined> =>
ipcRenderer.invoke("encryptionKey");
const getEncryptionKey = (): Promise<string> =>
ipcRenderer.invoke("getEncryptionKey");
const saveEncryptionKey = (encryptionKey: string): Promise<void> =>
ipcRenderer.invoke("saveEncryptionKey", encryptionKey);
const registerForegroundEventListener = (onForeground: () => void) => {
ipcRenderer.removeAllListeners("app-in-foreground");
@ -305,8 +305,8 @@ contextBridge.exposeInMainWorld("electron", {
openDirectory,
openLogDirectory,
clearStores,
getEncryptionKey,
setEncryptionKey,
encryptionKey,
saveEncryptionKey,
registerForegroundEventListener,
// - App update

View file

@ -11,16 +11,15 @@ export const clearStores = () => {
watchStore.clear();
};
export async function setEncryptionKey(encryptionKey: string) {
export const saveEncryptionKey = async (encryptionKey: string) => {
const encryptedKey: Buffer = await safeStorage.encryptString(encryptionKey);
const b64EncryptedKey = Buffer.from(encryptedKey).toString("base64");
safeStorageStore.set("encryptionKey", b64EncryptedKey);
}
};
export async function getEncryptionKey(): Promise<string> {
export const encryptionKey = async (): Promise<string | undefined> => {
const b64EncryptedKey = safeStorageStore.get("encryptionKey");
if (b64EncryptedKey) {
if (!b64EncryptedKey) return undefined;
const keyBuffer = Buffer.from(b64EncryptedKey, "base64");
return await safeStorage.decryptString(keyBuffer);
}
}
};

View file

@ -133,9 +133,9 @@ export default function LandingPage() {
const electron = globalThis.electron;
if (!key && electron) {
try {
key = await electron.getEncryptionKey();
key = await electron.encryptionKey();
} catch (e) {
log.error("getEncryptionKey failed", e);
log.error("Failed to get encryption key from electron", e);
}
if (key) {
await saveKeyInSessionStore(

View file

@ -70,9 +70,9 @@ export default function Credentials({ appContext, appName }: PageProps) {
const electron = globalThis.electron;
if (!key && electron) {
try {
key = await electron.getEncryptionKey();
key = await electron.encryptionKey();
} catch (e) {
log.error("getEncryptionKey failed", e);
log.error("Failed to get encryption key from electron", e);
}
if (key) {
await saveKeyInSessionStore(

View file

@ -71,9 +71,19 @@ export interface Electron {
*/
clearStores: () => void;
setEncryptionKey: (encryptionKey: string) => Promise<void>;
/**
* Return the previously saved encryption key from persintent safe storage.
*
* If no such key is found, return `undefined`.
*
* @see {@link saveEncryptionKey}.
*/
encryptionKey: () => Promise<string | undefined>;
getEncryptionKey: () => Promise<string>;
/**
* Save the given {@link encryptionKey} into persistent safe storage.
*/
saveEncryptionKey: (encryptionKey: string) => Promise<void>;
registerForegroundEventListener: (onForeground: () => void) => void;

View file

@ -103,7 +103,7 @@ export const saveKeyInSessionStore = async (
setKey(keyType, sessionKeyAttributes);
const electron = globalThis.electron;
if (electron && !fromDesktop && keyType === SESSION_KEYS.ENCRYPTION_KEY) {
electron.setEncryptionKey(key);
electron.saveEncryptionKey(key);
}
};