1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-12-03 13:33:32 +01:00
bitwarden-browser/src/services/nativeMessaging.service.ts

47 lines
1.7 KiB
TypeScript
Raw Normal View History

import { CryptoService } from 'jslib/abstractions/crypto.service';
import { PlatformUtilsService } from 'jslib/abstractions';
import { ipcRenderer } from 'electron';
2020-10-05 20:05:48 +02:00
export class NativeMessagingService {
constructor(private cryptoService: CryptoService, private platformUtilService: PlatformUtilsService) {
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()) > 10*1000) {
console.error("MESSAGE IS TO OLD");
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:
2020-10-12 18:03:16 +02:00
console.error('UNKNOWN COMMAND')
}
}
private async send(message: any) {
message.timestamp = Date.now();
const encrypted = await this.cryptoService.encrypt(JSON.stringify(message));
ipcRenderer.send('nativeMessagingReply', encrypted);
}
}