1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-21 18:53:29 +00:00

Refactor for barrels. Utils service to jslib

This commit is contained in:
Kyle Spearrin
2018-01-06 22:13:48 -05:00
parent c018f096b4
commit 03258e50f7
56 changed files with 270 additions and 433 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -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<T>(key: string): Promise<T> {

View File

@@ -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<any> {
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,

View File

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

View File

@@ -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(() => {

View File

@@ -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<string, SymmetricCryptoKey>;
constructor(private storageService: StorageService, private secureStorageService: StorageService) {
constructor(private storageService: Abstractions.StorageService,
private secureStorageService: Abstractions.StorageService) {
}
async setKey(key: SymmetricCryptoKey): Promise<any> {
@@ -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<string> {
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<any> {
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<ArrayBuffer> {
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;
}

View File

@@ -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<void> {

View File

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

View File

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

View File

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

View File

@@ -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<PasswordHistory[]>(Keys.history).then((encrypted) => {
return this.decryptHistory(encrypted);
}).then((history) => {

View File

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

View File

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

View File

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

View File

@@ -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<string> {

View File

@@ -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<any> {

View File

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

View File

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