1
0
mirror of https://github.com/bitwarden/browser.git synced 2025-03-02 03:41:09 +01:00
bitwarden-browser/src/services/nativeMessaging.service.ts

52 lines
1.9 KiB
TypeScript
Raw Normal View History

import { ipcRenderer } from 'electron';
2020-10-05 20:05:48 +02:00
import { CryptoService } from 'jslib/abstractions/crypto.service';
import { PlatformUtilsService } from 'jslib/abstractions/platformUtils.service';
import { LogService } from 'jslib/abstractions/log.service';
const MessageValidTimeout = 10 * 1000;
export class NativeMessagingService {
constructor(private cryptoService: CryptoService, private platformUtilService: PlatformUtilsService, private logService: LogService) {
ipcRenderer.on('nativeMessaging', async (event: any, message: any) => {
this.messageHandler(message);
});
}
private async messageHandler(rawMessage: any) {
const message = JSON.parse(await this.cryptoService.decryptToUtf8(rawMessage));
2020-10-05 20:05:48 +02:00
if (Math.abs(message.timestamp - Date.now()) > MessageValidTimeout) {
this.logService.error('NativeMessage is to old, ignoring.');
return;
}
switch (message.command) {
case 'biometricUnlock':
if (! this.platformUtilService.supportsBiometric()) {
ipcRenderer.send('nativeMessagingSync', )
return this.send({command: 'biometricUnlock', response: 'not supported'})
}
const response = await this.platformUtilService.authenticateBiometric();
if (response) {
this.send({command: 'biometricUnlock', response: 'unlocked'});
} else {
this.send({command: 'biometricUnlock', response: 'canceled'});
}
2020-10-12 18:03:16 +02:00
break;
default:
this.logService.error('NativeMessage, got unknown command.');
}
}
private async send(message: any) {
message.timestamp = Date.now();
const encrypted = await this.cryptoService.encrypt(JSON.stringify(message));
ipcRenderer.send('nativeMessagingReply', encrypted);
}
}