From 5af03ed2fbe83442f3383442354ba8a55ddca649 Mon Sep 17 00:00:00 2001 From: Justin Baur <19896123+justindbaur@users.noreply.github.com> Date: Mon, 23 Dec 2024 16:34:09 -0500 Subject: [PATCH] Use `PlatformUtilsService` Directly --- apps/browser/src/background/main.background.ts | 2 +- .../src/service-container/service-container.ts | 2 +- apps/desktop/src/app/services/services.module.ts | 13 +------------ .../services/electron-platform-utils.service.ts | 4 +--- libs/angular/src/services/injection-tokens.ts | 1 - .../angular/src/services/jslib-services.module.ts | 9 +-------- .../src/auth/services/token.service.spec.ts | 6 +++++- libs/common/src/auth/services/token.service.ts | 15 ++++++++------- 8 files changed, 18 insertions(+), 34 deletions(-) diff --git a/apps/browser/src/background/main.background.ts b/apps/browser/src/background/main.background.ts index aaee59ee3fa..d38e34991c9 100644 --- a/apps/browser/src/background/main.background.ts +++ b/apps/browser/src/background/main.background.ts @@ -589,7 +589,7 @@ export default class MainBackground { this.tokenService = new TokenService( this.singleUserStateProvider, this.globalStateProvider, - this.platformUtilsService.supportsSecureStorage(), + this.platformUtilsService, this.secureStorageService, this.keyGenerationService, this.encryptService, diff --git a/apps/cli/src/service-container/service-container.ts b/apps/cli/src/service-container/service-container.ts index 2afbae0782f..df3a2a805bc 100644 --- a/apps/cli/src/service-container/service-container.ts +++ b/apps/cli/src/service-container/service-container.ts @@ -374,7 +374,7 @@ export class ServiceContainer { this.tokenService = new TokenService( this.singleUserStateProvider, this.globalStateProvider, - this.platformUtilsService.supportsSecureStorage(), + this.platformUtilsService, this.secureStorageService, this.keyGenerationService, this.encryptService, diff --git a/apps/desktop/src/app/services/services.module.ts b/apps/desktop/src/app/services/services.module.ts index 0f541907995..f07ed41b56c 100644 --- a/apps/desktop/src/app/services/services.module.ts +++ b/apps/desktop/src/app/services/services.module.ts @@ -13,7 +13,6 @@ import { OBSERVABLE_MEMORY_STORAGE, OBSERVABLE_DISK_STORAGE, WINDOW, - SUPPORTS_SECURE_STORAGE, SYSTEM_THEME_OBSERVABLE, SafeInjectionToken, DEFAULT_VAULT_TIMEOUT, @@ -107,10 +106,7 @@ import { flagEnabled } from "../../platform/flags"; import { DesktopSettingsService } from "../../platform/services/desktop-settings.service"; import { ElectronKeyService } from "../../platform/services/electron-key.service"; import { ElectronLogRendererService } from "../../platform/services/electron-log.renderer.service"; -import { - ELECTRON_SUPPORTS_SECURE_STORAGE, - ElectronPlatformUtilsService, -} from "../../platform/services/electron-platform-utils.service"; +import { ElectronPlatformUtilsService } from "../../platform/services/electron-platform-utils.service"; import { ElectronRendererMessageSender } from "../../platform/services/electron-renderer-message.sender"; import { ElectronRendererSecureStorageService } from "../../platform/services/electron-renderer-secure-storage.service"; import { ElectronRendererStorageService } from "../../platform/services/electron-renderer-storage.service"; @@ -169,13 +165,6 @@ const safeProviders: SafeProvider[] = [ useClass: ElectronPlatformUtilsService, deps: [I18nServiceAbstraction, MessagingServiceAbstraction], }), - safeProvider({ - // We manually override the value of SUPPORTS_SECURE_STORAGE here to avoid - // the TokenService having to inject the PlatformUtilsService which introduces a - // circular dependency on Desktop only. - provide: SUPPORTS_SECURE_STORAGE, - useValue: ELECTRON_SUPPORTS_SECURE_STORAGE, - }), safeProvider({ provide: DEFAULT_VAULT_TIMEOUT, useValue: VaultTimeoutStringType.OnRestart, diff --git a/apps/desktop/src/platform/services/electron-platform-utils.service.ts b/apps/desktop/src/platform/services/electron-platform-utils.service.ts index b61d2a0c5e9..d19e106c068 100644 --- a/apps/desktop/src/platform/services/electron-platform-utils.service.ts +++ b/apps/desktop/src/platform/services/electron-platform-utils.service.ts @@ -10,8 +10,6 @@ import { import { ClipboardWriteMessage } from "../types/clipboard"; -export const ELECTRON_SUPPORTS_SECURE_STORAGE = true; - export class ElectronPlatformUtilsService implements PlatformUtilsService { constructor( protected i18nService: I18nService, @@ -134,7 +132,7 @@ export class ElectronPlatformUtilsService implements PlatformUtilsService { } supportsSecureStorage(): boolean { - return ELECTRON_SUPPORTS_SECURE_STORAGE; + return true; } getAutofillKeyboardShortcut(): Promise { diff --git a/libs/angular/src/services/injection-tokens.ts b/libs/angular/src/services/injection-tokens.ts index 3842c3250e1..b348a84d56f 100644 --- a/libs/angular/src/services/injection-tokens.ts +++ b/libs/angular/src/services/injection-tokens.ts @@ -45,7 +45,6 @@ export const LOGOUT_CALLBACK = new SafeInjectionToken< export const LOCKED_CALLBACK = new SafeInjectionToken<(userId?: string) => Promise>( "LOCKED_CALLBACK", ); -export const SUPPORTS_SECURE_STORAGE = new SafeInjectionToken("SUPPORTS_SECURE_STORAGE"); export const LOCALES_DIRECTORY = new SafeInjectionToken("LOCALES_DIRECTORY"); export const SYSTEM_LANGUAGE = new SafeInjectionToken("SYSTEM_LANGUAGE"); export const LOG_MAC_FAILURES = new SafeInjectionToken("LOG_MAC_FAILURES"); diff --git a/libs/angular/src/services/jslib-services.module.ts b/libs/angular/src/services/jslib-services.module.ts index 688507099de..79d1e38b21d 100644 --- a/libs/angular/src/services/jslib-services.module.ts +++ b/libs/angular/src/services/jslib-services.module.ts @@ -319,7 +319,6 @@ import { SafeInjectionToken, SECURE_STORAGE, STATE_FACTORY, - SUPPORTS_SECURE_STORAGE, SYSTEM_LANGUAGE, SYSTEM_THEME_OBSERVABLE, WINDOW, @@ -345,12 +344,6 @@ const safeProviders: SafeProvider[] = [ useFactory: (i18nService: I18nServiceAbstraction) => i18nService.translationLocale, deps: [I18nServiceAbstraction], }), - safeProvider({ - provide: SUPPORTS_SECURE_STORAGE, - useFactory: (platformUtilsService: PlatformUtilsServiceAbstraction) => - platformUtilsService.supportsSecureStorage(), - deps: [PlatformUtilsServiceAbstraction], - }), safeProvider({ provide: LOCALES_DIRECTORY, useValue: "./locales", @@ -589,7 +582,7 @@ const safeProviders: SafeProvider[] = [ deps: [ SingleUserStateProvider, GlobalStateProvider, - SUPPORTS_SECURE_STORAGE, + PlatformUtilsServiceAbstraction, SECURE_STORAGE, KeyGenerationServiceAbstraction, EncryptService, diff --git a/libs/common/src/auth/services/token.service.spec.ts b/libs/common/src/auth/services/token.service.spec.ts index f8882e1b118..ae03887bf27 100644 --- a/libs/common/src/auth/services/token.service.spec.ts +++ b/libs/common/src/auth/services/token.service.spec.ts @@ -8,6 +8,7 @@ import { VaultTimeoutAction } from "../../enums/vault-timeout-action.enum"; import { EncryptService } from "../../platform/abstractions/encrypt.service"; import { KeyGenerationService } from "../../platform/abstractions/key-generation.service"; import { LogService } from "../../platform/abstractions/log.service"; +import { PlatformUtilsService } from "../../platform/abstractions/platform-utils.service"; import { AbstractStorageService } from "../../platform/abstractions/storage.service"; import { StorageLocation } from "../../platform/enums"; import { StorageOptions } from "../../platform/models/domain/storage-options"; @@ -3011,10 +3012,13 @@ describe("TokenService", () => { // Helpers function createTokenService(supportsSecureStorage: boolean) { + const platformUtilsService = mock(); + platformUtilsService.supportsSecureStorage.mockReturnValue(supportsSecureStorage); + return new TokenService( singleUserStateProvider, globalStateProvider, - supportsSecureStorage, + platformUtilsService, secureStorageService, keyGenerationService, encryptService, diff --git a/libs/common/src/auth/services/token.service.ts b/libs/common/src/auth/services/token.service.ts index 4b7cc2cab01..f4f1f787d3d 100644 --- a/libs/common/src/auth/services/token.service.ts +++ b/libs/common/src/auth/services/token.service.ts @@ -9,6 +9,7 @@ import { VaultTimeoutAction } from "../../enums/vault-timeout-action.enum"; import { EncryptService } from "../../platform/abstractions/encrypt.service"; import { KeyGenerationService } from "../../platform/abstractions/key-generation.service"; import { LogService } from "../../platform/abstractions/log.service"; +import { PlatformUtilsService } from "../../platform/abstractions/platform-utils.service"; import { AbstractStorageService } from "../../platform/abstractions/storage.service"; import { StorageLocation } from "../../platform/enums"; import { Utils } from "../../platform/misc/utils"; @@ -131,7 +132,7 @@ export class TokenService implements TokenServiceAbstraction { // this service into the AccountService, we will make a circular dependency private singleUserStateProvider: SingleUserStateProvider, private globalStateProvider: GlobalStateProvider, - private readonly platformSupportsSecureStorage: boolean, + private readonly platforUtilsService: PlatformUtilsService, private secureStorageService: AbstractStorageService, private keyGenerationService: KeyGenerationService, private encryptService: EncryptService, @@ -262,7 +263,7 @@ export class TokenService implements TokenServiceAbstraction { } private async getOrCreateAccessTokenKey(userId: UserId): Promise { - if (!this.platformSupportsSecureStorage) { + if (!this.platforUtilsService.supportsSecureStorage()) { throw new Error("Platform does not support secure storage. Cannot obtain access token key."); } @@ -417,7 +418,7 @@ export class TokenService implements TokenServiceAbstraction { // we can't determine storage location w/out vaultTimeoutAction and vaultTimeout // but we can simply clear all locations to avoid the need to require those parameters. - if (this.platformSupportsSecureStorage) { + if (this.platforUtilsService.supportsSecureStorage()) { // Always clear the access token key when clearing the access token // The next set of the access token will create a new access token key await this.clearAccessTokenKey(userId); @@ -450,7 +451,7 @@ export class TokenService implements TokenServiceAbstraction { return null; } - if (this.platformSupportsSecureStorage) { + if (this.platforUtilsService.supportsSecureStorage()) { let accessTokenKey: AccessTokenKey; try { accessTokenKey = await this.getAccessTokenKey(userId); @@ -627,7 +628,7 @@ export class TokenService implements TokenServiceAbstraction { return refreshTokenDisk; } - if (this.platformSupportsSecureStorage) { + if (this.platforUtilsService.supportsSecureStorage()) { try { const refreshTokenSecureStorage = await this.getStringFromSecureStorage( userId, @@ -663,7 +664,7 @@ export class TokenService implements TokenServiceAbstraction { // we can't determine storage location w/out vaultTimeoutAction and vaultTimeout // but we can simply clear all locations to avoid the need to require those parameters - if (this.platformSupportsSecureStorage) { + if (this.platforUtilsService.supportsSecureStorage()) { await this.secureStorageService.remove( `${userId}${this.refreshTokenSecureStorageKey}`, this.getSecureStorageOptions(userId), @@ -1086,7 +1087,7 @@ export class TokenService implements TokenServiceAbstraction { ) { return TokenStorageLocation.Memory; } else { - if (useSecureStorage && this.platformSupportsSecureStorage) { + if (useSecureStorage && this.platforUtilsService.supportsSecureStorage()) { return TokenStorageLocation.SecureStorage; }