bitwarden-estensione-browser/src/main.ts

172 lines
7.0 KiB
TypeScript
Raw Normal View History

import { app, globalShortcut } from 'electron';
2018-02-15 21:27:56 +01:00
import * as path from 'path';
2018-01-16 21:58:17 +01:00
2018-02-14 05:38:18 +01:00
import { I18nService } from './services/i18n.service';
2018-02-08 19:10:13 +01:00
import { MenuMain } from './main/menu.main';
import { MessagingMain } from './main/messaging.main';
2018-02-11 05:24:22 +01:00
import { PowerMonitorMain } from './main/powerMonitor.main';
2018-04-24 22:30:47 +02:00
2018-04-25 05:53:20 +02:00
import { ConstantsService } from 'jslib/services/constants.service';
import { BiometricMain } from 'jslib/abstractions/biometric.main';
2019-02-02 18:27:06 +01:00
import { ElectronConstants } from 'jslib/electron/electronConstants';
import { KeytarStorageListener } from 'jslib/electron/keytarStorageListener';
2018-04-24 22:30:47 +02:00
import { ElectronLogService } from 'jslib/electron/services/electronLog.service';
2018-04-26 21:44:07 +02:00
import { ElectronMainMessagingService } from 'jslib/electron/services/electronMainMessaging.service';
2019-03-12 03:37:13 +01:00
import { ElectronStorageService } from 'jslib/electron/services/electronStorage.service';
2018-05-08 18:05:05 +02:00
import { TrayMain } from 'jslib/electron/tray.main';
2018-05-04 20:16:28 +02:00
import { UpdaterMain } from 'jslib/electron/updater.main';
2018-04-24 22:30:47 +02:00
import { WindowMain } from 'jslib/electron/window.main';
2018-05-04 20:16:28 +02:00
2018-02-14 05:38:18 +01:00
export class Main {
2018-04-24 22:30:47 +02:00
logService: ElectronLogService;
2018-02-14 05:38:18 +01:00
i18nService: I18nService;
2019-03-12 03:37:13 +01:00
storageService: ElectronStorageService;
2018-04-26 21:44:07 +02:00
messagingService: ElectronMainMessagingService;
keytarStorageListener: KeytarStorageListener;
2018-02-14 05:38:18 +01:00
windowMain: WindowMain;
messagingMain: MessagingMain;
updaterMain: UpdaterMain;
menuMain: MenuMain;
powerMonitorMain: PowerMonitorMain;
trayMain: TrayMain;
biometricMain: BiometricMain;
2018-02-14 05:38:18 +01:00
constructor() {
2018-02-15 21:27:56 +01:00
// Set paths for portable builds
let appDataPath = null;
if (process.env.BITWARDEN_APPDATA_DIR != null) {
appDataPath = process.env.BITWARDEN_APPDATA_DIR;
2018-02-23 23:11:18 +01:00
} else if (process.platform === 'win32' && process.env.PORTABLE_EXECUTABLE_DIR != null) {
appDataPath = path.join(process.env.PORTABLE_EXECUTABLE_DIR, 'bitwarden-appdata');
2018-02-23 23:11:18 +01:00
} else if (process.platform === 'linux' && process.env.SNAP_USER_DATA != null) {
2018-02-23 23:16:00 +01:00
appDataPath = path.join(process.env.SNAP_USER_DATA, 'appdata');
}
app.on('ready', () => {
/*
globalShortcut.register('CommandOrControl+Shift+L', async () => {
if (this.windowMain.win === null) {
await this.windowMain.createWindow();
}
this.messagingService.send('focusSearch');
this.windowMain.win.show();
});
globalShortcut.register('CommandOrControl+Shift+G', async () => {
if (this.windowMain.win === null) {
await this.windowMain.createWindow();
}
this.messagingService.send('openPasswordGenerator');
this.windowMain.win.show();
});
2020-03-12 14:50:19 +01:00
*/
});
if (appDataPath != null) {
2018-02-15 21:27:56 +01:00
app.setPath('userData', appDataPath);
}
2018-02-25 15:51:21 +01:00
app.setPath('logs', path.join(app.getPath('userData'), 'logs'));
2018-02-15 21:27:56 +01:00
2018-02-14 05:38:18 +01:00
const args = process.argv.slice(1);
const watch = args.some((val) => val === '--watch');
if (watch) {
// tslint:disable-next-line
require('electron-reload')(__dirname, {});
}
2018-04-24 22:30:47 +02:00
this.logService = new ElectronLogService(null, app.getPath('userData'));
2018-02-14 05:38:18 +01:00
this.i18nService = new I18nService('en', './locales/');
2018-04-26 22:17:14 +02:00
const storageDefaults: any = {};
// Default vault timeout to "on restart", and action to "lock"
storageDefaults[ConstantsService.vaultTimeoutKey] = -1;
storageDefaults[ConstantsService.vaultTimeoutActionKey] = 'lock';
2019-03-12 03:37:13 +01:00
this.storageService = new ElectronStorageService(app.getPath('userData'), storageDefaults);
2018-02-14 05:38:18 +01:00
merge sso feature branch (#523) * Update jslib (101c568 -> 14b01f2) (#506) * Update jslib (14b01f2 -> 1513b25) (#510) * [jslib] Update (1513b25 -> 7c3a9d6) (#516) * update jslib (1513b25 -> 7c3a9d6) * Updated call to constructor super * [SSO] Added SSO flows & functionality (#513) * update jslib * bump version * Added sso button (wip) * Added sso & change password // Added modules/routes // Added strings for localization * Added password strength comp // reverted login route * Updated sso component to send client id // added routing for sso // added crypto function to services module provider list * Added deep linking * First round of UI updates // Added sso browser launching // Added missing strings * Updated UI and added missing strings * Removed extra change password style * Let constructor for WindowMain handle default width/height * Prepared for jslib update * Update jslib (1513b25 -> 7c3a9d6) * Update login super * Added params for launchSsoBrowser function * Update jslib (7c3a9d6 -> 4203937) * Added missing strings, removed unnecessary class param * Upgrade TypeScript (#517) * Updated password score // Update styles * Removed password-strength component files * Cleaned up module class // Fixed UL/LI formatting issues * Use exisiting loading string // removed new string * Update jslib (4203937 -> 9957125) * Updated class to perform new submit actions * Upgrade Angular (#520) * di resolution for CryptoFunctionServiceAbstraction * Update jslib (9957125 -> 5d874d0) (#521) * Updated change password flow to match web * Updated callout style Co-authored-by: Kyle Spearrin <kyle.spearrin@gmail.com> Co-authored-by: Oscar Hinton <hinton.oscar@gmail.com> Co-authored-by: Vincent Salucci <26154748+vincentsalucci@users.noreply.github.com> Co-authored-by: Oscar Hinton <hinton.oscar@gmail.com>
2020-08-21 15:50:36 +02:00
this.windowMain = new WindowMain(this.storageService, true, undefined, undefined,
(arg) => this.processDeepLink(arg));
this.messagingMain = new MessagingMain(this, this.storageService);
2018-05-04 20:16:28 +02:00
this.updaterMain = new UpdaterMain(this.i18nService, this.windowMain, 'desktop', () => {
this.menuMain.updateMenuItem.enabled = false;
}, () => {
this.menuMain.updateMenuItem.enabled = true;
}, () => {
this.menuMain.updateMenuItem.label = this.i18nService.t('restartToUpdate');
2018-11-27 14:32:04 +01:00
}, 'bitwarden');
2018-02-14 05:38:18 +01:00
this.menuMain = new MenuMain(this);
this.powerMonitorMain = new PowerMonitorMain(this);
2018-05-08 18:05:05 +02:00
this.trayMain = new TrayMain(this.windowMain, this.i18nService, this.storageService);
2018-04-26 21:44:07 +02:00
this.messagingService = new ElectronMainMessagingService(this.windowMain, (message) => {
this.messagingMain.onMessage(message);
});
this.keytarStorageListener = new KeytarStorageListener('Bitwarden');
if (process.platform === 'win32') {
const BiometricWindowsMain = require('jslib/electron/biometric.windows.main').default;
this.biometricMain = new BiometricWindowsMain(this.storageService, this.i18nService);
} else if (process.platform === 'darwin') {
const BiometricDarwinMain = require('jslib/electron/biometric.darwin.main').default;
this.biometricMain = new BiometricDarwinMain(this.storageService, this.i18nService);
}
2018-02-14 05:38:18 +01:00
}
bootstrap() {
this.keytarStorageListener.init();
2018-04-25 05:53:20 +02:00
this.windowMain.init().then(async () => {
const locale = await this.storageService.get<string>(ConstantsService.localeKey);
await this.i18nService.init(locale != null ? locale : app.getLocale());
this.messagingMain.init();
this.menuMain.init();
2018-05-08 18:05:05 +02:00
await this.trayMain.init('Bitwarden', [{
2018-05-05 06:29:02 +02:00
label: this.i18nService.t('lockNow'),
enabled: false,
id: 'lockNow',
click: () => this.messagingService.send('lockVault'),
}]);
2019-02-02 18:27:06 +01:00
if (await this.storageService.get<boolean>(ElectronConstants.enableStartToTrayKey)) {
this.trayMain.hideToTray();
}
2019-03-12 04:18:04 +01:00
this.powerMonitorMain.init();
await this.updaterMain.init();
if (this.biometricMain != null) {
await this.biometricMain.init();
}
merge sso feature branch (#523) * Update jslib (101c568 -> 14b01f2) (#506) * Update jslib (14b01f2 -> 1513b25) (#510) * [jslib] Update (1513b25 -> 7c3a9d6) (#516) * update jslib (1513b25 -> 7c3a9d6) * Updated call to constructor super * [SSO] Added SSO flows & functionality (#513) * update jslib * bump version * Added sso button (wip) * Added sso & change password // Added modules/routes // Added strings for localization * Added password strength comp // reverted login route * Updated sso component to send client id // added routing for sso // added crypto function to services module provider list * Added deep linking * First round of UI updates // Added sso browser launching // Added missing strings * Updated UI and added missing strings * Removed extra change password style * Let constructor for WindowMain handle default width/height * Prepared for jslib update * Update jslib (1513b25 -> 7c3a9d6) * Update login super * Added params for launchSsoBrowser function * Update jslib (7c3a9d6 -> 4203937) * Added missing strings, removed unnecessary class param * Upgrade TypeScript (#517) * Updated password score // Update styles * Removed password-strength component files * Cleaned up module class // Fixed UL/LI formatting issues * Use exisiting loading string // removed new string * Update jslib (4203937 -> 9957125) * Updated class to perform new submit actions * Upgrade Angular (#520) * di resolution for CryptoFunctionServiceAbstraction * Update jslib (9957125 -> 5d874d0) (#521) * Updated change password flow to match web * Updated callout style Co-authored-by: Kyle Spearrin <kyle.spearrin@gmail.com> Co-authored-by: Oscar Hinton <hinton.oscar@gmail.com> Co-authored-by: Vincent Salucci <26154748+vincentsalucci@users.noreply.github.com> Co-authored-by: Oscar Hinton <hinton.oscar@gmail.com>
2020-08-21 15:50:36 +02:00
if (!app.isDefaultProtocolClient('bitwarden')) {
app.setAsDefaultProtocolClient('bitwarden');
}
// Process protocol for macOS
app.on('open-url', (event, url) => {
event.preventDefault();
this.processDeepLink([url]);
});
2018-04-25 05:53:20 +02:00
}, (e: any) => {
// tslint:disable-next-line
console.error(e);
2018-02-14 05:38:18 +01:00
});
}
merge sso feature branch (#523) * Update jslib (101c568 -> 14b01f2) (#506) * Update jslib (14b01f2 -> 1513b25) (#510) * [jslib] Update (1513b25 -> 7c3a9d6) (#516) * update jslib (1513b25 -> 7c3a9d6) * Updated call to constructor super * [SSO] Added SSO flows & functionality (#513) * update jslib * bump version * Added sso button (wip) * Added sso & change password // Added modules/routes // Added strings for localization * Added password strength comp // reverted login route * Updated sso component to send client id // added routing for sso // added crypto function to services module provider list * Added deep linking * First round of UI updates // Added sso browser launching // Added missing strings * Updated UI and added missing strings * Removed extra change password style * Let constructor for WindowMain handle default width/height * Prepared for jslib update * Update jslib (1513b25 -> 7c3a9d6) * Update login super * Added params for launchSsoBrowser function * Update jslib (7c3a9d6 -> 4203937) * Added missing strings, removed unnecessary class param * Upgrade TypeScript (#517) * Updated password score // Update styles * Removed password-strength component files * Cleaned up module class // Fixed UL/LI formatting issues * Use exisiting loading string // removed new string * Update jslib (4203937 -> 9957125) * Updated class to perform new submit actions * Upgrade Angular (#520) * di resolution for CryptoFunctionServiceAbstraction * Update jslib (9957125 -> 5d874d0) (#521) * Updated change password flow to match web * Updated callout style Co-authored-by: Kyle Spearrin <kyle.spearrin@gmail.com> Co-authored-by: Oscar Hinton <hinton.oscar@gmail.com> Co-authored-by: Vincent Salucci <26154748+vincentsalucci@users.noreply.github.com> Co-authored-by: Oscar Hinton <hinton.oscar@gmail.com>
2020-08-21 15:50:36 +02:00
private processDeepLink(argv: string[]): void {
argv.filter((s) => s.indexOf('bitwarden://') === 0).forEach((s) => {
const url = new URL(s);
const code = url.searchParams.get('code');
const receivedState = url.searchParams.get('state');
if (code != null && receivedState != null) {
this.messagingService.send('ssoCallback', { code: code, state: receivedState });
}
});
}
2018-01-16 23:30:57 +01:00
}
2018-02-14 05:38:18 +01:00
const main = new Main();
main.bootstrap();