1
0
mirror of https://github.com/bitwarden/directory-connector synced 2025-12-21 02:33:14 +00:00

menu and main keytar storage service

This commit is contained in:
Kyle Spearrin
2018-04-25 15:49:10 -04:00
parent b8d3c35e34
commit a4cb908390
5 changed files with 146 additions and 32 deletions

19
.vscode/launch.json vendored Normal file
View File

@@ -0,0 +1,19 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "Debug Main Process",
"type": "node",
"request": "launch",
"cwd": "${workspaceRoot}/build",
"runtimeExecutable": "${workspaceRoot}/node_modules/.bin/electron",
"windows": {
"runtimeExecutable": "${workspaceRoot}/node_modules/.bin/electron.cmd"
},
"args": [
"."
]
}
]
}

2
jslib

Submodule jslib updated: 6e6dc422ac...42bf9b2edb

View File

@@ -3,9 +3,11 @@ import * as path from 'path';
// import { ElectronMainMessagingService } from 'jslib/electron/services/desktopMainMessaging.service'; // import { ElectronMainMessagingService } from 'jslib/electron/services/desktopMainMessaging.service';
import { MenuMain } from './main/menu.main';
import { MessagingMain } from './main/messaging.main'; import { MessagingMain } from './main/messaging.main';
import { I18nService } from './services/i18n.service'; import { I18nService } from './services/i18n.service';
import { KeytarStorageListener } from 'jslib/electron/keytarStorageListener';
import { ElectronLogService } from 'jslib/electron/services/electronLog.service'; import { ElectronLogService } from 'jslib/electron/services/electronLog.service';
import { ElectronStorageService } from 'jslib/electron/services/electronStorage.service'; import { ElectronStorageService } from 'jslib/electron/services/electronStorage.service';
import { WindowMain } from 'jslib/electron/window.main'; import { WindowMain } from 'jslib/electron/window.main';
@@ -14,9 +16,11 @@ export class Main {
logService: ElectronLogService; logService: ElectronLogService;
i18nService: I18nService; i18nService: I18nService;
storageService: ElectronStorageService; storageService: ElectronStorageService;
keytarStorageListener: KeytarStorageListener;
windowMain: WindowMain; windowMain: WindowMain;
messagingMain: MessagingMain; messagingMain: MessagingMain;
menuMain: MenuMain;
constructor() { constructor() {
// Set paths for portable builds // Set paths for portable builds
@@ -48,12 +52,17 @@ export class Main {
// this.messagingService = new DesktopMainMessagingService(this); // this.messagingService = new DesktopMainMessagingService(this);
this.windowMain = new WindowMain(this.storageService); this.windowMain = new WindowMain(this.storageService);
this.messagingMain = new MessagingMain(this); this.menuMain = new MenuMain(this);
this.messagingMain = new MessagingMain(this.windowMain);
this.keytarStorageListener = new KeytarStorageListener('Bitwarden Directory Connector');
} }
bootstrap() { bootstrap() {
this.keytarStorageListener.init();
this.windowMain.init().then(async () => { this.windowMain.init().then(async () => {
await this.i18nService.init(app.getLocale()); await this.i18nService.init(app.getLocale());
this.menuMain.init();
this.messagingMain.init(); this.messagingMain.init();
}, (e: any) => { }, (e: any) => {
// tslint:disable-next-line // tslint:disable-next-line

112
src/main/menu.main.ts Normal file
View File

@@ -0,0 +1,112 @@
import {
app,
BrowserWindow,
clipboard,
dialog,
ipcMain,
Menu,
MenuItem,
MenuItemConstructorOptions,
shell,
} from 'electron';
import { Main } from '../main';
import { BaseMenu } from 'jslib/electron/baseMenu';
import { ConstantsService } from 'jslib/services/constants.service';
export class MenuMain extends BaseMenu {
menu: Menu;
logOut: MenuItem;
constructor(private main: Main) {
super(main.i18nService, main.windowMain, 'Bitwarden Directory Connector',
() => { /* TODO: Log Out Message */ });
}
init() {
this.initProperties();
this.initContextMenu();
this.initApplicationMenu();
this.logOut = this.menu.getMenuItemById('logOut');
this.updateApplicationMenuState(false, true);
}
updateApplicationMenuState(isAuthenticated: boolean, isLocked: boolean) {
this.logOut.enabled = isAuthenticated;
}
private initApplicationMenu() {
const accountSubmenu: MenuItemConstructorOptions[] = [
this.logOutMenuItemOptions,
];
const template: MenuItemConstructorOptions[] = [
{
label: this.i18nService.t('file'),
submenu: [ this.logOutMenuItemOptions ],
},
this.editMenuItemOptions,
{
label: this.main.i18nService.t('view'),
submenu: this.viewSubMenuItemOptions,
},
this.windowMenuItemOptions,
];
const firstMenuOptions: MenuItemConstructorOptions[] = [
{ type: 'separator' },
{
label: this.i18nService.t('settings'),
id: 'settings',
click: () => { /* Something */ },
},
];
const updateMenuItem = {
label: this.i18nService.t('checkForUpdates'),
click: () => { /* Something */ },
id: 'checkForUpdates',
};
if (process.platform === 'darwin') {
const firstMenuPart: MenuItemConstructorOptions[] = [
{
label: this.i18nService.t('aboutBitwarden'),
role: 'about',
},
updateMenuItem,
];
template.unshift({
label: this.appName,
submenu: firstMenuPart.concat(firstMenuOptions, [
{ type: 'separator' },
], this.macAppMenuItemOptions),
});
// Window menu
template[template.length - 1].submenu = this.macWindowSubmenuOptions;
} else {
// File menu
template[0].submenu = (template[0].submenu as MenuItemConstructorOptions[]).concat(
firstMenuOptions);
// About menu
const aboutMenuAdditions: MenuItemConstructorOptions[] = [
{ type: 'separator' },
updateMenuItem,
];
aboutMenuAdditions.push(this.aboutMenuItemOptions);
template[template.length - 1].submenu =
(template[template.length - 1].submenu as MenuItemConstructorOptions[]).concat(aboutMenuAdditions);
}
this.menu = Menu.buildFromTemplate(template);
Menu.setApplicationMenu(this.menu);
}
}

View File

@@ -3,44 +3,18 @@ import {
ipcMain, ipcMain,
} from 'electron'; } from 'electron';
import { import { WindowMain } from 'jslib/electron/window.main';
deletePassword,
getPassword,
setPassword,
} from 'keytar';
import { Main } from '../main';
const KeytarService = 'Bitwarden';
const SyncInterval = 5 * 60 * 1000; // 5 minutes const SyncInterval = 5 * 60 * 1000; // 5 minutes
export class MessagingMain { export class MessagingMain {
private syncTimeout: NodeJS.Timer; private syncTimeout: NodeJS.Timer;
constructor(private main: Main) { } constructor(private windowMain: WindowMain) { }
init() { init() {
this.scheduleNextSync(); this.scheduleNextSync();
ipcMain.on('messagingService', async (event: any, message: any) => this.onMessage(message)); ipcMain.on('messagingService', async (event: any, message: any) => this.onMessage(message));
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;
}
});
} }
onMessage(message: any) { onMessage(message: any) {
@@ -62,11 +36,11 @@ export class MessagingMain {
} }
this.syncTimeout = global.setTimeout(() => { this.syncTimeout = global.setTimeout(() => {
if (this.main.windowMain.win == null) { if (this.windowMain.win == null) {
return; return;
} }
this.main.windowMain.win.webContents.send('messagingService', { this.windowMain.win.webContents.send('messagingService', {
command: 'checkSyncVault', command: 'checkSyncVault',
}); });
}, SyncInterval); }, SyncInterval);