diff --git a/apps/desktop/src/platform/preload.ts b/apps/desktop/src/platform/preload.ts index d7c4dd430e..4d5e1c8e0c 100644 --- a/apps/desktop/src/platform/preload.ts +++ b/apps/desktop/src/platform/preload.ts @@ -4,6 +4,27 @@ import { DeviceType, ThemeType } from "@bitwarden/common/enums"; import { isDev, isWindowsStore } from "../utils"; +const storage = { + get: (key: string): Promise => ipcRenderer.invoke("storageService", { action: "get", key }), + has: (key: string): Promise => + ipcRenderer.invoke("storageService", { action: "has", key }), + save: (key: string, obj: any): Promise => + ipcRenderer.invoke("storageService", { action: "save", key, obj }), + remove: (key: string): Promise => + ipcRenderer.invoke("storageService", { action: "remove", key }), +}; + +const passwords = { + get: (key: string, keySuffix: string): Promise => + ipcRenderer.invoke("keytar", { action: "getPassword", key, keySuffix }), + has: (key: string, keySuffix: string): Promise => + ipcRenderer.invoke("keytar", { action: "hasPassword", key, keySuffix }), + set: (key: string, keySuffix: string, value: string): Promise => + ipcRenderer.invoke("keytar", { action: "setPassword", key, keySuffix, value }), + delete: (key: string, keySuffix: string): Promise => + ipcRenderer.invoke("keytar", { action: "deletePassword", key, keySuffix }), +}; + export default { versions: { app: (): Promise => ipcRenderer.invoke("appVersion"), @@ -27,6 +48,9 @@ export default { } }); }, + + storage, + passwords, }; function deviceType(): DeviceType { diff --git a/apps/desktop/src/platform/services/electron-renderer-secure-storage.service.ts b/apps/desktop/src/platform/services/electron-renderer-secure-storage.service.ts index ce3923af5f..d0ee76b419 100644 --- a/apps/desktop/src/platform/services/electron-renderer-secure-storage.service.ts +++ b/apps/desktop/src/platform/services/electron-renderer-secure-storage.service.ts @@ -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(key: string, options?: StorageOptions): Promise { - 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 { - 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 { - 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 { - await ipcRenderer.invoke("keytar", { - action: "deletePassword", - key: key, - keySuffix: options?.keySuffix ?? "", - }); + await ipc.platform.passwords.delete(key, options?.keySuffix ?? ""); } } diff --git a/apps/desktop/src/platform/services/electron-renderer-storage.service.ts b/apps/desktop/src/platform/services/electron-renderer-storage.service.ts index 3810e10ab5..0060a11858 100644 --- a/apps/desktop/src/platform/services/electron-renderer-storage.service.ts +++ b/apps/desktop/src/platform/services/electron-renderer-storage.service.ts @@ -1,34 +1,19 @@ -import { ipcRenderer } from "electron"; - import { AbstractStorageService } from "@bitwarden/common/platform/abstractions/storage.service"; export class ElectronRendererStorageService implements AbstractStorageService { get(key: string): Promise { - return ipcRenderer.invoke("storageService", { - action: "get", - key: key, - }); + return ipc.platform.storage.get(key); } has(key: string): Promise { - return ipcRenderer.invoke("storageService", { - action: "has", - key: key, - }); + return ipc.platform.storage.has(key); } save(key: string, obj: any): Promise { - return ipcRenderer.invoke("storageService", { - action: "save", - key: key, - obj: obj, - }); + return ipc.platform.storage.save(key, obj); } remove(key: string): Promise { - return ipcRenderer.invoke("storageService", { - action: "remove", - key: key, - }); + return ipc.platform.storage.remove(key); } }