diff --git a/apps/browser/src/background/main.background.ts b/apps/browser/src/background/main.background.ts index cd65220936e..f8b5f63710a 100644 --- a/apps/browser/src/background/main.background.ts +++ b/apps/browser/src/background/main.background.ts @@ -731,7 +731,6 @@ export default class MainBackground { this.badgeSettingsService = new BadgeSettingsService(this.stateProvider); this.policyApiService = new PolicyApiService(this.policyService, this.apiService); this.keyConnectorService = new KeyConnectorService( - this.accountService, this.masterPasswordService, this.keyService, this.apiService, diff --git a/apps/cli/src/service-container/service-container.ts b/apps/cli/src/service-container/service-container.ts index 0e776375e6a..3825e6777c8 100644 --- a/apps/cli/src/service-container/service-container.ts +++ b/apps/cli/src/service-container/service-container.ts @@ -560,7 +560,6 @@ export class ServiceContainer { this.policyApiService = new PolicyApiService(this.policyService, this.apiService); this.keyConnectorService = new KeyConnectorService( - this.accountService, this.masterPasswordService, this.keyService, this.apiService, diff --git a/libs/common/src/auth/abstractions/key-connector.service.ts b/libs/common/src/auth/abstractions/key-connector.service.ts index 9bcca1f5619..699fa7ad036 100644 --- a/libs/common/src/auth/abstractions/key-connector.service.ts +++ b/libs/common/src/auth/abstractions/key-connector.service.ts @@ -1,22 +1,29 @@ -// FIXME: Update this file to be type safe and remove this and next line -// @ts-strict-ignore import { Organization } from "../../admin-console/models/domain/organization"; import { UserId } from "../../types/guid"; import { IdentityTokenResponse } from "../models/response/identity-token.response"; export abstract class KeyConnectorService { - setMasterKeyFromUrl: (url: string, userId: UserId) => Promise; - getManagingOrganization: (userId?: UserId) => Promise; - getUsesKeyConnector: (userId: UserId) => Promise; - migrateUser: (userId?: UserId) => Promise; - userNeedsMigration: (userId: UserId) => Promise; - convertNewSsoUserToKeyConnector: ( + abstract setMasterKeyFromUrl(url: string, userId: UserId): Promise; + + abstract getManagingOrganization(userId: UserId): Promise; + + abstract getUsesKeyConnector(userId: UserId): Promise; + + abstract migrateUser(userId: UserId): Promise; + + abstract userNeedsMigration(userId: UserId): Promise; + + abstract convertNewSsoUserToKeyConnector( tokenResponse: IdentityTokenResponse, orgId: string, userId: UserId, - ) => Promise; - setUsesKeyConnector: (enabled: boolean, userId: UserId) => Promise; - setConvertAccountRequired: (status: boolean, userId?: UserId) => Promise; - getConvertAccountRequired: () => Promise; - removeConvertAccountRequired: (userId?: UserId) => Promise; + ): Promise; + + abstract setUsesKeyConnector(enabled: boolean, userId: UserId): Promise; + + abstract setConvertAccountRequired(status: boolean, userId: UserId): Promise; + + abstract getConvertAccountRequired(): Promise; + + abstract removeConvertAccountRequired(userId: UserId): Promise; } diff --git a/libs/common/src/auth/services/key-connector.service.spec.ts b/libs/common/src/auth/services/key-connector.service.spec.ts index ec03c7ece55..3ed4df1dd85 100644 --- a/libs/common/src/auth/services/key-connector.service.spec.ts +++ b/libs/common/src/auth/services/key-connector.service.spec.ts @@ -56,7 +56,6 @@ describe("KeyConnectorService", () => { stateProvider = new FakeStateProvider(accountService); keyConnectorService = new KeyConnectorService( - accountService, masterPasswordService, keyService, apiService, @@ -98,7 +97,7 @@ describe("KeyConnectorService", () => { organizationService.organizations$.mockReturnValue(of(orgs)); // Act - const result = await keyConnectorService.getManagingOrganization(); + const result = await keyConnectorService.getManagingOrganization(mockUserId); // Assert expect(result).toEqual(orgs[0]); @@ -113,7 +112,7 @@ describe("KeyConnectorService", () => { organizationService.organizations$.mockReturnValue(of(orgs)); // Act - const result = await keyConnectorService.getManagingOrganization(); + const result = await keyConnectorService.getManagingOrganization(mockUserId); // Assert expect(result).toBeUndefined(); @@ -128,7 +127,7 @@ describe("KeyConnectorService", () => { organizationService.organizations$.mockReturnValue(of(orgs)); // Act - const result = await keyConnectorService.getManagingOrganization(); + const result = await keyConnectorService.getManagingOrganization(mockUserId); // Assert expect(result).toBeUndefined(); @@ -143,7 +142,7 @@ describe("KeyConnectorService", () => { organizationService.organizations$.mockReturnValue(of(orgs)); // Act - const result = await keyConnectorService.getManagingOrganization(); + const result = await keyConnectorService.getManagingOrganization(mockUserId); // Assert expect(result).toBeUndefined(); @@ -157,7 +156,7 @@ describe("KeyConnectorService", () => { const newValue = true; - await keyConnectorService.setConvertAccountRequired(newValue); + await keyConnectorService.setConvertAccountRequired(newValue, mockUserId); expect(await keyConnectorService.getConvertAccountRequired()).toBe(newValue); }); @@ -166,9 +165,9 @@ describe("KeyConnectorService", () => { const state = stateProvider.activeUser.getFake(CONVERT_ACCOUNT_TO_KEY_CONNECTOR); state.nextState(false); - const newValue: boolean = null; + const newValue: boolean | null = null; - await keyConnectorService.setConvertAccountRequired(newValue); + await keyConnectorService.setConvertAccountRequired(newValue, mockUserId); expect(await keyConnectorService.getConvertAccountRequired()).toBe(newValue); }); @@ -258,7 +257,7 @@ describe("KeyConnectorService", () => { jest.spyOn(apiService, "postUserKeyToKeyConnector").mockResolvedValue(); // Act - await keyConnectorService.migrateUser(); + await keyConnectorService.migrateUser(mockUserId); // Assert expect(keyConnectorService.getManagingOrganization).toHaveBeenCalled(); @@ -284,7 +283,7 @@ describe("KeyConnectorService", () => { try { // Act - await keyConnectorService.migrateUser(); + await keyConnectorService.migrateUser(mockUserId); } catch { // Assert expect(logService.error).toHaveBeenCalledWith(error); diff --git a/libs/common/src/auth/services/key-connector.service.ts b/libs/common/src/auth/services/key-connector.service.ts index f6f76579ee5..3a3419dbae1 100644 --- a/libs/common/src/auth/services/key-connector.service.ts +++ b/libs/common/src/auth/services/key-connector.service.ts @@ -4,7 +4,6 @@ import { firstValueFrom } from "rxjs"; import { LogoutReason } from "@bitwarden/auth/common"; import { OrganizationService } from "@bitwarden/common/admin-console/abstractions/organization/organization.service.abstraction"; -import { AccountService } from "@bitwarden/common/auth/abstractions/account.service"; import { Argon2KdfConfig, KdfConfig, @@ -57,8 +56,8 @@ export const CONVERT_ACCOUNT_TO_KEY_CONNECTOR = new UserKeyDefinition; private convertAccountToKeyConnectorState: ActiveUserState; + constructor( - private accountService: AccountService, private masterPasswordService: InternalMasterPasswordServiceAbstraction, private keyService: KeyService, private apiService: ApiService, @@ -91,8 +90,7 @@ export class KeyConnectorService implements KeyConnectorServiceAbstraction { return loggedInUsingSso && requiredByOrganization && userIsNotUsingKeyConnector; } - async migrateUser(userId?: UserId) { - userId ??= (await firstValueFrom(this.accountService.activeAccount$))?.id; + async migrateUser(userId: UserId) { const organization = await this.getManagingOrganization(userId); const masterKey = await firstValueFrom(this.masterPasswordService.masterKey$(userId)); const keyConnectorRequest = new KeyConnectorUserKeyRequest(masterKey.encKeyB64); @@ -121,7 +119,7 @@ export class KeyConnectorService implements KeyConnectorServiceAbstraction { } } - async getManagingOrganization(userId?: UserId): Promise { + async getManagingOrganization(userId: UserId): Promise { const orgs = await firstValueFrom(this.organizationService.organizations$(userId)); return orgs.find( (o) => @@ -184,7 +182,7 @@ export class KeyConnectorService implements KeyConnectorServiceAbstraction { await this.apiService.postSetKeyConnectorKey(setPasswordRequest); } - async setConvertAccountRequired(status: boolean, userId?: UserId) { + async setConvertAccountRequired(status: boolean | null, userId: UserId) { await this.stateProvider.setUserState(CONVERT_ACCOUNT_TO_KEY_CONNECTOR, status, userId); } @@ -192,7 +190,7 @@ export class KeyConnectorService implements KeyConnectorServiceAbstraction { return firstValueFrom(this.convertAccountToKeyConnectorState.state$); } - async removeConvertAccountRequired(userId?: UserId) { + async removeConvertAccountRequired(userId: UserId) { await this.setConvertAccountRequired(null, userId); }