mirror of
https://github.com/bitwarden/browser
synced 2026-02-08 20:50:28 +00:00
key connector service explicit user id
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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<void>;
|
||||
getManagingOrganization: (userId?: UserId) => Promise<Organization>;
|
||||
getUsesKeyConnector: (userId: UserId) => Promise<boolean>;
|
||||
migrateUser: (userId?: UserId) => Promise<void>;
|
||||
userNeedsMigration: (userId: UserId) => Promise<boolean>;
|
||||
convertNewSsoUserToKeyConnector: (
|
||||
abstract setMasterKeyFromUrl(url: string, userId: UserId): Promise<void>;
|
||||
|
||||
abstract getManagingOrganization(userId: UserId): Promise<Organization>;
|
||||
|
||||
abstract getUsesKeyConnector(userId: UserId): Promise<boolean>;
|
||||
|
||||
abstract migrateUser(userId: UserId): Promise<void>;
|
||||
|
||||
abstract userNeedsMigration(userId: UserId): Promise<boolean>;
|
||||
|
||||
abstract convertNewSsoUserToKeyConnector(
|
||||
tokenResponse: IdentityTokenResponse,
|
||||
orgId: string,
|
||||
userId: UserId,
|
||||
) => Promise<void>;
|
||||
setUsesKeyConnector: (enabled: boolean, userId: UserId) => Promise<void>;
|
||||
setConvertAccountRequired: (status: boolean, userId?: UserId) => Promise<void>;
|
||||
getConvertAccountRequired: () => Promise<boolean>;
|
||||
removeConvertAccountRequired: (userId?: UserId) => Promise<void>;
|
||||
): Promise<void>;
|
||||
|
||||
abstract setUsesKeyConnector(enabled: boolean, userId: UserId): Promise<void>;
|
||||
|
||||
abstract setConvertAccountRequired(status: boolean, userId: UserId): Promise<void>;
|
||||
|
||||
abstract getConvertAccountRequired(): Promise<boolean>;
|
||||
|
||||
abstract removeConvertAccountRequired(userId: UserId): Promise<void>;
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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<boolean |
|
||||
export class KeyConnectorService implements KeyConnectorServiceAbstraction {
|
||||
private usesKeyConnectorState: ActiveUserState<boolean>;
|
||||
private convertAccountToKeyConnectorState: ActiveUserState<boolean>;
|
||||
|
||||
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<Organization> {
|
||||
async getManagingOrganization(userId: UserId): Promise<Organization> {
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user