diff --git a/libs/auth/src/angular/two-factor-auth/two-factor-auth.component.ts b/libs/auth/src/angular/two-factor-auth/two-factor-auth.component.ts index f184ffccfb7..032ec0b43a1 100644 --- a/libs/auth/src/angular/two-factor-auth/two-factor-auth.component.ts +++ b/libs/auth/src/angular/two-factor-auth/two-factor-auth.component.ts @@ -48,8 +48,8 @@ import { import { TwoFactorAuthComponentCacheService, - TwoFactorAuthData, -} from "../../common/services/auth-request/two-factor-auth-cache.service"; + TwoFactorAuthComponentData, +} from "../../common/services/auth-request/two-factor-auth-component-cache.service"; import { AnonLayoutWrapperDataService } from "../anon-layout/anon-layout-wrapper-data.service"; import { TwoFactorAuthAuthenticatorIcon, @@ -110,7 +110,7 @@ export class TwoFactorAuthComponent implements OnInit, OnDestroy { orgSsoIdentifier: string | undefined = undefined; providerType = TwoFactorProviderType; - selectedProviderType: TwoFactorProviderType | undefined = undefined; + selectedProviderType: TwoFactorProviderType = TwoFactorProviderType.Authenticator; // TODO: PM-17176 - build more specific type for 2FA metadata twoFactorProviders: Map | null = null; @@ -168,7 +168,7 @@ export class TwoFactorAuthComponent implements OnInit, OnDestroy { private anonLayoutWrapperDataService: AnonLayoutWrapperDataService, private environmentService: EnvironmentService, private loginSuccessHandlerService: LoginSuccessHandlerService, - private twoFactorCacheService: TwoFactorAuthComponentCacheService, + private twoFactorAuthComponentCacheService: TwoFactorAuthComponentCacheService, ) {} async ngOnInit() { @@ -178,11 +178,11 @@ export class TwoFactorAuthComponent implements OnInit, OnDestroy { this.listenForAuthnSessionTimeout(); // Initialize the cache - await this.twoFactorCacheService.init(); + await this.twoFactorAuthComponentCacheService.init(); // Load persisted form data if available let loadedCachedProviderType = false; - const persistedData = this.twoFactorCacheService.getCachedData(); + const persistedData = this.twoFactorAuthComponentCacheService.getCachedData(); if (persistedData) { if (persistedData.token) { this.form.patchValue({ token: persistedData.token }); @@ -216,11 +216,11 @@ export class TwoFactorAuthComponent implements OnInit, OnDestroy { /** * Save specific form data fields to the cache */ - async saveFormDataWithPartialData(data: Partial) { + async saveFormDataWithPartialData(data: Partial) { // Get current cached data - const currentData = this.twoFactorCacheService.getCachedData(); + const currentData = this.twoFactorAuthComponentCacheService.getCachedData(); - this.twoFactorCacheService.cacheData({ + this.twoFactorAuthComponentCacheService.cacheData({ token: data?.token ?? currentData?.token ?? "", remember: data?.remember ?? currentData?.remember ?? false, selectedProviderType: data?.selectedProviderType ?? currentData?.selectedProviderType, @@ -231,7 +231,7 @@ export class TwoFactorAuthComponent implements OnInit, OnDestroy { * Save all current form data to the cache */ async saveFormData() { - const formData: TwoFactorAuthData = { + const formData: TwoFactorAuthComponentData = { token: this.tokenFormControl.value || undefined, remember: this.rememberFormControl.value ?? undefined, selectedProviderType: this.selectedProviderType, @@ -259,8 +259,10 @@ export class TwoFactorAuthComponent implements OnInit, OnDestroy { private async set2faProvidersAndData() { this.twoFactorProviders = await this.twoFactorService.getProviders(); - const providerData = this.twoFactorProviders?.get(this.selectedProviderType); - this.selectedProviderData = providerData; + if (this.selectedProviderType !== undefined) { + const providerData = this.twoFactorProviders?.get(this.selectedProviderType); + this.selectedProviderData = providerData; + } } private listenForAuthnSessionTimeout() { @@ -327,7 +329,7 @@ export class TwoFactorAuthComponent implements OnInit, OnDestroy { const rememberValue = remember ?? this.rememberFormControl.value ?? false; // Persist form data before submitting - this.twoFactorCacheService.cacheData({ + this.twoFactorAuthComponentCacheService.cacheData({ token: tokenValue, remember: rememberValue, selectedProviderType: this.selectedProviderType, @@ -354,7 +356,7 @@ export class TwoFactorAuthComponent implements OnInit, OnDestroy { async selectOtherTwoFactorMethod() { // Persist current form data before navigating to another method - this.twoFactorCacheService.cacheData({ + this.twoFactorAuthComponentCacheService.cacheData({ token: "", remember: false, selectedProviderType: this.selectedProviderType, @@ -374,7 +376,7 @@ export class TwoFactorAuthComponent implements OnInit, OnDestroy { await this.setAnonLayoutDataByTwoFactorProviderType(); // Update the persisted provider type when a new one is chosen - this.twoFactorCacheService.cacheData({ + this.twoFactorAuthComponentCacheService.cacheData({ token: "", remember: false, selectedProviderType: response.type, @@ -458,7 +460,7 @@ export class TwoFactorAuthComponent implements OnInit, OnDestroy { private async handleAuthResult(authResult: AuthResult) { // Clear form cache - this.twoFactorCacheService.clearCachedData(); + this.twoFactorAuthComponentCacheService.clearCachedData(); if (await this.handleMigrateEncryptionKey(authResult)) { return; // stop login process diff --git a/libs/auth/src/common/services/auth-request/two-factor-auth-cache.service.spec.ts b/libs/auth/src/common/services/auth-request/two-factor-auth-component-cache.service.spec.ts similarity index 100% rename from libs/auth/src/common/services/auth-request/two-factor-auth-cache.service.spec.ts rename to libs/auth/src/common/services/auth-request/two-factor-auth-component-cache.service.spec.ts diff --git a/libs/auth/src/common/services/auth-request/two-factor-auth-cache.service.ts b/libs/auth/src/common/services/auth-request/two-factor-auth-component-cache.service.ts similarity index 71% rename from libs/auth/src/common/services/auth-request/two-factor-auth-cache.service.ts rename to libs/auth/src/common/services/auth-request/two-factor-auth-component-cache.service.ts index 6e19f745200..2212420f19f 100644 --- a/libs/auth/src/common/services/auth-request/two-factor-auth-cache.service.ts +++ b/libs/auth/src/common/services/auth-request/two-factor-auth-component-cache.service.ts @@ -6,27 +6,29 @@ import { TwoFactorProviderType } from "@bitwarden/common/auth/enums/two-factor-p import { FeatureFlag } from "@bitwarden/common/enums/feature-flag.enum"; import { ConfigService } from "@bitwarden/common/platform/abstractions/config/config.service"; -const TWO_FACTOR_AUTH_CACHE_KEY = "two-factor-auth-cache"; +const TWO_FACTOR_AUTH_COMPONENT_CACHE_KEY = "two-factor-auth-component-cache"; /** * This is a cache model for the two factor authentication data. */ -export class TwoFactorAuthCache { +export class TwoFactorAuthComponentCache { token: string | undefined = undefined; remember: boolean | undefined = undefined; selectedProviderType: TwoFactorProviderType | undefined = undefined; - static fromJSON(obj: Partial>): TwoFactorAuthCache | null { + static fromJSON( + obj: Partial>, + ): TwoFactorAuthComponentCache | null { // Return null if the cache is empty if (obj == null) { return null; } - return Object.assign(new TwoFactorAuthCache(), obj); + return Object.assign(new TwoFactorAuthComponentCache(), obj); } } -export interface TwoFactorAuthData { +export interface TwoFactorAuthComponentData { token?: string; remember?: boolean; selectedProviderType?: TwoFactorProviderType; @@ -49,11 +51,11 @@ export class TwoFactorAuthComponentCacheService { /** * Signal for the cached TwoFactorAuthData. */ - private twoFactorAuthCache: WritableSignal = - this.viewCacheService.signal({ - key: TWO_FACTOR_AUTH_CACHE_KEY, + private twoFactorAuthComponentCache: WritableSignal = + this.viewCacheService.signal({ + key: TWO_FACTOR_AUTH_COMPONENT_CACHE_KEY, initialValue: null, - deserializer: TwoFactorAuthCache.fromJSON, + deserializer: TwoFactorAuthComponentCache.fromJSON, }); constructor() {} @@ -70,16 +72,16 @@ export class TwoFactorAuthComponentCacheService { /** * Update the cache with the new TwoFactorAuthData. */ - cacheData(data: TwoFactorAuthData): void { + cacheData(data: TwoFactorAuthComponentData): void { if (!this.featureEnabled) { return; } - this.twoFactorAuthCache.set({ + this.twoFactorAuthComponentCache.set({ token: data.token, remember: data.remember, selectedProviderType: data.selectedProviderType, - } as TwoFactorAuthCache); + } as TwoFactorAuthComponentCache); } /** @@ -90,17 +92,17 @@ export class TwoFactorAuthComponentCacheService { return; } - this.twoFactorAuthCache.set(null); + this.twoFactorAuthComponentCache.set(null); } /** * Returns the cached TwoFactorAuthData when available. */ - getCachedData(): TwoFactorAuthCache | null { + getCachedData(): TwoFactorAuthComponentCache | null { if (!this.featureEnabled) { return null; } - return this.twoFactorAuthCache(); + return this.twoFactorAuthComponentCache(); } }