1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-11-09 09:51:02 +01:00

Cleanup code

This commit is contained in:
Hinton 2020-10-21 16:48:40 +02:00
parent 9ff0f204d8
commit 77fc0ce55b
4 changed files with 25 additions and 23 deletions

View File

@ -3,6 +3,10 @@ import IPC from './ipc';
const args = process.argv.slice(2);
// Proxy is a lightweight application which provides bi-directional communication
// between the browser extension and a running desktop application.
//
// Browser extension <-[native messaging]-> proxy <-[ipc]-> desktop
class Proxy {
private ipc: IPC;
private nativeMessage: NativeMessage;

View File

@ -17,12 +17,16 @@ export default class IPC {
'## connected to bitwarden desktop ##',
ipc.config.delay
);
// Notify browser extension, connection is established to desktop application.
this.onMessage({command: 'connected'})
});
ipc.of.bitwarden.on('disconnect', () => {
this.connected = false;
console.error('disconnected from world');
// Notify browser extension, no connection to desktop application.
this.onMessage({command: 'disconnected'})
});

View File

@ -5,8 +5,7 @@ import * as util from 'util';
import { homedir } from 'os';
import { LogService } from 'jslib/abstractions/log.service';
import { MessagingService } from 'jslib/abstractions/messaging.service';
import { ipcMain, ipcRenderer } from 'electron';
import { ipcMain } from 'electron';
import { WindowMain } from 'jslib/electron/window.main';
export class NativeMessagingMain {
@ -66,14 +65,9 @@ export class NativeMessagingMain {
const firefoxJson = {...baseJson, ...{ 'allowed_extensions': ['{446900e4-71c2-419f-a6a7-df9c091e268b}']}}
const chromeJson = {...baseJson, ...{ 'allowed_origins': ['chrome-extension://ijeheppnniijonkinoakkofcdhdfojda/']}}
if (!existsSync(path.join(this.userPath, 'browsers'))) {
fs.mkdir(path.join(this.userPath, 'browsers'))
.catch(this.logService.error)
}
switch (process.platform) {
case 'win32':
const destination = path.join(this.userPath, 'browsers')
const destination = path.join(this.userPath, 'browsers');
this.writeManifest(path.join(destination, 'firefox.json'), firefoxJson);
this.writeManifest(path.join(destination, 'chrome.json'), chromeJson);
@ -82,24 +76,20 @@ export class NativeMessagingMain {
break;
case 'darwin':
if (existsSync(`${homedir()}/Library/Application\ Support/Mozilla/`)) {
fs.mkdir(`${homedir()}/Library/Application\ Support/Mozilla/NativeMessagingHosts/`);
this.writeManifest(`${homedir()}/Library/Application\ Support/Mozilla/NativeMessagingHosts/com.8bit.bitwarden.json`, firefoxJson);
this.writeManifest(`${homedir()}/Library/Application\ Support/Mozilla/NativeMessagingHosts/com.8bit.bitwarden.json`, firefoxJson)
}
if (existsSync(`${homedir()}/Library/Application\ Support/Google/Chrome`)) {
fs.mkdir(`${homedir()}/Library/Application\ Support/Google/Chrome/NativeMessagingHosts/`);
this.writeManifest(`${homedir()}/Library/Application\ Support/Google/Chrome/NativeMessagingHosts/com.8bit.bitwarden.json`, chromeJson);
this.writeManifest(`${homedir()}/Library/Application\ Support/Google/Chrome/NativeMessagingHosts/com.8bit.bitwarden.json`, chromeJson)
}
break;
case 'linux':
if (existsSync(`${homedir()}/.mozilla/`)) {
fs.mkdir(`${homedir()}/.mozilla/native-messaging-hosts`);
this.writeManifest(`${homedir()}/.mozilla/native-messaging-hosts/com.8bit.bitwarden.json`, firefoxJson);
this.writeManifest(`${homedir()}/.mozilla/native-messaging-hosts/com.8bit.bitwarden.json`, firefoxJson)
}
if (existsSync(`${homedir()}/.config/google-chrome/`)) {
fs.mkdir(`${homedir()}/.config/google-chrome/NativeMessagingHosts/`);
this.writeManifest(`${homedir()}/.config/google-chrome/NativeMessagingHosts/com.8bit.bitwarden.json`, chromeJson);
this.writeManifest(`${homedir()}/.config/google-chrome/NativeMessagingHosts/com.8bit.bitwarden.json`, chromeJson)
}
break;
default:
@ -110,25 +100,27 @@ export class NativeMessagingMain {
removeManifests() {
switch (process.platform) {
case 'win32':
fs.unlink(path.join(this.userPath, 'browsers', 'firefox.json'));
fs.unlink(path.join(this.userPath, 'browsers', 'chrome.json'));
this.deleteWindowsRegistry('HKCU\\SOFTWARE\\Mozilla\\NativeMessagingHosts\\com.8bit.bitwarden');
this.deleteWindowsRegistry('HKCU\\SOFTWARE\\Google\\Chrome\\NativeMessagingHosts\\com.8bit.bitwarden');
break;
case 'darwin':
if (existsSync('~/Library/Application Support/Mozilla/NativeMessagingHosts/com.8bit.bitwarden.json')) {
fs.unlink('~/Library/Application Support/Mozilla/NativeMessagingHosts/com.8bit.bitwarden.json')
fs.unlink('~/Library/Application Support/Mozilla/NativeMessagingHosts/com.8bit.bitwarden.json');
}
if (existsSync('~/Library/Application Support/Google/Chrome/NativeMessagingHosts/com.8bit.bitwarden.json')) {
fs.unlink('~/Library/Application Support/Mozilla/NativeMessagingHosts/com.8bit.bitwarden.json')
fs.unlink('~/Library/Application Support/Mozilla/NativeMessagingHosts/com.8bit.bitwarden.json');
}
break;
case 'linux':
if (existsSync('~/.mozilla/native-messaging-hosts/com.8bit.bitwarden.json')) {
fs.unlink('~/.mozilla/native-messaging-hosts/com.8bit.bitwarden.json')
fs.unlink('~/.mozilla/native-messaging-hosts/com.8bit.bitwarden.json');
}
if (existsSync('~/.config/google-chrome/NativeMessagingHosts/com.8bit.bitwarden.json')) {
fs.unlink('~/.config/google-chrome/NativeMessagingHosts/com.8bit.bitwarden.json')
fs.unlink('~/.config/google-chrome/NativeMessagingHosts/com.8bit.bitwarden.json');
}
break;
default:
@ -137,6 +129,7 @@ export class NativeMessagingMain {
}
private writeManifest(destination: string, manifest: object) {
fs.mkdir(path.dirname(destination));
fs.writeFile(destination, JSON.stringify(manifest, null, 2)).catch(this.logService.error);
}

View File

@ -24,10 +24,13 @@ export class NativeMessagingService {
}
private async messageHandler(rawMessage: any) {
// Request to setup secure encryption
if (rawMessage.command == 'setupEncryption') {
const remotePublicKey = Utils.fromB64ToArray(rawMessage.publicKey).buffer;
const fingerprint = (await this.cryptoService.getFingerprint(await this.userService.getUserId(), remotePublicKey)).join(' ');
// Await confirmation that fingerprint is correct
const submitted = await Swal.fire({
title: this.i18nService.t('verifyBrowserTitle'),
html: `${this.i18nService.t('verifyBrowserDescription')}<br><br><strong>${fingerprint}</strong>`,
@ -46,7 +49,6 @@ export class NativeMessagingService {
return;
}
// TODO: Add error handler, if it fails we should invalidate the key and send a re-authenticate message to browser
const message = JSON.parse(await this.cryptoService.decryptToUtf8(rawMessage, this.sharedSecret));
if (Math.abs(message.timestamp - Date.now()) > MessageValidTimeout) {
@ -57,8 +59,7 @@ export class NativeMessagingService {
switch (message.command) {
case 'biometricUnlock':
if (! this.platformUtilService.supportsBiometric()) {
ipcRenderer.send('nativeMessagingSync', )
return this.send({command: 'biometricUnlock', response: 'not supported'})
return this.send({command: 'biometricUnlock', response: 'not supported'});
}
const response = await this.platformUtilService.authenticateBiometric();