From 11f392b0363606bd440cb17ac9ab331375eb65b0 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Mon, 13 Nov 2017 15:37:38 -0500 Subject: [PATCH] interfaced utilsservice for popup and dependencies --- src/models/domain/cipher.ts | 10 ++++- src/models/domain/cipherString.ts | 16 +++---- src/models/request/deviceRequest.ts | 11 +++-- .../components/action-buttons.component.ts | 4 +- src/popup/app/lock/lock.component.ts | 5 +-- src/popup/app/services/auth.service.ts | 5 ++- src/popup/app/services/background.service.ts | 42 ++++++++++--------- src/popup/app/services/state.service.ts | 6 ++- src/popup/app/settings/settingsController.js | 2 +- .../app/tools/password-generator.component.ts | 4 +- src/popup/app/tools/tools.component.ts | 3 +- src/services/abstractions/utils.service.ts | 22 ++++++++++ src/services/utils.service.ts | 27 ++++++------ 13 files changed, 98 insertions(+), 59 deletions(-) create mode 100644 src/services/abstractions/utils.service.ts diff --git a/src/models/domain/cipher.ts b/src/models/domain/cipher.ts index f8def1b022..208688398e 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 UtilsService from '../../services/utils.service'; +import { UtilsService } from '../../services/abstractions/utils.service'; class Cipher extends Domain { id: string; @@ -31,6 +31,8 @@ class Cipher extends Domain { attachments: Attachment[]; fields: Field[]; + private utilsService: UtilsService; + constructor(obj?: CipherData, alreadyEncrypted: boolean = false, localData: any = null) { super(); if (obj == null) { @@ -113,7 +115,11 @@ class Cipher extends Domain { model.login = await this.login.decrypt(this.organizationId); model.subTitle = model.login.username; if (model.login.uri) { - model.login.domain = UtilsService.getDomain(model.login.uri); + if (this.utilsService == null) { + this.utilsService = chrome.extension.getBackgroundPage().bg_utilsService as UtilsService; + } + + model.login.domain = this.utilsService.getDomain(model.login.uri); } break; case CipherType.SecureNote: diff --git a/src/models/domain/cipherString.ts b/src/models/domain/cipherString.ts index 69b458e7da..9166229778 100644 --- a/src/models/domain/cipherString.ts +++ b/src/models/domain/cipherString.ts @@ -8,11 +8,10 @@ class CipherString { cipherText?: string; initializationVector?: string; mac?: string; - cryptoService: CryptoService; + + private cryptoService: CryptoService; constructor(encryptedStringOrType: string | EncryptionType, ct?: string, iv?: string, mac?: string) { - this.cryptoService = chrome.extension.getBackgroundPage().bg_cryptoService as CryptoService; - if (ct != null) { // ct and header const encType = encryptedStringOrType as EncryptionType; @@ -90,13 +89,16 @@ class CipherString { } decrypt(orgId: string) { - const self = this; - if (this.decryptedValue) { - return Promise.resolve(self.decryptedValue); + return Promise.resolve(this.decryptedValue); } - return self.cryptoService.getOrgKey(orgId).then((orgKey: any) => { + const self = this; + if (this.cryptoService == null) { + this.cryptoService = chrome.extension.getBackgroundPage().bg_cryptoService as CryptoService; + } + + return this.cryptoService.getOrgKey(orgId).then((orgKey: any) => { return self.cryptoService.decrypt(self, orgKey); }).then((decValue: any) => { self.decryptedValue = decValue; diff --git a/src/models/request/deviceRequest.ts b/src/models/request/deviceRequest.ts index a7a4414409..928a46557f 100644 --- a/src/models/request/deviceRequest.ts +++ b/src/models/request/deviceRequest.ts @@ -1,12 +1,15 @@ +import { BrowserType } from '../../enums/browserType.enum'; +import { UtilsService } from '../../services/abstractions/utils.service'; + class DeviceRequest { - type: number; // TODO: enum + type: BrowserType; name: string; identifier: string; pushToken?: string; - constructor(appId: string, utilsService: any) { // TODO: utils service type - this.type = utilsService.getDeviceType(); - this.name = utilsService.getBrowser(); + constructor(appId: string, utilsService: UtilsService) { + this.type = utilsService.getBrowser(); + this.name = utilsService.getBrowserString(); this.identifier = appId; this.pushToken = null; } diff --git a/src/popup/app/components/action-buttons.component.ts b/src/popup/app/components/action-buttons.component.ts index f5ad0cddbc..b779700129 100644 --- a/src/popup/app/components/action-buttons.component.ts +++ b/src/popup/app/components/action-buttons.component.ts @@ -1,5 +1,7 @@ import * as template from './action-buttons.component.html'; +import { UtilsService } from '../../../services/abstractions/utils.service'; + class ActionButtonsController implements ng.IController { onView: Function; @@ -9,7 +11,7 @@ class ActionButtonsController implements ng.IController { constants: any; constructor(private i18nService: any, private $analytics: any, private constantsService: any, private toastr: any, - private $timeout: any, private $window: any, private utilsService: any) { + private $timeout: any, private $window: any, private utilsService: UtilsService) { this.i18n = i18nService; this.constants = constantsService; } diff --git a/src/popup/app/lock/lock.component.ts b/src/popup/app/lock/lock.component.ts index aaf260382a..251989081e 100644 --- a/src/popup/app/lock/lock.component.ts +++ b/src/popup/app/lock/lock.component.ts @@ -1,13 +1,10 @@ -import CryptoService from '../../../services/crypto.service'; -import UserService from '../../../services/user.service'; - import * as template from './lock.component.html'; class LockController { i18n: any; constructor(public $scope: any, public $state: any, public i18nService: any, - public cryptoService: CryptoService, public toastr: any, public userService: UserService, + public cryptoService: any, public toastr: any, public userService: any, public SweetAlert: any, public $timeout: any) { this.i18n = i18nService; diff --git a/src/popup/app/services/auth.service.ts b/src/popup/app/services/auth.service.ts index 4bc9fb52d7..8f05040d1f 100644 --- a/src/popup/app/services/auth.service.ts +++ b/src/popup/app/services/auth.service.ts @@ -1,10 +1,11 @@ import { DeviceRequest } from '../../../models/request/deviceRequest'; import { TokenRequest } from '../../../models/request/tokenRequest'; -class AuthService { +import { UtilsService } from '../../../services/abstractions/utils.service'; +class AuthService { constructor(public cryptoService: any, public apiService: any, public userService: any, public tokenService: any, - public $rootScope: any, public appIdService: any, public utilsService: any, + public $rootScope: any, public appIdService: any, public utilsService: UtilsService, public constantsService: any) { } diff --git a/src/popup/app/services/background.service.ts b/src/popup/app/services/background.service.ts index 517c5109e3..e654570724 100644 --- a/src/popup/app/services/background.service.ts +++ b/src/popup/app/services/background.service.ts @@ -1,24 +1,26 @@ -function getBackgroundService(service: string) { - return () => { +import { UtilsService } from '../../../services/abstractions/utils.service'; + +function getBackgroundService(service: string) { + return (): T => { const page = chrome.extension.getBackgroundPage(); - return page ? page['bg_' + service] : null; + return page ? page['bg_' + service] as T : null; }; } -export const tokenService = getBackgroundService('tokenService'); -export const cryptoService = getBackgroundService('cryptoService'); -export const userService = getBackgroundService('userService'); -export const apiService = getBackgroundService('apiService'); -export const folderService = getBackgroundService('folderService'); -export const cipherService = getBackgroundService('cipherService'); -export const syncService = getBackgroundService('syncService'); -export const autofillService = getBackgroundService('autofillService'); -export const passwordGenerationService = getBackgroundService('passwordGenerationService'); -export const utilsService = getBackgroundService('utilsService'); -export const appIdService = getBackgroundService('appIdService'); -export const i18nService = getBackgroundService('i18nService'); -export const constantsService = getBackgroundService('constantsService'); -export const settingsService = getBackgroundService('settingsService'); -export const lockService = getBackgroundService('lockService'); -export const totpService = getBackgroundService('totpService'); -export const environmentService = getBackgroundService('environmentService'); +export const tokenService = getBackgroundService('tokenService'); +export const cryptoService = getBackgroundService('cryptoService'); +export const userService = getBackgroundService('userService'); +export const apiService = getBackgroundService('apiService'); +export const folderService = getBackgroundService('folderService'); +export const cipherService = getBackgroundService('cipherService'); +export const syncService = getBackgroundService('syncService'); +export const autofillService = getBackgroundService('autofillService'); +export const passwordGenerationService = getBackgroundService('passwordGenerationService'); +export const utilsService = getBackgroundService('utilsService'); +export const appIdService = getBackgroundService('appIdService'); +export const i18nService = getBackgroundService('i18nService'); +export const constantsService = getBackgroundService('constantsService'); +export const settingsService = getBackgroundService('settingsService'); +export const lockService = getBackgroundService('lockService'); +export const totpService = getBackgroundService('totpService'); +export const environmentService = getBackgroundService('environmentService'); diff --git a/src/popup/app/services/state.service.ts b/src/popup/app/services/state.service.ts index 4c3b09506c..43b66c7acb 100644 --- a/src/popup/app/services/state.service.ts +++ b/src/popup/app/services/state.service.ts @@ -1,12 +1,14 @@ +import { UtilsService } from '../../../services/abstractions/utils.service'; + class StateService { private state: any = {}; - constructor(private utilsService: any, private constantsService: any) { + constructor(private utilsService: UtilsService, private constantsService: any) { } async init() { const faviconsDisabled = await this.utilsService - .getObjFromStorage(this.constantsService.disableFaviconKey); + .getObjFromStorage(this.constantsService.disableFaviconKey); this.saveState('faviconEnabled', !faviconsDisabled); } diff --git a/src/popup/app/settings/settingsController.js b/src/popup/app/settings/settingsController.js index 21ecf8f051..e6934a54cc 100644 --- a/src/popup/app/settings/settingsController.js +++ b/src/popup/app/settings/settingsController.js @@ -127,7 +127,7 @@ angular $scope.rate = function () { $analytics.eventTrack('Rate Extension'); - switch (utilsService.getBrowser()) { + switch (utilsService.getBrowserString()) { case 'chrome': chrome.tabs.create({ url: 'https://chrome.google.com/webstore/detail/bitwarden-free-password-m/' + diff --git a/src/popup/app/tools/password-generator.component.ts b/src/popup/app/tools/password-generator.component.ts index f4a1806188..0727f54192 100644 --- a/src/popup/app/tools/password-generator.component.ts +++ b/src/popup/app/tools/password-generator.component.ts @@ -1,6 +1,8 @@ import * as angular from 'angular'; import * as template from './password-generator.component.html'; +import { UtilsService } from '../../../services/abstractions/utils.service'; + export class PasswordGeneratorController { $transition$: any; options: any; @@ -11,7 +13,7 @@ export class PasswordGeneratorController { i18n: any; constructor(private $state: any, private passwordGenerationService: any, - private toastr: any, private utilsService: any, private $analytics: any, + private toastr: any, private utilsService: UtilsService, private $analytics: any, private i18nService: any, private $timeout: any) { this.i18n = i18nService; diff --git a/src/popup/app/tools/tools.component.ts b/src/popup/app/tools/tools.component.ts index b2c95148bb..c627248fd8 100644 --- a/src/popup/app/tools/tools.component.ts +++ b/src/popup/app/tools/tools.component.ts @@ -1,6 +1,7 @@ -import UtilsService from '../../../services/utils.service'; import * as template from './tools.component.html'; +import { UtilsService } from '../../../services/abstractions/utils.service'; + class ToolsController { showExport: boolean; i18n: any; diff --git a/src/services/abstractions/utils.service.ts b/src/services/abstractions/utils.service.ts new file mode 100644 index 0000000000..e52eb44fec --- /dev/null +++ b/src/services/abstractions/utils.service.ts @@ -0,0 +1,22 @@ +import { BrowserType } from '../../enums/browserType.enum'; + +export interface UtilsService { + getBrowser(): BrowserType; + getBrowserString(): string; + isFirefox(): boolean; + isChrome(): boolean; + isEdge(): boolean; + isOpera(): boolean; + analyticsId(): string; + initListSectionItemListeners(doc: Document, angular: any): void; + copyToClipboard(text: string, doc?: Document): void; + getDomain(uriString: string): string; + getHostname(uriString: string): string; + inSidebar(theWindow: Window): boolean; + inTab(theWindow: Window): boolean; + inPopout(theWindow: Window): boolean; + inPopup(theWindow: Window): boolean; + saveObjToStorage(key: string, obj: any): Promise; + removeFromStorage(key: string): Promise; + getObjFromStorage(key: string): Promise; +} diff --git a/src/services/utils.service.ts b/src/services/utils.service.ts index 76c8b55134..62b5490187 100644 --- a/src/services/utils.service.ts +++ b/src/services/utils.service.ts @@ -1,4 +1,5 @@ import { BrowserType } from '../enums/browserType.enum'; +import { UtilsService as UtilsServiceInterface } from './abstractions/utils.service'; const AnalyticsIds = { [BrowserType.Chrome]: 'UA-81915606-6', @@ -7,7 +8,7 @@ const AnalyticsIds = { [BrowserType.Edge]: 'UA-81915606-9', }; -export default class UtilsService { +export default class UtilsService implements UtilsServiceInterface { static copyToClipboard(text: string, doc?: Document): void { doc = doc || document; if ((window as any).clipboardData && (window as any).clipboardData.setData) { @@ -264,8 +265,8 @@ export default class UtilsService { return this.browserCache; } - getDeviceType(): number { - return this.getBrowser(); + getBrowserString(): string { + return BrowserType[this.getBrowser()].toLowerCase(); } isFirefox(): boolean { @@ -293,7 +294,7 @@ export default class UtilsService { return this.analyticsIdCache; } - initListSectionItemListeners(doc: Document, angular: any) { + initListSectionItemListeners(doc: Document, angular: any): void { if (!doc) { throw new Error('doc parameter required'); } @@ -378,34 +379,32 @@ export default class UtilsService { UtilsService.copyToClipboard(text, doc); } - inSidebar(theWindow: Window) { + inSidebar(theWindow: Window): boolean { return theWindow.location.search !== '' && theWindow.location.search.indexOf('uilocation=sidebar') > -1; } - inTab(theWindow: Window) { + inTab(theWindow: Window): boolean { return theWindow.location.search !== '' && theWindow.location.search.indexOf('uilocation=tab') > -1; } - inPopout(theWindow: Window) { + inPopout(theWindow: Window): boolean { return theWindow.location.search !== '' && theWindow.location.search.indexOf('uilocation=popout') > -1; } - inPopup(theWindow: Window) { + inPopup(theWindow: Window): boolean { return theWindow.location.search === '' || theWindow.location.search.indexOf('uilocation=') === -1 || theWindow.location.search.indexOf('uilocation=popup') > -1; } - // remove these in favor of static - - saveObjToStorage(key: string, obj: any) { + saveObjToStorage(key: string, obj: any): Promise { return UtilsService.saveObjToStorage(key, obj); } - removeFromStorage(key: string) { + removeFromStorage(key: string): Promise { return UtilsService.removeFromStorage(key); } - getObjFromStorage(key: string) { - return UtilsService.getObjFromStorage(key); + getObjFromStorage(key: string): Promise { + return UtilsService.getObjFromStorage(key); } }