attach/read container service to window

This commit is contained in:
Kyle Spearrin 2018-01-07 00:15:12 -05:00
parent a387d8f7d0
commit fd931c23d1
4 changed files with 42 additions and 11 deletions

View File

@ -56,6 +56,7 @@ export default class MainBackground {
passwordGenerationService: PasswordGenerationService; passwordGenerationService: PasswordGenerationService;
totpService: TotpService; totpService: TotpService;
autofillService: AutofillService; autofillService: AutofillService;
containerService: ContainerService;
onUpdatedRan: boolean; onUpdatedRan: boolean;
onReplacedRan: boolean; onReplacedRan: boolean;
@ -83,7 +84,7 @@ export default class MainBackground {
this.storageService = new BrowserStorageService(this.platformUtilsService); this.storageService = new BrowserStorageService(this.platformUtilsService);
this.i18nService = i18nService(this.platformUtilsService); this.i18nService = i18nService(this.platformUtilsService);
this.constantsService = new ConstantsService(this.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.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);
@ -107,6 +108,7 @@ export default class MainBackground {
this.totpService = new TotpService(this.storageService); this.totpService = new TotpService(this.storageService);
this.autofillService = new AutofillService(this.cipherService, this.tokenService, this.autofillService = new AutofillService(this.cipherService, this.tokenService,
this.totpService, this.utilsService, this.platformUtilsService); this.totpService, this.utilsService, this.platformUtilsService);
this.containerService = new ContainerService(this.cryptoService, this.platformUtilsService);
// Other fields // Other fields
this.sidebarAction = (typeof opr !== 'undefined') && opr.sidebarAction ? this.sidebarAction = (typeof opr !== 'undefined') && opr.sidebarAction ?
@ -125,6 +127,8 @@ export default class MainBackground {
} }
async bootstrap() { async bootstrap() {
this.containerService.attachToWindow(window);
await this.commandsBackground.init(); await this.commandsBackground.init();
await this.contextMenusBackground.init(); await this.contextMenusBackground.init();
await this.idleBackground.init(); await this.idleBackground.init();

View File

@ -1,4 +1,4 @@
import { Enums } from '@bitwarden/jslib'; import { Abstractions, Enums } from '@bitwarden/jslib';
import { CipherData } from '../data/cipherData'; import { CipherData } from '../data/cipherData';
@ -11,8 +11,6 @@ import { Identity } from './identity';
import { Login } from './login'; import { Login } from './login';
import { SecureNote } from './secureNote'; import { SecureNote } from './secureNote';
import BrowserPlatformUtilsService from '../../services/browserPlatformUtils.service';
class Cipher extends Domain { class Cipher extends Domain {
id: string; id: string;
organizationId: string; organizationId: string;
@ -117,7 +115,14 @@ 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) {
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; break;
case Enums.CipherType.SecureNote: case Enums.CipherType.SecureNote:

View File

@ -1,6 +1,6 @@
import { Enums } from '@bitwarden/jslib'; import { Enums } from '@bitwarden/jslib';
import ContainerService from '../../services/container.service'; import { CryptoService } from '../../services/abstractions/crypto.service';
class CipherString { class CipherString {
encryptedString?: string; encryptedString?: string;
@ -92,12 +92,16 @@ class CipherString {
return Promise.resolve(this.decryptedValue); return Promise.resolve(this.decryptedValue);
} }
if (ContainerService.cryptoService == null) { let cryptoService: CryptoService;
throw new Error('ContainerService.cryptoService not initialized'); 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 cryptoService.getOrgKey(orgId).then((orgKey: any) => {
return ContainerService.cryptoService.decrypt(this, orgKey); return cryptoService.decrypt(this, orgKey);
}).then((decValue: any) => { }).then((decValue: any) => {
this.decryptedValue = decValue; this.decryptedValue = decValue;
return this.decryptedValue; return this.decryptedValue;

View File

@ -1,5 +1,23 @@
import { Abstractions } from '@bitwarden/jslib';
import { CryptoService } from './abstractions/crypto.service'; import { CryptoService } from './abstractions/crypto.service';
export default class ContainerService { 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;
}
} }