1
0
mirror of https://github.com/bitwarden/desktop.git synced 2024-11-28 12:35:40 +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); 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 { class Proxy {
private ipc: IPC; private ipc: IPC;
private nativeMessage: NativeMessage; private nativeMessage: NativeMessage;

View File

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

View File

@ -5,8 +5,7 @@ import * as util from 'util';
import { homedir } from 'os'; import { homedir } from 'os';
import { LogService } from 'jslib/abstractions/log.service'; import { LogService } from 'jslib/abstractions/log.service';
import { MessagingService } from 'jslib/abstractions/messaging.service'; import { ipcMain } from 'electron';
import { ipcMain, ipcRenderer } from 'electron';
import { WindowMain } from 'jslib/electron/window.main'; import { WindowMain } from 'jslib/electron/window.main';
export class NativeMessagingMain { export class NativeMessagingMain {
@ -66,14 +65,9 @@ export class NativeMessagingMain {
const firefoxJson = {...baseJson, ...{ 'allowed_extensions': ['{446900e4-71c2-419f-a6a7-df9c091e268b}']}} const firefoxJson = {...baseJson, ...{ 'allowed_extensions': ['{446900e4-71c2-419f-a6a7-df9c091e268b}']}}
const chromeJson = {...baseJson, ...{ 'allowed_origins': ['chrome-extension://ijeheppnniijonkinoakkofcdhdfojda/']}} 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) { switch (process.platform) {
case 'win32': 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, 'firefox.json'), firefoxJson);
this.writeManifest(path.join(destination, 'chrome.json'), chromeJson); this.writeManifest(path.join(destination, 'chrome.json'), chromeJson);
@ -82,24 +76,20 @@ export class NativeMessagingMain {
break; break;
case 'darwin': case 'darwin':
if (existsSync(`${homedir()}/Library/Application\ Support/Mozilla/`)) { 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`)) { 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; break;
case 'linux': case 'linux':
if (existsSync(`${homedir()}/.mozilla/`)) { 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/`)) { 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; break;
default: default:
@ -110,25 +100,27 @@ export class NativeMessagingMain {
removeManifests() { removeManifests() {
switch (process.platform) { switch (process.platform) {
case 'win32': 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\\Mozilla\\NativeMessagingHosts\\com.8bit.bitwarden');
this.deleteWindowsRegistry('HKCU\\SOFTWARE\\Google\\Chrome\\NativeMessagingHosts\\com.8bit.bitwarden'); this.deleteWindowsRegistry('HKCU\\SOFTWARE\\Google\\Chrome\\NativeMessagingHosts\\com.8bit.bitwarden');
break; break;
case 'darwin': case 'darwin':
if (existsSync('~/Library/Application Support/Mozilla/NativeMessagingHosts/com.8bit.bitwarden.json')) { 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')) { 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; break;
case 'linux': case 'linux':
if (existsSync('~/.mozilla/native-messaging-hosts/com.8bit.bitwarden.json')) { 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')) { 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; break;
default: default:
@ -137,6 +129,7 @@ export class NativeMessagingMain {
} }
private writeManifest(destination: string, manifest: object) { private writeManifest(destination: string, manifest: object) {
fs.mkdir(path.dirname(destination));
fs.writeFile(destination, JSON.stringify(manifest, null, 2)).catch(this.logService.error); 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) { private async messageHandler(rawMessage: any) {
// Request to setup secure encryption
if (rawMessage.command == 'setupEncryption') { if (rawMessage.command == 'setupEncryption') {
const remotePublicKey = Utils.fromB64ToArray(rawMessage.publicKey).buffer; const remotePublicKey = Utils.fromB64ToArray(rawMessage.publicKey).buffer;
const fingerprint = (await this.cryptoService.getFingerprint(await this.userService.getUserId(), remotePublicKey)).join(' '); const fingerprint = (await this.cryptoService.getFingerprint(await this.userService.getUserId(), remotePublicKey)).join(' ');
// Await confirmation that fingerprint is correct
const submitted = await Swal.fire({ const submitted = await Swal.fire({
title: this.i18nService.t('verifyBrowserTitle'), title: this.i18nService.t('verifyBrowserTitle'),
html: `${this.i18nService.t('verifyBrowserDescription')}<br><br><strong>${fingerprint}</strong>`, html: `${this.i18nService.t('verifyBrowserDescription')}<br><br><strong>${fingerprint}</strong>`,
@ -46,7 +49,6 @@ export class NativeMessagingService {
return; 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)); const message = JSON.parse(await this.cryptoService.decryptToUtf8(rawMessage, this.sharedSecret));
if (Math.abs(message.timestamp - Date.now()) > MessageValidTimeout) { if (Math.abs(message.timestamp - Date.now()) > MessageValidTimeout) {
@ -57,8 +59,7 @@ export class NativeMessagingService {
switch (message.command) { switch (message.command) {
case 'biometricUnlock': case 'biometricUnlock':
if (! this.platformUtilService.supportsBiometric()) { 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(); const response = await this.platformUtilService.authenticateBiometric();