mirror of
https://github.com/bitwarden/browser
synced 2025-12-12 22:33:35 +00:00
[PM-26778] Make VaultTimeoutService use LogoutService (#16820)
* Make vaulttimeoutservice use logoutservice * Fix browser build * Fix mv3 build * Fix lint
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
import { firstValueFrom } from "rxjs";
|
import { firstValueFrom } from "rxjs";
|
||||||
|
|
||||||
|
import { LogoutService } from "@bitwarden/auth/common";
|
||||||
import { AccountService } from "@bitwarden/common/auth/abstractions/account.service";
|
import { AccountService } from "@bitwarden/common/auth/abstractions/account.service";
|
||||||
import {
|
import {
|
||||||
VaultTimeoutAction,
|
VaultTimeoutAction,
|
||||||
@@ -8,6 +9,7 @@ import {
|
|||||||
VaultTimeoutStringType,
|
VaultTimeoutStringType,
|
||||||
} from "@bitwarden/common/key-management/vault-timeout";
|
} from "@bitwarden/common/key-management/vault-timeout";
|
||||||
import { ServerNotificationsService } from "@bitwarden/common/platform/server-notifications";
|
import { ServerNotificationsService } from "@bitwarden/common/platform/server-notifications";
|
||||||
|
import { UserId } from "@bitwarden/user-core";
|
||||||
|
|
||||||
const IdleInterval = 60 * 5; // 5 minutes
|
const IdleInterval = 60 * 5; // 5 minutes
|
||||||
|
|
||||||
@@ -21,6 +23,7 @@ export default class IdleBackground {
|
|||||||
private serverNotificationsService: ServerNotificationsService,
|
private serverNotificationsService: ServerNotificationsService,
|
||||||
private accountService: AccountService,
|
private accountService: AccountService,
|
||||||
private vaultTimeoutSettingsService: VaultTimeoutSettingsService,
|
private vaultTimeoutSettingsService: VaultTimeoutSettingsService,
|
||||||
|
private logoutService: LogoutService,
|
||||||
) {
|
) {
|
||||||
this.idle = chrome.idle || (browser != null ? browser.idle : null);
|
this.idle = chrome.idle || (browser != null ? browser.idle : null);
|
||||||
}
|
}
|
||||||
@@ -61,7 +64,7 @@ export default class IdleBackground {
|
|||||||
this.vaultTimeoutSettingsService.getVaultTimeoutActionByUserId$(userId),
|
this.vaultTimeoutSettingsService.getVaultTimeoutActionByUserId$(userId),
|
||||||
);
|
);
|
||||||
if (action === VaultTimeoutAction.LogOut) {
|
if (action === VaultTimeoutAction.LogOut) {
|
||||||
await this.vaultTimeoutService.logOut(userId);
|
await this.logoutService.logout(userId as UserId, "vaultTimeout");
|
||||||
} else {
|
} else {
|
||||||
await this.vaultTimeoutService.lock(userId);
|
await this.vaultTimeoutService.lock(userId);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ import {
|
|||||||
AuthRequestServiceAbstraction,
|
AuthRequestServiceAbstraction,
|
||||||
DefaultAuthRequestApiService,
|
DefaultAuthRequestApiService,
|
||||||
DefaultLockService,
|
DefaultLockService,
|
||||||
|
DefaultLogoutService,
|
||||||
InternalUserDecryptionOptionsServiceAbstraction,
|
InternalUserDecryptionOptionsServiceAbstraction,
|
||||||
LoginEmailServiceAbstraction,
|
LoginEmailServiceAbstraction,
|
||||||
LogoutReason,
|
LogoutReason,
|
||||||
@@ -976,6 +977,7 @@ export default class MainBackground {
|
|||||||
this.restrictedItemTypesService,
|
this.restrictedItemTypesService,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const logoutService = new DefaultLogoutService(this.messagingService);
|
||||||
this.vaultTimeoutService = new VaultTimeoutService(
|
this.vaultTimeoutService = new VaultTimeoutService(
|
||||||
this.accountService,
|
this.accountService,
|
||||||
this.masterPasswordService,
|
this.masterPasswordService,
|
||||||
@@ -994,7 +996,7 @@ export default class MainBackground {
|
|||||||
this.logService,
|
this.logService,
|
||||||
this.biometricsService,
|
this.biometricsService,
|
||||||
lockedCallback,
|
lockedCallback,
|
||||||
logoutCallback,
|
logoutService,
|
||||||
);
|
);
|
||||||
this.containerService = new ContainerService(this.keyService, this.encryptService);
|
this.containerService = new ContainerService(this.keyService, this.encryptService);
|
||||||
|
|
||||||
@@ -1386,6 +1388,7 @@ export default class MainBackground {
|
|||||||
this.serverNotificationsService,
|
this.serverNotificationsService,
|
||||||
this.accountService,
|
this.accountService,
|
||||||
this.vaultTimeoutSettingsService,
|
this.vaultTimeoutSettingsService,
|
||||||
|
logoutService,
|
||||||
);
|
);
|
||||||
|
|
||||||
this.usernameGenerationService = legacyUsernameGenerationServiceFactory(
|
this.usernameGenerationService = legacyUsernameGenerationServiceFactory(
|
||||||
|
|||||||
@@ -13,8 +13,4 @@ export class ForegroundVaultTimeoutService implements BaseVaultTimeoutService {
|
|||||||
async lock(userId?: UserId): Promise<void> {
|
async lock(userId?: UserId): Promise<void> {
|
||||||
this.messagingService.send("lockVault", { userId });
|
this.messagingService.send("lockVault", { userId });
|
||||||
}
|
}
|
||||||
|
|
||||||
async logOut(userId?: string): Promise<void> {
|
|
||||||
this.messagingService.send("logout", { userId });
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import { Injectable } from "@angular/core";
|
import { Injectable } from "@angular/core";
|
||||||
import { firstValueFrom, Observable } from "rxjs";
|
import { firstValueFrom, Observable } from "rxjs";
|
||||||
|
|
||||||
|
import { LogoutService } from "@bitwarden/auth/common";
|
||||||
import { Account } from "@bitwarden/common/auth/abstractions/account.service";
|
import { Account } from "@bitwarden/common/auth/abstractions/account.service";
|
||||||
import { FeatureFlag } from "@bitwarden/common/enums/feature-flag.enum";
|
import { FeatureFlag } from "@bitwarden/common/enums/feature-flag.enum";
|
||||||
import { CryptoFunctionService } from "@bitwarden/common/key-management/crypto/abstractions/crypto-function.service";
|
import { CryptoFunctionService } from "@bitwarden/common/key-management/crypto/abstractions/crypto-function.service";
|
||||||
@@ -14,7 +15,6 @@ import {
|
|||||||
WrappedPrivateKey,
|
WrappedPrivateKey,
|
||||||
WrappedSigningKey,
|
WrappedSigningKey,
|
||||||
} from "@bitwarden/common/key-management/types";
|
} from "@bitwarden/common/key-management/types";
|
||||||
import { VaultTimeoutService } from "@bitwarden/common/key-management/vault-timeout";
|
|
||||||
import { ConfigService } from "@bitwarden/common/platform/abstractions/config/config.service";
|
import { ConfigService } from "@bitwarden/common/platform/abstractions/config/config.service";
|
||||||
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
|
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
|
||||||
import { LogService } from "@bitwarden/common/platform/abstractions/log.service";
|
import { LogService } from "@bitwarden/common/platform/abstractions/log.service";
|
||||||
@@ -89,7 +89,7 @@ export class UserKeyRotationService {
|
|||||||
private syncService: SyncService,
|
private syncService: SyncService,
|
||||||
private webauthnLoginAdminService: WebauthnLoginAdminService,
|
private webauthnLoginAdminService: WebauthnLoginAdminService,
|
||||||
private logService: LogService,
|
private logService: LogService,
|
||||||
private vaultTimeoutService: VaultTimeoutService,
|
private logoutService: LogoutService,
|
||||||
private toastService: ToastService,
|
private toastService: ToastService,
|
||||||
private i18nService: I18nService,
|
private i18nService: I18nService,
|
||||||
private dialogService: DialogService,
|
private dialogService: DialogService,
|
||||||
@@ -189,8 +189,7 @@ export class UserKeyRotationService {
|
|||||||
timeout: 15000,
|
timeout: 15000,
|
||||||
});
|
});
|
||||||
|
|
||||||
// temporary until userkey can be better verified
|
await this.logoutService.logout(user.id);
|
||||||
await this.vaultTimeoutService.logOut();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected async ensureIsAllowedToRotateUserKey(): Promise<void> {
|
protected async ensureIsAllowedToRotateUserKey(): Promise<void> {
|
||||||
|
|||||||
@@ -891,7 +891,7 @@ const safeProviders: SafeProvider[] = [
|
|||||||
LogService,
|
LogService,
|
||||||
BiometricsService,
|
BiometricsService,
|
||||||
LOCKED_CALLBACK,
|
LOCKED_CALLBACK,
|
||||||
LOGOUT_CALLBACK,
|
LogoutService,
|
||||||
],
|
],
|
||||||
}),
|
}),
|
||||||
safeProvider({
|
safeProvider({
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
export abstract class VaultTimeoutService {
|
export abstract class VaultTimeoutService {
|
||||||
abstract checkVaultTimeout(): Promise<void>;
|
abstract checkVaultTimeout(): Promise<void>;
|
||||||
abstract lock(userId?: string): Promise<void>;
|
abstract lock(userId?: string): Promise<void>;
|
||||||
abstract logOut(userId?: string): Promise<void>;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
// FIXME: Update this file to be type safe and remove this and next line
|
// FIXME: Update this file to be type safe and remove this and next line
|
||||||
// @ts-strict-ignore
|
// @ts-strict-ignore
|
||||||
import { MockProxy, any, mock } from "jest-mock-extended";
|
import { MockProxy, mock } from "jest-mock-extended";
|
||||||
import { BehaviorSubject, from, of } from "rxjs";
|
import { BehaviorSubject, from, of } from "rxjs";
|
||||||
|
|
||||||
// This import has been flagged as unallowed for this class. It may be involved in a circular dependency loop.
|
// This import has been flagged as unallowed for this class. It may be involved in a circular dependency loop.
|
||||||
@@ -8,7 +8,7 @@ import { BehaviorSubject, from, of } from "rxjs";
|
|||||||
import { CollectionService } from "@bitwarden/admin-console/common";
|
import { CollectionService } from "@bitwarden/admin-console/common";
|
||||||
// This import has been flagged as unallowed for this class. It may be involved in a circular dependency loop.
|
// This import has been flagged as unallowed for this class. It may be involved in a circular dependency loop.
|
||||||
// eslint-disable-next-line no-restricted-imports
|
// eslint-disable-next-line no-restricted-imports
|
||||||
import { LogoutReason } from "@bitwarden/auth/common";
|
import { LogoutService } from "@bitwarden/auth/common";
|
||||||
// This import has been flagged as unallowed for this class. It may be involved in a circular dependency loop.
|
// This import has been flagged as unallowed for this class. It may be involved in a circular dependency loop.
|
||||||
// eslint-disable-next-line no-restricted-imports
|
// eslint-disable-next-line no-restricted-imports
|
||||||
import { BiometricsService } from "@bitwarden/key-management";
|
import { BiometricsService } from "@bitwarden/key-management";
|
||||||
@@ -53,8 +53,8 @@ describe("VaultTimeoutService", () => {
|
|||||||
let taskSchedulerService: MockProxy<TaskSchedulerService>;
|
let taskSchedulerService: MockProxy<TaskSchedulerService>;
|
||||||
let logService: MockProxy<LogService>;
|
let logService: MockProxy<LogService>;
|
||||||
let biometricsService: MockProxy<BiometricsService>;
|
let biometricsService: MockProxy<BiometricsService>;
|
||||||
|
let logoutService: MockProxy<LogoutService>;
|
||||||
let lockedCallback: jest.Mock<Promise<void>, [userId: string]>;
|
let lockedCallback: jest.Mock<Promise<void>, [userId: string]>;
|
||||||
let loggedOutCallback: jest.Mock<Promise<void>, [logoutReason: LogoutReason, userId?: string]>;
|
|
||||||
|
|
||||||
let vaultTimeoutActionSubject: BehaviorSubject<VaultTimeoutAction>;
|
let vaultTimeoutActionSubject: BehaviorSubject<VaultTimeoutAction>;
|
||||||
let availableVaultTimeoutActionsSubject: BehaviorSubject<VaultTimeoutAction[]>;
|
let availableVaultTimeoutActionsSubject: BehaviorSubject<VaultTimeoutAction[]>;
|
||||||
@@ -80,9 +80,9 @@ describe("VaultTimeoutService", () => {
|
|||||||
taskSchedulerService = mock<TaskSchedulerService>();
|
taskSchedulerService = mock<TaskSchedulerService>();
|
||||||
logService = mock<LogService>();
|
logService = mock<LogService>();
|
||||||
biometricsService = mock<BiometricsService>();
|
biometricsService = mock<BiometricsService>();
|
||||||
|
logoutService = mock<LogoutService>();
|
||||||
|
|
||||||
lockedCallback = jest.fn();
|
lockedCallback = jest.fn();
|
||||||
loggedOutCallback = jest.fn();
|
|
||||||
|
|
||||||
vaultTimeoutActionSubject = new BehaviorSubject(VaultTimeoutAction.Lock);
|
vaultTimeoutActionSubject = new BehaviorSubject(VaultTimeoutAction.Lock);
|
||||||
|
|
||||||
@@ -110,7 +110,7 @@ describe("VaultTimeoutService", () => {
|
|||||||
logService,
|
logService,
|
||||||
biometricsService,
|
biometricsService,
|
||||||
lockedCallback,
|
lockedCallback,
|
||||||
loggedOutCallback,
|
logoutService,
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -213,12 +213,12 @@ describe("VaultTimeoutService", () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const expectUserToHaveLoggedOut = (userId: string) => {
|
const expectUserToHaveLoggedOut = (userId: string) => {
|
||||||
expect(loggedOutCallback).toHaveBeenCalledWith("vaultTimeout", userId);
|
expect(logoutService.logout).toHaveBeenCalledWith(userId, "vaultTimeout");
|
||||||
};
|
};
|
||||||
|
|
||||||
const expectNoAction = (userId: string) => {
|
const expectNoAction = (userId: string) => {
|
||||||
expect(lockedCallback).not.toHaveBeenCalledWith(userId);
|
expect(lockedCallback).not.toHaveBeenCalledWith(userId);
|
||||||
expect(loggedOutCallback).not.toHaveBeenCalledWith(any(), userId);
|
expect(logoutService.logout).not.toHaveBeenCalledWith(userId, "vaultTimeout");
|
||||||
};
|
};
|
||||||
|
|
||||||
describe("checkVaultTimeout", () => {
|
describe("checkVaultTimeout", () => {
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import { combineLatest, concatMap, filter, firstValueFrom, map, timeout } from "
|
|||||||
import { CollectionService } from "@bitwarden/admin-console/common";
|
import { CollectionService } from "@bitwarden/admin-console/common";
|
||||||
// This import has been flagged as unallowed for this class. It may be involved in a circular dependency loop.
|
// This import has been flagged as unallowed for this class. It may be involved in a circular dependency loop.
|
||||||
// eslint-disable-next-line no-restricted-imports
|
// eslint-disable-next-line no-restricted-imports
|
||||||
import { LogoutReason } from "@bitwarden/auth/common";
|
import { LogoutService } from "@bitwarden/auth/common";
|
||||||
// This import has been flagged as unallowed for this class. It may be involved in a circular dependency loop.
|
// This import has been flagged as unallowed for this class. It may be involved in a circular dependency loop.
|
||||||
// eslint-disable-next-line no-restricted-imports
|
// eslint-disable-next-line no-restricted-imports
|
||||||
import { BiometricsService } from "@bitwarden/key-management";
|
import { BiometricsService } from "@bitwarden/key-management";
|
||||||
@@ -52,10 +52,7 @@ export class VaultTimeoutService implements VaultTimeoutServiceAbstraction {
|
|||||||
protected logService: LogService,
|
protected logService: LogService,
|
||||||
private biometricService: BiometricsService,
|
private biometricService: BiometricsService,
|
||||||
private lockedCallback: (userId: UserId) => Promise<void> = null,
|
private lockedCallback: (userId: UserId) => Promise<void> = null,
|
||||||
private loggedOutCallback: (
|
private logoutService: LogoutService,
|
||||||
logoutReason: LogoutReason,
|
|
||||||
userId?: string,
|
|
||||||
) => Promise<void> = null,
|
|
||||||
) {
|
) {
|
||||||
this.taskSchedulerService.registerTaskHandler(
|
this.taskSchedulerService.registerTaskHandler(
|
||||||
ScheduledTaskNames.vaultTimeoutCheckInterval,
|
ScheduledTaskNames.vaultTimeoutCheckInterval,
|
||||||
@@ -123,7 +120,7 @@ export class VaultTimeoutService implements VaultTimeoutServiceAbstraction {
|
|||||||
);
|
);
|
||||||
const supportsLock = availableActions.includes(VaultTimeoutAction.Lock);
|
const supportsLock = availableActions.includes(VaultTimeoutAction.Lock);
|
||||||
if (!supportsLock) {
|
if (!supportsLock) {
|
||||||
await this.logOut(userId);
|
await this.logoutService.logout(userId, "vaultTimeout");
|
||||||
}
|
}
|
||||||
|
|
||||||
// HACK: Start listening for the transition of the locking user from something to the locked state.
|
// HACK: Start listening for the transition of the locking user from something to the locked state.
|
||||||
@@ -165,12 +162,6 @@ export class VaultTimeoutService implements VaultTimeoutServiceAbstraction {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async logOut(userId?: string): Promise<void> {
|
|
||||||
if (this.loggedOutCallback != null) {
|
|
||||||
await this.loggedOutCallback("vaultTimeout", userId);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private async shouldLock(
|
private async shouldLock(
|
||||||
userId: string,
|
userId: string,
|
||||||
lastActive: Date,
|
lastActive: Date,
|
||||||
@@ -214,7 +205,7 @@ export class VaultTimeoutService implements VaultTimeoutServiceAbstraction {
|
|||||||
this.vaultTimeoutSettingsService.getVaultTimeoutActionByUserId$(userId),
|
this.vaultTimeoutSettingsService.getVaultTimeoutActionByUserId$(userId),
|
||||||
);
|
);
|
||||||
timeoutAction === VaultTimeoutAction.LogOut
|
timeoutAction === VaultTimeoutAction.LogOut
|
||||||
? await this.logOut(userId)
|
? await this.logoutService.logout(userId, "vaultTimeout")
|
||||||
: await this.lock(userId);
|
: await this.lock(userId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user