2020-10-12 18:01:34 +02:00
|
|
|
import { CryptoService, VaultTimeoutService } from 'jslib/abstractions';
|
|
|
|
import { StorageService } from 'jslib/abstractions/storage.service';
|
|
|
|
import { ConstantsService } from 'jslib/services';
|
2020-10-11 20:45:25 +02:00
|
|
|
import { BrowserApi } from '../browser/browserApi';
|
2020-10-12 18:01:34 +02:00
|
|
|
import RuntimeBackground from './runtime.background';
|
2020-10-09 17:16:15 +02:00
|
|
|
|
|
|
|
export class NativeMessagingBackground {
|
|
|
|
private connected = false;
|
|
|
|
private port: browser.runtime.Port | chrome.runtime.Port;
|
|
|
|
|
|
|
|
private resolver: any = null;
|
|
|
|
|
2020-10-12 18:01:34 +02:00
|
|
|
constructor(private storageService: StorageService, private cryptoService: CryptoService,
|
|
|
|
private vaultTimeoutService: VaultTimeoutService, private runtimeBackground: RuntimeBackground) {}
|
|
|
|
|
2020-10-09 17:16:15 +02:00
|
|
|
connect() {
|
2020-10-11 20:45:25 +02:00
|
|
|
this.port = BrowserApi.connectNative('com.8bit.bitwarden');
|
2020-10-09 17:16:15 +02:00
|
|
|
|
|
|
|
this.connected = true;
|
2020-10-12 18:01:34 +02:00
|
|
|
|
|
|
|
this.port.onMessage.addListener((msg) => this.onMessage(msg));
|
|
|
|
|
2020-10-09 17:16:15 +02:00
|
|
|
this.port.onDisconnect.addListener(() => {
|
|
|
|
this.connected = false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
send(message: object) {
|
|
|
|
// If not connected, try to connect
|
|
|
|
if (!this.connected) {
|
|
|
|
this.connect();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.port.postMessage(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
await(): Promise<any> {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this.resolver = resolve;
|
|
|
|
});
|
|
|
|
}
|
2020-10-12 18:01:34 +02:00
|
|
|
|
|
|
|
private async onMessage(msg: any) {
|
|
|
|
switch(msg.command) {
|
|
|
|
case 'biometricUnlock': {
|
|
|
|
await this.storageService.remove(ConstantsService.biometricAwaitingAcceptance);
|
|
|
|
|
|
|
|
const enabled = await this.storageService.get(ConstantsService.biometricUnlockKey);
|
|
|
|
if (enabled === null || enabled === false) {
|
|
|
|
if (msg.response === 'unlocked') {
|
|
|
|
await this.storageService.save(ConstantsService.biometricUnlockKey, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
await this.cryptoService.toggleKey();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.vaultTimeoutService.biometricLocked) {
|
|
|
|
this.runtimeBackground.processMessage({command: 'unlocked'}, null, null);
|
|
|
|
this.vaultTimeoutService.biometricLocked = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.resolver) {
|
|
|
|
this.resolver(msg);
|
|
|
|
}
|
|
|
|
}
|
2020-10-09 17:16:15 +02:00
|
|
|
}
|