1
0
mirror of https://github.com/bitwarden/browser.git synced 2025-04-01 17:57:27 +02:00

Update Account Service observables from state service

This is a temporary stop-gap to avoid needing to reroute all account
activity and status changes through the account service. That can be
done as part of the breakup of state service.
This commit is contained in:
Matt Gibson 2023-09-27 17:44:37 -04:00 committed by Justin Baur
parent 4f97c7e283
commit 0425b03ba1
No known key found for this signature in database
GPG Key ID: 46438BBD28B69008
7 changed files with 48 additions and 5 deletions

View File

@ -15,6 +15,7 @@ import { InternalPolicyService as InternalPolicyServiceAbstraction } from "@bitw
import { ProviderService as ProviderServiceAbstraction } from "@bitwarden/common/admin-console/abstractions/provider.service";
import { PolicyApiService } from "@bitwarden/common/admin-console/services/policy/policy-api.service";
import { ProviderService } from "@bitwarden/common/admin-console/services/provider.service";
import { AccountService as AccountServiceAbstraction } from "@bitwarden/common/auth/abstractions/account.service";
import { AuthRequestCryptoServiceAbstraction } from "@bitwarden/common/auth/abstractions/auth-request-crypto.service.abstraction";
import { AuthService as AuthServiceAbstraction } from "@bitwarden/common/auth/abstractions/auth.service";
import { DeviceTrustCryptoServiceAbstraction } from "@bitwarden/common/auth/abstractions/device-trust-crypto.service.abstraction";
@ -24,6 +25,7 @@ import { TokenService as TokenServiceAbstraction } from "@bitwarden/common/auth/
import { TwoFactorService as TwoFactorServiceAbstraction } from "@bitwarden/common/auth/abstractions/two-factor.service";
import { UserVerificationApiServiceAbstraction } from "@bitwarden/common/auth/abstractions/user-verification/user-verification-api.service.abstraction";
import { UserVerificationService as UserVerificationServiceAbstraction } from "@bitwarden/common/auth/abstractions/user-verification/user-verification.service.abstraction";
import { AccountServiceImplementation } from "@bitwarden/common/auth/services/account.service";
import { AuthRequestCryptoServiceImplementation } from "@bitwarden/common/auth/services/auth-request-crypto.service.implementation";
import { AuthService } from "@bitwarden/common/auth/services/auth.service";
import { DeviceTrustCryptoService } from "@bitwarden/common/auth/services/device-trust-crypto.service.implementation";
@ -207,6 +209,7 @@ export default class MainBackground {
deviceTrustCryptoService: DeviceTrustCryptoServiceAbstraction;
authRequestCryptoService: AuthRequestCryptoServiceAbstraction;
browserPopoutWindowService: BrowserPopoutWindowService;
accountService: AccountServiceAbstraction;
// Passed to the popup for Safari to workaround issues with theming, downloading, etc.
backgroundWindow = window;
@ -261,12 +264,14 @@ export default class MainBackground {
new KeyGenerationService(this.cryptoFunctionService)
)
: new MemoryStorageService();
this.accountService = new AccountServiceImplementation(this.messagingService, this.logService);
this.stateService = new BrowserStateService(
this.storageService,
this.secureStorageService,
this.memoryStorageService,
this.logService,
new StateFactory(GlobalState, Account)
new StateFactory(GlobalState, Account),
this.accountService
);
this.platformUtilsService = new BrowserPlatformUtilsService(
this.messagingService,

View File

@ -1,5 +1,6 @@
import { mock, MockProxy } from "jest-mock-extended";
import { AccountService } from "@bitwarden/common/auth/abstractions/account.service";
import { LogService } from "@bitwarden/common/platform/abstractions/log.service";
import {
AbstractMemoryStorageService,
@ -27,6 +28,7 @@ describe("Browser State Service", () => {
let logService: MockProxy<LogService>;
let stateFactory: MockProxy<StateFactory<GlobalState, Account>>;
let useAccountCache: boolean;
let accountService: MockProxy<AccountService>;
let state: State<GlobalState, Account>;
const userId = "userId";
@ -38,6 +40,7 @@ describe("Browser State Service", () => {
diskStorageService = mock();
logService = mock();
stateFactory = mock();
accountService = mock();
// turn off account cache for tests
useAccountCache = false;
@ -62,6 +65,7 @@ describe("Browser State Service", () => {
memoryStorageService,
logService,
stateFactory,
accountService,
useAccountCache
);
});

View File

@ -1,5 +1,6 @@
import { BehaviorSubject } from "rxjs";
import { AccountService } from "@bitwarden/common/auth/abstractions/account.service";
import { LogService } from "@bitwarden/common/platform/abstractions/log.service";
import {
AbstractStorageService,
@ -42,6 +43,7 @@ export class BrowserStateService
memoryStorageService: AbstractMemoryStorageService,
logService: LogService,
stateFactory: StateFactory<GlobalState, Account>,
accountService: AccountService,
useAccountCache = true
) {
super(
@ -50,6 +52,7 @@ export class BrowserStateService
memoryStorageService,
logService,
stateFactory,
accountService,
useAccountCache
);

View File

@ -24,6 +24,7 @@ import {
} from "@bitwarden/common/admin-console/abstractions/policy/policy.service.abstraction";
import { ProviderService } from "@bitwarden/common/admin-console/abstractions/provider.service";
import { PolicyApiService } from "@bitwarden/common/admin-console/services/policy/policy-api.service";
import { AccountService as AccountServiceAbstraction } from "@bitwarden/common/auth/abstractions/account.service";
import { AuthRequestCryptoServiceAbstraction } from "@bitwarden/common/auth/abstractions/auth-request-crypto.service.abstraction";
import { AuthService as AuthServiceAbstraction } from "@bitwarden/common/auth/abstractions/auth.service";
import { DeviceTrustCryptoServiceAbstraction } from "@bitwarden/common/auth/abstractions/device-trust-crypto.service.abstraction";
@ -450,17 +451,25 @@ function getBgService<T>(service: keyof MainBackground) {
storageService: AbstractStorageService,
secureStorageService: AbstractStorageService,
memoryStorageService: AbstractMemoryStorageService,
logService: LogServiceAbstraction
logService: LogServiceAbstraction,
accountService: AccountServiceAbstraction
) => {
return new BrowserStateService(
storageService,
secureStorageService,
memoryStorageService,
logService,
new StateFactory(GlobalState, Account)
new StateFactory(GlobalState, Account),
accountService
);
},
deps: [AbstractStorageService, SECURE_STORAGE, MEMORY_STORAGE, LogServiceAbstraction],
deps: [
AbstractStorageService,
SECURE_STORAGE,
MEMORY_STORAGE,
LogServiceAbstraction,
AccountServiceAbstraction,
],
},
{
provide: UsernameGenerationServiceAbstraction,

View File

@ -11,6 +11,7 @@ import {
import { JslibServicesModule } from "@bitwarden/angular/services/jslib-services.module";
import { AbstractThemingService } from "@bitwarden/angular/services/theming/theming.service.abstraction";
import { PolicyService as PolicyServiceAbstraction } from "@bitwarden/common/admin-console/abstractions/policy/policy.service.abstraction";
import { AccountService as AccountServiceAbstraction } from "@bitwarden/common/auth/abstractions/account.service";
import { AuthService as AuthServiceAbstraction } from "@bitwarden/common/auth/abstractions/auth.service";
import { LoginService as LoginServiceAbstraction } from "@bitwarden/common/auth/abstractions/login.service";
import { LoginService } from "@bitwarden/common/auth/services/login.service";
@ -123,6 +124,7 @@ const RELOAD_CALLBACK = new InjectionToken<() => any>("RELOAD_CALLBACK");
MEMORY_STORAGE,
LogService,
STATE_FACTORY,
AccountServiceAbstraction,
STATE_SERVICE_USE_CACHE,
],
},

View File

@ -483,6 +483,7 @@ import { AbstractThemingService } from "./theming/theming.service.abstraction";
MEMORY_STORAGE,
LogService,
STATE_FACTORY,
AccountServiceAbstraction,
STATE_SERVICE_USE_CACHE,
],
},

View File

@ -6,6 +6,8 @@ import { OrganizationData } from "../../admin-console/models/data/organization.d
import { PolicyData } from "../../admin-console/models/data/policy.data";
import { ProviderData } from "../../admin-console/models/data/provider.data";
import { Policy } from "../../admin-console/models/domain/policy";
import { AccountService } from "../../auth/abstractions/account.service";
import { AuthenticationStatus } from "../../auth/enums/authentication-status";
import { AdminAuthRequestStorable } from "../../auth/models/domain/admin-auth-req-storable";
import { EnvironmentUrls } from "../../auth/models/domain/environment-urls";
import { ForceResetPasswordReason } from "../../auth/models/domain/force-reset-password-reason";
@ -27,6 +29,7 @@ import { GeneratedPasswordHistory, PasswordGeneratorOptions } from "../../tools/
import { UsernameGeneratorOptions } from "../../tools/generator/username";
import { SendData } from "../../tools/send/models/data/send.data";
import { SendView } from "../../tools/send/models/view/send.view";
import { UserId } from "../../types/guid";
import { CipherData } from "../../vault/models/data/cipher.data";
import { CollectionData } from "../../vault/models/data/collection.data";
import { FolderData } from "../../vault/models/data/folder.data";
@ -110,6 +113,7 @@ export class StateService<
protected memoryStorageService: AbstractMemoryStorageService,
protected logService: LogService,
protected stateFactory: StateFactory<TGlobalState, TAccount>,
protected accountService: AccountService,
protected useAccountCache: boolean = true
) {
// If the account gets changed, verify the new account is unlocked
@ -168,6 +172,8 @@ export class StateService<
}
await this.pushAccounts();
this.activeAccountSubject.next(state.activeUserId);
// TODO: Temporary update to avoid routing all account status changes through account service for now.
this.accountService.switchAccount(state.activeUserId as UserId);
return state;
});
@ -186,6 +192,9 @@ export class StateService<
state.accounts[userId].profile = diskAccount.profile;
return state;
});
// TODO: Temporary update to avoid routing all account status changes through account service for now.
this.accountService.setAccountStatus(userId as UserId, AuthenticationStatus.Locked);
}
async addAccount(account: TAccount) {
@ -208,6 +217,9 @@ export class StateService<
state.activeUserId = userId;
await this.storageService.save(keys.activeUserId, userId);
this.activeAccountSubject.next(state.activeUserId);
// TODO: temporary update to avoid routing all account status changes through account service for now.
this.accountService.switchAccount(userId as UserId);
return state;
});
@ -548,6 +560,9 @@ export class StateService<
this.reconcileOptions(options, await this.defaultInMemoryOptions())
);
const nextStatus = value != null ? AuthenticationStatus.Unlocked : AuthenticationStatus.Locked;
this.accountService.setAccountStatus(options.userId as UserId, nextStatus);
if (options.userId == this.activeAccountSubject.getValue()) {
const nextValue = value != null;
@ -581,6 +596,9 @@ export class StateService<
this.reconcileOptions(options, await this.defaultInMemoryOptions())
);
const nextStatus = value != null ? AuthenticationStatus.Unlocked : AuthenticationStatus.Locked;
this.accountService.setAccountStatus(options.userId as UserId, nextStatus);
if (options?.userId == this.activeAccountSubject.getValue()) {
const nextValue = value != null;
@ -3062,7 +3080,6 @@ export class StateService<
this.reconcileOptions({ userId: account.profile.userId }, await this.defaultOnDiskOptions())
);
}
//
protected async pushAccounts(): Promise<void> {
await this.pruneInMemoryAccounts();
@ -3180,6 +3197,8 @@ export class StateService<
return state;
});
// TODO: Invert this logic, we should remove accounts based on logged out emit
this.accountService.setAccountStatus(userId as UserId, AuthenticationStatus.LoggedOut);
}
protected async pruneInMemoryAccounts() {