diff --git a/src/background/main.background.ts b/src/background/main.background.ts index 427803acd7..758573a2ac 100644 --- a/src/background/main.background.ts +++ b/src/background/main.background.ts @@ -56,6 +56,7 @@ export default class MainBackground { passwordGenerationService: PasswordGenerationService; totpService: TotpService; autofillService: AutofillService; + containerService: ContainerService; onUpdatedRan: boolean; onReplacedRan: boolean; @@ -83,7 +84,7 @@ export default class MainBackground { this.storageService = new BrowserStorageService(this.platformUtilsService); this.i18nService = i18nService(this.platformUtilsService); this.constantsService = new ConstantsService(this.i18nService, this.platformUtilsService); - this.cryptoService = ContainerService.cryptoService = new CryptoService(this.storageService, + this.cryptoService = new CryptoService(this.storageService, this.storageService); this.tokenService = new TokenService(this.storageService); this.appIdService = new AppIdService(this.storageService); @@ -107,6 +108,7 @@ export default class MainBackground { this.totpService = new TotpService(this.storageService); this.autofillService = new AutofillService(this.cipherService, this.tokenService, this.totpService, this.utilsService, this.platformUtilsService); + this.containerService = new ContainerService(this.cryptoService, this.platformUtilsService); // Other fields this.sidebarAction = (typeof opr !== 'undefined') && opr.sidebarAction ? @@ -125,6 +127,8 @@ export default class MainBackground { } async bootstrap() { + this.containerService.attachToWindow(window); + await this.commandsBackground.init(); await this.contextMenusBackground.init(); await this.idleBackground.init(); diff --git a/src/models/domain/cipher.ts b/src/models/domain/cipher.ts index d89432c9c0..ba008a3d80 100644 --- a/src/models/domain/cipher.ts +++ b/src/models/domain/cipher.ts @@ -1,4 +1,4 @@ -import { Enums } from '@bitwarden/jslib'; +import { Abstractions, Enums } from '@bitwarden/jslib'; import { CipherData } from '../data/cipherData'; @@ -11,8 +11,6 @@ import { Identity } from './identity'; import { Login } from './login'; import { SecureNote } from './secureNote'; -import BrowserPlatformUtilsService from '../../services/browserPlatformUtils.service'; - class Cipher extends Domain { id: string; organizationId: string; @@ -117,7 +115,14 @@ class Cipher extends Domain { model.login = await this.login.decrypt(this.organizationId); model.subTitle = model.login.username; if (model.login.uri) { - model.login.domain = BrowserPlatformUtilsService.getDomain(model.login.uri); + const containerService = (window as any).BitwardenContainerService; + if (containerService) { + const platformUtilsService: Abstractions.PlatformUtilsService = + containerService.getPlatformUtilsService(); + model.login.domain = platformUtilsService.getDomain(model.login.uri); + } else { + throw new Error('window.BitwardenContainerService not initialized.'); + } } break; case Enums.CipherType.SecureNote: diff --git a/src/models/domain/cipherString.ts b/src/models/domain/cipherString.ts index d17f1c3706..3dc5d64510 100644 --- a/src/models/domain/cipherString.ts +++ b/src/models/domain/cipherString.ts @@ -1,6 +1,6 @@ import { Enums } from '@bitwarden/jslib'; -import ContainerService from '../../services/container.service'; +import { CryptoService } from '../../services/abstractions/crypto.service'; class CipherString { encryptedString?: string; @@ -92,12 +92,16 @@ class CipherString { return Promise.resolve(this.decryptedValue); } - if (ContainerService.cryptoService == null) { - throw new Error('ContainerService.cryptoService not initialized'); + let cryptoService: CryptoService; + const containerService = (window as any).BitwardenContainerService; + if (containerService) { + cryptoService = containerService.getCryptoService(); + } else { + throw new Error('window.BitwardenContainerService not initialized.'); } - return ContainerService.cryptoService.getOrgKey(orgId).then((orgKey: any) => { - return ContainerService.cryptoService.decrypt(this, orgKey); + return cryptoService.getOrgKey(orgId).then((orgKey: any) => { + return cryptoService.decrypt(this, orgKey); }).then((decValue: any) => { this.decryptedValue = decValue; return this.decryptedValue; diff --git a/src/services/container.service.ts b/src/services/container.service.ts index 04d8dc1c3b..6000b797e1 100644 --- a/src/services/container.service.ts +++ b/src/services/container.service.ts @@ -1,5 +1,23 @@ +import { Abstractions } from '@bitwarden/jslib'; + import { CryptoService } from './abstractions/crypto.service'; export default class ContainerService { - static cryptoService: CryptoService = null; + constructor(private cryptoService: CryptoService, + private platformUtilsService: Abstractions.PlatformUtilsService) { + } + + attachToWindow(win: any) { + if (!win.BitwardenContainerService) { + win.BitwardenContainerService = this; + } + } + + getCryptoService(): CryptoService { + return this.cryptoService; + } + + getPlatformUtilsService(): Abstractions.PlatformUtilsService { + return this.platformUtilsService; + } }