From 78b1f5df9938395bd14f8779aafdb3a8b042beba Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Tue, 23 Jan 2018 15:08:57 -0500 Subject: [PATCH] secure storage via ipc to main --- src/app/services/services.module.ts | 5 ++-- src/main.ts | 28 +++++++++++++++++--- src/services/desktopSecureStorage.service.ts | 25 ++++++++++++----- 3 files changed, 45 insertions(+), 13 deletions(-) diff --git a/src/app/services/services.module.ts b/src/app/services/services.module.ts index bc891155..5343a8d4 100644 --- a/src/app/services/services.module.ts +++ b/src/app/services/services.module.ts @@ -5,6 +5,7 @@ import { NgModule } from '@angular/core'; import { DesktopMessagingService } from '../../services/desktopMessaging.service'; import { DesktopPlatformUtilsService } from '../../services/desktopPlatformUtils.service'; import { DesktopStorageService } from '../../services/desktopStorage.service'; +import { DesktopSecureStorageService } from '../../services/desktopSecureStorage.service'; import { ApiService, @@ -53,9 +54,9 @@ const utilsService = new UtilsService(); const platformUtilsService = new DesktopPlatformUtilsService(); const messagingService = new DesktopMessagingService(); const storageService: StorageServiceAbstraction = new DesktopStorageService(); -const secureStorageService: StorageServiceAbstraction = storageService; //remote.getGlobal('secureStorageService'); +const secureStorageService: StorageServiceAbstraction = new DesktopSecureStorageService(); const constantsService = new ConstantsService({}, 0); -const cryptoService = new CryptoService(storageService, storageService); +const cryptoService = new CryptoService(storageService, storageService); // TODO: use secure storage const tokenService = new TokenService(storageService); const appIdService = new AppIdService(storageService); const apiService = new ApiService(tokenService, platformUtilsService, diff --git a/src/main.ts b/src/main.ts index 59bfeb1e..8d0fa1a3 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,10 +1,30 @@ -import { app, BrowserWindow, screen } from 'electron'; +import { app, BrowserWindow, screen, ipcMain } from 'electron'; import * as path from 'path'; import * as url from 'url'; +/* +import { getPassword, setPassword, deletePassword } from 'keytar'; -//import { DesktopSecureStorageService } from './services/desktopSecureStorage.service'; -//const secureStorageService = new DesktopSecureStorageService(); -//(global as any).secureStorageService = secureStorageService; +const keytarService = 'bitwarden'; +ipcMain.on('keytar', async (event: any, message: any) => { + try { + let val: string = null; + if (message.action && message.key) { + if (message.action === 'getPassword') { + val = await getPassword(keytarService, message.key); + } else if (message.action === 'setPassword' && message.value) { + await setPassword(keytarService, message.key, message.value); + } else if (message.action === 'deletePassword') { + await deletePassword(keytarService, message.key); + } + } + + event.returnValue = val; + } + catch { + event.returnValue = null; + } +}); +*/ let win: BrowserWindow; const args = process.argv.slice(1); diff --git a/src/services/desktopSecureStorage.service.ts b/src/services/desktopSecureStorage.service.ts index 8a625d8d..f858a430 100644 --- a/src/services/desktopSecureStorage.service.ts +++ b/src/services/desktopSecureStorage.service.ts @@ -1,18 +1,29 @@ -import { getPassword, setPassword, deletePassword } from 'keytar'; - -import { StorageService } from 'jslib/abstractions'; +import { ipcRenderer } from 'electron'; +import { StorageService } from 'jslib/abstractions/storage.service'; export class DesktopSecureStorageService implements StorageService { async get(key: string): Promise { - const val: string = await getPassword('bitwarden', key); - return val ? JSON.parse(val) as T : null + const val = ipcRenderer.sendSync('keytar', { + action: 'getPassword', + key: key, + }); + return Promise.resolve(val ? JSON.parse(val) as T : null); } async save(key: string, obj: any): Promise { - await setPassword('bitwarden', key, JSON.stringify(obj)); + ipcRenderer.sendSync('keytar', { + action: 'setPassword', + key: key, + value: JSON.stringify(obj), + }); + return Promise.resolve(); } async remove(key: string): Promise { - await deletePassword('bitwarden', key); + ipcRenderer.sendSync('keytar', { + action: 'deletePassword', + key: key, + }); + return Promise.resolve(); } }