interfaced utilsservice for popup and dependencies

This commit is contained in:
Kyle Spearrin 2017-11-13 15:37:38 -05:00
parent 907247b3a7
commit 11f392b036
13 changed files with 98 additions and 59 deletions

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 UtilsService from '../../services/utils.service'; import { UtilsService } from '../../services/abstractions/utils.service';
class Cipher extends Domain { class Cipher extends Domain {
id: string; id: string;
@ -31,6 +31,8 @@ class Cipher extends Domain {
attachments: Attachment[]; attachments: Attachment[];
fields: Field[]; fields: Field[];
private utilsService: UtilsService;
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) {
@ -113,7 +115,11 @@ 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 = 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; break;
case CipherType.SecureNote: case CipherType.SecureNote:

View File

@ -8,11 +8,10 @@ class CipherString {
cipherText?: string; cipherText?: string;
initializationVector?: string; initializationVector?: string;
mac?: string; mac?: string;
cryptoService: CryptoService;
private cryptoService: CryptoService;
constructor(encryptedStringOrType: string | EncryptionType, ct?: string, iv?: string, mac?: string) { constructor(encryptedStringOrType: string | EncryptionType, ct?: string, iv?: string, mac?: string) {
this.cryptoService = chrome.extension.getBackgroundPage().bg_cryptoService as CryptoService;
if (ct != null) { if (ct != null) {
// ct and header // ct and header
const encType = encryptedStringOrType as EncryptionType; const encType = encryptedStringOrType as EncryptionType;
@ -90,13 +89,16 @@ class CipherString {
} }
decrypt(orgId: string) { decrypt(orgId: string) {
const self = this;
if (this.decryptedValue) { 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); return self.cryptoService.decrypt(self, orgKey);
}).then((decValue: any) => { }).then((decValue: any) => {
self.decryptedValue = decValue; self.decryptedValue = decValue;

View File

@ -1,12 +1,15 @@
import { BrowserType } from '../../enums/browserType.enum';
import { UtilsService } from '../../services/abstractions/utils.service';
class DeviceRequest { class DeviceRequest {
type: number; // TODO: enum type: BrowserType;
name: string; name: string;
identifier: string; identifier: string;
pushToken?: string; pushToken?: string;
constructor(appId: string, utilsService: any) { // TODO: utils service type constructor(appId: string, utilsService: UtilsService) {
this.type = utilsService.getDeviceType(); this.type = utilsService.getBrowser();
this.name = utilsService.getBrowser(); this.name = utilsService.getBrowserString();
this.identifier = appId; this.identifier = appId;
this.pushToken = null; this.pushToken = null;
} }

View File

@ -1,5 +1,7 @@
import * as template from './action-buttons.component.html'; import * as template from './action-buttons.component.html';
import { UtilsService } from '../../../services/abstractions/utils.service';
class ActionButtonsController implements ng.IController { class ActionButtonsController implements ng.IController {
onView: Function; onView: Function;
@ -9,7 +11,7 @@ class ActionButtonsController implements ng.IController {
constants: any; constants: any;
constructor(private i18nService: any, private $analytics: any, private constantsService: any, private toastr: 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.i18n = i18nService;
this.constants = constantsService; this.constants = constantsService;
} }

View File

@ -1,13 +1,10 @@
import CryptoService from '../../../services/crypto.service';
import UserService from '../../../services/user.service';
import * as template from './lock.component.html'; import * as template from './lock.component.html';
class LockController { class LockController {
i18n: any; i18n: any;
constructor(public $scope: any, public $state: any, public i18nService: 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) { public SweetAlert: any, public $timeout: any) {
this.i18n = i18nService; this.i18n = i18nService;

View File

@ -1,10 +1,11 @@
import { DeviceRequest } from '../../../models/request/deviceRequest'; import { DeviceRequest } from '../../../models/request/deviceRequest';
import { TokenRequest } from '../../../models/request/tokenRequest'; 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, 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) { public constantsService: any) {
} }

View File

@ -1,24 +1,26 @@
function getBackgroundService(service: string) { import { UtilsService } from '../../../services/abstractions/utils.service';
return () => {
function getBackgroundService<T>(service: string) {
return (): T => {
const page = chrome.extension.getBackgroundPage(); 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 tokenService = getBackgroundService<any>('tokenService');
export const cryptoService = getBackgroundService('cryptoService'); export const cryptoService = getBackgroundService<any>('cryptoService');
export const userService = getBackgroundService('userService'); export const userService = getBackgroundService<any>('userService');
export const apiService = getBackgroundService('apiService'); export const apiService = getBackgroundService<any>('apiService');
export const folderService = getBackgroundService('folderService'); export const folderService = getBackgroundService<any>('folderService');
export const cipherService = getBackgroundService('cipherService'); export const cipherService = getBackgroundService<any>('cipherService');
export const syncService = getBackgroundService('syncService'); export const syncService = getBackgroundService<any>('syncService');
export const autofillService = getBackgroundService('autofillService'); export const autofillService = getBackgroundService<any>('autofillService');
export const passwordGenerationService = getBackgroundService('passwordGenerationService'); export const passwordGenerationService = getBackgroundService<any>('passwordGenerationService');
export const utilsService = getBackgroundService('utilsService'); export const utilsService = getBackgroundService<UtilsService>('utilsService');
export const appIdService = getBackgroundService('appIdService'); export const appIdService = getBackgroundService<any>('appIdService');
export const i18nService = getBackgroundService('i18nService'); export const i18nService = getBackgroundService<any>('i18nService');
export const constantsService = getBackgroundService('constantsService'); export const constantsService = getBackgroundService<any>('constantsService');
export const settingsService = getBackgroundService('settingsService'); export const settingsService = getBackgroundService<any>('settingsService');
export const lockService = getBackgroundService('lockService'); export const lockService = getBackgroundService<any>('lockService');
export const totpService = getBackgroundService('totpService'); export const totpService = getBackgroundService<any>('totpService');
export const environmentService = getBackgroundService('environmentService'); export const environmentService = getBackgroundService<any>('environmentService');

View File

@ -1,12 +1,14 @@
import { UtilsService } from '../../../services/abstractions/utils.service';
class StateService { class StateService {
private state: any = {}; private state: any = {};
constructor(private utilsService: any, private constantsService: any) { constructor(private utilsService: UtilsService, private constantsService: any) {
} }
async init() { async init() {
const faviconsDisabled = await this.utilsService const faviconsDisabled = await this.utilsService
.getObjFromStorage(this.constantsService.disableFaviconKey); .getObjFromStorage<boolean>(this.constantsService.disableFaviconKey);
this.saveState('faviconEnabled', !faviconsDisabled); this.saveState('faviconEnabled', !faviconsDisabled);
} }

View File

@ -127,7 +127,7 @@ angular
$scope.rate = function () { $scope.rate = function () {
$analytics.eventTrack('Rate Extension'); $analytics.eventTrack('Rate Extension');
switch (utilsService.getBrowser()) { switch (utilsService.getBrowserString()) {
case 'chrome': case 'chrome':
chrome.tabs.create({ chrome.tabs.create({
url: 'https://chrome.google.com/webstore/detail/bitwarden-free-password-m/' + url: 'https://chrome.google.com/webstore/detail/bitwarden-free-password-m/' +

View File

@ -1,6 +1,8 @@
import * as angular from 'angular'; import * as angular from 'angular';
import * as template from './password-generator.component.html'; import * as template from './password-generator.component.html';
import { UtilsService } from '../../../services/abstractions/utils.service';
export class PasswordGeneratorController { export class PasswordGeneratorController {
$transition$: any; $transition$: any;
options: any; options: any;
@ -11,7 +13,7 @@ export class PasswordGeneratorController {
i18n: any; i18n: any;
constructor(private $state: any, private passwordGenerationService: 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) { private i18nService: any, private $timeout: any) {
this.i18n = i18nService; this.i18n = i18nService;

View File

@ -1,6 +1,7 @@
import UtilsService from '../../../services/utils.service';
import * as template from './tools.component.html'; import * as template from './tools.component.html';
import { UtilsService } from '../../../services/abstractions/utils.service';
class ToolsController { class ToolsController {
showExport: boolean; showExport: boolean;
i18n: any; i18n: any;

View File

@ -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<any>;
removeFromStorage(key: string): Promise<any>;
getObjFromStorage<T>(key: string): Promise<T>;
}

View File

@ -1,4 +1,5 @@
import { BrowserType } from '../enums/browserType.enum'; import { BrowserType } from '../enums/browserType.enum';
import { UtilsService as UtilsServiceInterface } from './abstractions/utils.service';
const AnalyticsIds = { const AnalyticsIds = {
[BrowserType.Chrome]: 'UA-81915606-6', [BrowserType.Chrome]: 'UA-81915606-6',
@ -7,7 +8,7 @@ const AnalyticsIds = {
[BrowserType.Edge]: 'UA-81915606-9', [BrowserType.Edge]: 'UA-81915606-9',
}; };
export default class UtilsService { export default class UtilsService implements UtilsServiceInterface {
static copyToClipboard(text: string, doc?: Document): void { static copyToClipboard(text: string, doc?: Document): void {
doc = doc || document; doc = doc || document;
if ((window as any).clipboardData && (window as any).clipboardData.setData) { if ((window as any).clipboardData && (window as any).clipboardData.setData) {
@ -264,8 +265,8 @@ export default class UtilsService {
return this.browserCache; return this.browserCache;
} }
getDeviceType(): number { getBrowserString(): string {
return this.getBrowser(); return BrowserType[this.getBrowser()].toLowerCase();
} }
isFirefox(): boolean { isFirefox(): boolean {
@ -293,7 +294,7 @@ export default class UtilsService {
return this.analyticsIdCache; return this.analyticsIdCache;
} }
initListSectionItemListeners(doc: Document, angular: any) { initListSectionItemListeners(doc: Document, angular: any): void {
if (!doc) { if (!doc) {
throw new Error('doc parameter required'); throw new Error('doc parameter required');
} }
@ -378,34 +379,32 @@ export default class UtilsService {
UtilsService.copyToClipboard(text, doc); UtilsService.copyToClipboard(text, doc);
} }
inSidebar(theWindow: Window) { inSidebar(theWindow: Window): boolean {
return theWindow.location.search !== '' && theWindow.location.search.indexOf('uilocation=sidebar') > -1; 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; 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; 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 || return theWindow.location.search === '' || theWindow.location.search.indexOf('uilocation=') === -1 ||
theWindow.location.search.indexOf('uilocation=popup') > -1; theWindow.location.search.indexOf('uilocation=popup') > -1;
} }
// remove these in favor of static saveObjToStorage(key: string, obj: any): Promise<any> {
saveObjToStorage(key: string, obj: any) {
return UtilsService.saveObjToStorage(key, obj); return UtilsService.saveObjToStorage(key, obj);
} }
removeFromStorage(key: string) { removeFromStorage(key: string): Promise<any> {
return UtilsService.removeFromStorage(key); return UtilsService.removeFromStorage(key);
} }
getObjFromStorage(key: string) { getObjFromStorage<T>(key: string): Promise<T> {
return UtilsService.getObjFromStorage(key); return UtilsService.getObjFromStorage<T>(key);
} }
} }