2020-10-12 21:18:28 +02:00
|
|
|
import { CryptoService } from 'jslib/abstractions/crypto.service';
|
|
|
|
import { PlatformUtilsService } from 'jslib/abstractions';
|
|
|
|
import { ipcRenderer } from 'electron';
|
2020-10-05 20:05:48 +02:00
|
|
|
|
2020-10-05 15:11:37 +02:00
|
|
|
export class NativeMessagingService {
|
2020-10-12 21:18:28 +02:00
|
|
|
constructor(private cryptoService: CryptoService, private platformUtilService: PlatformUtilsService) {
|
|
|
|
ipcRenderer.on('nativeMessaging', async (event: any, message: any) => {
|
|
|
|
this.messageHandler(message);
|
2020-10-05 15:11:37 +02:00
|
|
|
});
|
2020-10-05 19:48:51 +02:00
|
|
|
}
|
|
|
|
|
2020-10-12 21:18:28 +02:00
|
|
|
private async messageHandler(rawMessage: any) {
|
|
|
|
const message = JSON.parse(await this.cryptoService.decryptToUtf8(rawMessage));
|
2020-10-05 20:05:48 +02:00
|
|
|
|
2020-10-12 21:18:28 +02:00
|
|
|
if (Math.abs(message.timestamp - Date.now()) > 10*1000) {
|
|
|
|
console.error("MESSAGE IS TO OLD");
|
2020-10-07 15:11:01 +02:00
|
|
|
return;
|
|
|
|
}
|
2020-10-05 19:48:51 +02:00
|
|
|
|
2020-10-11 20:41:10 +02:00
|
|
|
switch (message.command) {
|
|
|
|
case 'biometricUnlock':
|
2020-10-12 21:18:28 +02:00
|
|
|
if (! this.platformUtilService.supportsBiometric()) {
|
|
|
|
ipcRenderer.send('nativeMessagingSync', )
|
|
|
|
return this.send({command: 'biometricUnlock', response: 'not supported'})
|
2020-10-11 20:41:10 +02:00
|
|
|
}
|
|
|
|
|
2020-10-12 21:18:28 +02:00
|
|
|
const response = await this.platformUtilService.authenticateBiometric();
|
2020-10-11 20:41:10 +02:00
|
|
|
if (response) {
|
2020-10-12 21:18:28 +02:00
|
|
|
this.send({command: 'biometricUnlock', response: 'unlocked'});
|
2020-10-11 20:41:10 +02:00
|
|
|
} else {
|
2020-10-12 21:18:28 +02:00
|
|
|
this.send({command: 'biometricUnlock', response: 'canceled'});
|
2020-10-11 20:41:10 +02:00
|
|
|
}
|
2020-10-12 18:03:16 +02:00
|
|
|
|
2020-10-11 20:41:10 +02:00
|
|
|
break;
|
|
|
|
default:
|
2020-10-12 18:03:16 +02:00
|
|
|
console.error('UNKNOWN COMMAND')
|
2020-10-11 20:41:10 +02:00
|
|
|
}
|
|
|
|
}
|
2020-10-12 21:18:28 +02:00
|
|
|
|
|
|
|
private async send(message: any) {
|
|
|
|
message.timestamp = Date.now();
|
|
|
|
const encrypted = await this.cryptoService.encrypt(JSON.stringify(message));
|
|
|
|
|
|
|
|
ipcRenderer.send('nativeMessagingReply', encrypted);
|
|
|
|
}
|
2020-10-05 15:11:37 +02:00
|
|
|
}
|