diff --git a/libs/angular/src/services/jslib-services.module.ts b/libs/angular/src/services/jslib-services.module.ts index 04a99947a9..953a6aa2eb 100644 --- a/libs/angular/src/services/jslib-services.module.ts +++ b/libs/angular/src/services/jslib-services.module.ts @@ -440,7 +440,6 @@ const safeProviders: SafeProvider[] = [ KdfConfigServiceAbstraction, TaskSchedulerService, UserAsymmetricKeysRegenerationService, - ConfigService, ], }), safeProvider({ @@ -1387,6 +1386,7 @@ const safeProviders: SafeProvider[] = [ LogService, SdkService, ApiServiceAbstraction, + ConfigService, ], }), ]; diff --git a/libs/auth/src/common/services/login-strategies/login-strategy.service.ts b/libs/auth/src/common/services/login-strategies/login-strategy.service.ts index 2613ab2fe3..26d6b60c3d 100644 --- a/libs/auth/src/common/services/login-strategies/login-strategy.service.ts +++ b/libs/auth/src/common/services/login-strategies/login-strategy.service.ts @@ -29,7 +29,6 @@ import { BillingAccountProfileStateService } from "@bitwarden/common/billing/abs import { PreloginRequest } from "@bitwarden/common/models/request/prelogin.request"; import { ErrorResponse } from "@bitwarden/common/models/response/error.response"; import { AppIdService } from "@bitwarden/common/platform/abstractions/app-id.service"; -import { ConfigService } from "@bitwarden/common/platform/abstractions/config/config.service"; import { EncryptService } from "@bitwarden/common/platform/abstractions/encrypt.service"; import { EnvironmentService } from "@bitwarden/common/platform/abstractions/environment.service"; import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service"; @@ -116,7 +115,6 @@ export class LoginStrategyService implements LoginStrategyServiceAbstraction { protected kdfConfigService: KdfConfigService, protected taskSchedulerService: TaskSchedulerService, protected userAsymmetricKeysRegenerationService: UserAsymmetricKeysRegenerationService, - protected configService: ConfigService, ) { this.currentAuthnTypeState = this.stateProvider.get(CURRENT_LOGIN_STRATEGY_KEY); this.loginStrategyCacheState = this.stateProvider.get(CACHE_KEY); @@ -337,7 +335,6 @@ export class LoginStrategyService implements LoginStrategyServiceAbstraction { this.vaultTimeoutSettingsService, this.kdfConfigService, this.userAsymmetricKeysRegenerationService, - this.configService, ]; return source.pipe( diff --git a/libs/key-management/src/user-asymmetric-key-regeneration/abstractions/user-asymmetric-key-regeneration.service.ts b/libs/key-management/src/user-asymmetric-key-regeneration/abstractions/user-asymmetric-key-regeneration.service.ts index c565e593a4..407338f845 100644 --- a/libs/key-management/src/user-asymmetric-key-regeneration/abstractions/user-asymmetric-key-regeneration.service.ts +++ b/libs/key-management/src/user-asymmetric-key-regeneration/abstractions/user-asymmetric-key-regeneration.service.ts @@ -1,6 +1,8 @@ import { UserId } from "@bitwarden/common/types/guid"; export abstract class UserAsymmetricKeysRegenerationService { - abstract shouldRegenerate: (userId: UserId) => Promise; - abstract regenerateUserAsymmetricKeys: (userId: UserId) => Promise; + /** + * Handle regeneration of the user's asymmetric keys if they are invalid. + */ + abstract handleUserAsymmetricKeysRegeneration: (userId: UserId) => Promise; } diff --git a/libs/key-management/src/user-asymmetric-key-regeneration/services/default-user-asymmetric-key-regeneration.service.ts b/libs/key-management/src/user-asymmetric-key-regeneration/services/default-user-asymmetric-key-regeneration.service.ts index 93ee2d8c60..cc894dd826 100644 --- a/libs/key-management/src/user-asymmetric-key-regeneration/services/default-user-asymmetric-key-regeneration.service.ts +++ b/libs/key-management/src/user-asymmetric-key-regeneration/services/default-user-asymmetric-key-regeneration.service.ts @@ -1,6 +1,8 @@ import { firstValueFrom, map } from "rxjs"; import { ApiService } from "@bitwarden/common/abstractions/api.service"; +import { FeatureFlag } from "@bitwarden/common/enums/feature-flag.enum"; +import { ConfigService } from "@bitwarden/common/platform/abstractions/config/config.service"; import { LogService } from "@bitwarden/common/platform/abstractions/log.service"; import { SdkService } from "@bitwarden/common/platform/abstractions/sdk/sdk.service"; import { EncString } from "@bitwarden/common/platform/models/domain/enc-string"; @@ -24,9 +26,23 @@ export class DefaultUserAsymmetricKeysRegenerationService protected logService: LogService, protected sdkService: SdkService, protected apiService: ApiService, + protected configService: ConfigService, ) {} - async shouldRegenerate(userId: UserId): Promise { + async handleUserAsymmetricKeysRegeneration(userId: UserId): Promise { + const privateKeyRegenerationFlag = await this.configService.getFeatureFlag( + FeatureFlag.PrivateKeyRegeneration, + ); + + if (privateKeyRegenerationFlag) { + const shouldRegenerate = await this.shouldRegenerate(userId); + if (shouldRegenerate) { + await this.regenerateUserAsymmetricKeys(userId); + } + } + } + + private async shouldRegenerate(userId: UserId): Promise { const [userKey, userKeyEncryptedPrivateKey, publicKeyResponse] = await Promise.all([ firstValueFrom(this.keyService.userKey$(userId)), firstValueFrom(this.keyService.userEncryptedPrivateKey$(userId)), @@ -71,7 +87,7 @@ export class DefaultUserAsymmetricKeysRegenerationService return false; } - async regenerateUserAsymmetricKeys(userId: UserId): Promise { + private async regenerateUserAsymmetricKeys(userId: UserId): Promise { const makeKeyPairResponse = await firstValueFrom( this.sdkService.userClient$(userId).pipe(map((sdk) => sdk.crypto().make_key_pair())), );