diff --git a/proxy/app.ts b/proxy/app.ts index 57e1a04976f..3fac5b15d3d 100644 --- a/proxy/app.ts +++ b/proxy/app.ts @@ -15,6 +15,8 @@ class Proxy { run() { this.ipc.connect(); this.nativeMessage.listen(); + + this.ipc.onMessage = this.nativeMessage.send; } } diff --git a/proxy/ipc.ts b/proxy/ipc.ts index a8be8ac115d..fde39557fec 100644 --- a/proxy/ipc.ts +++ b/proxy/ipc.ts @@ -7,6 +7,7 @@ ipc.config.logger = console.warn; // Stdout is used for native messaging export default class IPC { private connected = false; + public onMessage: (message: object) => void connect() { ipc.connectTo('bitwarden', () => { @@ -24,8 +25,8 @@ export default class IPC { console.error('disconnected from world'); }); - ipc.of.bitwarden.on('message', (data: any) => { - console.error('got a message from world : ', data); + ipc.of.bitwarden.on('message', (message: any) => { + this.onMessage(message); }); /* diff --git a/proxy/nativemessage.ts b/proxy/nativemessage.ts index 0af48d9802b..f3f61633b1e 100644 --- a/proxy/nativemessage.ts +++ b/proxy/nativemessage.ts @@ -61,7 +61,6 @@ export default class NativeMessage { flushChunksQueue(); const json = JSON.parse(contentWithoutSize); - console.error(json); // Forward to desktop application this.ipc.send(json); diff --git a/src/main.ts b/src/main.ts index bc94286ef36..3d1286c2ac0 100644 --- a/src/main.ts +++ b/src/main.ts @@ -119,7 +119,7 @@ export class Main { this.biometricMain = new BiometricDarwinMain(this.storageService, this.i18nService); } - this.nativeMessagingService = new NativeMessagingService(this.logService, app.getPath('userData'), app.getAppPath()); + this.nativeMessagingService = new NativeMessagingService(this.logService, this.biometricMain, app.getPath('userData'), app.getAppPath()); } bootstrap() { diff --git a/src/services/nativeMessaging.service.ts b/src/services/nativeMessaging.service.ts index 97080e4338d..e0078997bb6 100644 --- a/src/services/nativeMessaging.service.ts +++ b/src/services/nativeMessaging.service.ts @@ -4,11 +4,12 @@ import * as path from 'path'; import * as util from 'util'; import { LogService } from 'jslib/abstractions/log.service'; +import { BiometricMain } from 'jslib/abstractions/biometric.main'; export class NativeMessagingService { private connected = false; - constructor(private logService: LogService, private userPath: string, private appPath: string) {} + constructor(private logService: LogService, private biometricMain: BiometricMain, private userPath: string, private appPath: string) {} listen() { ipc.config.id = 'bitwarden'; @@ -16,8 +17,7 @@ export class NativeMessagingService { ipc.serve(() => { ipc.server.on('message', (data: any, socket: any) => { - ipc.log('got a message : ', data); - ipc.server.emit(socket, 'message', data + ' world!'); + this.messageHandler(data, socket); }); ipc.server.on('connect', () => { @@ -42,6 +42,10 @@ export class NativeMessagingService { ipc.server.stop(); } + send(message: object, socket: any) { + ipc.server.emit(socket, 'message', message); + } + generateManifests() { const baseJson = { 'name': 'com.8bit.bitwarden', @@ -163,7 +167,7 @@ export class NativeMessagingService { const obj: any = {}; obj[location] = { 'default': { - value: path.join(this.userPath, 'browsers', jsonFile), + value: jsonFile, type: 'REG_DEFAULT', }, } @@ -175,18 +179,39 @@ export class NativeMessagingService { } private async deleteWindowsRegistry(key: string) { - const regedit = require("regedit"); + const regedit = require('regedit'); const list = util.promisify(regedit.list); const deleteKey = util.promisify(regedit.deleteKey); - this.logService.debug(`Removing registry: ${location}`) + this.logService.debug(`Removing registry: ${key}`) try { await list(key); - await deleteKey(key); + await deleteKey(key); } catch { // Do nothing } } + + private async messageHandler(message: any, socket: any) { + switch (message.command) { + case 'biometricUnlock': + if (! this.biometricMain) { + return this.send({command: 'biometricUnlock', response: 'not supported'}, socket) + } + + const response = await this.biometricMain.requestCreate(); + + if (response) { + this.send({command: 'biometricUnlock', response: 'unlocked'}, socket); + } else { + this.send({command: 'biometricUnlock', response: 'canceled'}, socket); + } + + break; + default: + console.error("UNKNOWN COMMAND") + } + } }