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

import ts through node_modules alias

This commit is contained in:
Kyle Spearrin
2018-01-09 14:26:20 -05:00
parent e165b5cc70
commit f51bebd99a
49 changed files with 515 additions and 322 deletions

View File

@@ -2,7 +2,36 @@ import AppIdService from './appId.service';
import ConstantsService from './constants.service';
import TokenService from './token.service';
import { Abstractions, Domain, Request as Req, Response as Res } from '@bitwarden/jslib';
import { PlatformUtilsService } from 'jslib/abstractions';
import { EnvironmentUrls } from 'jslib/models/domain';
import {
CipherRequest,
DeviceRequest,
DeviceTokenRequest,
FolderRequest,
PasswordHintRequest,
RegisterRequest,
TokenRequest,
TwoFactorEmailRequest,
} from 'jslib/models/request';
import {
AttachmentResponse,
CipherResponse,
DeviceResponse,
DomainsResponse,
ErrorResponse,
FolderResponse,
GlobalDomainResponse,
IdentityTokenResponse,
KeysResponse,
ListResponse,
ProfileOrganizationResponse,
ProfileResponse,
SyncResponse,
} from 'jslib/models/response';
export default class ApiService {
urlsSet: boolean = false;
@@ -11,13 +40,13 @@ export default class ApiService {
deviceType: string;
logoutCallback: Function;
constructor(private tokenService: TokenService, platformUtilsService: Abstractions.PlatformUtilsService,
constructor(private tokenService: TokenService, platformUtilsService: PlatformUtilsService,
logoutCallback: Function) {
this.logoutCallback = logoutCallback;
this.deviceType = platformUtilsService.getDevice().toString();
}
setUrls(urls: Domain.EnvironmentUrls) {
setUrls(urls: EnvironmentUrls) {
this.urlsSet = true;
if (urls.base != null) {
@@ -57,7 +86,7 @@ export default class ApiService {
// Auth APIs
async postIdentityToken(request: Req.Token): Promise<Res.IdentityToken | any> {
async postIdentityToken(request: TokenRequest): Promise<IdentityTokenResponse | any> {
const response = await fetch(new Request(this.identityBaseUrl + '/connect/token', {
body: this.qsStringify(request.toIdentityToken()),
cache: 'no-cache',
@@ -77,7 +106,7 @@ export default class ApiService {
if (responseJson != null) {
if (response.status === 200) {
return new Res.IdentityToken(responseJson);
return new IdentityTokenResponse(responseJson);
} else if (response.status === 400 && responseJson.TwoFactorProviders2 &&
Object.keys(responseJson.TwoFactorProviders2).length) {
await this.tokenService.clearTwoFactorToken(request.email);
@@ -85,7 +114,7 @@ export default class ApiService {
}
}
return Promise.reject(new Res.Error(responseJson, response.status, true));
return Promise.reject(new ErrorResponse(responseJson, response.status, true));
}
async refreshIdentityToken(): Promise<any> {
@@ -98,7 +127,7 @@ export default class ApiService {
// Two Factor APIs
async postTwoFactorEmail(request: Req.TwoFactorEmail): Promise<any> {
async postTwoFactorEmail(request: TwoFactorEmailRequest): Promise<any> {
const response = await fetch(new Request(this.baseUrl + '/two-factor/send-email-login', {
body: JSON.stringify(request),
cache: 'no-cache',
@@ -136,7 +165,7 @@ export default class ApiService {
}
}
async postPasswordHint(request: Req.PasswordHint): Promise<any> {
async postPasswordHint(request: PasswordHintRequest): Promise<any> {
const response = await fetch(new Request(this.baseUrl + '/accounts/password-hint', {
body: JSON.stringify(request),
cache: 'no-cache',
@@ -153,7 +182,7 @@ export default class ApiService {
}
}
async postRegister(request: Req.Register): Promise<any> {
async postRegister(request: RegisterRequest): Promise<any> {
const response = await fetch(new Request(this.baseUrl + '/accounts/register', {
body: JSON.stringify(request),
cache: 'no-cache',
@@ -172,7 +201,7 @@ export default class ApiService {
// Folder APIs
async postFolder(request: Req.Folder): Promise<Res.Folder> {
async postFolder(request: FolderRequest): Promise<FolderResponse> {
const authHeader = await this.handleTokenState();
const response = await fetch(new Request(this.baseUrl + '/folders', {
body: JSON.stringify(request),
@@ -188,14 +217,14 @@ export default class ApiService {
if (response.status === 200) {
const responseJson = await response.json();
return new Res.Folder(responseJson);
return new FolderResponse(responseJson);
} else {
const error = await this.handleError(response, false);
return Promise.reject(error);
}
}
async putFolder(id: string, request: Req.Folder): Promise<Res.Folder> {
async putFolder(id: string, request: FolderRequest): Promise<FolderResponse> {
const authHeader = await this.handleTokenState();
const response = await fetch(new Request(this.baseUrl + '/folders/' + id, {
body: JSON.stringify(request),
@@ -211,7 +240,7 @@ export default class ApiService {
if (response.status === 200) {
const responseJson = await response.json();
return new Res.Folder(responseJson);
return new FolderResponse(responseJson);
} else {
const error = await this.handleError(response, false);
return Promise.reject(error);
@@ -237,7 +266,7 @@ export default class ApiService {
// Cipher APIs
async postCipher(request: Req.Cipher): Promise<Res.Cipher> {
async postCipher(request: CipherRequest): Promise<CipherResponse> {
const authHeader = await this.handleTokenState();
const response = await fetch(new Request(this.baseUrl + '/ciphers', {
body: JSON.stringify(request),
@@ -253,14 +282,14 @@ export default class ApiService {
if (response.status === 200) {
const responseJson = await response.json();
return new Res.Cipher(responseJson);
return new CipherResponse(responseJson);
} else {
const error = await this.handleError(response, false);
return Promise.reject(error);
}
}
async putCipher(id: string, request: Req.Cipher): Promise<Res.Cipher> {
async putCipher(id: string, request: CipherRequest): Promise<CipherResponse> {
const authHeader = await this.handleTokenState();
const response = await fetch(new Request(this.baseUrl + '/ciphers/' + id, {
body: JSON.stringify(request),
@@ -276,7 +305,7 @@ export default class ApiService {
if (response.status === 200) {
const responseJson = await response.json();
return new Res.Cipher(responseJson);
return new CipherResponse(responseJson);
} else {
const error = await this.handleError(response, false);
return Promise.reject(error);
@@ -302,7 +331,7 @@ export default class ApiService {
// Attachments APIs
async postCipherAttachment(id: string, data: FormData): Promise<Res.Cipher> {
async postCipherAttachment(id: string, data: FormData): Promise<CipherResponse> {
const authHeader = await this.handleTokenState();
const response = await fetch(new Request(this.baseUrl + '/ciphers/' + id + '/attachment', {
body: data,
@@ -317,7 +346,7 @@ export default class ApiService {
if (response.status === 200) {
const responseJson = await response.json();
return new Res.Cipher(responseJson);
return new CipherResponse(responseJson);
} else {
const error = await this.handleError(response, false);
return Promise.reject(error);
@@ -343,7 +372,7 @@ export default class ApiService {
// Sync APIs
async getSync(): Promise<Res.Sync> {
async getSync(): Promise<SyncResponse> {
const authHeader = await this.handleTokenState();
const response = await fetch(new Request(this.baseUrl + '/sync', {
cache: 'no-cache',
@@ -356,7 +385,7 @@ export default class ApiService {
if (response.status === 200) {
const responseJson = await response.json();
return new Res.Sync(responseJson);
return new SyncResponse(responseJson);
} else {
const error = await this.handleError(response, false);
return Promise.reject(error);
@@ -365,7 +394,7 @@ export default class ApiService {
// Helpers
private async handleError(response: Response, tokenError: boolean): Promise<Res.Error> {
private async handleError(response: Response, tokenError: boolean): Promise<ErrorResponse> {
if ((tokenError && response.status === 400) || response.status === 401 || response.status === 403) {
this.logoutCallback(true);
return null;
@@ -377,7 +406,7 @@ export default class ApiService {
responseJson = await response.json();
}
return new Res.Error(responseJson, response.status, tokenError);
return new ErrorResponse(responseJson, response.status, tokenError);
}
private async handleTokenState(): Promise<string> {
@@ -392,7 +421,7 @@ export default class ApiService {
return 'Bearer ' + accessToken;
}
private async doRefreshToken(): Promise<Res.IdentityToken> {
private async doRefreshToken(): Promise<IdentityTokenResponse> {
const refreshToken = await this.tokenService.getRefreshToken();
if (refreshToken == null || refreshToken === '') {
throw new Error();
@@ -415,7 +444,7 @@ export default class ApiService {
if (response.status === 200) {
const responseJson = await response.json();
const tokenResponse = new Res.IdentityToken(responseJson);
const tokenResponse = new IdentityTokenResponse(responseJson);
await this.tokenService.setTokens(tokenResponse.accessToken, tokenResponse.refreshToken);
return tokenResponse;
} else {

View File

@@ -1,7 +1,9 @@
import { Abstractions, Services } from '@bitwarden/jslib';
import { UtilsService } from 'jslib/services';
import { StorageService } from 'jslib/abstractions';
export default class AppIdService {
constructor(private storageService: Abstractions.StorageService) {
constructor(private storageService: StorageService) {
}
getAppId(): Promise<string> {
@@ -18,7 +20,7 @@ export default class AppIdService {
return existingId;
}
const guid = Services.UtilsService.newGuid();
const guid = UtilsService.newGuid();
await this.storageService.save(key, guid);
return guid;
}

View File

@@ -1,4 +1,7 @@
import { Abstractions, Enums, Services } from '@bitwarden/jslib';
import {
CipherType,
FieldType,
} from 'jslib/enums';
import AutofillField from '../models/domain/autofillField';
import AutofillPageDetails from '../models/domain/autofillPageDetails';
@@ -8,6 +11,13 @@ import CipherService from './cipher.service';
import TokenService from './token.service';
import TotpService from './totp.service';
import { UtilsService } from 'jslib/services';
import {
PlatformUtilsService,
UtilsService as UtilsServiceAbstraction,
} from 'jslib/abstractions';
const CardAttributes: string[] = ['autoCompleteType', 'data-stripe', 'htmlName', 'htmlID', 'label-tag',
'placeholder', 'label-left', 'label-top'];
@@ -90,8 +100,8 @@ var IsoProvinces: { [id: string]: string; } = {
export default class AutofillService {
constructor(public cipherService: CipherService, public tokenService: TokenService,
public totpService: TotpService, public utilsService: Services.UtilsService,
public platformUtilsService: Abstractions.PlatformUtilsService) {
public totpService: TotpService, public utilsService: UtilsServiceAbstraction,
public platformUtilsService: PlatformUtilsService) {
}
getFormsWithPasswordFields(pageDetails: AutofillPageDetails): any[] {
@@ -165,7 +175,7 @@ export default class AutofillService {
fillScript: fillScript,
}, { frameId: pd.frameId });
if (options.cipher.type !== Enums.CipherType.Login || totpPromise ||
if (options.cipher.type !== CipherType.Login || totpPromise ||
(options.fromBackground && this.platformUtilsService.isFirefox()) || options.skipTotp ||
!options.cipher.login.totp || !this.tokenService.getPremium()) {
return;
@@ -179,7 +189,7 @@ export default class AutofillService {
return null;
}).then((code: string) => {
if (code) {
Services.UtilsService.copyToClipboard(code);
UtilsService.copyToClipboard(code);
}
return code;
@@ -267,7 +277,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 === Enums.FieldType.Boolean) {
if (val == null && fields[matchingIndex].type === FieldType.Boolean) {
val = 'false';
}
@@ -279,13 +289,13 @@ export default class AutofillService {
}
switch (options.cipher.type) {
case Enums.CipherType.Login:
case CipherType.Login:
fillScript = this.generateLoginFillScript(fillScript, pageDetails, filledFields, options);
break;
case Enums.CipherType.Card:
case CipherType.Card:
fillScript = this.generateCardFillScript(fillScript, pageDetails, filledFields, options);
break;
case Enums.CipherType.Identity:
case CipherType.Identity:
fillScript = this.generateIdentityFillScript(fillScript, pageDetails, filledFields, options);
break;
default:

View File

@@ -1,7 +1,10 @@
import { Abstractions } from '@bitwarden/jslib';
import {
MessagingService,
PlatformUtilsService,
} from 'jslib/abstractions';
export default class BrowserMessagingService implements Abstractions.MessagingService {
constructor(private platformUtilsService: Abstractions.PlatformUtilsService) {
export default class BrowserMessagingService implements MessagingService {
constructor(private platformUtilsService: PlatformUtilsService) {
}
send(subscriber: string, arg: any = {}) {

View File

@@ -1,5 +1,6 @@
import BrowserPlatformUtilsService from './browserPlatformUtils.service';
import { Enums } from '@bitwarden/jslib';
import { DeviceType } from 'jslib/enums';
describe('Browser Utils Service', () => {
describe('getDomain', () => {
@@ -48,7 +49,7 @@ describe('Browser Utils Service', () => {
});
const browserPlatformUtilsService = new BrowserPlatformUtilsService();
expect(browserPlatformUtilsService.getDevice()).toBe(Enums.DeviceType.Chrome);
expect(browserPlatformUtilsService.getDevice()).toBe(DeviceType.Chrome);
});
it('should detect firefox', () => {
@@ -58,7 +59,7 @@ describe('Browser Utils Service', () => {
});
const browserPlatformUtilsService = new BrowserPlatformUtilsService();
expect(browserPlatformUtilsService.getDevice()).toBe(Enums.DeviceType.Firefox);
expect(browserPlatformUtilsService.getDevice()).toBe(DeviceType.Firefox);
});
it('should detect opera', () => {
@@ -68,7 +69,7 @@ describe('Browser Utils Service', () => {
});
const browserPlatformUtilsService = new BrowserPlatformUtilsService();
expect(browserPlatformUtilsService.getDevice()).toBe(Enums.DeviceType.Opera);
expect(browserPlatformUtilsService.getDevice()).toBe(DeviceType.Opera);
});
it('should detect edge', () => {
@@ -78,7 +79,7 @@ describe('Browser Utils Service', () => {
});
const browserPlatformUtilsService = new BrowserPlatformUtilsService();
expect(browserPlatformUtilsService.getDevice()).toBe(Enums.DeviceType.Edge);
expect(browserPlatformUtilsService.getDevice()).toBe(DeviceType.Edge);
});
});
});

View File

@@ -1,17 +1,19 @@
import * as tldjs from 'tldjs';
import { Abstractions, Enums } from '@bitwarden/jslib';
import { DeviceType } from 'jslib/enums';
import { PlatformUtilsService } from 'jslib/abstractions';
const AnalyticsIds = {
[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',
[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',
};
export default class BrowserPlatformUtilsService implements Abstractions.PlatformUtilsService {
export default class BrowserPlatformUtilsService implements PlatformUtilsService {
static getDomain(uriString: string): string {
if (uriString == null) {
return null;
@@ -49,56 +51,56 @@ export default class BrowserPlatformUtilsService implements Abstractions.Platfor
return ipRegex.test(ipString);
}
private deviceCache: Enums.DeviceType = null;
private deviceCache: DeviceType = null;
private analyticsIdCache: string = null;
getDevice(): Enums.DeviceType {
getDevice(): DeviceType {
if (this.deviceCache) {
return this.deviceCache;
}
if (navigator.userAgent.indexOf('Firefox') !== -1 || navigator.userAgent.indexOf('Gecko/') !== -1) {
this.deviceCache = Enums.DeviceType.Firefox;
this.deviceCache = DeviceType.Firefox;
} else if ((!!(window as any).opr && !!opr.addons) || !!(window as any).opera ||
navigator.userAgent.indexOf(' OPR/') >= 0) {
this.deviceCache = Enums.DeviceType.Opera;
this.deviceCache = DeviceType.Opera;
} else if (navigator.userAgent.indexOf(' Edge/') !== -1) {
this.deviceCache = Enums.DeviceType.Edge;
this.deviceCache = DeviceType.Edge;
} else if (navigator.userAgent.indexOf(' Vivaldi/') !== -1) {
this.deviceCache = Enums.DeviceType.Vivaldi;
this.deviceCache = DeviceType.Vivaldi;
} else if ((window as any).chrome) {
this.deviceCache = Enums.DeviceType.Chrome;
this.deviceCache = DeviceType.Chrome;
}
return this.deviceCache;
}
getDeviceString(): string {
return Enums.DeviceType[this.getDevice()].toLowerCase();
return DeviceType[this.getDevice()].toLowerCase();
}
isFirefox(): boolean {
return this.getDevice() === Enums.DeviceType.Firefox;
return this.getDevice() === DeviceType.Firefox;
}
isChrome(): boolean {
return this.getDevice() === Enums.DeviceType.Chrome;
return this.getDevice() === DeviceType.Chrome;
}
isEdge(): boolean {
return this.getDevice() === Enums.DeviceType.Edge;
return this.getDevice() === DeviceType.Edge;
}
isOpera(): boolean {
return this.getDevice() === Enums.DeviceType.Opera;
return this.getDevice() === DeviceType.Opera;
}
isVivaldi(): boolean {
return this.getDevice() === Enums.DeviceType.Vivaldi;
return this.getDevice() === DeviceType.Vivaldi;
}
isSafari(): boolean {
return this.getDevice() === Enums.DeviceType.Safari;
return this.getDevice() === DeviceType.Safari;
}
analyticsId(): string {

View File

@@ -1,7 +1,10 @@
import { Abstractions } from '@bitwarden/jslib';
import {
PlatformUtilsService,
StorageService,
} from 'jslib/abstractions';
export default class BrowserStorageService implements Abstractions.StorageService {
constructor(private platformUtilsService: Abstractions.PlatformUtilsService) {
export default class BrowserStorageService implements StorageService {
constructor(private platformUtilsService: PlatformUtilsService) {
}
get<T>(key: string): Promise<T> {

View File

@@ -1,4 +1,25 @@
import { Abstractions, Data, Domain, Enums, Request, Response } from '@bitwarden/jslib';
import { CipherType } from 'jslib/enums';
import { CipherData } from 'jslib/models/data';
import {
Cipher,
CipherString,
Field,
SymmetricCryptoKey,
} from 'jslib/models/domain';
import { CipherRequest } from 'jslib/models/request';
import {
CipherResponse,
ErrorResponse,
} from 'jslib/models/response';
import {
CryptoService,
StorageService,
} from 'jslib/abstractions';
import ApiService from './api.service';
import ConstantsService from './constants.service';
@@ -54,17 +75,17 @@ export default class CipherService {
decryptedCipherCache: any[];
constructor(private cryptoService: Abstractions.CryptoService, private userService: UserService,
constructor(private cryptoService: CryptoService, private userService: UserService,
private settingsService: SettingsService, private apiService: ApiService,
private storageService: Abstractions.StorageService) {
private storageService: StorageService) {
}
clearCache(): void {
this.decryptedCipherCache = null;
}
async encrypt(model: any): Promise<Domain.Cipher> {
const cipher = new Domain.Cipher();
async encrypt(model: any): Promise<Cipher> {
const cipher = new Cipher();
cipher.id = model.id;
cipher.folderId = model.folderId;
cipher.favorite = model.favorite;
@@ -87,17 +108,17 @@ export default class CipherService {
return cipher;
}
async encryptFields(fieldsModel: any[], key: Domain.SymmetricCryptoKey): Promise<Domain.Field[]> {
async encryptFields(fieldsModel: any[], key: SymmetricCryptoKey): Promise<Field[]> {
if (!fieldsModel || !fieldsModel.length) {
return null;
}
const self = this;
const encFields: Domain.Field[] = [];
const encFields: Field[] = [];
await fieldsModel.reduce((promise, field) => {
return promise.then(() => {
return self.encryptField(field, key);
}).then((encField: Domain.Field) => {
}).then((encField: Field) => {
encFields.push(encField);
});
}, Promise.resolve());
@@ -105,8 +126,8 @@ export default class CipherService {
return encFields;
}
async encryptField(fieldModel: any, key: Domain.SymmetricCryptoKey): Promise<Domain.Field> {
const field = new Domain.Field();
async encryptField(fieldModel: any, key: SymmetricCryptoKey): Promise<Field> {
const field = new Field();
field.type = fieldModel.type;
await this.encryptObjProperty(fieldModel, field, {
@@ -117,27 +138,27 @@ export default class CipherService {
return field;
}
async get(id: string): Promise<Domain.Cipher> {
async get(id: string): Promise<Cipher> {
const userId = await this.userService.getUserId();
const localData = await this.storageService.get<any>(Keys.localData);
const ciphers = await this.storageService.get<{ [id: string]: Data.Cipher; }>(
const ciphers = await this.storageService.get<{ [id: string]: CipherData; }>(
Keys.ciphersPrefix + userId);
if (ciphers == null || !ciphers.hasOwnProperty(id)) {
return null;
}
return new Domain.Cipher(ciphers[id], false, localData ? localData[id] : null);
return new Cipher(ciphers[id], false, localData ? localData[id] : null);
}
async getAll(): Promise<Domain.Cipher[]> {
async getAll(): Promise<Cipher[]> {
const userId = await this.userService.getUserId();
const localData = await this.storageService.get<any>(Keys.localData);
const ciphers = await this.storageService.get<{ [id: string]: Data.Cipher; }>(
const ciphers = await this.storageService.get<{ [id: string]: CipherData; }>(
Keys.ciphersPrefix + userId);
const response: Domain.Cipher[] = [];
const response: Cipher[] = [];
for (const id in ciphers) {
if (ciphers.hasOwnProperty(id)) {
response.push(new Domain.Cipher(ciphers[id], false, localData ? localData[id] : null));
response.push(new Cipher(ciphers[id], false, localData ? localData[id] : null));
}
}
return response;
@@ -209,7 +230,7 @@ export default class CipherService {
const ciphersToReturn: any[] = [];
ciphers.forEach((cipher) => {
if (domain && cipher.type === Enums.CipherType.Login && cipher.login.domain &&
if (domain && cipher.type === CipherType.Login && cipher.login.domain &&
matchingDomains.indexOf(cipher.login.domain) > -1) {
ciphersToReturn.push(cipher);
} else if (includeOtherTypes && includeOtherTypes.indexOf(cipher.type) > -1) {
@@ -272,10 +293,10 @@ export default class CipherService {
await this.storageService.save(Keys.neverDomains, domains);
}
async saveWithServer(cipher: Domain.Cipher): Promise<any> {
const request = new Request.Cipher(cipher);
async saveWithServer(cipher: Cipher): Promise<any> {
const request = new CipherRequest(cipher);
let response: Response.Cipher;
let response: CipherResponse;
if (cipher.id == null) {
response = await this.apiService.postCipher(request);
cipher.id = response.id;
@@ -284,11 +305,11 @@ export default class CipherService {
}
const userId = await this.userService.getUserId();
const data = new Data.Cipher(response, userId, cipher.collectionIds);
const data = new CipherData(response, userId, cipher.collectionIds);
await this.upsert(data);
}
saveAttachmentWithServer(cipher: Domain.Cipher, unencryptedFile: any): Promise<any> {
saveAttachmentWithServer(cipher: Cipher, unencryptedFile: any): Promise<any> {
const self = this;
return new Promise((resolve, reject) => {
@@ -304,18 +325,18 @@ export default class CipherService {
const blob = new Blob([encData], { type: 'application/octet-stream' });
fd.append('data', blob, encFileName.encryptedString);
let response: Response.Cipher;
let response: CipherResponse;
try {
response = await self.apiService.postCipherAttachment(cipher.id, fd);
} catch (e) {
reject((e as Response.Error).getSingleMessage());
reject((e as ErrorResponse).getSingleMessage());
return;
}
const userId = await self.userService.getUserId();
const data = new Data.Cipher(response, userId, cipher.collectionIds);
const data = new CipherData(response, userId, cipher.collectionIds);
this.upsert(data);
resolve(new Domain.Cipher(data));
resolve(new Cipher(data));
};
@@ -325,19 +346,19 @@ export default class CipherService {
});
}
async upsert(cipher: Data.Cipher | Data.Cipher[]): Promise<any> {
async upsert(cipher: CipherData | CipherData[]): Promise<any> {
const userId = await this.userService.getUserId();
let ciphers = await this.storageService.get<{ [id: string]: Data.Cipher; }>(
let ciphers = await this.storageService.get<{ [id: string]: CipherData; }>(
Keys.ciphersPrefix + userId);
if (ciphers == null) {
ciphers = {};
}
if (cipher instanceof Data.Cipher) {
const c = cipher as Data.Cipher;
if (cipher instanceof CipherData) {
const c = cipher as CipherData;
ciphers[c.id] = c;
} else {
(cipher as Data.Cipher[]).forEach((c) => {
(cipher as CipherData[]).forEach((c) => {
ciphers[c.id] = c;
});
}
@@ -346,7 +367,7 @@ export default class CipherService {
this.decryptedCipherCache = null;
}
async replace(ciphers: { [id: string]: Data.Cipher; }): Promise<any> {
async replace(ciphers: { [id: string]: CipherData; }): Promise<any> {
const userId = await this.userService.getUserId();
await this.storageService.save(Keys.ciphersPrefix + userId, ciphers);
this.decryptedCipherCache = null;
@@ -359,7 +380,7 @@ export default class CipherService {
async delete(id: string | string[]): Promise<any> {
const userId = await this.userService.getUserId();
const ciphers = await this.storageService.get<{ [id: string]: Data.Cipher; }>(
const ciphers = await this.storageService.get<{ [id: string]: CipherData; }>(
Keys.ciphersPrefix + userId);
if (ciphers == null) {
return;
@@ -385,7 +406,7 @@ export default class CipherService {
async deleteAttachment(id: string, attachmentId: string): Promise<void> {
const userId = await this.userService.getUserId();
const ciphers = await this.storageService.get<{ [id: string]: Data.Cipher; }>(
const ciphers = await this.storageService.get<{ [id: string]: CipherData; }>(
Keys.ciphersPrefix + userId);
if (ciphers == null || !ciphers.hasOwnProperty(id) || ciphers[id].attachments == null) {
@@ -406,7 +427,7 @@ export default class CipherService {
try {
await this.apiService.deleteCipherAttachment(id, attachmentId);
} catch (e) {
return Promise.reject((e as Response.Error).getSingleMessage());
return Promise.reject((e as ErrorResponse).getSingleMessage());
}
await this.deleteAttachment(id, attachmentId);
}
@@ -421,7 +442,7 @@ export default class CipherService {
// Helpers
private encryptObjProperty(model: any, obj: any, map: any, key: Domain.SymmetricCryptoKey): Promise<void[]> {
private encryptObjProperty(model: any, obj: any, map: any, key: SymmetricCryptoKey): Promise<void[]> {
const promises = [];
const self = this;
@@ -438,7 +459,7 @@ export default class CipherService {
return self.cryptoService.encrypt(modelProp, key);
}
return null;
}).then((val: Domain.CipherString) => {
}).then((val: CipherString) => {
theObj[theProp] = val;
});
promises.push(p);
@@ -448,9 +469,9 @@ export default class CipherService {
return Promise.all(promises);
}
private encryptCipherData(cipher: Domain.Cipher, model: any, key: Domain.SymmetricCryptoKey): Promise<any> {
private encryptCipherData(cipher: Cipher, model: any, key: SymmetricCryptoKey): Promise<any> {
switch (cipher.type) {
case Enums.CipherType.Login:
case CipherType.Login:
model.login = {};
return this.encryptObjProperty(cipher.login, model.login, {
uri: null,
@@ -458,12 +479,12 @@ export default class CipherService {
password: null,
totp: null,
}, key);
case Enums.CipherType.SecureNote:
case CipherType.SecureNote:
model.secureNote = {
type: cipher.secureNote.type,
};
return Promise.resolve();
case Enums.CipherType.Card:
case CipherType.Card:
model.card = {};
return this.encryptObjProperty(cipher.card, model.card, {
cardholderName: null,
@@ -473,7 +494,7 @@ export default class CipherService {
expYear: null,
code: null,
}, key);
case Enums.CipherType.Identity:
case CipherType.Identity:
model.identity = {};
return this.encryptObjProperty(cipher.identity, model.identity, {
title: null,

View File

@@ -1,6 +1,13 @@
import UserService from './user.service';
import { Abstractions, Data, Domain } from '@bitwarden/jslib';
import {
CryptoService,
StorageService,
} from 'jslib/abstractions';
import { CollectionData } from 'jslib/models/data';
import { Collection } from 'jslib/models/domain';
const Keys = {
collectionsPrefix: 'collections_',
@@ -9,33 +16,33 @@ const Keys = {
export default class CollectionService {
decryptedCollectionCache: any[];
constructor(private cryptoService: Abstractions.CryptoService, private userService: UserService,
private storageService: Abstractions.StorageService) {
constructor(private cryptoService: CryptoService, private userService: UserService,
private storageService: StorageService) {
}
clearCache(): void {
this.decryptedCollectionCache = null;
}
async get(id: string): Promise<Domain.Collection> {
async get(id: string): Promise<Collection> {
const userId = await this.userService.getUserId();
const collections = await this.storageService.get<{ [id: string]: Data.Collection; }>(
const collections = await this.storageService.get<{ [id: string]: CollectionData; }>(
Keys.collectionsPrefix + userId);
if (collections == null || !collections.hasOwnProperty(id)) {
return null;
}
return new Domain.Collection(collections[id]);
return new Collection(collections[id]);
}
async getAll(): Promise<Domain.Collection[]> {
async getAll(): Promise<Collection[]> {
const userId = await this.userService.getUserId();
const collections = await this.storageService.get<{ [id: string]: Data.Collection; }>(
const collections = await this.storageService.get<{ [id: string]: CollectionData; }>(
Keys.collectionsPrefix + userId);
const response: Domain.Collection[] = [];
const response: Collection[] = [];
for (const id in collections) {
if (collections.hasOwnProperty(id)) {
response.push(new Domain.Collection(collections[id]));
response.push(new Collection(collections[id]));
}
}
return response;
@@ -65,19 +72,19 @@ export default class CollectionService {
return this.decryptedCollectionCache;
}
async upsert(collection: Data.Collection | Data.Collection[]): Promise<any> {
async upsert(collection: CollectionData | CollectionData[]): Promise<any> {
const userId = await this.userService.getUserId();
let collections = await this.storageService.get<{ [id: string]: Data.Collection; }>(
let collections = await this.storageService.get<{ [id: string]: CollectionData; }>(
Keys.collectionsPrefix + userId);
if (collections == null) {
collections = {};
}
if (collection instanceof Data.Collection) {
const c = collection as Data.Collection;
if (collection instanceof CollectionData) {
const c = collection as CollectionData;
collections[c.id] = c;
} else {
(collection as Data.Collection[]).forEach((c) => {
(collection as CollectionData[]).forEach((c) => {
collections[c.id] = c;
});
}
@@ -86,7 +93,7 @@ export default class CollectionService {
this.decryptedCollectionCache = null;
}
async replace(collections: { [id: string]: Data.Collection; }): Promise<any> {
async replace(collections: { [id: string]: CollectionData; }): Promise<any> {
const userId = await this.userService.getUserId();
await this.storageService.save(Keys.collectionsPrefix + userId, collections);
this.decryptedCollectionCache = null;
@@ -99,7 +106,7 @@ export default class CollectionService {
async delete(id: string | string[]): Promise<any> {
const userId = await this.userService.getUserId();
const collections = await this.storageService.get<{ [id: string]: Data.Collection; }>(
const collections = await this.storageService.get<{ [id: string]: CollectionData; }>(
Keys.collectionsPrefix + userId);
if (collections == null) {
return;

View File

@@ -1,4 +1,4 @@
import { Abstractions } from '@bitwarden/jslib';
import { PlatformUtilsService } from 'jslib/abstractions';
export default class ConstantsService {
static readonly environmentUrlsKey: string = 'environmentUrls';
@@ -57,7 +57,7 @@ export default class ConstantsService {
twoFactorProviderInfo: any[];
constructor(i18nService: any, platformUtilsService: Abstractions.PlatformUtilsService) {
constructor(i18nService: any, platformUtilsService: PlatformUtilsService) {
if (platformUtilsService.isEdge()) {
// delay for i18n fetch
setTimeout(() => {

View File

@@ -1,8 +1,11 @@
import { Abstractions } from '@bitwarden/jslib';
import {
CryptoService,
PlatformUtilsService,
} from 'jslib/abstractions';
export default class ContainerService {
constructor(private cryptoService: Abstractions.CryptoService,
private platformUtilsService: Abstractions.PlatformUtilsService) {
constructor(private cryptoService: CryptoService,
private platformUtilsService: PlatformUtilsService) {
}
attachToWindow(win: any) {
@@ -11,11 +14,11 @@ export default class ContainerService {
}
}
getCryptoService(): Abstractions.CryptoService {
getCryptoService(): CryptoService {
return this.cryptoService;
}
getPlatformUtilsService(): Abstractions.PlatformUtilsService {
getPlatformUtilsService(): PlatformUtilsService {
return this.platformUtilsService;
}
}

View File

@@ -1,7 +1,9 @@
import ApiService from './api.service';
import ConstantsService from './constants.service';
import { Abstractions, Domain } from '@bitwarden/jslib';
import { EnvironmentUrls } from 'jslib/models/domain';
import { StorageService } from 'jslib/abstractions';
export default class EnvironmentService {
baseUrl: string;
@@ -10,7 +12,7 @@ export default class EnvironmentService {
identityUrl: string;
iconsUrl: string;
constructor(private apiService: ApiService, private storageService: Abstractions.StorageService) {
constructor(private apiService: ApiService, private storageService: StorageService) {
}
async setUrlsFromStorage(): Promise<void> {
@@ -23,7 +25,7 @@ export default class EnvironmentService {
webVault: null,
};
const envUrls = new Domain.EnvironmentUrls();
const envUrls = new EnvironmentUrls();
if (urls.base) {
this.baseUrl = envUrls.base = urls.base;
@@ -59,7 +61,7 @@ export default class EnvironmentService {
this.identityUrl = urls.identity;
this.iconsUrl = urls.icons;
const envUrls = new Domain.EnvironmentUrls();
const envUrls = new EnvironmentUrls();
if (this.baseUrl) {
envUrls.base = this.baseUrl;
} else {

View File

@@ -1,7 +1,18 @@
import ApiService from './api.service';
import UserService from './user.service';
import { Abstractions, Data, Domain, Request, Response } from '@bitwarden/jslib';
import { FolderData } from 'jslib/models/data';
import { Folder } from 'jslib/models/domain';
import { FolderRequest } from 'jslib/models/request';
import { FolderResponse } from 'jslib/models/response';
import {
CryptoService,
StorageService,
} from 'jslib/abstractions';
const Keys = {
foldersPrefix: 'folders_',
@@ -10,41 +21,41 @@ const Keys = {
export default class FolderService {
decryptedFolderCache: any[];
constructor(private cryptoService: Abstractions.CryptoService, private userService: UserService,
constructor(private cryptoService: CryptoService, private userService: UserService,
private i18nService: any, private apiService: ApiService,
private storageService: Abstractions.StorageService) {
private storageService: StorageService) {
}
clearCache(): void {
this.decryptedFolderCache = null;
}
async encrypt(model: any): Promise<Domain.Folder> {
const folder = new Domain.Folder();
async encrypt(model: any): Promise<Folder> {
const folder = new Folder();
folder.id = model.id;
folder.name = await this.cryptoService.encrypt(model.name);
return folder;
}
async get(id: string): Promise<Domain.Folder> {
async get(id: string): Promise<Folder> {
const userId = await this.userService.getUserId();
const folders = await this.storageService.get<{ [id: string]: Data.Folder; }>(
const folders = await this.storageService.get<{ [id: string]: FolderData; }>(
Keys.foldersPrefix + userId);
if (folders == null || !folders.hasOwnProperty(id)) {
return null;
}
return new Domain.Folder(folders[id]);
return new Folder(folders[id]);
}
async getAll(): Promise<Domain.Folder[]> {
async getAll(): Promise<Folder[]> {
const userId = await this.userService.getUserId();
const folders = await this.storageService.get<{ [id: string]: Data.Folder; }>(
const folders = await this.storageService.get<{ [id: string]: FolderData; }>(
Keys.foldersPrefix + userId);
const response: Domain.Folder[] = [];
const response: Folder[] = [];
for (const id in folders) {
if (folders.hasOwnProperty(id)) {
response.push(new Domain.Folder(folders[id]));
response.push(new Folder(folders[id]));
}
}
return response;
@@ -78,10 +89,10 @@ export default class FolderService {
return this.decryptedFolderCache;
}
async saveWithServer(folder: Domain.Folder): Promise<any> {
const request = new Request.Folder(folder);
async saveWithServer(folder: Folder): Promise<any> {
const request = new FolderRequest(folder);
let response: Response.Folder;
let response: FolderResponse;
if (folder.id == null) {
response = await this.apiService.postFolder(request);
folder.id = response.id;
@@ -90,23 +101,23 @@ export default class FolderService {
}
const userId = await this.userService.getUserId();
const data = new Data.Folder(response, userId);
const data = new FolderData(response, userId);
await this.upsert(data);
}
async upsert(folder: Data.Folder | Data.Folder[]): Promise<any> {
async upsert(folder: FolderData | FolderData[]): Promise<any> {
const userId = await this.userService.getUserId();
let folders = await this.storageService.get<{ [id: string]: Data.Folder; }>(
let folders = await this.storageService.get<{ [id: string]: FolderData; }>(
Keys.foldersPrefix + userId);
if (folders == null) {
folders = {};
}
if (folder instanceof Data.Folder) {
const f = folder as Data.Folder;
if (folder instanceof FolderData) {
const f = folder as FolderData;
folders[f.id] = f;
} else {
(folder as Data.Folder[]).forEach((f) => {
(folder as FolderData[]).forEach((f) => {
folders[f.id] = f;
});
}
@@ -115,7 +126,7 @@ export default class FolderService {
this.decryptedFolderCache = null;
}
async replace(folders: { [id: string]: Data.Folder; }): Promise<any> {
async replace(folders: { [id: string]: FolderData; }): Promise<any> {
const userId = await this.userService.getUserId();
await this.storageService.save(Keys.foldersPrefix + userId, folders);
this.decryptedFolderCache = null;
@@ -128,7 +139,7 @@ export default class FolderService {
async delete(id: string | string[]): Promise<any> {
const userId = await this.userService.getUserId();
const folders = await this.storageService.get<{ [id: string]: Data.Folder; }>(
const folders = await this.storageService.get<{ [id: string]: FolderData; }>(
Keys.foldersPrefix + userId);
if (folders == null) {
return;

View File

@@ -1,6 +1,6 @@
import { Abstractions } from '@bitwarden/jslib';
import { PlatformUtilsService } from 'jslib/abstractions';
export default function i18nService(platformUtilsService: Abstractions.PlatformUtilsService) {
export default function i18nService(platformUtilsService: PlatformUtilsService) {
const edgeMessages: any = {};
if (platformUtilsService.isEdge()) {

View File

@@ -3,13 +3,17 @@ import CollectionService from './collection.service';
import ConstantsService from './constants.service';
import FolderService from './folder.service';
import { Abstractions } from '@bitwarden/jslib';
import {
CryptoService,
PlatformUtilsService,
StorageService,
} from 'jslib/abstractions';
export default class LockService {
constructor(private cipherService: CipherService, private folderService: FolderService,
private collectionService: CollectionService, private cryptoService: Abstractions.CryptoService,
private platformUtilsService: Abstractions.PlatformUtilsService,
private storageService: Abstractions.StorageService,
private collectionService: CollectionService, private cryptoService: CryptoService,
private platformUtilsService: PlatformUtilsService,
private storageService: StorageService,
private setIcon: Function, private refreshBadgeAndMenu: Function) {
this.checkLock();
setInterval(() => this.checkLock(), 10 * 1000); // check every 10 seconds

View File

@@ -1,4 +1,14 @@
import { Abstractions, Domain, Services } from '@bitwarden/jslib';
import {
CipherString,
PasswordHistory,
} from 'jslib/models/domain';
import { UtilsService } from 'jslib/services/utils.service';
import {
CryptoService,
StorageService,
} from 'jslib/abstractions';
const DefaultOptions = {
length: 14,
@@ -75,7 +85,7 @@ export default class PasswordGenerationService {
// shuffle
positions.sort(() => {
return Services.UtilsService.secureRandomNumber(0, 1) * 2 - 1;
return UtilsService.secureRandomNumber(0, 1) * 2 - 1;
});
// build out the char sets
@@ -131,7 +141,7 @@ export default class PasswordGenerationService {
break;
}
const randomCharIndex = Services.UtilsService.secureRandomNumber(0, positionChars.length - 1);
const randomCharIndex = UtilsService.secureRandomNumber(0, positionChars.length - 1);
password += positionChars.charAt(randomCharIndex);
}
@@ -139,11 +149,11 @@ export default class PasswordGenerationService {
}
optionsCache: any;
history: Domain.PasswordHistory[] = [];
history: PasswordHistory[] = [];
constructor(private cryptoService: Abstractions.CryptoService,
private storageService: Abstractions.StorageService) {
storageService.get<Domain.PasswordHistory[]>(Keys.history).then((encrypted) => {
constructor(private cryptoService: CryptoService,
private storageService: StorageService) {
storageService.get<PasswordHistory[]>(Keys.history).then((encrypted) => {
return this.decryptHistory(encrypted);
}).then((history) => {
this.history = history;
@@ -173,7 +183,7 @@ export default class PasswordGenerationService {
}
getHistory() {
return this.history || new Array<Domain.PasswordHistory>();
return this.history || new Array<PasswordHistory>();
}
async addHistory(password: string): Promise<any> {
@@ -182,7 +192,7 @@ export default class PasswordGenerationService {
return;
}
this.history.push(new Domain.PasswordHistory(password, Date.now()));
this.history.push(new PasswordHistory(password, Date.now()));
// Remove old items.
if (this.history.length > MaxPasswordsInHistory) {
@@ -198,27 +208,27 @@ export default class PasswordGenerationService {
return await this.storageService.remove(Keys.history);
}
private async encryptHistory(): Promise<Domain.PasswordHistory[]> {
private async encryptHistory(): Promise<PasswordHistory[]> {
if (this.history == null || this.history.length === 0) {
return Promise.resolve([]);
}
const promises = this.history.map(async (item) => {
const encrypted = await this.cryptoService.encrypt(item.password);
return new Domain.PasswordHistory(encrypted.encryptedString, item.date);
return new PasswordHistory(encrypted.encryptedString, item.date);
});
return await Promise.all(promises);
}
private async decryptHistory(history: Domain.PasswordHistory[]): Promise<Domain.PasswordHistory[]> {
private async decryptHistory(history: PasswordHistory[]): Promise<PasswordHistory[]> {
if (history == null || history.length === 0) {
return Promise.resolve([]);
}
const promises = history.map(async (item) => {
const decrypted = await this.cryptoService.decrypt(new Domain.CipherString(item.password));
return new Domain.PasswordHistory(decrypted, item.date);
const decrypted = await this.cryptoService.decrypt(new CipherString(item.password));
return new PasswordHistory(decrypted, item.date);
});
return await Promise.all(promises);

View File

@@ -1,6 +1,6 @@
import UserService from './user.service';
import { Abstractions } from '@bitwarden/jslib';
import { StorageService } from 'jslib/abstractions';
const Keys = {
settingsPrefix: 'settings_',
@@ -10,7 +10,7 @@ const Keys = {
export default class SettingsService {
private settingsCache: any;
constructor(private userService: UserService, private storageService: Abstractions.StorageService) {
constructor(private userService: UserService, private storageService: StorageService) {
}
clearCache(): void {

View File

@@ -5,7 +5,25 @@ import FolderService from './folder.service';
import SettingsService from './settings.service';
import UserService from './user.service';
import { Abstractions, Data, Response } from '@bitwarden/jslib';
import {
CryptoService,
MessagingService,
StorageService,
} from 'jslib/abstractions';
import {
CipherData,
CollectionData,
FolderData,
} from 'jslib/models/data';
import {
CipherResponse,
CollectionResponse,
DomainsResponse,
FolderResponse,
ProfileResponse,
} from 'jslib/models/response';
const Keys = {
lastSyncPrefix: 'lastSync_',
@@ -16,9 +34,9 @@ export default class SyncService {
constructor(private userService: UserService, private apiService: ApiService,
private settingsService: SettingsService, private folderService: FolderService,
private cipherService: CipherService, private cryptoService: Abstractions.CryptoService,
private collectionService: CollectionService, private storageService: Abstractions.StorageService,
private messagingService: Abstractions.MessagingService, private logoutCallback: Function) {
private cipherService: CipherService, private cryptoService: CryptoService,
private collectionService: CollectionService, private storageService: StorageService,
private messagingService: MessagingService, private logoutCallback: Function) {
}
async getLastSync() {
@@ -110,7 +128,7 @@ export default class SyncService {
}
}
private async syncProfile(response: Response.Profile) {
private async syncProfile(response: ProfileResponse) {
const stamp = await this.userService.getSecurityStamp();
if (stamp != null && stamp !== response.securityStamp) {
if (this.logoutCallback != null) {
@@ -126,31 +144,31 @@ export default class SyncService {
await this.userService.setSecurityStamp(response.securityStamp);
}
private async syncFolders(userId: string, response: Response.Folder[]) {
const folders: { [id: string]: Data.Folder; } = {};
private async syncFolders(userId: string, response: FolderResponse[]) {
const folders: { [id: string]: FolderData; } = {};
response.forEach((f) => {
folders[f.id] = new Data.Folder(f, userId);
folders[f.id] = new FolderData(f, userId);
});
return await this.folderService.replace(folders);
}
private async syncCollections(response: Response.Collection[]) {
const collections: { [id: string]: Data.Collection; } = {};
private async syncCollections(response: CollectionResponse[]) {
const collections: { [id: string]: CollectionData; } = {};
response.forEach((c) => {
collections[c.id] = new Data.Collection(c);
collections[c.id] = new CollectionData(c);
});
return await this.collectionService.replace(collections);
}
private async syncCiphers(userId: string, response: Response.Cipher[]) {
const ciphers: { [id: string]: Data.Cipher; } = {};
private async syncCiphers(userId: string, response: CipherResponse[]) {
const ciphers: { [id: string]: CipherData; } = {};
response.forEach((c) => {
ciphers[c.id] = new Data.Cipher(c, userId);
ciphers[c.id] = new CipherData(c, userId);
});
return await this.cipherService.replace(ciphers);
}
private async syncSettings(userId: string, response: Response.Domains) {
private async syncSettings(userId: string, response: DomainsResponse) {
let eqDomains: string[][] = [];
if (response != null && response.equivalentDomains != null) {
eqDomains = eqDomains.concat(response.equivalentDomains);

View File

@@ -1,6 +1,8 @@
import ConstantsService from './constants.service';
import { Abstractions, Services } from '@bitwarden/jslib';
import { UtilsService } from 'jslib/services/utils.service';
import { StorageService } from 'jslib/abstractions';
const Keys = {
accessToken: 'accessToken',
@@ -13,7 +15,7 @@ export default class TokenService {
decodedToken: any;
refreshToken: string;
constructor(private storageService: Abstractions.StorageService) {
constructor(private storageService: StorageService) {
}
setTokens(accessToken: string, refreshToken: string): Promise<any> {
@@ -92,7 +94,7 @@ export default class TokenService {
throw new Error('JWT must have 3 parts');
}
const decoded = Services.UtilsService.urlBase64Decode(parts[1]);
const decoded = 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 { Abstractions } from '@bitwarden/jslib';
import { StorageService } from 'jslib/abstractions';
const b32Chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567';
@@ -10,7 +10,7 @@ const TotpAlgorithm = {
};
export default class TotpService {
constructor(private storageService: Abstractions.StorageService) {
constructor(private storageService: StorageService) {
}
async getCode(keyb32: string): Promise<string> {

View File

@@ -1,6 +1,6 @@
import TokenService from './token.service';
import { Abstractions } from '@bitwarden/jslib';
import { StorageService } from 'jslib/abstractions';
const Keys = {
userId: 'userId',
@@ -13,7 +13,7 @@ export default class UserService {
email: string;
stamp: string;
constructor(private tokenService: TokenService, private storageService: Abstractions.StorageService) {
constructor(private tokenService: TokenService, private storageService: StorageService) {
}
setUserIdAndEmail(userId: string, email: string): Promise<any> {

View File

@@ -1,32 +1,32 @@
import { Services } from '@bitwarden/jslib';
import { UtilsService } from 'jslib/services';
describe('Utils Service', () => {
describe('getHostname', () => {
it('should fail for invalid urls', () => {
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();
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();
});
it('should handle valid urls', () => {
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');
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');
});
it('should support localhost and IP', () => {
expect(Services.UtilsService.getHostname('https://localhost')).toBe('localhost');
expect(Services.UtilsService.getHostname('https://192.168.1.1')).toBe('192.168.1.1');
expect(UtilsService.getHostname('https://localhost')).toBe('localhost');
expect(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(Services.UtilsService.newGuid()).toMatch(validGuid);
expect(UtilsService.newGuid()).toMatch(validGuid);
});
});
});