From 4f71c1832c64c8a314b1594d73e2d454536877cf Mon Sep 17 00:00:00 2001 From: addison Date: Mon, 1 Nov 2021 12:35:27 -0400 Subject: [PATCH] [refactor] Extract, rename, and expand StorageServiceOptions * Pulled StorageServiceOptions into its own file * Renamed StorageServiceOptions to StorageOptions * Pulled KeySuffixOpptions into its own file * Converted KeySuffixOptions into an enum from a union type --- common/src/abstractions/storage.service.ts | 15 ++++++--------- common/src/enums/keySuffixOptions.ts | 4 ++++ common/src/enums/storageLocation.ts | 5 +++++ common/src/models/domain/storageOptions.ts | 9 +++++++++ .../electronRendererSecureStorage.service.ts | 12 +++++++----- 5 files changed, 31 insertions(+), 14 deletions(-) create mode 100644 common/src/enums/keySuffixOptions.ts create mode 100644 common/src/enums/storageLocation.ts create mode 100644 common/src/models/domain/storageOptions.ts diff --git a/common/src/abstractions/storage.service.ts b/common/src/abstractions/storage.service.ts index cfedb2d0..6599267a 100644 --- a/common/src/abstractions/storage.service.ts +++ b/common/src/abstractions/storage.service.ts @@ -1,12 +1,9 @@ +import { StorageOptions } from '../models/domain/storageOptions'; + export abstract class StorageService { - get: (key: string, options?: StorageServiceOptions) => Promise; - has: (key: string, options?: StorageServiceOptions) => Promise; - save: (key: string, obj: any, options?: StorageServiceOptions) => Promise; - remove: (key: string, options?: StorageServiceOptions) => Promise; + get: (key: string, options?: StorageOptions) => Promise; + has: (key: string, options?: StorageOptions) => Promise; + save: (key: string, obj: any, options?: StorageOptions) => Promise; + remove: (key: string, options?: StorageOptions) => Promise; } -export interface StorageServiceOptions { - keySuffix: KeySuffixOptions; -} - -export type KeySuffixOptions = 'auto' | 'biometric'; diff --git a/common/src/enums/keySuffixOptions.ts b/common/src/enums/keySuffixOptions.ts new file mode 100644 index 00000000..40d3e58c --- /dev/null +++ b/common/src/enums/keySuffixOptions.ts @@ -0,0 +1,4 @@ +export enum KeySuffixOptions { + Auto = 'auto', + Biometric = 'biometric', +} diff --git a/common/src/enums/storageLocation.ts b/common/src/enums/storageLocation.ts new file mode 100644 index 00000000..d45e5fa6 --- /dev/null +++ b/common/src/enums/storageLocation.ts @@ -0,0 +1,5 @@ +export enum StorageLocation { + Both = 'both', + Disk = 'disk', + Memory = 'memory', +} diff --git a/common/src/models/domain/storageOptions.ts b/common/src/models/domain/storageOptions.ts new file mode 100644 index 00000000..d4537a12 --- /dev/null +++ b/common/src/models/domain/storageOptions.ts @@ -0,0 +1,9 @@ +import { KeySuffixOptions } from '../../enums/keySuffixOptions'; +import { StorageLocation } from '../../enums/storageLocation'; + +export type StorageOptions = { + keySuffix?: KeySuffixOptions; + storageLocation?: StorageLocation; + useSecureStorage?: boolean; + userId?: string; +}; diff --git a/electron/src/services/electronRendererSecureStorage.service.ts b/electron/src/services/electronRendererSecureStorage.service.ts index fb718058..13a7459a 100644 --- a/electron/src/services/electronRendererSecureStorage.service.ts +++ b/electron/src/services/electronRendererSecureStorage.service.ts @@ -1,9 +1,11 @@ import { ipcRenderer } from 'electron'; -import { StorageService, StorageServiceOptions } from 'jslib-common/abstractions/storage.service'; +import { StorageService } from 'jslib-common/abstractions/storage.service'; + +import { StorageOptions } from 'jslib-common/models/domain/storageOptions'; export class ElectronRendererSecureStorageService implements StorageService { - async get(key: string, options?: StorageServiceOptions): Promise { + async get(key: string, options?: StorageOptions): Promise { const val = ipcRenderer.sendSync('keytar', { action: 'getPassword', key: key, @@ -12,7 +14,7 @@ export class ElectronRendererSecureStorageService implements StorageService { return Promise.resolve(val != null ? JSON.parse(val) as T : null); } - async has(key: string, options?: StorageServiceOptions): Promise { + async has(key: string, options?: StorageOptions): Promise { const val = ipcRenderer.sendSync('keytar', { action: 'hasPassword', key: key, @@ -21,7 +23,7 @@ export class ElectronRendererSecureStorageService implements StorageService { return Promise.resolve(!!val); } - async save(key: string, obj: any, options?: StorageServiceOptions): Promise { + async save(key: string, obj: any, options?: StorageOptions): Promise { ipcRenderer.sendSync('keytar', { action: 'setPassword', key: key, @@ -31,7 +33,7 @@ export class ElectronRendererSecureStorageService implements StorageService { return Promise.resolve(); } - async remove(key: string, options?: StorageServiceOptions): Promise { + async remove(key: string, options?: StorageOptions): Promise { ipcRenderer.sendSync('keytar', { action: 'deletePassword', key: key,