1
0
mirror of https://github.com/bitwarden/browser synced 2026-01-31 00:33:33 +00:00

Replaced uses of messaging with service.

This commit is contained in:
Todd Martin
2026-01-11 15:16:50 -05:00
parent 73dfb75be6
commit 6f5b160b25
15 changed files with 60 additions and 56 deletions

View File

@@ -7,7 +7,7 @@ import { firstValueFrom, of, BehaviorSubject } from "rxjs";
import { CollectionService } from "@bitwarden/admin-console/common";
import { NudgesService } from "@bitwarden/angular/vault";
import { LockService } from "@bitwarden/auth/common";
import { LockService, LogoutService } from "@bitwarden/auth/common";
import { AutomaticUserConfirmationService } from "@bitwarden/auto-confirm";
import { ApiService } from "@bitwarden/common/abstractions/api.service";
import { OrganizationService } from "@bitwarden/common/admin-console/abstractions/organization/organization.service.abstraction";
@@ -75,6 +75,7 @@ describe("AccountSecurityComponent", () => {
const dialogService = mock<DialogService>();
const keyService = mock<KeyService>();
const lockService = mock<LockService>();
const logoutService = mock<LogoutService>();
const policyService = mock<PolicyService>();
const phishingDetectionSettingsService = mock<PhishingDetectionSettingsServiceAbstraction>();
const pinServiceAbstraction = mock<PinServiceAbstraction>();
@@ -109,6 +110,7 @@ describe("AccountSecurityComponent", () => {
{ provide: I18nService, useValue: mockI18nService },
{ provide: KeyService, useValue: keyService },
{ provide: LockService, useValue: lockService },
{ provide: LogoutService, useValue: logoutService },
{ provide: LogService, useValue: mock<LogService>() },
{ provide: MessageSender, useValue: mock<MessageSender>() },
{ provide: NudgesService, useValue: vaultNudgesService },

View File

@@ -25,7 +25,7 @@ import { JslibModule } from "@bitwarden/angular/jslib.module";
import { NudgesService, NudgeType } from "@bitwarden/angular/vault";
import { SpotlightComponent } from "@bitwarden/angular/vault/components/spotlight/spotlight.component";
import { FingerprintDialogComponent } from "@bitwarden/auth/angular";
import { LockService } from "@bitwarden/auth/common";
import { LockService, LogoutService } from "@bitwarden/auth/common";
import { PolicyService } from "@bitwarden/common/admin-console/abstractions/policy/policy.service.abstraction";
import { PolicyType } from "@bitwarden/common/admin-console/enums";
import { getFirstPolicy } from "@bitwarden/common/admin-console/services/policy/default-policy.service";
@@ -174,6 +174,7 @@ export class AccountSecurityComponent implements OnInit, OnDestroy {
private validationService: ValidationService,
private logService: LogService,
private phishingDetectionSettingsService: PhishingDetectionSettingsServiceAbstraction,
private logoutService: LogoutService,
) {
this.consolidatedSessionTimeoutComponent$ = this.configService.getFeatureFlag$(
FeatureFlag.ConsolidatedSessionTimeoutComponent,
@@ -751,8 +752,8 @@ export class AccountSecurityComponent implements OnInit, OnDestroy {
});
const userId = (await firstValueFrom(this.accountService.activeAccount$))?.id;
if (confirmed) {
this.messagingService.send("logout", { userId: userId });
if (confirmed && userId) {
await this.logoutService.logout(userId, "userInitiated");
}
}

View File

@@ -4,13 +4,13 @@ import mock, { MockProxy } from "jest-mock-extended/lib/Mock";
import { firstValueFrom, of } from "rxjs";
import { ApiService } from "@bitwarden/common/abstractions/api.service";
import { LogoutService } from "@bitwarden/auth/common";
import { AccountService } from "@bitwarden/common/auth/abstractions/account.service";
import { TwoFactorProviderType } from "@bitwarden/common/auth/enums/two-factor-provider-type";
import { TwoFactorProviderResponse } from "@bitwarden/common/auth/models/response/two-factor-provider.response";
import { TwoFactorService } from "@bitwarden/common/auth/two-factor";
import { ListResponse } from "@bitwarden/common/models/response/list.response";
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
import { MessagingService } from "@bitwarden/common/platform/abstractions/messaging.service";
import { FakeAccountService, mockAccountServiceWith } from "@bitwarden/common/spec";
import { UserId } from "@bitwarden/common/types/guid";
import { ToastService } from "@bitwarden/components";
@@ -43,7 +43,7 @@ describe("ChangeEmailComponent", () => {
{ provide: TwoFactorService, useValue: twoFactorService },
{ provide: I18nService, useValue: { t: (key: string) => key } },
{ provide: KeyService, useValue: keyService },
{ provide: MessagingService, useValue: mock<MessagingService>() },
{ provide: LogoutService, useValue: mock<LogoutService>() },
{ provide: KdfConfigService, useValue: kdfConfigService },
{ provide: ToastService, useValue: mock<ToastService>() },
{ provide: FormBuilder, useClass: FormBuilder },

View File

@@ -3,6 +3,7 @@ import { FormBuilder, Validators } from "@angular/forms";
import { firstValueFrom } from "rxjs";
import { ApiService } from "@bitwarden/common/abstractions/api.service";
import { LogoutService } from "@bitwarden/auth/common";
import { AccountService } from "@bitwarden/common/auth/abstractions/account.service";
import { TwoFactorProviderType } from "@bitwarden/common/auth/enums/two-factor-provider-type";
import { EmailTokenRequest } from "@bitwarden/common/auth/models/request/email-token.request";
@@ -10,7 +11,6 @@ import { EmailRequest } from "@bitwarden/common/auth/models/request/email.reques
import { getUserId } from "@bitwarden/common/auth/services/account.service";
import { TwoFactorService } from "@bitwarden/common/auth/two-factor";
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
import { MessagingService } from "@bitwarden/common/platform/abstractions/messaging.service";
import { UserId } from "@bitwarden/common/types/guid";
import { ToastService } from "@bitwarden/components";
import { KdfConfigService, KeyService } from "@bitwarden/key-management";
@@ -43,10 +43,10 @@ export class ChangeEmailComponent implements OnInit {
private twoFactorService: TwoFactorService,
private i18nService: I18nService,
private keyService: KeyService,
private messagingService: MessagingService,
private formBuilder: FormBuilder,
private kdfConfigService: KdfConfigService,
private toastService: ToastService,
private logoutService: LogoutService,
) {}
async ngOnInit() {
@@ -132,7 +132,7 @@ export class ChangeEmailComponent implements OnInit {
title: this.i18nService.t("emailChanged"),
message: this.i18nService.t("logBackIn"),
});
this.messagingService.send("logout");
await this.logoutService.logout(this.userId, "emailChanged");
}
};

View File

@@ -1,13 +1,16 @@
import { Component } from "@angular/core";
import { FormBuilder } from "@angular/forms";
import { firstValueFrom } from "rxjs";
import { UserVerificationFormInputComponent } from "@bitwarden/auth/angular";
import { LogoutService } from "@bitwarden/auth/common";
import { ApiService } from "@bitwarden/common/abstractions/api.service";
import { AccountService } from "@bitwarden/common/auth/abstractions/account.service";
import { UserVerificationService } from "@bitwarden/common/auth/abstractions/user-verification/user-verification.service.abstraction";
import { getUserId } from "@bitwarden/common/auth/services/account.service";
import { Verification } from "@bitwarden/common/auth/types/verification";
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
import { LogService } from "@bitwarden/common/platform/abstractions/log.service";
import { MessagingService } from "@bitwarden/common/platform/abstractions/messaging.service";
import { DialogService, ToastService } from "@bitwarden/components";
import { SharedModule } from "../../../shared";
@@ -29,9 +32,10 @@ export class DeauthorizeSessionsComponent {
private i18nService: I18nService,
private formBuilder: FormBuilder,
private userVerificationService: UserVerificationService,
private messagingService: MessagingService,
private logService: LogService,
private toastService: ToastService,
private logoutService: LogoutService,
private accountService: AccountService,
) {}
submit = async () => {
@@ -44,7 +48,8 @@ export class DeauthorizeSessionsComponent {
title: this.i18nService.t("sessionsDeauthorized"),
message: this.i18nService.t("logBackIn"),
});
this.messagingService.send("logout");
const userId = await firstValueFrom(getUserId(this.accountService.activeAccount$));
await this.logoutService.logout(userId, "sessionsDeauthorized");
} catch (e) {
this.logService.error(e);
}

View File

@@ -3,11 +3,11 @@ import { FormControl } from "@angular/forms";
import { mock, MockProxy } from "jest-mock-extended";
import { of } from "rxjs";
import { LogoutService } from "@bitwarden/auth/common";
import { AccountService } from "@bitwarden/common/auth/abstractions/account.service";
import { ChangeKdfService } from "@bitwarden/common/key-management/kdf/change-kdf.service.abstraction";
import { ConfigService } from "@bitwarden/common/platform/abstractions/config/config.service";
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
import { MessagingService } from "@bitwarden/common/platform/abstractions/messaging.service";
import { FakeAccountService, mockAccountServiceWith } from "@bitwarden/common/spec";
import { UserId } from "@bitwarden/common/types/guid";
import { DIALOG_DATA, DialogRef, ToastService } from "@bitwarden/components";
@@ -23,7 +23,7 @@ describe("ChangeKdfConfirmationComponent", () => {
// Mock Services
let mockI18nService: MockProxy<I18nService>;
let mockMessagingService: MockProxy<MessagingService>;
let mockLogoutService: MockProxy<LogoutService>;
let mockToastService: MockProxy<ToastService>;
let mockDialogRef: MockProxy<DialogRef<ChangeKdfConfirmationComponent>>;
let mockConfigService: MockProxy<ConfigService>;
@@ -38,7 +38,7 @@ describe("ChangeKdfConfirmationComponent", () => {
beforeEach(() => {
mockI18nService = mock<I18nService>();
mockMessagingService = mock<MessagingService>();
mockLogoutService = mock<LogoutService>();
mockToastService = mock<ToastService>();
mockDialogRef = mock<DialogRef<ChangeKdfConfirmationComponent>>();
mockConfigService = mock<ConfigService>();
@@ -60,7 +60,7 @@ describe("ChangeKdfConfirmationComponent", () => {
imports: [SharedModule],
providers: [
{ provide: I18nService, useValue: mockI18nService },
{ provide: MessagingService, useValue: mockMessagingService },
{ provide: LogoutService, useValue: mockLogoutService },
{ provide: AccountService, useValue: accountService },
{ provide: ToastService, useValue: mockToastService },
{ provide: DialogRef, useValue: mockDialogRef },
@@ -211,7 +211,7 @@ describe("ChangeKdfConfirmationComponent", () => {
message: "encKeySettingsChanged-used-i18n",
});
expect(mockDialogRef.close).toHaveBeenCalled();
expect(mockMessagingService.send).not.toHaveBeenCalled();
expect(mockLogoutService.logout).not.toHaveBeenCalled();
});
it("sends a logout and displays a log back in toast when feature flag is disabled", async () => {
@@ -235,7 +235,7 @@ describe("ChangeKdfConfirmationComponent", () => {
title: "encKeySettingsChanged-used-i18n",
message: "logBackIn-used-i18n",
});
expect(mockMessagingService.send).toHaveBeenCalledWith("logout");
expect(mockLogoutService.logout).toHaveBeenCalledWith(mockUserId, "kdfSettingsChanged");
expect(mockDialogRef.close).not.toHaveBeenCalled();
});
});

View File

@@ -2,13 +2,13 @@ import { Component, Inject } from "@angular/core";
import { FormGroup, FormControl, Validators } from "@angular/forms";
import { firstValueFrom, Observable } from "rxjs";
import { LogoutService } from "@bitwarden/auth/common";
import { AccountService } from "@bitwarden/common/auth/abstractions/account.service";
import { getUserId } from "@bitwarden/common/auth/services/account.service";
import { FeatureFlag } from "@bitwarden/common/enums/feature-flag.enum";
import { ChangeKdfService } from "@bitwarden/common/key-management/kdf/change-kdf.service.abstraction";
import { ConfigService } from "@bitwarden/common/platform/abstractions/config/config.service";
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
import { MessagingService } from "@bitwarden/common/platform/abstractions/messaging.service";
import { DIALOG_DATA, DialogRef, ToastService } from "@bitwarden/components";
import { KdfConfig, KdfType } from "@bitwarden/key-management";
@@ -32,13 +32,13 @@ export class ChangeKdfConfirmationComponent {
constructor(
private i18nService: I18nService,
private messagingService: MessagingService,
@Inject(DIALOG_DATA) params: { kdf: KdfType; kdfConfig: KdfConfig },
private accountService: AccountService,
private toastService: ToastService,
private changeKdfService: ChangeKdfService,
private dialogRef: DialogRef<ChangeKdfConfirmationComponent>,
configService: ConfigService,
private logoutService: LogoutService,
) {
this.kdfConfig = params.kdfConfig;
this.noLogoutOnKdfChangeFeatureFlag$ = configService.getFeatureFlag$(
@@ -51,6 +51,7 @@ export class ChangeKdfConfirmationComponent {
return;
}
this.loading = true;
const activeAccountId = await firstValueFrom(getUserId(this.accountService.activeAccount$));
await this.makeKeyAndSave();
if (await firstValueFrom(this.noLogoutOnKdfChangeFeatureFlag$)) {
this.toastService.showToast({
@@ -64,7 +65,9 @@ export class ChangeKdfConfirmationComponent {
title: this.i18nService.t("encKeySettingsChanged"),
message: this.i18nService.t("logBackIn"),
});
this.messagingService.send("logout");
if (activeAccountId) {
await this.logoutService.logout(activeAccountId, "kdfSettingsChanged");
}
}
this.loading = false;
};

View File

@@ -2,9 +2,10 @@
// @ts-strict-ignore
import { Component, Input } from "@angular/core";
import { ActivatedRoute } from "@angular/router";
import { map, Observable } from "rxjs";
import { firstValueFrom, map, Observable } from "rxjs";
import { User } from "@bitwarden/angular/pipes/user-name.pipe";
import { LogoutService } from "@bitwarden/auth/common";
import { AccountService } from "@bitwarden/common/auth/abstractions/account.service";
import {
VaultTimeoutAction,
@@ -48,6 +49,7 @@ export class WebHeaderComponent {
private vaultTimeoutSettingsService: VaultTimeoutSettingsService,
private messagingService: MessagingService,
private accountService: AccountService,
private logoutService: LogoutService,
) {
this.routeData$ = this.route.data.pipe(
map((params) => {
@@ -69,7 +71,10 @@ export class WebHeaderComponent {
this.messagingService.send("lockVault");
}
protected logout() {
this.messagingService.send("logout");
protected async logout() {
const account = await firstValueFrom(this.account$);
if (account?.id) {
await this.logoutService.logout(account.id, "userInitiated");
}
}
}

View File

@@ -25,7 +25,6 @@ import { assertTruthy, assertNonNullish } from "@bitwarden/common/auth/utils";
import { InternalMasterPasswordServiceAbstraction } from "@bitwarden/common/key-management/master-password/abstractions/master-password.service.abstraction";
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
import { LogService } from "@bitwarden/common/platform/abstractions/log.service";
import { MessagingService } from "@bitwarden/common/platform/abstractions/messaging.service";
import { ValidationService } from "@bitwarden/common/platform/abstractions/validation.service";
import { SyncService } from "@bitwarden/common/platform/sync";
import { UserId } from "@bitwarden/common/types/guid";
@@ -76,7 +75,6 @@ export class SetInitialPasswordComponent implements OnInit {
private logoutService: LogoutService,
private logService: LogService,
private masterPasswordService: InternalMasterPasswordServiceAbstraction,
private messagingService: MessagingService,
private organizationApiService: OrganizationApiServiceAbstraction,
private policyApiService: PolicyApiServiceAbstraction,
private policyService: PolicyService,
@@ -317,7 +315,7 @@ export class SetInitialPasswordComponent implements OnInit {
});
if (confirmed) {
this.messagingService.send("logout");
await this.logoutService.logout(this.userId, "userInitiated");
}
}
}

View File

@@ -1,18 +1,10 @@
import { MockProxy, mock } from "jest-mock-extended";
import { MessagingService } from "@bitwarden/common/platform/abstractions/messaging.service";
import { DefaultLoginDecryptionOptionsService } from "./default-login-decryption-options.service";
describe("DefaultLoginDecryptionOptionsService", () => {
let service: DefaultLoginDecryptionOptionsService;
let messagingService: MockProxy<MessagingService>;
beforeEach(() => {
messagingService = mock<MessagingService>();
service = new DefaultLoginDecryptionOptionsService(messagingService);
service = new DefaultLoginDecryptionOptionsService();
});
it("should instantiate the service", () => {
@@ -26,12 +18,4 @@ describe("DefaultLoginDecryptionOptionsService", () => {
expect(result).toBeNull();
});
});
describe("logOut()", () => {
it("should send a logout message", async () => {
await service.logOut();
expect(messagingService.send).toHaveBeenCalledWith("logout");
});
});
});

View File

@@ -1,17 +1,11 @@
// FIXME: Update this file to be type safe and remove this and next line
// @ts-strict-ignore
import { MessagingService } from "@bitwarden/common/platform/abstractions/messaging.service";
import { LoginDecryptionOptionsService } from "./login-decryption-options.service";
export class DefaultLoginDecryptionOptionsService implements LoginDecryptionOptionsService {
constructor(protected messagingService: MessagingService) {}
constructor() {}
handleCreateUserSuccess(): Promise<void | null> {
return null;
}
async logOut(): Promise<void> {
this.messagingService.send("logout");
}
}

View File

@@ -1,15 +1,19 @@
export type LogoutReason =
| "accessTokenUnableToBeDecrypted"
| "accountDeleted"
| "emailChanged"
| "invalidAccessToken"
| "invalidSecurityStamp"
| "kdfSettingsChanged"
| "keyConnectorError"
| "logoutNotification"
| "missingEmailError"
| "noUnlockOptionsAvailable"
| "refreshTokenSecureStorageRetrievalFailure"
| "sessionsDeauthorized"
| "sessionExpired"
| "setInitialPassword"
| "tooManyInvalidPinAttempts"
| "userInitiated"
| "userKeyRotation"
| "vaultTimeout";

View File

@@ -2,6 +2,7 @@ import { Router } from "@angular/router";
import { mock } from "jest-mock-extended";
import { of } from "rxjs";
import { LogoutService } from "@bitwarden/auth/common";
import { KeyConnectorApiService } from "@bitwarden/common/key-management/key-connector/abstractions/key-connector-api.service";
import { KeyConnectorService } from "@bitwarden/common/key-management/key-connector/abstractions/key-connector.service";
import { KeyConnectorDomainConfirmation } from "@bitwarden/common/key-management/key-connector/models/key-connector-domain-confirmation";
@@ -35,6 +36,7 @@ describe("ConfirmKeyConnectorDomainComponent", () => {
const mockToastService = mock<ToastService>();
const mockI18nService = mock<I18nService>();
const mockAnonLayoutWrapperDataService = mock<AnonLayoutWrapperDataService>();
const mockLogoutService = mock<LogoutService>();
let mockAccountService = mockAccountServiceWith(userId);
const onBeforeNavigation = jest.fn();
@@ -56,6 +58,7 @@ describe("ConfirmKeyConnectorDomainComponent", () => {
mockToastService,
mockI18nService,
mockAnonLayoutWrapperDataService,
mockLogoutService,
);
jest.spyOn(component, "onBeforeNavigation").mockImplementation(onBeforeNavigation);
@@ -79,7 +82,7 @@ describe("ConfirmKeyConnectorDomainComponent", () => {
await component.ngOnInit();
expect(mockMessagingService.send).toHaveBeenCalledWith("logout");
expect(mockLogoutService.logout).toHaveBeenCalledWith(userId, "keyConnectorError");
expect(component.loading).toEqual(true);
});
@@ -186,7 +189,7 @@ describe("ConfirmKeyConnectorDomainComponent", () => {
await component.cancel();
expect(mockMessagingService.send).toHaveBeenCalledWith("logout");
expect(mockLogoutService.logout).toHaveBeenCalledWith(userId, "userInitiated");
expect(mockKeyConnectorService.convertNewSsoUserToKeyConnector).not.toHaveBeenCalled();
});
});

View File

@@ -3,6 +3,7 @@ import { Component, Input, OnInit } from "@angular/core";
import { Router } from "@angular/router";
import { firstValueFrom } from "rxjs";
import { LogoutService } from "@bitwarden/auth/common";
import { AccountService } from "@bitwarden/common/auth/abstractions/account.service";
import { getUserId } from "@bitwarden/common/auth/services/account.service";
import { KeyConnectorApiService } from "@bitwarden/common/key-management/key-connector/abstractions/key-connector-api.service";
@@ -52,6 +53,7 @@ export class ConfirmKeyConnectorDomainComponent implements OnInit {
private toastService: ToastService,
private i18nService: I18nService,
private anonLayoutWrapperDataService: AnonLayoutWrapperDataService,
private logoutService: LogoutService,
) {}
async ngOnInit() {
@@ -59,6 +61,7 @@ export class ConfirmKeyConnectorDomainComponent implements OnInit {
this.userId = await firstValueFrom(getUserId(this.accountService.activeAccount$));
} catch {
this.logService.info("[confirm-key-connector-domain] no active account");
// No userId available, use messaging service directly
this.messagingService.send("logout");
return;
}
@@ -68,7 +71,7 @@ export class ConfirmKeyConnectorDomainComponent implements OnInit {
);
if (confirmation == null) {
this.logService.info("[confirm-key-connector-domain] missing required parameters");
this.messagingService.send("logout");
await this.logoutService.logout(this.userId, "keyConnectorError");
return;
}
@@ -112,7 +115,7 @@ export class ConfirmKeyConnectorDomainComponent implements OnInit {
};
cancel = async () => {
this.messagingService.send("logout");
await this.logoutService.logout(this.userId, "userInitiated");
};
private async getOrganizationName(

View File

@@ -499,7 +499,9 @@ export class LockComponent implements OnInit, OnDestroy {
variant: "error",
message: this.i18nService.t("tooManyInvalidPinEntryAttemptsLoggingOut"),
});
this.messagingService.send("logout");
if (this.activeAccount?.id != null) {
await this.logoutService.logout(this.activeAccount.id, "tooManyInvalidPinAttempts");
}
return;
}