secure storage via ipc to main

This commit is contained in:
Kyle Spearrin 2018-01-23 15:08:57 -05:00
parent 95e6d0b92d
commit 78b1f5df99
3 changed files with 45 additions and 13 deletions

View File

@ -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,

View File

@ -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);

View File

@ -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<T>(key: string): Promise<T> {
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<any> {
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<any> {
await deletePassword('bitwarden', key);
ipcRenderer.sendSync('keytar', {
action: 'deletePassword',
key: key,
});
return Promise.resolve();
}
}