static ContainerService for edge case dependencies

This commit is contained in:
Kyle Spearrin 2018-01-04 14:16:40 -05:00
parent 0dd711471b
commit 0fbbc4a0b9
4 changed files with 21 additions and 24 deletions

View File

@ -19,6 +19,7 @@ import ChromeStorageService from '../services/chromeStorage.service';
import CipherService from '../services/cipher.service'; import CipherService from '../services/cipher.service';
import CollectionService from '../services/collection.service'; import CollectionService from '../services/collection.service';
import ConstantsService from '../services/constants.service'; import ConstantsService from '../services/constants.service';
import ContainerService from '../services/container.service';
import CryptoService from '../services/crypto.service'; import CryptoService from '../services/crypto.service';
import EnvironmentService from '../services/environment.service'; import EnvironmentService from '../services/environment.service';
import FolderService from '../services/folder.service'; import FolderService from '../services/folder.service';
@ -80,7 +81,8 @@ export default class MainBackground {
this.browserUtilsService = new BrowserUtilsService(); this.browserUtilsService = new BrowserUtilsService();
this.i18nService = i18nService(this.browserUtilsService); this.i18nService = i18nService(this.browserUtilsService);
this.constantsService = new ConstantsService(this.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.tokenService = new TokenService(this.storageService);
this.appIdService = new AppIdService(this.storageService); this.appIdService = new AppIdService(this.storageService);
this.apiService = new ApiService(this.tokenService, this.browserUtilsService, this.apiService = new ApiService(this.tokenService, this.browserUtilsService,

View File

@ -11,7 +11,7 @@ import { Identity } from './identity';
import { Login } from './login'; import { Login } from './login';
import { SecureNote } from './secureNote'; import { SecureNote } from './secureNote';
import { BrowserUtilsService } from '../../services/abstractions/browserUtils.service'; import BrowserUtilsService from '../../services/browserUtils.service';
class Cipher extends Domain { class Cipher extends Domain {
id: string; id: string;
@ -32,8 +32,6 @@ class Cipher extends Domain {
fields: Field[]; fields: Field[];
collectionIds: string[]; collectionIds: string[];
private browserUtilsService: BrowserUtilsService;
constructor(obj?: CipherData, alreadyEncrypted: boolean = false, localData: any = null) { constructor(obj?: CipherData, alreadyEncrypted: boolean = false, localData: any = null) {
super(); super();
if (obj == null) { if (obj == null) {
@ -119,12 +117,7 @@ class Cipher extends Domain {
model.login = await this.login.decrypt(this.organizationId); model.login = await this.login.decrypt(this.organizationId);
model.subTitle = model.login.username; model.subTitle = model.login.username;
if (model.login.uri) { if (model.login.uri) {
if (this.browserUtilsService == null) { model.login.domain = BrowserUtilsService.getDomain(model.login.uri);
this.browserUtilsService = chrome.extension.getBackgroundPage()
.bitwardenMain.browserUtilsService as BrowserUtilsService;
}
model.login.domain = this.browserUtilsService.getDomain(model.login.uri);
} }
break; break;
case CipherType.SecureNote: case CipherType.SecureNote:

View File

@ -1,5 +1,6 @@
import { EncryptionType } from '../../enums/encryptionType.enum'; import { EncryptionType } from '../../enums/encryptionType.enum';
import { CryptoService } from '../../services/abstractions/crypto.service';
import ContainerService from '../../services/container.service';
class CipherString { class CipherString {
encryptedString?: string; encryptedString?: string;
@ -9,8 +10,6 @@ class CipherString {
initializationVector?: string; initializationVector?: string;
mac?: string; mac?: string;
private cryptoService: CryptoService;
constructor(encryptedStringOrType: string | EncryptionType, ct?: string, iv?: string, mac?: string) { constructor(encryptedStringOrType: string | EncryptionType, ct?: string, iv?: string, mac?: string) {
if (ct != null) { if (ct != null) {
// ct and header // ct and header
@ -88,25 +87,23 @@ class CipherString {
} }
} }
decrypt(orgId: string) { decrypt(orgId: string): Promise<string> {
if (this.decryptedValue) { if (this.decryptedValue) {
return Promise.resolve(this.decryptedValue); return Promise.resolve(this.decryptedValue);
} }
const self = this; if (ContainerService.cryptoService == null) {
if (this.cryptoService == null) { throw new Error('ContainerService.cryptoService not initialized');
this.cryptoService = chrome.extension.getBackgroundPage()
.bitwardenMain.cryptoService as CryptoService;
} }
return this.cryptoService.getOrgKey(orgId).then((orgKey: any) => { return ContainerService.cryptoService.getOrgKey(orgId).then((orgKey: any) => {
return self.cryptoService.decrypt(self, orgKey); return ContainerService.cryptoService.decrypt(this, orgKey);
}).then((decValue: any) => { }).then((decValue: any) => {
self.decryptedValue = decValue; this.decryptedValue = decValue;
return self.decryptedValue; return this.decryptedValue;
}).catch(() => { }).catch(() => {
self.decryptedValue = '[error: cannot decrypt]'; this.decryptedValue = '[error: cannot decrypt]';
return self.decryptedValue; return this.decryptedValue;
}); });
} }
} }

View File

@ -0,0 +1,5 @@
import { CryptoService } from './abstractions/crypto.service';
export default class ContainerService {
static cryptoService: CryptoService = null;
}