diff --git a/src/background/main.background.ts b/src/background/main.background.ts index 768d1058fc..61ce6002da 100644 --- a/src/background/main.background.ts +++ b/src/background/main.background.ts @@ -19,6 +19,7 @@ import ChromeStorageService from '../services/chromeStorage.service'; import CipherService from '../services/cipher.service'; import CollectionService from '../services/collection.service'; import ConstantsService from '../services/constants.service'; +import ContainerService from '../services/container.service'; import CryptoService from '../services/crypto.service'; import EnvironmentService from '../services/environment.service'; import FolderService from '../services/folder.service'; @@ -80,7 +81,8 @@ export default class MainBackground { this.browserUtilsService = new BrowserUtilsService(); this.i18nService = i18nService(this.browserUtilsService); this.constantsService = new ConstantsService(this.i18nService, this.browserUtilsService); - this.cryptoService = new CryptoService(this.storageService, this.storageService); + this.cryptoService = ContainerService.cryptoService = new CryptoService(this.storageService, + this.storageService); this.tokenService = new TokenService(this.storageService); this.appIdService = new AppIdService(this.storageService); this.apiService = new ApiService(this.tokenService, this.browserUtilsService, diff --git a/src/models/domain/cipher.ts b/src/models/domain/cipher.ts index 3c18f7a666..e916f24242 100644 --- a/src/models/domain/cipher.ts +++ b/src/models/domain/cipher.ts @@ -11,7 +11,7 @@ import { Identity } from './identity'; import { Login } from './login'; import { SecureNote } from './secureNote'; -import { BrowserUtilsService } from '../../services/abstractions/browserUtils.service'; +import BrowserUtilsService from '../../services/browserUtils.service'; class Cipher extends Domain { id: string; @@ -32,8 +32,6 @@ class Cipher extends Domain { fields: Field[]; collectionIds: string[]; - private browserUtilsService: BrowserUtilsService; - constructor(obj?: CipherData, alreadyEncrypted: boolean = false, localData: any = null) { super(); if (obj == null) { @@ -119,12 +117,7 @@ class Cipher extends Domain { model.login = await this.login.decrypt(this.organizationId); model.subTitle = model.login.username; if (model.login.uri) { - if (this.browserUtilsService == null) { - this.browserUtilsService = chrome.extension.getBackgroundPage() - .bitwardenMain.browserUtilsService as BrowserUtilsService; - } - - model.login.domain = this.browserUtilsService.getDomain(model.login.uri); + model.login.domain = BrowserUtilsService.getDomain(model.login.uri); } break; case CipherType.SecureNote: diff --git a/src/models/domain/cipherString.ts b/src/models/domain/cipherString.ts index 61aec8ca6f..ce3a184aae 100644 --- a/src/models/domain/cipherString.ts +++ b/src/models/domain/cipherString.ts @@ -1,5 +1,6 @@ import { EncryptionType } from '../../enums/encryptionType.enum'; -import { CryptoService } from '../../services/abstractions/crypto.service'; + +import ContainerService from '../../services/container.service'; class CipherString { encryptedString?: string; @@ -9,8 +10,6 @@ class CipherString { initializationVector?: string; mac?: string; - private cryptoService: CryptoService; - constructor(encryptedStringOrType: string | EncryptionType, ct?: string, iv?: string, mac?: string) { if (ct != null) { // ct and header @@ -88,25 +87,23 @@ class CipherString { } } - decrypt(orgId: string) { + decrypt(orgId: string): Promise { if (this.decryptedValue) { return Promise.resolve(this.decryptedValue); } - const self = this; - if (this.cryptoService == null) { - this.cryptoService = chrome.extension.getBackgroundPage() - .bitwardenMain.cryptoService as CryptoService; + if (ContainerService.cryptoService == null) { + throw new Error('ContainerService.cryptoService not initialized'); } - return this.cryptoService.getOrgKey(orgId).then((orgKey: any) => { - return self.cryptoService.decrypt(self, orgKey); + return ContainerService.cryptoService.getOrgKey(orgId).then((orgKey: any) => { + return ContainerService.cryptoService.decrypt(this, orgKey); }).then((decValue: any) => { - self.decryptedValue = decValue; - return self.decryptedValue; + this.decryptedValue = decValue; + return this.decryptedValue; }).catch(() => { - self.decryptedValue = '[error: cannot decrypt]'; - return self.decryptedValue; + this.decryptedValue = '[error: cannot decrypt]'; + return this.decryptedValue; }); } } diff --git a/src/services/container.service.ts b/src/services/container.service.ts new file mode 100644 index 0000000000..04d8dc1c3b --- /dev/null +++ b/src/services/container.service.ts @@ -0,0 +1,5 @@ +import { CryptoService } from './abstractions/crypto.service'; + +export default class ContainerService { + static cryptoService: CryptoService = null; +}