1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-12-21 16:18:28 +01:00

[PM-3685] Remove ipcRenderer from electron-renderer-storage (#6481)

* [PM-3685] Remove ipcRenderer from renderer-storage

* Break out storage and keytar into separate functions
This commit is contained in:
Daniel García 2023-10-23 12:27:49 +02:00 committed by GitHub
parent c2613af4f6
commit 55bc275f40
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 42 deletions

View File

@ -4,6 +4,27 @@ import { DeviceType, ThemeType } from "@bitwarden/common/enums";
import { isDev, isWindowsStore } from "../utils";
const storage = {
get: <T>(key: string): Promise<T> => ipcRenderer.invoke("storageService", { action: "get", key }),
has: (key: string): Promise<boolean> =>
ipcRenderer.invoke("storageService", { action: "has", key }),
save: (key: string, obj: any): Promise<void> =>
ipcRenderer.invoke("storageService", { action: "save", key, obj }),
remove: (key: string): Promise<void> =>
ipcRenderer.invoke("storageService", { action: "remove", key }),
};
const passwords = {
get: (key: string, keySuffix: string): Promise<string> =>
ipcRenderer.invoke("keytar", { action: "getPassword", key, keySuffix }),
has: (key: string, keySuffix: string): Promise<boolean> =>
ipcRenderer.invoke("keytar", { action: "hasPassword", key, keySuffix }),
set: (key: string, keySuffix: string, value: string): Promise<void> =>
ipcRenderer.invoke("keytar", { action: "setPassword", key, keySuffix, value }),
delete: (key: string, keySuffix: string): Promise<void> =>
ipcRenderer.invoke("keytar", { action: "deletePassword", key, keySuffix }),
};
export default {
versions: {
app: (): Promise<string> => ipcRenderer.invoke("appVersion"),
@ -27,6 +48,9 @@ export default {
}
});
},
storage,
passwords,
};
function deviceType(): DeviceType {

View File

@ -1,41 +1,22 @@
import { ipcRenderer } from "electron";
import { AbstractStorageService } from "@bitwarden/common/platform/abstractions/storage.service";
import { StorageOptions } from "@bitwarden/common/platform/models/domain/storage-options";
export class ElectronRendererSecureStorageService implements AbstractStorageService {
async get<T>(key: string, options?: StorageOptions): Promise<T> {
const val = await ipcRenderer.invoke("keytar", {
action: "getPassword",
key: key,
keySuffix: options?.keySuffix ?? "",
});
const val = await ipc.platform.passwords.get(key, options?.keySuffix ?? "");
return val != null ? (JSON.parse(val) as T) : null;
}
async has(key: string, options?: StorageOptions): Promise<boolean> {
const val = await ipcRenderer.invoke("keytar", {
action: "hasPassword",
key: key,
keySuffix: options?.keySuffix ?? "",
});
const val = await ipc.platform.passwords.has(key, options?.keySuffix ?? "");
return !!val;
}
async save(key: string, obj: any, options?: StorageOptions): Promise<any> {
await ipcRenderer.invoke("keytar", {
action: "setPassword",
key: key,
keySuffix: options?.keySuffix ?? "",
value: JSON.stringify(obj),
});
await ipc.platform.passwords.set(key, options?.keySuffix ?? "", JSON.stringify(obj));
}
async remove(key: string, options?: StorageOptions): Promise<any> {
await ipcRenderer.invoke("keytar", {
action: "deletePassword",
key: key,
keySuffix: options?.keySuffix ?? "",
});
await ipc.platform.passwords.delete(key, options?.keySuffix ?? "");
}
}

View File

@ -1,34 +1,19 @@
import { ipcRenderer } from "electron";
import { AbstractStorageService } from "@bitwarden/common/platform/abstractions/storage.service";
export class ElectronRendererStorageService implements AbstractStorageService {
get<T>(key: string): Promise<T> {
return ipcRenderer.invoke("storageService", {
action: "get",
key: key,
});
return ipc.platform.storage.get(key);
}
has(key: string): Promise<boolean> {
return ipcRenderer.invoke("storageService", {
action: "has",
key: key,
});
return ipc.platform.storage.has(key);
}
save(key: string, obj: any): Promise<any> {
return ipcRenderer.invoke("storageService", {
action: "save",
key: key,
obj: obj,
});
return ipc.platform.storage.save(key, obj);
}
remove(key: string): Promise<any> {
return ipcRenderer.invoke("storageService", {
action: "remove",
key: key,
});
return ipc.platform.storage.remove(key);
}
}