From 03258e50f7b52a91b4034412958f6430c52a1882 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Sat, 6 Jan 2018 22:13:48 -0500 Subject: [PATCH] Refactor for barrels. Utils service to jslib --- src/background/commands.background.ts | 5 +- src/background/contextMenus.background.ts | 9 +- src/background/idle.background.ts | 4 +- src/background/main.background.ts | 19 +-- src/background/runtime.background.ts | 11 +- src/background/webRequest.background.ts | 5 +- src/models/data/cipherData.ts | 12 +- src/models/data/fieldData.ts | 4 +- src/models/data/secureNoteData.ts | 4 +- src/models/domain/cipher.ts | 20 +-- src/models/domain/cipherString.ts | 22 +-- src/models/domain/field.ts | 4 +- src/models/domain/secureNote.ts | 4 +- src/models/domain/symmetricCryptoKey.ts | 20 +-- src/models/request/cipherRequest.ts | 12 +- src/models/request/deviceRequest.ts | 6 +- src/models/response/deviceResponse.ts | 4 +- .../components/action-buttons.component.ts | 5 +- src/popup/app/components/pop-out.component.ts | 5 +- src/popup/app/current/current.component.ts | 18 +- src/popup/app/global/main.controller.ts | 4 +- src/popup/app/lock/lock.component.ts | 7 +- src/popup/app/services/auth.service.ts | 6 +- src/popup/app/services/background.service.ts | 8 +- src/popup/app/services/state.service.ts | 4 +- .../app/settings/environment.component.ts | 5 +- .../settings/folders/add-folder.component.ts | 4 +- .../settings/folders/edit-folder.component.ts | 4 +- src/popup/app/settings/options.component.ts | 8 +- src/popup/app/settings/settings.component.ts | 24 +-- src/popup/app/tools/export.component.ts | 10 +- .../app/tools/password-generator.component.ts | 6 +- src/popup/app/tools/tools.component.ts | 4 +- src/services/api.service.ts | 4 +- src/services/appId.service.ts | 8 +- src/services/autofill.service.ts | 22 +-- src/services/browserMessaging.service.ts | 6 +- .../browserPlatformUtils.service.spec.ts | 10 +- src/services/browserPlatformUtils.service.ts | 44 ++--- src/services/browserStorage.service.ts | 6 +- src/services/cipher.service.ts | 16 +- src/services/collection.service.ts | 4 +- src/services/constants.service.ts | 4 +- src/services/crypto.service.ts | 59 ++++--- src/services/environment.service.ts | 4 +- src/services/folder.service.ts | 5 +- src/services/i18n.service.ts | 4 +- src/services/lock.service.ts | 5 +- src/services/passwordGeneration.service.ts | 9 +- src/services/settings.service.ts | 4 +- src/services/sync.service.ts | 6 +- src/services/token.service.ts | 7 +- src/services/totp.service.ts | 4 +- src/services/user.service.ts | 4 +- src/services/utils.service.spec.ts | 26 +-- src/services/utils.service.ts | 155 ------------------ 56 files changed, 270 insertions(+), 433 deletions(-) delete mode 100644 src/services/utils.service.ts diff --git a/src/background/commands.background.ts b/src/background/commands.background.ts index 2d2deded06..97af8a7f1d 100644 --- a/src/background/commands.background.ts +++ b/src/background/commands.background.ts @@ -3,7 +3,8 @@ import BrowserApi from '../browser/browserApi'; import MainBackground from './main.background'; import PasswordGenerationService from '../services/passwordGeneration.service'; -import UtilsService from '../services/utils.service'; + +import { Services } from '@bitwarden/jslib'; export default class CommandsBackground { private commands: any; @@ -34,7 +35,7 @@ export default class CommandsBackground { private async generatePasswordToClipboard() { const options = await this.passwordGenerationService.getOptions(); const password = PasswordGenerationService.generatePassword(options); - UtilsService.copyToClipboard(password); + Services.UtilsService.copyToClipboard(password); this.passwordGenerationService.addHistory(password); (window as any).ga('send', { diff --git a/src/background/contextMenus.background.ts b/src/background/contextMenus.background.ts index 51f30f93e7..8859af8e90 100644 --- a/src/background/contextMenus.background.ts +++ b/src/background/contextMenus.background.ts @@ -4,7 +4,8 @@ import MainBackground from './main.background'; import CipherService from '../services/cipher.service'; import PasswordGenerationService from '../services/passwordGeneration.service'; -import UtilsService from '../services/utils.service'; + +import { Services } from '@bitwarden/jslib'; export default class ContextMenusBackground { private contextMenus: any; @@ -32,7 +33,7 @@ export default class ContextMenusBackground { private async generatePasswordToClipboard() { const options = await this.passwordGenerationService.getOptions(); const password = PasswordGenerationService.generatePassword(options); - UtilsService.copyToClipboard(password); + Services.UtilsService.copyToClipboard(password); this.passwordGenerationService.addHistory(password); (window as any).ga('send', { @@ -68,13 +69,13 @@ export default class ContextMenusBackground { hitType: 'event', eventAction: 'Copied Username From Context Menu', }); - UtilsService.copyToClipboard(cipher.login.username); + Services.UtilsService.copyToClipboard(cipher.login.username); } else if (info.parentMenuItemId === 'copy-password') { (window as any).ga('send', { hitType: 'event', eventAction: 'Copied Password From Context Menu', }); - UtilsService.copyToClipboard(cipher.login.password); + Services.UtilsService.copyToClipboard(cipher.login.password); } break; diff --git a/src/background/idle.background.ts b/src/background/idle.background.ts index 5c679bfe0f..d03010e8e2 100644 --- a/src/background/idle.background.ts +++ b/src/background/idle.background.ts @@ -2,13 +2,13 @@ import ConstantsService from '../services/constants.service'; import LockService from '../services/lock.service'; import MainBackground from './main.background'; -import { StorageService } from '@bitwarden/jslib'; +import { Abstractions } from '@bitwarden/jslib'; export default class IdleBackground { private idle: any; constructor(private main: MainBackground, private lockService: LockService, - private storageService: StorageService) { + private storageService: Abstractions.StorageService) { this.idle = chrome.idle; } diff --git a/src/background/main.background.ts b/src/background/main.background.ts index 955f88f057..427803acd7 100644 --- a/src/background/main.background.ts +++ b/src/background/main.background.ts @@ -1,4 +1,4 @@ -import { CipherType } from '@bitwarden/jslib'; +import { Abstractions, Enums, Services } from '@bitwarden/jslib'; import { Cipher } from '../models/domain/cipher'; @@ -33,16 +33,13 @@ import SyncService from '../services/sync.service'; import TokenService from '../services/token.service'; import TotpService from '../services/totp.service'; import UserService from '../services/user.service'; -import UtilsService from '../services/utils.service'; - -import { MessagingService, PlatformUtilsService, StorageService } from '@bitwarden/jslib'; export default class MainBackground { - messagingService: MessagingService; - storageService: StorageService; + messagingService: Abstractions.MessagingService; + storageService: Abstractions.StorageService; i18nService: any; - platformUtilsService: PlatformUtilsService; - utilsService: UtilsService; + platformUtilsService: Abstractions.PlatformUtilsService; + utilsService: Abstractions.UtilsService; constantsService: ConstantsService; cryptoService: CryptoService; tokenService: TokenService; @@ -80,7 +77,7 @@ export default class MainBackground { constructor() { // Services - this.utilsService = new UtilsService(); + this.utilsService = new Services.UtilsService(); this.platformUtilsService = new BrowserPlatformUtilsService(); this.messagingService = new BrowserMessagingService(this.platformUtilsService); this.storageService = new BrowserStorageService(this.platformUtilsService); @@ -348,7 +345,7 @@ export default class MainBackground { } private async loadLoginContextMenuOptions(cipher: any) { - if (cipher == null || cipher.type !== CipherType.Login) { + if (cipher == null || cipher.type !== Enums.CipherType.Login) { return; } @@ -365,7 +362,7 @@ export default class MainBackground { private async loadContextMenuOptions(title: string, idSuffix: string, cipher: any) { if (!chrome.contextMenus || this.menuOptionsLoaded.indexOf(idSuffix) > -1 || - (cipher != null && cipher.type !== CipherType.Login)) { + (cipher != null && cipher.type !== Enums.CipherType.Login)) { return; } diff --git a/src/background/runtime.background.ts b/src/background/runtime.background.ts index 4fc78c0c6d..213558874a 100644 --- a/src/background/runtime.background.ts +++ b/src/background/runtime.background.ts @@ -1,4 +1,4 @@ -import { CipherType } from '@bitwarden/jslib'; +import { Abstractions, Enums, Services } from '@bitwarden/jslib'; import BrowserApi from '../browser/browserApi'; @@ -6,9 +6,6 @@ import MainBackground from './main.background'; import AutofillService from '../services/autofill.service'; import CipherService from '../services/cipher.service'; -import UtilsService from '../services/utils.service'; - -import { PlatformUtilsService } from '@bitwarden/jslib'; export default class RuntimeBackground { private runtime: any; @@ -16,7 +13,7 @@ export default class RuntimeBackground { private pageDetailsToAutoFill: any[] = []; constructor(private main: MainBackground, private autofillService: AutofillService, - private cipherService: CipherService, private platformUtilsService: PlatformUtilsService) { + private cipherService: CipherService, private platformUtilsService: Abstractions.PlatformUtilsService) { this.runtime = chrome.runtime; } @@ -149,7 +146,7 @@ export default class RuntimeBackground { favorite: false, name: loginInfo.name, notes: null, - type: CipherType.Login, + type: Enums.CipherType.Login, login: { uri: loginInfo.uri, username: loginInfo.username, @@ -180,7 +177,7 @@ export default class RuntimeBackground { } this.main.loginsToAdd.splice(i, 1); - const hostname = UtilsService.getHostname(tab.url); + const hostname = Services.UtilsService.getHostname(tab.url); await this.cipherService.saveNeverDomain(hostname); BrowserApi.tabSendMessage(tab, 'closeNotificationBar'); } diff --git a/src/background/webRequest.background.ts b/src/background/webRequest.background.ts index ba9e3ecf42..061e6263de 100644 --- a/src/background/webRequest.background.ts +++ b/src/background/webRequest.background.ts @@ -1,13 +1,14 @@ import CipherService from '../services/cipher.service'; -import { PlatformUtilsService } from '@bitwarden/jslib'; +import { Abstractions } from '@bitwarden/jslib'; export default class WebRequestBackground { private pendingAuthRequests: any[] = []; private webRequest: any; private isFirefox: boolean; - constructor(private platformUtilsService: PlatformUtilsService, private cipherService: CipherService) { + constructor(private platformUtilsService: Abstractions.PlatformUtilsService, + private cipherService: CipherService) { this.webRequest = (window as any).chrome.webRequest; this.isFirefox = platformUtilsService.isFirefox(); } diff --git a/src/models/data/cipherData.ts b/src/models/data/cipherData.ts index f8fc266d45..2bfed6c346 100644 --- a/src/models/data/cipherData.ts +++ b/src/models/data/cipherData.ts @@ -1,4 +1,4 @@ -import { CipherType } from '@bitwarden/jslib'; +import { Enums } from '@bitwarden/jslib'; import { AttachmentData } from './attachmentData'; import { CardData } from './cardData'; @@ -18,7 +18,7 @@ class CipherData { organizationUseTotp: boolean; favorite: boolean; revisionDate: string; - type: CipherType; + type: Enums.CipherType; sizeName: string; name: string; notes: string; @@ -51,16 +51,16 @@ class CipherData { this.notes = response.data.Notes; switch (this.type) { - case CipherType.Login: + case Enums.CipherType.Login: this.login = new LoginData(response.data); break; - case CipherType.SecureNote: + case Enums.CipherType.SecureNote: this.secureNote = new SecureNoteData(response.data); break; - case CipherType.Card: + case Enums.CipherType.Card: this.card = new CardData(response.data); break; - case CipherType.Identity: + case Enums.CipherType.Identity: this.identity = new IdentityData(response.data); break; default: diff --git a/src/models/data/fieldData.ts b/src/models/data/fieldData.ts index c7d28df368..99e68e5cdf 100644 --- a/src/models/data/fieldData.ts +++ b/src/models/data/fieldData.ts @@ -1,7 +1,7 @@ -import { FieldType } from '@bitwarden/jslib'; +import { Enums } from '@bitwarden/jslib'; class FieldData { - type: FieldType; + type: Enums.FieldType; name: string; value: string; diff --git a/src/models/data/secureNoteData.ts b/src/models/data/secureNoteData.ts index 832bf1b6df..22d5af918a 100644 --- a/src/models/data/secureNoteData.ts +++ b/src/models/data/secureNoteData.ts @@ -1,7 +1,7 @@ -import { SecureNoteType } from '@bitwarden/jslib'; +import { Enums } from '@bitwarden/jslib'; class SecureNoteData { - type: SecureNoteType; + type: Enums.SecureNoteType; constructor(data: any) { this.type = data.Type; diff --git a/src/models/domain/cipher.ts b/src/models/domain/cipher.ts index d4aba918ae..d89432c9c0 100644 --- a/src/models/domain/cipher.ts +++ b/src/models/domain/cipher.ts @@ -1,4 +1,4 @@ -import { CipherType } from '@bitwarden/jslib'; +import { Enums } from '@bitwarden/jslib'; import { CipherData } from '../data/cipherData'; @@ -19,7 +19,7 @@ class Cipher extends Domain { folderId: string; name: CipherString; notes: CipherString; - type: CipherType; + type: Enums.CipherType; favorite: boolean; organizationUseTotp: boolean; edit: boolean; @@ -54,16 +54,16 @@ class Cipher extends Domain { this.localData = localData; switch (this.type) { - case CipherType.Login: + case Enums.CipherType.Login: this.login = new Login(obj.login, alreadyEncrypted); break; - case CipherType.SecureNote: + case Enums.CipherType.SecureNote: this.secureNote = new SecureNote(obj.secureNote, alreadyEncrypted); break; - case CipherType.Card: + case Enums.CipherType.Card: this.card = new Card(obj.card, alreadyEncrypted); break; - case CipherType.Identity: + case Enums.CipherType.Identity: this.identity = new Identity(obj.identity, alreadyEncrypted); break; default: @@ -113,18 +113,18 @@ class Cipher extends Domain { }, this.organizationId); switch (this.type) { - case CipherType.Login: + case Enums.CipherType.Login: model.login = await this.login.decrypt(this.organizationId); model.subTitle = model.login.username; if (model.login.uri) { model.login.domain = BrowserPlatformUtilsService.getDomain(model.login.uri); } break; - case CipherType.SecureNote: + case Enums.CipherType.SecureNote: model.secureNote = await this.secureNote.decrypt(this.organizationId); model.subTitle = null; break; - case CipherType.Card: + case Enums.CipherType.Card: model.card = await this.card.decrypt(this.organizationId); model.subTitle = model.card.brand; if (model.card.number && model.card.number.length >= 4) { @@ -134,7 +134,7 @@ class Cipher extends Domain { model.subTitle += ('*' + model.card.number.substr(model.card.number.length - 4)); } break; - case CipherType.Identity: + case Enums.CipherType.Identity: model.identity = await this.identity.decrypt(this.organizationId); model.subTitle = ''; if (model.identity.firstName) { diff --git a/src/models/domain/cipherString.ts b/src/models/domain/cipherString.ts index be88b6ebd6..d17f1c3706 100644 --- a/src/models/domain/cipherString.ts +++ b/src/models/domain/cipherString.ts @@ -1,19 +1,19 @@ -import { EncryptionType } from '@bitwarden/jslib'; +import { Enums } from '@bitwarden/jslib'; import ContainerService from '../../services/container.service'; class CipherString { encryptedString?: string; - encryptionType?: EncryptionType; + encryptionType?: Enums.EncryptionType; decryptedValue?: string; cipherText?: string; initializationVector?: string; mac?: string; - constructor(encryptedStringOrType: string | EncryptionType, ct?: string, iv?: string, mac?: string) { + constructor(encryptedStringOrType: string | Enums.EncryptionType, ct?: string, iv?: string, mac?: string) { if (ct != null) { // ct and header - const encType = encryptedStringOrType as EncryptionType; + const encType = encryptedStringOrType as Enums.EncryptionType; this.encryptedString = encType + '.' + ct; // iv @@ -51,13 +51,13 @@ class CipherString { } } else { encPieces = this.encryptedString.split('|'); - this.encryptionType = encPieces.length === 3 ? EncryptionType.AesCbc128_HmacSha256_B64 : - EncryptionType.AesCbc256_B64; + this.encryptionType = encPieces.length === 3 ? Enums.EncryptionType.AesCbc128_HmacSha256_B64 : + Enums.EncryptionType.AesCbc256_B64; } switch (this.encryptionType) { - case EncryptionType.AesCbc128_HmacSha256_B64: - case EncryptionType.AesCbc256_HmacSha256_B64: + case Enums.EncryptionType.AesCbc128_HmacSha256_B64: + case Enums.EncryptionType.AesCbc256_HmacSha256_B64: if (encPieces.length !== 3) { return; } @@ -66,7 +66,7 @@ class CipherString { this.cipherText = encPieces[1]; this.mac = encPieces[2]; break; - case EncryptionType.AesCbc256_B64: + case Enums.EncryptionType.AesCbc256_B64: if (encPieces.length !== 2) { return; } @@ -74,8 +74,8 @@ class CipherString { this.initializationVector = encPieces[0]; this.cipherText = encPieces[1]; break; - case EncryptionType.Rsa2048_OaepSha256_B64: - case EncryptionType.Rsa2048_OaepSha1_B64: + case Enums.EncryptionType.Rsa2048_OaepSha256_B64: + case Enums.EncryptionType.Rsa2048_OaepSha1_B64: if (encPieces.length !== 1) { return; } diff --git a/src/models/domain/field.ts b/src/models/domain/field.ts index 2fa4f6d143..a174050e60 100644 --- a/src/models/domain/field.ts +++ b/src/models/domain/field.ts @@ -1,4 +1,4 @@ -import { FieldType } from '@bitwarden/jslib'; +import { Enums } from '@bitwarden/jslib'; import { FieldData } from '../data/fieldData'; @@ -8,7 +8,7 @@ import Domain from './domain'; class Field extends Domain { name: CipherString; vault: CipherString; - type: FieldType; + type: Enums.FieldType; constructor(obj?: FieldData, alreadyEncrypted: boolean = false) { super(); diff --git a/src/models/domain/secureNote.ts b/src/models/domain/secureNote.ts index e2ff830eaa..d2273cd398 100644 --- a/src/models/domain/secureNote.ts +++ b/src/models/domain/secureNote.ts @@ -1,11 +1,11 @@ -import { SecureNoteType } from '@bitwarden/jslib'; +import { Enums } from '@bitwarden/jslib'; import { SecureNoteData } from '../data/secureNoteData'; import Domain from './domain'; class SecureNote extends Domain { - type: SecureNoteType; + type: Enums.SecureNoteType; constructor(obj?: SecureNoteData, alreadyEncrypted: boolean = false) { super(); diff --git a/src/models/domain/symmetricCryptoKey.ts b/src/models/domain/symmetricCryptoKey.ts index 53d2348737..3cf6e91986 100644 --- a/src/models/domain/symmetricCryptoKey.ts +++ b/src/models/domain/symmetricCryptoKey.ts @@ -1,20 +1,18 @@ import * as forge from 'node-forge'; -import { EncryptionType } from '@bitwarden/jslib'; +import { Enums, Services } from '@bitwarden/jslib'; import SymmetricCryptoKeyBuffers from './symmetricCryptoKeyBuffers'; -import UtilsService from '../../services/utils.service'; - export default class SymmetricCryptoKey { key: string; keyB64: string; encKey: string; macKey: string; - encType: EncryptionType; + encType: Enums.EncryptionType; keyBuf: SymmetricCryptoKeyBuffers; - constructor(keyBytes: string, b64KeyBytes?: boolean, encType?: EncryptionType) { + constructor(keyBytes: string, b64KeyBytes?: boolean, encType?: Enums.EncryptionType) { if (b64KeyBytes) { keyBytes = forge.util.decode64(keyBytes); } @@ -32,9 +30,9 @@ export default class SymmetricCryptoKey { if (encType == null) { if (bufferLength === 32) { - encType = EncryptionType.AesCbc256_B64; + encType = Enums.EncryptionType.AesCbc256_B64; } else if (bufferLength === 64) { - encType = EncryptionType.AesCbc256_HmacSha256_B64; + encType = Enums.EncryptionType.AesCbc256_HmacSha256_B64; } else { throw new Error('Unable to determine encType.'); } @@ -44,13 +42,13 @@ export default class SymmetricCryptoKey { this.keyB64 = forge.util.encode64(keyBytes); this.encType = encType; - if (encType === EncryptionType.AesCbc256_B64 && bufferLength === 32) { + if (encType === Enums.EncryptionType.AesCbc256_B64 && bufferLength === 32) { this.encKey = keyBytes; this.macKey = null; - } else if (encType === EncryptionType.AesCbc128_HmacSha256_B64 && bufferLength === 32) { + } else if (encType === Enums.EncryptionType.AesCbc128_HmacSha256_B64 && bufferLength === 32) { this.encKey = buffer.getBytes(16); // first half this.macKey = buffer.getBytes(16); // second half - } else if (encType === EncryptionType.AesCbc256_HmacSha256_B64 && bufferLength === 64) { + } else if (encType === Enums.EncryptionType.AesCbc256_HmacSha256_B64 && bufferLength === 64) { this.encKey = buffer.getBytes(32); // first half this.macKey = buffer.getBytes(32); // second half } else { @@ -63,7 +61,7 @@ export default class SymmetricCryptoKey { return this.keyBuf; } - const key = UtilsService.fromB64ToArray(this.keyB64); + const key = Services.UtilsService.fromB64ToArray(this.keyB64); const keys = new SymmetricCryptoKeyBuffers(key.buffer); if (this.macKey) { diff --git a/src/models/request/cipherRequest.ts b/src/models/request/cipherRequest.ts index 956f7d3d62..64cf9866b1 100644 --- a/src/models/request/cipherRequest.ts +++ b/src/models/request/cipherRequest.ts @@ -1,7 +1,7 @@ -import { CipherType } from '@bitwarden/jslib'; +import { Enums } from '@bitwarden/jslib'; class CipherRequest { - type: CipherType; + type: Enums.CipherType; folderId: string; organizationId: string; name: string; @@ -22,7 +22,7 @@ class CipherRequest { this.favorite = cipher.favorite; switch (this.type) { - case CipherType.Login: + case Enums.CipherType.Login: this.login = { uri: cipher.login.uri ? cipher.login.uri.encryptedString : null, username: cipher.login.username ? cipher.login.username.encryptedString : null, @@ -30,12 +30,12 @@ class CipherRequest { totp: cipher.login.totp ? cipher.login.totp.encryptedString : null, }; break; - case CipherType.SecureNote: + case Enums.CipherType.SecureNote: this.secureNote = { type: cipher.secureNote.type, }; break; - case CipherType.Card: + case Enums.CipherType.Card: this.card = { cardholderName: cipher.card.cardholderName ? cipher.card.cardholderName.encryptedString : null, brand: cipher.card.brand ? cipher.card.brand.encryptedString : null, @@ -45,7 +45,7 @@ class CipherRequest { code: cipher.card.code ? cipher.card.code.encryptedString : null, }; break; - case CipherType.Identity: + case Enums.CipherType.Identity: this.identity = { title: cipher.identity.title ? cipher.identity.title.encryptedString : null, firstName: cipher.identity.firstName ? cipher.identity.firstName.encryptedString : null, diff --git a/src/models/request/deviceRequest.ts b/src/models/request/deviceRequest.ts index a6076f9782..24da902d3c 100644 --- a/src/models/request/deviceRequest.ts +++ b/src/models/request/deviceRequest.ts @@ -1,12 +1,12 @@ -import { DeviceType, PlatformUtilsService } from '@bitwarden/jslib'; +import { Abstractions, Enums } from '@bitwarden/jslib'; class DeviceRequest { - type: DeviceType; + type: Enums.DeviceType; name: string; identifier: string; pushToken?: string; - constructor(appId: string, platformUtilsService: PlatformUtilsService) { + constructor(appId: string, platformUtilsService: Abstractions.PlatformUtilsService) { this.type = platformUtilsService.getDevice(); this.name = platformUtilsService.getDeviceString(); this.identifier = appId; diff --git a/src/models/response/deviceResponse.ts b/src/models/response/deviceResponse.ts index 0ef59ba27d..9b9b5e3057 100644 --- a/src/models/response/deviceResponse.ts +++ b/src/models/response/deviceResponse.ts @@ -1,10 +1,10 @@ -import { DeviceType } from '@bitwarden/jslib'; +import { Enums } from '@bitwarden/jslib'; class DeviceResponse { id: string; name: number; identifier: string; - type: DeviceType; + type: Enums.DeviceType; creationDate: string; constructor(response: any) { diff --git a/src/popup/app/components/action-buttons.component.ts b/src/popup/app/components/action-buttons.component.ts index f1c84c01a0..5ac7ea638f 100644 --- a/src/popup/app/components/action-buttons.component.ts +++ b/src/popup/app/components/action-buttons.component.ts @@ -1,6 +1,6 @@ import * as template from './action-buttons.component.html'; -import { PlatformUtilsService } from '@bitwarden/jslib'; +import { Abstractions } from '@bitwarden/jslib'; export class ActionButtonsController implements ng.IController { onView: Function; @@ -11,7 +11,8 @@ export 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 platformUtilsService: PlatformUtilsService) { + private $timeout: any, private $window: any, + private platformUtilsService: Abstractions.PlatformUtilsService) { this.i18n = i18nService; this.constants = constantsService; } diff --git a/src/popup/app/components/pop-out.component.ts b/src/popup/app/components/pop-out.component.ts index d974bf8335..931b2014e4 100644 --- a/src/popup/app/components/pop-out.component.ts +++ b/src/popup/app/components/pop-out.component.ts @@ -1,11 +1,12 @@ import * as template from './pop-out.component.html'; -import { PlatformUtilsService } from '@bitwarden/jslib'; +import { Abstractions } from '@bitwarden/jslib'; export class PopOutController implements ng.IController { i18n: any; - constructor(private $analytics: any, private $window: any, private platformUtilsService: PlatformUtilsService, + constructor(private $analytics: any, private $window: any, + private platformUtilsService: Abstractions.PlatformUtilsService, private i18nService: any) { this.i18n = i18nService; } diff --git a/src/popup/app/current/current.component.ts b/src/popup/app/current/current.component.ts index 7d0bbae731..f85d77ac26 100644 --- a/src/popup/app/current/current.component.ts +++ b/src/popup/app/current/current.component.ts @@ -1,6 +1,5 @@ -import { CipherType } from '@bitwarden/jslib'; +import { Abstractions, Enums } from '@bitwarden/jslib'; -import { PlatformUtilsService, UtilsService } from '@bitwarden/jslib'; import * as template from './current.component.html'; export class CurrentController { @@ -17,8 +16,9 @@ export class CurrentController { inSidebar: boolean = false; disableSearch: boolean = false; - constructor($scope: any, private cipherService: any, private platformUtilsService: PlatformUtilsService, - private utilsService: UtilsService, private toastr: any, private $window: any, private $state: any, + constructor($scope: any, private cipherService: any, + private platformUtilsService: Abstractions.PlatformUtilsService, + private utilsService: Abstractions.UtilsService, private toastr: any, private $window: any, private $state: any, private $timeout: any, private autofillService: any, private $analytics: any, private i18nService: any, private $filter: any) { this.i18n = i18nService; @@ -117,8 +117,8 @@ export class CurrentController { }); const otherTypes = [ - CipherType.Card, - CipherType.Identity, + Enums.CipherType.Card, + Enums.CipherType.Identity, ]; this.cipherService.getAllDecryptedForDomain(this.domain, otherTypes).then((ciphers: any[]) => { @@ -131,13 +131,13 @@ export class CurrentController { sortedCiphers.forEach((cipher: any) => { switch (cipher.type) { - case CipherType.Login: + case Enums.CipherType.Login: loginCiphers.push(cipher); break; - case CipherType.Card: + case Enums.CipherType.Card: cardCiphers.push(cipher); break; - case CipherType.Identity: + case Enums.CipherType.Identity: identityCiphers.push(cipher); break; default: diff --git a/src/popup/app/global/main.controller.ts b/src/popup/app/global/main.controller.ts index 2f70b9c749..8c82d2f283 100644 --- a/src/popup/app/global/main.controller.ts +++ b/src/popup/app/global/main.controller.ts @@ -1,4 +1,4 @@ -import { UtilsService } from '@bitwarden/jslib'; +import { Abstractions } from '@bitwarden/jslib'; export class MainController implements ng.IController { smBody: boolean; @@ -6,7 +6,7 @@ export class MainController implements ng.IController { animation: string; constructor($scope: any, $transitions: any, $state: any, authService: any, toastr: any, - i18nService: any, $analytics: any, utilsService: UtilsService, $window: any) { + i18nService: any, $analytics: any, utilsService: Abstractions.UtilsService, $window: any) { this.animation = ''; this.xsBody = $window.screen.availHeight < 600; this.smBody = !this.xsBody && $window.screen.availHeight <= 800; diff --git a/src/popup/app/lock/lock.component.ts b/src/popup/app/lock/lock.component.ts index 7648517e01..19b84b4a69 100644 --- a/src/popup/app/lock/lock.component.ts +++ b/src/popup/app/lock/lock.component.ts @@ -3,15 +3,16 @@ import * as template from './lock.component.html'; import { CryptoService } from '../../../services/abstractions/crypto.service'; -import { MessagingService, PlatformUtilsService } from '@bitwarden/jslib'; +import { Abstractions } from '@bitwarden/jslib'; export class LockController { i18n: any; masterPassword: string; constructor(public $state: any, public i18nService: any, private $timeout: any, - private platformUtilsService: PlatformUtilsService, public cryptoService: CryptoService, public toastr: any, - public userService: any, public messagingService: MessagingService, public SweetAlert: any) { + private platformUtilsService: Abstractions.PlatformUtilsService, + public cryptoService: CryptoService, public toastr: any, public userService: any, + public messagingService: Abstractions.MessagingService, public SweetAlert: any) { this.i18n = i18nService; } diff --git a/src/popup/app/services/auth.service.ts b/src/popup/app/services/auth.service.ts index 8b62c6f2a9..7757894ac6 100644 --- a/src/popup/app/services/auth.service.ts +++ b/src/popup/app/services/auth.service.ts @@ -3,13 +3,13 @@ import { TokenRequest } from '../../../models/request/tokenRequest'; import { CryptoService } from '../../../services/abstractions/crypto.service'; -import { MessagingService, PlatformUtilsService } from '@bitwarden/jslib'; +import { Abstractions } from '@bitwarden/jslib'; class AuthService { constructor(public cryptoService: CryptoService, public apiService: any, public userService: any, public tokenService: any, public $rootScope: any, public appIdService: any, - public platformUtilsService: PlatformUtilsService, public constantsService: any, - public messagingService: MessagingService) { + public platformUtilsService: Abstractions.PlatformUtilsService, public constantsService: any, + public messagingService: Abstractions.MessagingService) { } async logIn(email: string, masterPassword: string, twoFactorProvider?: number, diff --git a/src/popup/app/services/background.service.ts b/src/popup/app/services/background.service.ts index 5ef03597b5..d9168bff8c 100644 --- a/src/popup/app/services/background.service.ts +++ b/src/popup/app/services/background.service.ts @@ -1,6 +1,6 @@ import { CryptoService } from '../../../services/abstractions/crypto.service'; -import { PlatformUtilsService, StorageService, UtilsService } from '@bitwarden/jslib'; +import { Abstractions } from '@bitwarden/jslib'; function getBackgroundService(service: string) { return (): T => { @@ -9,7 +9,7 @@ function getBackgroundService(service: string) { }; } -export const storageService = getBackgroundService('storageService'); +export const storageService = getBackgroundService('storageService'); export const tokenService = getBackgroundService('tokenService'); export const cryptoService = getBackgroundService('cryptoService'); export const userService = getBackgroundService('userService'); @@ -19,8 +19,8 @@ export const cipherService = getBackgroundService('cipherService' export const syncService = getBackgroundService('syncService'); export const autofillService = getBackgroundService('autofillService'); export const passwordGenerationService = getBackgroundService('passwordGenerationService'); -export const platformUtilsService = getBackgroundService('platformUtilsService'); -export const utilsService = getBackgroundService('utilsService'); +export const platformUtilsService = getBackgroundService('platformUtilsService'); +export const utilsService = getBackgroundService('utilsService'); export const appIdService = getBackgroundService('appIdService'); export const i18nService = getBackgroundService('i18nService'); export const constantsService = getBackgroundService('constantsService'); diff --git a/src/popup/app/services/state.service.ts b/src/popup/app/services/state.service.ts index 9dedbd9bc0..8515a6ebc4 100644 --- a/src/popup/app/services/state.service.ts +++ b/src/popup/app/services/state.service.ts @@ -1,9 +1,9 @@ -import { StorageService } from '@bitwarden/jslib'; +import { Abstractions } from '@bitwarden/jslib'; class StateService { private state: any = {}; - constructor(private storageService: StorageService, private constantsService: any) { + constructor(private storageService: Abstractions.StorageService, private constantsService: any) { } async init() { diff --git a/src/popup/app/settings/environment.component.ts b/src/popup/app/settings/environment.component.ts index 437b33d544..31da09d57a 100644 --- a/src/popup/app/settings/environment.component.ts +++ b/src/popup/app/settings/environment.component.ts @@ -1,7 +1,7 @@ import * as angular from 'angular'; import * as template from './environment.component.html'; -import { PlatformUtilsService } from '@bitwarden/jslib'; +import { Abstractions } from '@bitwarden/jslib'; export class EnvironmentController { iconsUrl: string; @@ -11,7 +11,8 @@ export class EnvironmentController { baseUrl: string; i18n: any; - constructor(private i18nService: any, private $analytics: any, platformUtilsService: PlatformUtilsService, + constructor(private i18nService: any, private $analytics: any, + platformUtilsService: Abstractions.PlatformUtilsService, private environmentService: any, private toastr: any, private $timeout: ng.ITimeoutService) { this.i18n = i18nService; diff --git a/src/popup/app/settings/folders/add-folder.component.ts b/src/popup/app/settings/folders/add-folder.component.ts index 1442a99e93..034fba2804 100644 --- a/src/popup/app/settings/folders/add-folder.component.ts +++ b/src/popup/app/settings/folders/add-folder.component.ts @@ -2,7 +2,7 @@ import * as angular from 'angular'; import { Folder } from '../../../../models/domain/folder'; import * as template from './add-folder.component.html'; -import { PlatformUtilsService } from '@bitwarden/jslib'; +import { Abstractions } from '@bitwarden/jslib'; export class AddFolderController { savePromise: any; @@ -10,7 +10,7 @@ export class AddFolderController { i18n: any; constructor(private folderService: any, private $state: any, private toastr: any, - platformUtilsService: PlatformUtilsService, private $analytics: any, private i18nService: any, + platformUtilsService: Abstractions.PlatformUtilsService, private $analytics: any, private i18nService: any, $timeout: any) { $timeout(() => { platformUtilsService.initListSectionItemListeners(document, angular); diff --git a/src/popup/app/settings/folders/edit-folder.component.ts b/src/popup/app/settings/folders/edit-folder.component.ts index 4f698c663e..2e15c790f0 100644 --- a/src/popup/app/settings/folders/edit-folder.component.ts +++ b/src/popup/app/settings/folders/edit-folder.component.ts @@ -2,7 +2,7 @@ import * as angular from 'angular'; import { Folder } from '../../../../models/domain/folder'; import * as template from './edit-folder.component.html'; -import { PlatformUtilsService } from '@bitwarden/jslib'; +import { Abstractions } from '@bitwarden/jslib'; export class EditFolderController { $transition$: any; @@ -12,7 +12,7 @@ export class EditFolderController { folder: Folder; constructor($scope: any, $stateParams: any, private folderService: any, private toastr: any, private $state: any, - private SweetAlert: any, platformUtilsService: PlatformUtilsService, private $analytics: any, + private SweetAlert: any, platformUtilsService: Abstractions.PlatformUtilsService, private $analytics: any, private i18nService: any, $timeout: any) { this.i18n = i18nService; diff --git a/src/popup/app/settings/options.component.ts b/src/popup/app/settings/options.component.ts index b8407579df..bf4e4a8103 100644 --- a/src/popup/app/settings/options.component.ts +++ b/src/popup/app/settings/options.component.ts @@ -1,6 +1,6 @@ import * as angular from 'angular'; -import { MessagingService, PlatformUtilsService, StorageService } from '@bitwarden/jslib'; +import { Abstractions } from '@bitwarden/jslib'; import StateService from '../services/state.service'; import * as template from './options.component.html'; @@ -15,9 +15,9 @@ export class OptionsController { i18n: any; constructor(private i18nService: any, private $analytics: any, private constantsService: any, - private platformUtilsService: PlatformUtilsService, private totpService: any, - private stateService: StateService, private storageService: StorageService, - public messagingService: MessagingService, private $timeout: ng.ITimeoutService) { + private platformUtilsService: Abstractions.PlatformUtilsService, private totpService: any, + private stateService: StateService, private storageService: Abstractions.StorageService, + public messagingService: Abstractions.MessagingService, private $timeout: ng.ITimeoutService) { this.i18n = i18nService; $timeout(() => { diff --git a/src/popup/app/settings/settings.component.ts b/src/popup/app/settings/settings.component.ts index b018ebbc02..f9fe04772e 100644 --- a/src/popup/app/settings/settings.component.ts +++ b/src/popup/app/settings/settings.component.ts @@ -1,6 +1,6 @@ import * as angular from 'angular'; -import { DeviceType, MessagingService, PlatformUtilsService, StorageService } from '@bitwarden/jslib'; +import { Abstractions, Enums } from '@bitwarden/jslib'; import { CryptoService } from '../../../services/abstractions/crypto.service'; import ConstantsService from '../../../services/constants.service'; @@ -8,17 +8,17 @@ import ConstantsService from '../../../services/constants.service'; import * as template from './settings.component.html'; const RateUrls = { - [DeviceType.Chrome]: + [Enums.DeviceType.Chrome]: 'https://chrome.google.com/webstore/detail/bitwarden-free-password-m/nngceckbapebfimnlniiiahkandclblb/reviews', - [DeviceType.Firefox]: + [Enums.DeviceType.Firefox]: 'https://addons.mozilla.org/en-US/firefox/addon/bitwarden-password-manager/#reviews', - [DeviceType.Opera]: + [Enums.DeviceType.Opera]: 'https://addons.opera.com/en/extensions/details/bitwarden-free-password-manager/#feedback-container', - [DeviceType.Edge]: + [Enums.DeviceType.Edge]: 'https://www.microsoft.com/store/p/bitwarden-free-password-manager/9p6kxl0svnnl', - [DeviceType.Vivaldi]: + [Enums.DeviceType.Vivaldi]: 'https://chrome.google.com/webstore/detail/bitwarden-free-password-m/nngceckbapebfimnlniiiahkandclblb/reviews', - [DeviceType.Safari]: + [Enums.DeviceType.Safari]: 'https://itunes.com', // TODO }; @@ -27,10 +27,12 @@ export class SettingsController { i18n: any; showOnLocked: boolean; - constructor(private $state: any, private SweetAlert: any, private platformUtilsService: PlatformUtilsService, - private $analytics: any, private i18nService: any, private constantsService: ConstantsService, - private cryptoService: CryptoService, private lockService: any, private storageService: StorageService, - public messagingService: MessagingService, private $timeout: ng.ITimeoutService) { + constructor(private $state: any, private SweetAlert: any, + private platformUtilsService: Abstractions.PlatformUtilsService, private $analytics: any, + private i18nService: any, private constantsService: ConstantsService, + private cryptoService: CryptoService, private lockService: any, + private storageService: Abstractions.StorageService, + public messagingService: Abstractions.MessagingService, private $timeout: ng.ITimeoutService) { this.i18n = i18nService; $timeout(() => { diff --git a/src/popup/app/tools/export.component.ts b/src/popup/app/tools/export.component.ts index daf8330dde..960c54eada 100644 --- a/src/popup/app/tools/export.component.ts +++ b/src/popup/app/tools/export.component.ts @@ -2,7 +2,7 @@ import * as angular from 'angular'; import * as papa from 'papaparse'; import * as template from './export.component.html'; -import { CipherType, UtilsService } from '@bitwarden/jslib'; +import { Abstractions, Enums } from '@bitwarden/jslib'; import { CryptoService } from '../../../services/abstractions/crypto.service'; @@ -11,7 +11,7 @@ export class ExportController { masterPassword: string; constructor(private $state: any, private cryptoService: CryptoService, - private toastr: any, private utilsService: UtilsService, private $analytics: any, + private toastr: any, private utilsService: Abstractions.UtilsService, private $analytics: any, private i18nService: any, private folderService: any, private cipherService: any, private $window: any, private userService: any) { this.i18n = i18nService; @@ -74,7 +74,7 @@ export class ExportController { const exportCiphers = []; for (const c of decCiphers) { // only export logins and secure notes - if (c.type !== CipherType.Login && c.type !== CipherType.SecureNote) { + if (c.type !== Enums.CipherType.Login && c.type !== Enums.CipherType.SecureNote) { continue; } @@ -105,14 +105,14 @@ export class ExportController { } switch (c.type) { - case CipherType.Login: + case Enums.CipherType.Login: cipher.type = 'login'; cipher.login_uri = c.login.uri; cipher.login_username = c.login.username; cipher.login_password = c.login.password; cipher.login_totp = c.login.totp; break; - case CipherType.SecureNote: + case Enums.CipherType.SecureNote: cipher.type = 'note'; break; default: diff --git a/src/popup/app/tools/password-generator.component.ts b/src/popup/app/tools/password-generator.component.ts index 016033e7ff..50a0e24427 100644 --- a/src/popup/app/tools/password-generator.component.ts +++ b/src/popup/app/tools/password-generator.component.ts @@ -1,7 +1,7 @@ import * as angular from 'angular'; import * as template from './password-generator.component.html'; -import { PlatformUtilsService } from '@bitwarden/jslib'; +import { Abstractions } from '@bitwarden/jslib'; export class PasswordGeneratorController { $transition$: any; @@ -13,8 +13,8 @@ export class PasswordGeneratorController { i18n: any; constructor(private $state: any, private passwordGenerationService: any, - private toastr: any, private platformUtilsService: PlatformUtilsService, private $analytics: any, - private i18nService: any, private $timeout: any) { + private toastr: any, private platformUtilsService: Abstractions.PlatformUtilsService, + private $analytics: any, private i18nService: any, private $timeout: any) { this.i18n = i18nService; passwordGenerationService.getOptions().then((options: any) => { diff --git a/src/popup/app/tools/tools.component.ts b/src/popup/app/tools/tools.component.ts index 3db5c005ae..974844f353 100644 --- a/src/popup/app/tools/tools.component.ts +++ b/src/popup/app/tools/tools.component.ts @@ -1,6 +1,6 @@ import * as template from './tools.component.html'; -import { PlatformUtilsService } from '@bitwarden/jslib'; +import { Abstractions } from '@bitwarden/jslib'; export class ToolsController { showExport: boolean; @@ -8,7 +8,7 @@ export class ToolsController { private webVaultBaseUrl: string = 'https://vault.bitwarden.com'; constructor(private SweetAlert: any, private i18nService: any, - private $analytics: any, private platformUtilsService: PlatformUtilsService, + private $analytics: any, private platformUtilsService: Abstractions.PlatformUtilsService, private environmentService: any) { this.i18n = i18nService; this.showExport = !platformUtilsService.isEdge(); diff --git a/src/services/api.service.ts b/src/services/api.service.ts index 08f9aee490..a24c674b2d 100644 --- a/src/services/api.service.ts +++ b/src/services/api.service.ts @@ -2,7 +2,7 @@ import AppIdService from './appId.service'; import ConstantsService from './constants.service'; import TokenService from './token.service'; -import { PlatformUtilsService } from '@bitwarden/jslib'; +import { Abstractions } from '@bitwarden/jslib'; import EnvironmentUrls from '../models/domain/environmentUrls'; @@ -36,7 +36,7 @@ export default class ApiService { deviceType: string; logoutCallback: Function; - constructor(private tokenService: TokenService, platformUtilsService: PlatformUtilsService, + constructor(private tokenService: TokenService, platformUtilsService: Abstractions.PlatformUtilsService, logoutCallback: Function) { this.logoutCallback = logoutCallback; this.deviceType = platformUtilsService.getDevice().toString(); diff --git a/src/services/appId.service.ts b/src/services/appId.service.ts index 50cbc65ba6..27dec20b43 100644 --- a/src/services/appId.service.ts +++ b/src/services/appId.service.ts @@ -1,9 +1,7 @@ -import UtilsService from './utils.service'; - -import { StorageService } from '@bitwarden/jslib'; +import { Abstractions, Services } from '@bitwarden/jslib'; export default class AppIdService { - constructor(private storageService: StorageService) { + constructor(private storageService: Abstractions.StorageService) { } getAppId(): Promise { @@ -20,7 +18,7 @@ export default class AppIdService { return existingId; } - const guid = UtilsService.newGuid(); + const guid = Services.UtilsService.newGuid(); await this.storageService.save(key, guid); return guid; } diff --git a/src/services/autofill.service.ts b/src/services/autofill.service.ts index 187ec4e34f..b009e68b5c 100644 --- a/src/services/autofill.service.ts +++ b/src/services/autofill.service.ts @@ -1,5 +1,4 @@ -import { CipherType } from '@bitwarden/jslib'; -import { FieldType } from '@bitwarden/jslib'; +import { Abstractions, Enums, Services } from '@bitwarden/jslib'; import AutofillField from '../models/domain/autofillField'; import AutofillPageDetails from '../models/domain/autofillPageDetails'; @@ -8,9 +7,6 @@ import AutofillScript from '../models/domain/autofillScript'; import CipherService from './cipher.service'; import TokenService from './token.service'; import TotpService from './totp.service'; -import UtilsService from './utils.service'; - -import { PlatformUtilsService } from '@bitwarden/jslib'; const CardAttributes: string[] = ['autoCompleteType', 'data-stripe', 'htmlName', 'htmlID', 'label-tag', 'placeholder', 'label-left', 'label-top']; @@ -94,8 +90,8 @@ var IsoProvinces: { [id: string]: string; } = { export default class AutofillService { constructor(public cipherService: CipherService, public tokenService: TokenService, - public totpService: TotpService, public utilsService: UtilsService, - public platformUtilsService: PlatformUtilsService) { + public totpService: TotpService, public utilsService: Services.UtilsService, + public platformUtilsService: Abstractions.PlatformUtilsService) { } getFormsWithPasswordFields(pageDetails: AutofillPageDetails): any[] { @@ -169,7 +165,7 @@ export default class AutofillService { fillScript: fillScript, }, { frameId: pd.frameId }); - if (options.cipher.type !== CipherType.Login || totpPromise || + if (options.cipher.type !== Enums.CipherType.Login || totpPromise || (options.fromBackground && this.platformUtilsService.isFirefox()) || options.skipTotp || !options.cipher.login.totp || !this.tokenService.getPremium()) { return; @@ -183,7 +179,7 @@ export default class AutofillService { return null; }).then((code: string) => { if (code) { - UtilsService.copyToClipboard(code); + Services.UtilsService.copyToClipboard(code); } return code; @@ -271,7 +267,7 @@ export default class AutofillService { const matchingIndex = this.findMatchingFieldIndex(field, fieldNames); if (matchingIndex > -1) { let val = fields[matchingIndex].value; - if (val == null && fields[matchingIndex].type === FieldType.Boolean) { + if (val == null && fields[matchingIndex].type === Enums.FieldType.Boolean) { val = 'false'; } @@ -283,13 +279,13 @@ export default class AutofillService { } switch (options.cipher.type) { - case CipherType.Login: + case Enums.CipherType.Login: fillScript = this.generateLoginFillScript(fillScript, pageDetails, filledFields, options); break; - case CipherType.Card: + case Enums.CipherType.Card: fillScript = this.generateCardFillScript(fillScript, pageDetails, filledFields, options); break; - case CipherType.Identity: + case Enums.CipherType.Identity: fillScript = this.generateIdentityFillScript(fillScript, pageDetails, filledFields, options); break; default: diff --git a/src/services/browserMessaging.service.ts b/src/services/browserMessaging.service.ts index 1ce2521a3c..b17acd3760 100644 --- a/src/services/browserMessaging.service.ts +++ b/src/services/browserMessaging.service.ts @@ -1,7 +1,7 @@ -import { MessagingService as MessagingServiceInterface, PlatformUtilsService } from '@bitwarden/jslib'; +import { Abstractions } from '@bitwarden/jslib'; -export default class BrowserMessagingService implements MessagingServiceInterface { - constructor(private platformUtilsService: PlatformUtilsService) { +export default class BrowserMessagingService implements Abstractions.MessagingService { + constructor(private platformUtilsService: Abstractions.PlatformUtilsService) { } send(subscriber: string, arg: any = {}) { diff --git a/src/services/browserPlatformUtils.service.spec.ts b/src/services/browserPlatformUtils.service.spec.ts index 369b8d7d27..353f824bf2 100644 --- a/src/services/browserPlatformUtils.service.spec.ts +++ b/src/services/browserPlatformUtils.service.spec.ts @@ -1,5 +1,5 @@ import BrowserPlatformUtilsService from './browserPlatformUtils.service'; -import { DeviceType } from '@bitwarden/jslib'; +import { Enums } from '@bitwarden/jslib'; describe('Browser Utils Service', () => { describe('getDomain', () => { @@ -48,7 +48,7 @@ describe('Browser Utils Service', () => { }); const browserPlatformUtilsService = new BrowserPlatformUtilsService(); - expect(browserPlatformUtilsService.getDevice()).toBe(DeviceType.Chrome); + expect(browserPlatformUtilsService.getDevice()).toBe(Enums.DeviceType.Chrome); }); it('should detect firefox', () => { @@ -58,7 +58,7 @@ describe('Browser Utils Service', () => { }); const browserPlatformUtilsService = new BrowserPlatformUtilsService(); - expect(browserPlatformUtilsService.getDevice()).toBe(DeviceType.Firefox); + expect(browserPlatformUtilsService.getDevice()).toBe(Enums.DeviceType.Firefox); }); it('should detect opera', () => { @@ -68,7 +68,7 @@ describe('Browser Utils Service', () => { }); const browserPlatformUtilsService = new BrowserPlatformUtilsService(); - expect(browserPlatformUtilsService.getDevice()).toBe(DeviceType.Opera); + expect(browserPlatformUtilsService.getDevice()).toBe(Enums.DeviceType.Opera); }); it('should detect edge', () => { @@ -78,7 +78,7 @@ describe('Browser Utils Service', () => { }); const browserPlatformUtilsService = new BrowserPlatformUtilsService(); - expect(browserPlatformUtilsService.getDevice()).toBe(DeviceType.Edge); + expect(browserPlatformUtilsService.getDevice()).toBe(Enums.DeviceType.Edge); }); }); }); diff --git a/src/services/browserPlatformUtils.service.ts b/src/services/browserPlatformUtils.service.ts index c25ddcf43e..e879c3d563 100644 --- a/src/services/browserPlatformUtils.service.ts +++ b/src/services/browserPlatformUtils.service.ts @@ -1,17 +1,17 @@ import * as tldjs from 'tldjs'; -import { DeviceType, PlatformUtilsService as PlatformUtilsServiceInterface } from '@bitwarden/jslib'; +import { Abstractions, Enums } from '@bitwarden/jslib'; const AnalyticsIds = { - [DeviceType.Chrome]: 'UA-81915606-6', - [DeviceType.Firefox]: 'UA-81915606-7', - [DeviceType.Opera]: 'UA-81915606-8', - [DeviceType.Edge]: 'UA-81915606-9', - [DeviceType.Vivaldi]: 'UA-81915606-15', - [DeviceType.Safari]: 'UA-81915606-16', + [Enums.DeviceType.Chrome]: 'UA-81915606-6', + [Enums.DeviceType.Firefox]: 'UA-81915606-7', + [Enums.DeviceType.Opera]: 'UA-81915606-8', + [Enums.DeviceType.Edge]: 'UA-81915606-9', + [Enums.DeviceType.Vivaldi]: 'UA-81915606-15', + [Enums.DeviceType.Safari]: 'UA-81915606-16', }; -export default class BrowserPlatformUtilsService implements PlatformUtilsServiceInterface { +export default class BrowserPlatformUtilsService implements Abstractions.PlatformUtilsService { static getDomain(uriString: string): string { if (uriString == null) { return null; @@ -49,56 +49,56 @@ export default class BrowserPlatformUtilsService implements PlatformUtilsService return ipRegex.test(ipString); } - private deviceCache: DeviceType = null; + private deviceCache: Enums.DeviceType = null; private analyticsIdCache: string = null; - getDevice(): DeviceType { + getDevice(): Enums.DeviceType { if (this.deviceCache) { return this.deviceCache; } if (navigator.userAgent.indexOf('Firefox') !== -1 || navigator.userAgent.indexOf('Gecko/') !== -1) { - this.deviceCache = DeviceType.Firefox; + this.deviceCache = Enums.DeviceType.Firefox; } else if ((!!(window as any).opr && !!opr.addons) || !!(window as any).opera || navigator.userAgent.indexOf(' OPR/') >= 0) { - this.deviceCache = DeviceType.Opera; + this.deviceCache = Enums.DeviceType.Opera; } else if (navigator.userAgent.indexOf(' Edge/') !== -1) { - this.deviceCache = DeviceType.Edge; + this.deviceCache = Enums.DeviceType.Edge; } else if (navigator.userAgent.indexOf(' Vivaldi/') !== -1) { - this.deviceCache = DeviceType.Vivaldi; + this.deviceCache = Enums.DeviceType.Vivaldi; } else if ((window as any).chrome) { - this.deviceCache = DeviceType.Chrome; + this.deviceCache = Enums.DeviceType.Chrome; } return this.deviceCache; } getDeviceString(): string { - return DeviceType[this.getDevice()].toLowerCase(); + return Enums.DeviceType[this.getDevice()].toLowerCase(); } isFirefox(): boolean { - return this.getDevice() === DeviceType.Firefox; + return this.getDevice() === Enums.DeviceType.Firefox; } isChrome(): boolean { - return this.getDevice() === DeviceType.Chrome; + return this.getDevice() === Enums.DeviceType.Chrome; } isEdge(): boolean { - return this.getDevice() === DeviceType.Edge; + return this.getDevice() === Enums.DeviceType.Edge; } isOpera(): boolean { - return this.getDevice() === DeviceType.Opera; + return this.getDevice() === Enums.DeviceType.Opera; } isVivaldi(): boolean { - return this.getDevice() === DeviceType.Vivaldi; + return this.getDevice() === Enums.DeviceType.Vivaldi; } isSafari(): boolean { - return this.getDevice() === DeviceType.Safari; + return this.getDevice() === Enums.DeviceType.Safari; } analyticsId(): string { diff --git a/src/services/browserStorage.service.ts b/src/services/browserStorage.service.ts index 131b057019..b4e1b5d93f 100644 --- a/src/services/browserStorage.service.ts +++ b/src/services/browserStorage.service.ts @@ -1,7 +1,7 @@ -import { PlatformUtilsService, StorageService as StorageServiceInterface } from '@bitwarden/jslib'; +import { Abstractions } from '@bitwarden/jslib'; -export default class BrowserStorageService implements StorageServiceInterface { - constructor(private platformUtilsService: PlatformUtilsService) { +export default class BrowserStorageService implements Abstractions.StorageService { + constructor(private platformUtilsService: Abstractions.PlatformUtilsService) { } get(key: string): Promise { diff --git a/src/services/cipher.service.ts b/src/services/cipher.service.ts index 2bda5e8d10..2460877db9 100644 --- a/src/services/cipher.service.ts +++ b/src/services/cipher.service.ts @@ -1,4 +1,4 @@ -import { CipherType } from '@bitwarden/jslib'; +import { Abstractions, Enums } from '@bitwarden/jslib'; import { Cipher } from '../models/domain/cipher'; import { CipherString } from '../models/domain/cipherString'; @@ -17,8 +17,6 @@ import CryptoService from './crypto.service'; import SettingsService from './settings.service'; import UserService from './user.service'; -import { StorageService } from '@bitwarden/jslib'; - const Keys = { ciphersPrefix: 'ciphers_', localData: 'sitesLocalData', @@ -70,7 +68,7 @@ export default class CipherService { constructor(private cryptoService: CryptoService, private userService: UserService, private settingsService: SettingsService, private apiService: ApiService, - private storageService: StorageService) { + private storageService: Abstractions.StorageService) { } clearCache(): void { @@ -223,7 +221,7 @@ export default class CipherService { const ciphersToReturn: any[] = []; ciphers.forEach((cipher) => { - if (domain && cipher.type === CipherType.Login && cipher.login.domain && + if (domain && cipher.type === Enums.CipherType.Login && cipher.login.domain && matchingDomains.indexOf(cipher.login.domain) > -1) { ciphersToReturn.push(cipher); } else if (includeOtherTypes && includeOtherTypes.indexOf(cipher.type) > -1) { @@ -464,7 +462,7 @@ export default class CipherService { private encryptCipherData(cipher: Cipher, model: any, key: SymmetricCryptoKey): Promise { switch (cipher.type) { - case CipherType.Login: + case Enums.CipherType.Login: model.login = {}; return this.encryptObjProperty(cipher.login, model.login, { uri: null, @@ -472,12 +470,12 @@ export default class CipherService { password: null, totp: null, }, key); - case CipherType.SecureNote: + case Enums.CipherType.SecureNote: model.secureNote = { type: cipher.secureNote.type, }; return Promise.resolve(); - case CipherType.Card: + case Enums.CipherType.Card: model.card = {}; return this.encryptObjProperty(cipher.card, model.card, { cardholderName: null, @@ -487,7 +485,7 @@ export default class CipherService { expYear: null, code: null, }, key); - case CipherType.Identity: + case Enums.CipherType.Identity: model.identity = {}; return this.encryptObjProperty(cipher.identity, model.identity, { title: null, diff --git a/src/services/collection.service.ts b/src/services/collection.service.ts index 2b6a04b6f8..890f12a25d 100644 --- a/src/services/collection.service.ts +++ b/src/services/collection.service.ts @@ -6,7 +6,7 @@ import { CollectionData } from '../models/data/collectionData'; import CryptoService from './crypto.service'; import UserService from './user.service'; -import { StorageService } from '@bitwarden/jslib'; +import { Abstractions } from '@bitwarden/jslib'; const Keys = { collectionsPrefix: 'collections_', @@ -16,7 +16,7 @@ export default class CollectionService { decryptedCollectionCache: any[]; constructor(private cryptoService: CryptoService, private userService: UserService, - private storageService: StorageService) { + private storageService: Abstractions.StorageService) { } clearCache(): void { diff --git a/src/services/constants.service.ts b/src/services/constants.service.ts index bc519d3aaa..f730d54a7c 100644 --- a/src/services/constants.service.ts +++ b/src/services/constants.service.ts @@ -1,4 +1,4 @@ -import { PlatformUtilsService } from '@bitwarden/jslib'; +import { Abstractions } from '@bitwarden/jslib'; export default class ConstantsService { static readonly environmentUrlsKey: string = 'environmentUrls'; @@ -57,7 +57,7 @@ export default class ConstantsService { twoFactorProviderInfo: any[]; - constructor(i18nService: any, platformUtilsService: PlatformUtilsService) { + constructor(i18nService: any, platformUtilsService: Abstractions.PlatformUtilsService) { if (platformUtilsService.isEdge()) { // delay for i18n fetch setTimeout(() => { diff --git a/src/services/crypto.service.ts b/src/services/crypto.service.ts index e648d30f6d..5db0366c43 100644 --- a/src/services/crypto.service.ts +++ b/src/services/crypto.service.ts @@ -1,6 +1,6 @@ import * as forge from 'node-forge'; -import { EncryptionType } from '@bitwarden/jslib'; +import { Abstractions, Enums, Services } from '@bitwarden/jslib'; import { CipherString } from '../models/domain/cipherString'; import EncryptedObject from '../models/domain/encryptedObject'; @@ -8,12 +8,9 @@ import SymmetricCryptoKey from '../models/domain/symmetricCryptoKey'; import { ProfileOrganizationResponse } from '../models/response/profileOrganizationResponse'; import ConstantsService from './constants.service'; -import UtilsService from './utils.service'; import { CryptoService as CryptoServiceInterface } from './abstractions/crypto.service'; -import { StorageService } from '@bitwarden/jslib'; - const Keys = { key: 'key', encOrgKeys: 'encOrgKeys', @@ -42,7 +39,8 @@ export default class CryptoService implements CryptoServiceInterface { private privateKey: ArrayBuffer; private orgKeys: Map; - constructor(private storageService: StorageService, private secureStorageService: StorageService) { + constructor(private storageService: Abstractions.StorageService, + private secureStorageService: Abstractions.StorageService) { } async setKey(key: SymmetricCryptoKey): Promise { @@ -150,7 +148,7 @@ export default class CryptoService implements CryptoServiceInterface { const privateKey = await this.decrypt(new CipherString(encPrivateKey), null, 'raw'); const privateKeyB64 = forge.util.encode64(privateKey); - this.privateKey = UtilsService.fromB64ToArray(privateKeyB64).buffer; + this.privateKey = Services.UtilsService.fromB64ToArray(privateKeyB64).buffer; return this.privateKey; } @@ -286,15 +284,15 @@ export default class CryptoService implements CryptoServiceInterface { let plainValueArr: Uint8Array; if (plainValueEncoding === 'utf8') { - plainValueArr = UtilsService.fromUtf8ToArray(plainValue as string); + plainValueArr = Services.UtilsService.fromUtf8ToArray(plainValue as string); } else { plainValueArr = plainValue as Uint8Array; } const encValue = await this.aesEncrypt(plainValueArr.buffer, key); - const iv = UtilsService.fromBufferToB64(encValue.iv.buffer); - const ct = UtilsService.fromBufferToB64(encValue.ct.buffer); - const mac = encValue.mac ? UtilsService.fromBufferToB64(encValue.mac.buffer) : null; + const iv = Services.UtilsService.fromBufferToB64(encValue.iv.buffer); + const ct = Services.UtilsService.fromBufferToB64(encValue.ct.buffer); + const mac = encValue.mac ? Services.UtilsService.fromBufferToB64(encValue.mac.buffer) : null; return new CipherString(encValue.key.encType, iv, ct, mac); } @@ -345,8 +343,8 @@ export default class CryptoService implements CryptoServiceInterface { let macBytes: Uint8Array = null; switch (encType) { - case EncryptionType.AesCbc128_HmacSha256_B64: - case EncryptionType.AesCbc256_HmacSha256_B64: + case Enums.EncryptionType.AesCbc128_HmacSha256_B64: + case Enums.EncryptionType.AesCbc256_HmacSha256_B64: if (encBytes.length <= 49) { // 1 + 16 + 32 + ctLength return null; } @@ -355,7 +353,7 @@ export default class CryptoService implements CryptoServiceInterface { macBytes = encBytes.slice(17, 49); ctBytes = encBytes.slice(49); break; - case EncryptionType.AesCbc256_B64: + case Enums.EncryptionType.AesCbc256_B64: if (encBytes.length <= 17) { // 1 + 16 + ctLength return null; } @@ -372,11 +370,11 @@ export default class CryptoService implements CryptoServiceInterface { async rsaDecrypt(encValue: string): Promise { const headerPieces = encValue.split('.'); - let encType: EncryptionType = null; + let encType: Enums.EncryptionType = null; let encPieces: string[]; if (headerPieces.length === 1) { - encType = EncryptionType.Rsa2048_OaepSha256_B64; + encType = Enums.EncryptionType.Rsa2048_OaepSha256_B64; encPieces = [headerPieces[0]]; } else if (headerPieces.length === 2) { try { @@ -386,14 +384,14 @@ export default class CryptoService implements CryptoServiceInterface { } switch (encType) { - case EncryptionType.Rsa2048_OaepSha256_B64: - case EncryptionType.Rsa2048_OaepSha1_B64: + case Enums.EncryptionType.Rsa2048_OaepSha256_B64: + case Enums.EncryptionType.Rsa2048_OaepSha1_B64: if (encPieces.length !== 1) { throw new Error('Invalid cipher format.'); } break; - case EncryptionType.Rsa2048_OaepSha256_HmacSha256_B64: - case EncryptionType.Rsa2048_OaepSha1_HmacSha256_B64: + case Enums.EncryptionType.Rsa2048_OaepSha256_HmacSha256_B64: + case Enums.EncryptionType.Rsa2048_OaepSha1_HmacSha256_B64: if (encPieces.length !== 2) { throw new Error('Invalid cipher format.'); } @@ -424,15 +422,15 @@ export default class CryptoService implements CryptoServiceInterface { let rsaAlgorithm: any = null; switch (encType) { - case EncryptionType.Rsa2048_OaepSha256_B64: - case EncryptionType.Rsa2048_OaepSha256_HmacSha256_B64: + case Enums.EncryptionType.Rsa2048_OaepSha256_B64: + case Enums.EncryptionType.Rsa2048_OaepSha256_HmacSha256_B64: rsaAlgorithm = { name: 'RSA-OAEP', hash: { name: 'SHA-256' }, }; break; - case EncryptionType.Rsa2048_OaepSha1_B64: - case EncryptionType.Rsa2048_OaepSha1_HmacSha256_B64: + case Enums.EncryptionType.Rsa2048_OaepSha1_B64: + case Enums.EncryptionType.Rsa2048_OaepSha1_HmacSha256_B64: rsaAlgorithm = { name: 'RSA-OAEP', hash: { name: 'SHA-1' }, @@ -443,9 +441,9 @@ export default class CryptoService implements CryptoServiceInterface { } const privateKey = await Subtle.importKey('pkcs8', privateKeyBytes, rsaAlgorithm, false, ['decrypt']); - const ctArr = UtilsService.fromB64ToArray(encPieces[0]); + const ctArr = Services.UtilsService.fromB64ToArray(encPieces[0]); const decBytes = await Subtle.decrypt(rsaAlgorithm, privateKey, ctArr.buffer); - const b64DecValue = UtilsService.fromBufferToB64(decBytes); + const b64DecValue = Services.UtilsService.fromBufferToB64(decBytes); return b64DecValue; } @@ -474,7 +472,7 @@ export default class CryptoService implements CryptoServiceInterface { return obj; } - private async aesDecrypt(encType: EncryptionType, ctBytes: string, ivBytes: string, macBytes: string, + private async aesDecrypt(encType: Enums.EncryptionType, ctBytes: string, ivBytes: string, macBytes: string, key: SymmetricCryptoKey): Promise { const keyForEnc = await this.getKeyForEncryption(key); const theKey = this.resolveLegacyKey(encType, keyForEnc); @@ -503,7 +501,7 @@ export default class CryptoService implements CryptoServiceInterface { return decipher; } - private async aesDecryptWC(encType: EncryptionType, ctBuf: ArrayBuffer, ivBuf: ArrayBuffer, + private async aesDecryptWC(encType: Enums.EncryptionType, ctBuf: ArrayBuffer, ivBuf: ArrayBuffer, macBuf: ArrayBuffer, key: SymmetricCryptoKey): Promise { const theKey = await this.getKeyForEncryption(key); const keyBuf = theKey.getBuffers(); @@ -589,11 +587,12 @@ export default class CryptoService implements CryptoServiceInterface { return encKey || (await this.getKey()); } - private resolveLegacyKey(encType: EncryptionType, key: SymmetricCryptoKey): SymmetricCryptoKey { - if (encType === EncryptionType.AesCbc128_HmacSha256_B64 && key.encType === EncryptionType.AesCbc256_B64) { + private resolveLegacyKey(encType: Enums.EncryptionType, key: SymmetricCryptoKey): SymmetricCryptoKey { + if (encType === Enums.EncryptionType.AesCbc128_HmacSha256_B64 && + key.encType === Enums.EncryptionType.AesCbc256_B64) { // Old encrypt-then-mac scheme, make a new key this.legacyEtmKey = this.legacyEtmKey || - new SymmetricCryptoKey(key.key, false, EncryptionType.AesCbc128_HmacSha256_B64); + new SymmetricCryptoKey(key.key, false, Enums.EncryptionType.AesCbc128_HmacSha256_B64); return this.legacyEtmKey; } diff --git a/src/services/environment.service.ts b/src/services/environment.service.ts index 55d2361eeb..fcacfb8f0f 100644 --- a/src/services/environment.service.ts +++ b/src/services/environment.service.ts @@ -1,7 +1,7 @@ import ApiService from './api.service'; import ConstantsService from './constants.service'; -import { StorageService } from '@bitwarden/jslib'; +import { Abstractions } from '@bitwarden/jslib'; import EnvironmentUrls from '../models/domain/environmentUrls'; @@ -12,7 +12,7 @@ export default class EnvironmentService { identityUrl: string; iconsUrl: string; - constructor(private apiService: ApiService, private storageService: StorageService) { + constructor(private apiService: ApiService, private storageService: Abstractions.StorageService) { } async setUrlsFromStorage(): Promise { diff --git a/src/services/folder.service.ts b/src/services/folder.service.ts index fbd89000c1..dc3f2ae833 100644 --- a/src/services/folder.service.ts +++ b/src/services/folder.service.ts @@ -10,7 +10,7 @@ import ApiService from './api.service'; import CryptoService from './crypto.service'; import UserService from './user.service'; -import { StorageService } from '@bitwarden/jslib'; +import { Abstractions } from '@bitwarden/jslib'; const Keys = { foldersPrefix: 'folders_', @@ -20,7 +20,8 @@ export default class FolderService { decryptedFolderCache: any[]; constructor(private cryptoService: CryptoService, private userService: UserService, - private i18nService: any, private apiService: ApiService, private storageService: StorageService) { + private i18nService: any, private apiService: ApiService, + private storageService: Abstractions.StorageService) { } clearCache(): void { diff --git a/src/services/i18n.service.ts b/src/services/i18n.service.ts index 50a332dae2..9196d2dc7b 100644 --- a/src/services/i18n.service.ts +++ b/src/services/i18n.service.ts @@ -1,6 +1,6 @@ -import { PlatformUtilsService } from '@bitwarden/jslib'; +import { Abstractions } from '@bitwarden/jslib'; -export default function i18nService(platformUtilsService: PlatformUtilsService) { +export default function i18nService(platformUtilsService: Abstractions.PlatformUtilsService) { const edgeMessages: any = {}; if (platformUtilsService.isEdge()) { diff --git a/src/services/lock.service.ts b/src/services/lock.service.ts index e1969a79a2..2fb47542c6 100644 --- a/src/services/lock.service.ts +++ b/src/services/lock.service.ts @@ -4,12 +4,13 @@ import ConstantsService from './constants.service'; import CryptoService from './crypto.service'; import FolderService from './folder.service'; -import { PlatformUtilsService, StorageService } from '@bitwarden/jslib'; +import { Abstractions } from '@bitwarden/jslib'; export default class LockService { constructor(private cipherService: CipherService, private folderService: FolderService, private collectionService: CollectionService, private cryptoService: CryptoService, - private platformUtilsService: PlatformUtilsService, private storageService: StorageService, + private platformUtilsService: Abstractions.PlatformUtilsService, + private storageService: Abstractions.StorageService, private setIcon: Function, private refreshBadgeAndMenu: Function) { this.checkLock(); setInterval(() => this.checkLock(), 10 * 1000); // check every 10 seconds diff --git a/src/services/passwordGeneration.service.ts b/src/services/passwordGeneration.service.ts index b9fa82be65..82536900ae 100644 --- a/src/services/passwordGeneration.service.ts +++ b/src/services/passwordGeneration.service.ts @@ -2,9 +2,8 @@ import { CipherString } from '../models/domain/cipherString'; import PasswordHistory from '../models/domain/passwordHistory'; import CryptoService from './crypto.service'; -import UtilsService from './utils.service'; -import { StorageService } from '@bitwarden/jslib'; +import { Abstractions, Services } from '@bitwarden/jslib'; const DefaultOptions = { length: 14, @@ -81,7 +80,7 @@ export default class PasswordGenerationService { // shuffle positions.sort(() => { - return UtilsService.secureRandomNumber(0, 1) * 2 - 1; + return Services.UtilsService.secureRandomNumber(0, 1) * 2 - 1; }); // build out the char sets @@ -137,7 +136,7 @@ export default class PasswordGenerationService { break; } - const randomCharIndex = UtilsService.secureRandomNumber(0, positionChars.length - 1); + const randomCharIndex = Services.UtilsService.secureRandomNumber(0, positionChars.length - 1); password += positionChars.charAt(randomCharIndex); } @@ -147,7 +146,7 @@ export default class PasswordGenerationService { optionsCache: any; history: PasswordHistory[] = []; - constructor(private cryptoService: CryptoService, private storageService: StorageService) { + constructor(private cryptoService: CryptoService, private storageService: Abstractions.StorageService) { storageService.get(Keys.history).then((encrypted) => { return this.decryptHistory(encrypted); }).then((history) => { diff --git a/src/services/settings.service.ts b/src/services/settings.service.ts index 983763cadf..c82ba26d46 100644 --- a/src/services/settings.service.ts +++ b/src/services/settings.service.ts @@ -1,6 +1,6 @@ import UserService from './user.service'; -import { StorageService } from '@bitwarden/jslib'; +import { Abstractions } from '@bitwarden/jslib'; const Keys = { settingsPrefix: 'settings_', @@ -10,7 +10,7 @@ const Keys = { export default class SettingsService { private settingsCache: any; - constructor(private userService: UserService, private storageService: StorageService) { + constructor(private userService: UserService, private storageService: Abstractions.StorageService) { } clearCache(): void { diff --git a/src/services/sync.service.ts b/src/services/sync.service.ts index e869ddbbeb..d3b268f563 100644 --- a/src/services/sync.service.ts +++ b/src/services/sync.service.ts @@ -17,7 +17,7 @@ import FolderService from './folder.service'; import SettingsService from './settings.service'; import UserService from './user.service'; -import { MessagingService, StorageService } from '@bitwarden/jslib'; +import { Abstractions } from '@bitwarden/jslib'; const Keys = { lastSyncPrefix: 'lastSync_', @@ -29,8 +29,8 @@ export default class SyncService { constructor(private userService: UserService, private apiService: ApiService, private settingsService: SettingsService, private folderService: FolderService, private cipherService: CipherService, private cryptoService: CryptoService, - private collectionService: CollectionService, private storageService: StorageService, - private messagingService: MessagingService, private logoutCallback: Function) { + private collectionService: CollectionService, private storageService: Abstractions.StorageService, + private messagingService: Abstractions.MessagingService, private logoutCallback: Function) { } async getLastSync() { diff --git a/src/services/token.service.ts b/src/services/token.service.ts index d06e4ca13c..933fb4e016 100644 --- a/src/services/token.service.ts +++ b/src/services/token.service.ts @@ -1,7 +1,6 @@ import ConstantsService from './constants.service'; -import UtilsService from './utils.service'; -import { StorageService } from '@bitwarden/jslib'; +import { Abstractions, Services } from '@bitwarden/jslib'; const Keys = { accessToken: 'accessToken', @@ -14,7 +13,7 @@ export default class TokenService { decodedToken: any; refreshToken: string; - constructor(private storageService: StorageService) { + constructor(private storageService: Abstractions.StorageService) { } setTokens(accessToken: string, refreshToken: string): Promise { @@ -93,7 +92,7 @@ export default class TokenService { throw new Error('JWT must have 3 parts'); } - const decoded = UtilsService.urlBase64Decode(parts[1]); + const decoded = Services.UtilsService.urlBase64Decode(parts[1]); if (decoded == null) { throw new Error('Cannot decode the token'); } diff --git a/src/services/totp.service.ts b/src/services/totp.service.ts index 059db51995..b7edf0fc00 100644 --- a/src/services/totp.service.ts +++ b/src/services/totp.service.ts @@ -1,6 +1,6 @@ import ConstantsService from './constants.service'; -import { StorageService } from '@bitwarden/jslib'; +import { Abstractions } from '@bitwarden/jslib'; const b32Chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567'; @@ -10,7 +10,7 @@ const TotpAlgorithm = { }; export default class TotpService { - constructor(private storageService: StorageService) { + constructor(private storageService: Abstractions.StorageService) { } async getCode(keyb32: string): Promise { diff --git a/src/services/user.service.ts b/src/services/user.service.ts index c37649111c..de71cf9e0d 100644 --- a/src/services/user.service.ts +++ b/src/services/user.service.ts @@ -1,6 +1,6 @@ import TokenService from './token.service'; -import { StorageService } from '@bitwarden/jslib'; +import { Abstractions } from '@bitwarden/jslib'; const Keys = { userId: 'userId', @@ -13,7 +13,7 @@ export default class UserService { email: string; stamp: string; - constructor(private tokenService: TokenService, private storageService: StorageService) { + constructor(private tokenService: TokenService, private storageService: Abstractions.StorageService) { } setUserIdAndEmail(userId: string, email: string): Promise { diff --git a/src/services/utils.service.spec.ts b/src/services/utils.service.spec.ts index fe3843e1fd..cd27c38c1b 100644 --- a/src/services/utils.service.spec.ts +++ b/src/services/utils.service.spec.ts @@ -1,32 +1,32 @@ -import UtilsService from './utils.service'; +import { Services } from '@bitwarden/jslib'; describe('Utils Service', () => { describe('getHostname', () => { it('should fail for invalid urls', () => { - expect(UtilsService.getHostname(null)).toBeNull(); - expect(UtilsService.getHostname(undefined)).toBeNull(); - expect(UtilsService.getHostname(' ')).toBeNull(); - expect(UtilsService.getHostname('https://bit!:"_&ward.com')).toBeNull(); - expect(UtilsService.getHostname('bitwarden')).toBeNull(); + expect(Services.UtilsService.getHostname(null)).toBeNull(); + expect(Services.UtilsService.getHostname(undefined)).toBeNull(); + expect(Services.UtilsService.getHostname(' ')).toBeNull(); + expect(Services.UtilsService.getHostname('https://bit!:"_&ward.com')).toBeNull(); + expect(Services.UtilsService.getHostname('bitwarden')).toBeNull(); }); it('should handle valid urls', () => { - expect(UtilsService.getHostname('https://bitwarden.com')).toBe('bitwarden.com'); - expect(UtilsService.getHostname('http://bitwarden.com')).toBe('bitwarden.com'); - expect(UtilsService.getHostname('http://vault.bitwarden.com')).toBe('vault.bitwarden.com'); - expect(UtilsService.getHostname('https://user:password@bitwarden.com:8080/password/sites?and&query#hash')).toBe('bitwarden.com'); + expect(Services.UtilsService.getHostname('https://bitwarden.com')).toBe('bitwarden.com'); + expect(Services.UtilsService.getHostname('http://bitwarden.com')).toBe('bitwarden.com'); + expect(Services.UtilsService.getHostname('http://vault.bitwarden.com')).toBe('vault.bitwarden.com'); + expect(Services.UtilsService.getHostname('https://user:password@bitwarden.com:8080/password/sites?and&query#hash')).toBe('bitwarden.com'); }); it('should support localhost and IP', () => { - expect(UtilsService.getHostname('https://localhost')).toBe('localhost'); - expect(UtilsService.getHostname('https://192.168.1.1')).toBe('192.168.1.1'); + expect(Services.UtilsService.getHostname('https://localhost')).toBe('localhost'); + expect(Services.UtilsService.getHostname('https://192.168.1.1')).toBe('192.168.1.1'); }); }); describe('newGuid', () => { it('should create a valid guid', () => { const validGuid = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i; - expect(UtilsService.newGuid()).toMatch(validGuid); + expect(Services.UtilsService.newGuid()).toMatch(validGuid); }); }); }); diff --git a/src/services/utils.service.ts b/src/services/utils.service.ts deleted file mode 100644 index 01f671260a..0000000000 --- a/src/services/utils.service.ts +++ /dev/null @@ -1,155 +0,0 @@ -import { UtilsService as UtilsServiceInterface } from '@bitwarden/jslib'; - -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) { - // IE specific code path to prevent textarea being shown while dialog is visible. - (window as any).clipboardData.setData('Text', text); - } else if (doc.queryCommandSupported && doc.queryCommandSupported('copy')) { - const textarea = doc.createElement('textarea'); - textarea.textContent = text; - // Prevent scrolling to bottom of page in MS Edge. - textarea.style.position = 'fixed'; - doc.body.appendChild(textarea); - textarea.select(); - - try { - // Security exception may be thrown by some browsers. - doc.execCommand('copy'); - } catch (e) { - // tslint:disable-next-line - console.warn('Copy to clipboard failed.', e); - } finally { - doc.body.removeChild(textarea); - } - } - } - - static urlBase64Decode(str: string): string { - let output = str.replace(/-/g, '+').replace(/_/g, '/'); - switch (output.length % 4) { - case 0: - break; - case 2: - output += '=='; - break; - case 3: - output += '='; - break; - default: - throw new Error('Illegal base64url string!'); - } - - return decodeURIComponent(escape(window.atob(output))); - } - - // ref: http://stackoverflow.com/a/2117523/1090359 - static newGuid(): string { - return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => { - // tslint:disable-next-line - const r = Math.random() * 16 | 0; - // tslint:disable-next-line - const v = c === 'x' ? r : (r & 0x3 | 0x8); - return v.toString(16); - }); - } - - // EFForg/OpenWireless - // ref https://github.com/EFForg/OpenWireless/blob/master/app/js/diceware.js - static secureRandomNumber(min: number, max: number): number { - let rval = 0; - const range = max - min + 1; - const bitsNeeded = Math.ceil(Math.log2(range)); - if (bitsNeeded > 53) { - throw new Error('We cannot generate numbers larger than 53 bits.'); - } - - const bytesNeeded = Math.ceil(bitsNeeded / 8); - const mask = Math.pow(2, bitsNeeded) - 1; - // 7776 -> (2^13 = 8192) -1 == 8191 or 0x00001111 11111111 - - // Create byte array and fill with N random numbers - const byteArray = new Uint8Array(bytesNeeded); - window.crypto.getRandomValues(byteArray); - - let p = (bytesNeeded - 1) * 8; - for (let i = 0; i < bytesNeeded; i++) { - rval += byteArray[i] * Math.pow(2, p); - p -= 8; - } - - // Use & to apply the mask and reduce the number of recursive lookups - // tslint:disable-next-line - rval = rval & mask; - - if (rval >= range) { - // Integer out of acceptable range - return UtilsService.secureRandomNumber(min, max); - } - - // Return an integer that falls within the range - return min + rval; - } - - static fromB64ToArray(str: string): Uint8Array { - const binaryString = window.atob(str); - const bytes = new Uint8Array(binaryString.length); - for (let i = 0; i < binaryString.length; i++) { - bytes[i] = binaryString.charCodeAt(i); - } - return bytes; - } - - static fromUtf8ToArray(str: string): Uint8Array { - const strUtf8 = unescape(encodeURIComponent(str)); - const arr = new Uint8Array(strUtf8.length); - for (let i = 0; i < strUtf8.length; i++) { - arr[i] = strUtf8.charCodeAt(i); - } - return arr; - } - - static fromBufferToB64(buffer: ArrayBuffer): string { - let binary = ''; - const bytes = new Uint8Array(buffer); - for (let i = 0; i < bytes.byteLength; i++) { - binary += String.fromCharCode(bytes[i]); - } - return window.btoa(binary); - } - - static fromBufferToUtf8(buffer: ArrayBuffer): string { - const bytes = new Uint8Array(buffer); - const encodedString = String.fromCharCode.apply(null, bytes); - return decodeURIComponent(escape(encodedString)); - } - - static getHostname(uriString: string): string { - if (uriString == null) { - return null; - } - - uriString = uriString.trim(); - if (uriString === '') { - return null; - } - - if (uriString.startsWith('http://') || uriString.startsWith('https://')) { - try { - const url = new URL(uriString); - return url.hostname; - } catch (e) { } - } - - return null; - } - - getHostname(uriString: string): string { - return UtilsService.getHostname(uriString); - } - - copyToClipboard(text: string, doc?: Document) { - UtilsService.copyToClipboard(text, doc); - } -}