From 7cbddf72784e4b9ad5431f477f46aaede0b0a9a8 Mon Sep 17 00:00:00 2001 From: Andreas Coroiu Date: Thu, 27 Apr 2023 11:59:01 +0200 Subject: [PATCH] [EC-598] chore: refactor observables a little bit --- .../fido2/popup/fido2/fido2.component.html | 10 +-- .../src/fido2/popup/fido2/fido2.component.ts | 88 +++++++++++-------- 2 files changed, 54 insertions(+), 44 deletions(-) diff --git a/apps/browser/src/fido2/popup/fido2/fido2.component.html b/apps/browser/src/fido2/popup/fido2/fido2.component.html index fcd4aa8621e..7ddb13601c4 100644 --- a/apps/browser/src/fido2/popup/fido2/fido2.component.html +++ b/apps/browser/src/fido2/popup/fido2/fido2.component.html @@ -3,8 +3,8 @@ A site is asking for authentication, please choose one of the following credentials to use: @@ -18,7 +18,7 @@ - + A site wants to create the following passkey in your vault
@@ -27,7 +27,7 @@
- + A passkey already exists in Bitwarden for this account
@@ -35,7 +35,7 @@
- + You do not have a matching login for this site.
diff --git a/apps/browser/src/fido2/popup/fido2/fido2.component.ts b/apps/browser/src/fido2/popup/fido2/fido2.component.ts index 2e83b2fc540..9e48b55a100 100644 --- a/apps/browser/src/fido2/popup/fido2/fido2.component.ts +++ b/apps/browser/src/fido2/popup/fido2/fido2.component.ts @@ -4,6 +4,7 @@ import { BehaviorSubject, combineLatest, concatMap, + filter, map, Observable, Subject, @@ -23,6 +24,10 @@ import { BrowserFido2UserInterfaceSession, } from "../../../services/fido2/browser-fido2-user-interface.service"; +interface ViewData { + message: BrowserFido2Message; +} + @Component({ selector: "app-fido2", templateUrl: "fido2.component.html", @@ -31,11 +36,13 @@ import { export class Fido2Component implements OnInit, OnDestroy { private destroy$ = new Subject(); - protected data$ = new BehaviorSubject(null); + protected data$: Observable; protected sessionId?: string; protected ciphers?: CipherView[] = []; protected loading = false; + private message$ = new BehaviorSubject(null); + constructor( private activatedRoute: ActivatedRoute, private cipherService: CipherService, @@ -64,44 +71,47 @@ export class Fido2Component implements OnInit, OnDestroy { return this.abort(false); } - this.data$.next(message); + this.message$.next(message); }); - this.data$ - .pipe( - concatMap(async (data) => { - if (data?.type === "ConfirmNewCredentialRequest") { - const cipher = new CipherView(); - cipher.name = data.credentialName; - cipher.type = CipherType.Fido2Key; - cipher.fido2Key = new Fido2KeyView(); - cipher.fido2Key.userName = data.userName; - this.ciphers = [cipher]; - } else if (data?.type === "PickCredentialRequest") { - this.ciphers = await Promise.all( - data.cipherIds.map(async (cipherId) => { - const cipher = await this.cipherService.get(cipherId); - return cipher.decrypt(); - }) - ); - } else if (data?.type === "ConfirmNewNonDiscoverableCredentialRequest") { - this.ciphers = (await this.cipherService.getAllDecrypted()).filter( - (cipher) => cipher.type === CipherType.Login && !cipher.isDeleted - ); - } else if (data?.type === "InformExcludedCredentialRequest") { - this.ciphers = await Promise.all( - data.existingCipherIds.map(async (cipherId) => { - const cipher = await this.cipherService.get(cipherId); - return cipher.decrypt(); - }) - ); - } else if (data?.type === "CloseRequest") { - window.close(); - } - }), - takeUntil(this.destroy$) - ) - .subscribe(); + this.data$ = this.message$.pipe( + filter((message) => message != undefined), + concatMap(async (message) => { + if (message.type === "ConfirmNewCredentialRequest") { + const cipher = new CipherView(); + cipher.name = message.credentialName; + cipher.type = CipherType.Fido2Key; + cipher.fido2Key = new Fido2KeyView(); + cipher.fido2Key.userName = message.userName; + this.ciphers = [cipher]; + } else if (message.type === "PickCredentialRequest") { + this.ciphers = await Promise.all( + message.cipherIds.map(async (cipherId) => { + const cipher = await this.cipherService.get(cipherId); + return cipher.decrypt(); + }) + ); + } else if (message.type === "ConfirmNewNonDiscoverableCredentialRequest") { + this.ciphers = (await this.cipherService.getAllDecrypted()).filter( + (cipher) => cipher.type === CipherType.Login && !cipher.isDeleted + ); + } else if (message.type === "InformExcludedCredentialRequest") { + this.ciphers = await Promise.all( + message.existingCipherIds.map(async (cipherId) => { + const cipher = await this.cipherService.get(cipherId); + return cipher.decrypt(); + }) + ); + } else if (message.type === "CloseRequest") { + window.close(); + } + + return { + message, + }; + }), + takeUntil(this.destroy$) + ); sessionId$.pipe(takeUntil(this.destroy$)).subscribe((sessionId) => { this.send({ @@ -112,7 +122,7 @@ export class Fido2Component implements OnInit, OnDestroy { } async pick(cipher: CipherView) { - const data = this.data$.value; + const data = this.message$.value; if (data?.type === "PickCredentialRequest") { let userVerified = false; if (data.userVerification) { @@ -143,7 +153,7 @@ export class Fido2Component implements OnInit, OnDestroy { } async confirm() { - const data = this.data$.value; + const data = this.message$.value; if (data.type !== "ConfirmNewCredentialRequest") { return; }