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 { 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:

View File

@ -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;

View File

@ -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;
}

View File

@ -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;
}

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';
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;

View File

@ -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) {
}

View File

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

View File

@ -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<boolean>(this.constantsService.disableFaviconKey);
this.saveState('faviconEnabled', !faviconsDisabled);
}

View File

@ -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/' +

View File

@ -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;

View File

@ -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;

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 { 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<any> {
return UtilsService.saveObjToStorage(key, obj);
}
removeFromStorage(key: string) {
removeFromStorage(key: string): Promise<any> {
return UtilsService.removeFromStorage(key);
}
getObjFromStorage(key: string) {
return UtilsService.getObjFromStorage(key);
getObjFromStorage<T>(key: string): Promise<T> {
return UtilsService.getObjFromStorage<T>(key);
}
}