mirror of
https://github.com/bitwarden/browser
synced 2025-12-21 18:53:29 +00:00
convert domain models to jslib
This commit is contained in:
@@ -1,28 +1,27 @@
|
||||
import { CipherString } from '../../models/domain/cipherString';
|
||||
import SymmetricCryptoKey from '../../models/domain/symmetricCryptoKey';
|
||||
|
||||
import { Response } from '@bitwarden/jslib';
|
||||
import { Domain, Response } from '@bitwarden/jslib';
|
||||
|
||||
export interface CryptoService {
|
||||
setKey(key: SymmetricCryptoKey): Promise<any>;
|
||||
setKey(key: Domain.SymmetricCryptoKey): Promise<any>;
|
||||
setKeyHash(keyHash: string): Promise<{}>;
|
||||
setEncKey(encKey: string): Promise<{}>;
|
||||
setEncPrivateKey(encPrivateKey: string): Promise<{}>;
|
||||
setOrgKeys(orgs: Response.ProfileOrganization[]): Promise<{}>;
|
||||
getKey(): Promise<SymmetricCryptoKey>;
|
||||
getKey(): Promise<Domain.SymmetricCryptoKey>;
|
||||
getKeyHash(): Promise<string>;
|
||||
getEncKey(): Promise<SymmetricCryptoKey>;
|
||||
getEncKey(): Promise<Domain.SymmetricCryptoKey>;
|
||||
getPrivateKey(): Promise<ArrayBuffer>;
|
||||
getOrgKeys(): Promise<Map<string, SymmetricCryptoKey>>;
|
||||
getOrgKey(orgId: string): Promise<SymmetricCryptoKey>;
|
||||
getOrgKeys(): Promise<Map<string, Domain.SymmetricCryptoKey>>;
|
||||
getOrgKey(orgId: string): Promise<Domain.SymmetricCryptoKey>;
|
||||
clearKeys(): Promise<any>;
|
||||
toggleKey(): Promise<any>;
|
||||
makeKey(password: string, salt: string): SymmetricCryptoKey;
|
||||
hashPassword(password: string, key: SymmetricCryptoKey): Promise<string>;
|
||||
makeEncKey(key: SymmetricCryptoKey): Promise<CipherString>;
|
||||
encrypt(plainValue: string | Uint8Array, key?: SymmetricCryptoKey, plainValueEncoding?: string): Promise<CipherString>;
|
||||
encryptToBytes(plainValue: ArrayBuffer, key?: SymmetricCryptoKey): Promise<ArrayBuffer>;
|
||||
decrypt(cipherString: CipherString, key?: SymmetricCryptoKey, outputEncoding?: string): Promise<string>;
|
||||
decryptFromBytes(encBuf: ArrayBuffer, key: SymmetricCryptoKey): Promise<ArrayBuffer>;
|
||||
makeKey(password: string, salt: string): Domain.SymmetricCryptoKey;
|
||||
hashPassword(password: string, key: Domain.SymmetricCryptoKey): Promise<string>;
|
||||
makeEncKey(key: Domain.SymmetricCryptoKey): Promise<Domain.CipherString>;
|
||||
encrypt(plainValue: string | Uint8Array, key?: Domain.SymmetricCryptoKey,
|
||||
plainValueEncoding?: string): Promise<Domain.CipherString>;
|
||||
encryptToBytes(plainValue: ArrayBuffer, key?: Domain.SymmetricCryptoKey): Promise<ArrayBuffer>;
|
||||
decrypt(cipherString: Domain.CipherString, key?: Domain.SymmetricCryptoKey,
|
||||
outputEncoding?: string): Promise<string>;
|
||||
decryptFromBytes(encBuf: ArrayBuffer, key: Domain.SymmetricCryptoKey): Promise<ArrayBuffer>;
|
||||
rsaDecrypt(encValue: string): Promise<string>;
|
||||
}
|
||||
|
||||
@@ -2,9 +2,7 @@ import AppIdService from './appId.service';
|
||||
import ConstantsService from './constants.service';
|
||||
import TokenService from './token.service';
|
||||
|
||||
import { Abstractions, Request as Req, Response as Res } from '@bitwarden/jslib';
|
||||
|
||||
import EnvironmentUrls from '../models/domain/environmentUrls';
|
||||
import { Abstractions, Domain, Request as Req, Response as Res } from '@bitwarden/jslib';
|
||||
|
||||
export default class ApiService {
|
||||
urlsSet: boolean = false;
|
||||
@@ -19,7 +17,7 @@ export default class ApiService {
|
||||
this.deviceType = platformUtilsService.getDevice().toString();
|
||||
}
|
||||
|
||||
setUrls(urls: EnvironmentUrls) {
|
||||
setUrls(urls: Domain.EnvironmentUrls) {
|
||||
this.urlsSet = true;
|
||||
|
||||
if (urls.base != null) {
|
||||
|
||||
@@ -1,9 +1,4 @@
|
||||
import { Abstractions, Data, Enums, Request, Response } from '@bitwarden/jslib';
|
||||
|
||||
import { Cipher } from '../models/domain/cipher';
|
||||
import { CipherString } from '../models/domain/cipherString';
|
||||
import { Field } from '../models/domain/field';
|
||||
import SymmetricCryptoKey from '../models/domain/symmetricCryptoKey';
|
||||
import { Abstractions, Data, Domain, Enums, Request, Response } from '@bitwarden/jslib';
|
||||
|
||||
import ApiService from './api.service';
|
||||
import ConstantsService from './constants.service';
|
||||
@@ -69,8 +64,8 @@ export default class CipherService {
|
||||
this.decryptedCipherCache = null;
|
||||
}
|
||||
|
||||
async encrypt(model: any): Promise<Cipher> {
|
||||
const cipher = new Cipher();
|
||||
async encrypt(model: any): Promise<Domain.Cipher> {
|
||||
const cipher = new Domain.Cipher();
|
||||
cipher.id = model.id;
|
||||
cipher.folderId = model.folderId;
|
||||
cipher.favorite = model.favorite;
|
||||
@@ -93,17 +88,17 @@ export default class CipherService {
|
||||
return cipher;
|
||||
}
|
||||
|
||||
async encryptFields(fieldsModel: any[], key: SymmetricCryptoKey): Promise<Field[]> {
|
||||
async encryptFields(fieldsModel: any[], key: Domain.SymmetricCryptoKey): Promise<Domain.Field[]> {
|
||||
if (!fieldsModel || !fieldsModel.length) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const self = this;
|
||||
const encFields: Field[] = [];
|
||||
const encFields: Domain.Field[] = [];
|
||||
await fieldsModel.reduce((promise, field) => {
|
||||
return promise.then(() => {
|
||||
return self.encryptField(field, key);
|
||||
}).then((encField: Field) => {
|
||||
}).then((encField: Domain.Field) => {
|
||||
encFields.push(encField);
|
||||
});
|
||||
}, Promise.resolve());
|
||||
@@ -111,8 +106,8 @@ export default class CipherService {
|
||||
return encFields;
|
||||
}
|
||||
|
||||
async encryptField(fieldModel: any, key: SymmetricCryptoKey): Promise<Field> {
|
||||
const field = new Field();
|
||||
async encryptField(fieldModel: any, key: Domain.SymmetricCryptoKey): Promise<Domain.Field> {
|
||||
const field = new Domain.Field();
|
||||
field.type = fieldModel.type;
|
||||
|
||||
await this.encryptObjProperty(fieldModel, field, {
|
||||
@@ -123,7 +118,7 @@ export default class CipherService {
|
||||
return field;
|
||||
}
|
||||
|
||||
async get(id: string): Promise<Cipher> {
|
||||
async get(id: string): Promise<Domain.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; }>(
|
||||
@@ -132,18 +127,18 @@ export default class CipherService {
|
||||
return null;
|
||||
}
|
||||
|
||||
return new Cipher(ciphers[id], false, localData ? localData[id] : null);
|
||||
return new Domain.Cipher(ciphers[id], false, localData ? localData[id] : null);
|
||||
}
|
||||
|
||||
async getAll(): Promise<Cipher[]> {
|
||||
async getAll(): Promise<Domain.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; }>(
|
||||
Keys.ciphersPrefix + userId);
|
||||
const response: Cipher[] = [];
|
||||
const response: Domain.Cipher[] = [];
|
||||
for (const id in ciphers) {
|
||||
if (ciphers.hasOwnProperty(id)) {
|
||||
response.push(new Cipher(ciphers[id], false, localData ? localData[id] : null));
|
||||
response.push(new Domain.Cipher(ciphers[id], false, localData ? localData[id] : null));
|
||||
}
|
||||
}
|
||||
return response;
|
||||
@@ -278,7 +273,7 @@ export default class CipherService {
|
||||
await this.storageService.save(Keys.neverDomains, domains);
|
||||
}
|
||||
|
||||
async saveWithServer(cipher: Cipher): Promise<any> {
|
||||
async saveWithServer(cipher: Domain.Cipher): Promise<any> {
|
||||
const request = new Request.Cipher(cipher);
|
||||
|
||||
let response: Response.Cipher;
|
||||
@@ -294,7 +289,7 @@ export default class CipherService {
|
||||
await this.upsert(data);
|
||||
}
|
||||
|
||||
saveAttachmentWithServer(cipher: Cipher, unencryptedFile: any): Promise<any> {
|
||||
saveAttachmentWithServer(cipher: Domain.Cipher, unencryptedFile: any): Promise<any> {
|
||||
const self = this;
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
@@ -321,7 +316,7 @@ export default class CipherService {
|
||||
const userId = await self.userService.getUserId();
|
||||
const data = new Data.Cipher(response, userId, cipher.collectionIds);
|
||||
this.upsert(data);
|
||||
resolve(new Cipher(data));
|
||||
resolve(new Domain.Cipher(data));
|
||||
|
||||
};
|
||||
|
||||
@@ -427,7 +422,7 @@ export default class CipherService {
|
||||
|
||||
// Helpers
|
||||
|
||||
private encryptObjProperty(model: any, obj: any, map: any, key: SymmetricCryptoKey): Promise<void[]> {
|
||||
private encryptObjProperty(model: any, obj: any, map: any, key: Domain.SymmetricCryptoKey): Promise<void[]> {
|
||||
const promises = [];
|
||||
const self = this;
|
||||
|
||||
@@ -444,7 +439,7 @@ export default class CipherService {
|
||||
return self.cryptoService.encrypt(modelProp, key);
|
||||
}
|
||||
return null;
|
||||
}).then((val: CipherString) => {
|
||||
}).then((val: Domain.CipherString) => {
|
||||
theObj[theProp] = val;
|
||||
});
|
||||
promises.push(p);
|
||||
@@ -454,7 +449,7 @@ export default class CipherService {
|
||||
return Promise.all(promises);
|
||||
}
|
||||
|
||||
private encryptCipherData(cipher: Cipher, model: any, key: SymmetricCryptoKey): Promise<any> {
|
||||
private encryptCipherData(cipher: Domain.Cipher, model: any, key: Domain.SymmetricCryptoKey): Promise<any> {
|
||||
switch (cipher.type) {
|
||||
case Enums.CipherType.Login:
|
||||
model.login = {};
|
||||
|
||||
@@ -1,10 +1,7 @@
|
||||
import { CipherString } from '../models/domain/cipherString';
|
||||
import { Collection } from '../models/domain/collection';
|
||||
|
||||
import CryptoService from './crypto.service';
|
||||
import UserService from './user.service';
|
||||
|
||||
import { Abstractions, Data } from '@bitwarden/jslib';
|
||||
import { Abstractions, Data, Domain } from '@bitwarden/jslib';
|
||||
|
||||
const Keys = {
|
||||
collectionsPrefix: 'collections_',
|
||||
@@ -21,7 +18,7 @@ export default class CollectionService {
|
||||
this.decryptedCollectionCache = null;
|
||||
}
|
||||
|
||||
async get(id: string): Promise<Collection> {
|
||||
async get(id: string): Promise<Domain.Collection> {
|
||||
const userId = await this.userService.getUserId();
|
||||
const collections = await this.storageService.get<{ [id: string]: Data.Collection; }>(
|
||||
Keys.collectionsPrefix + userId);
|
||||
@@ -29,17 +26,17 @@ export default class CollectionService {
|
||||
return null;
|
||||
}
|
||||
|
||||
return new Collection(collections[id]);
|
||||
return new Domain.Collection(collections[id]);
|
||||
}
|
||||
|
||||
async getAll(): Promise<Collection[]> {
|
||||
async getAll(): Promise<Domain.Collection[]> {
|
||||
const userId = await this.userService.getUserId();
|
||||
const collections = await this.storageService.get<{ [id: string]: Data.Collection; }>(
|
||||
Keys.collectionsPrefix + userId);
|
||||
const response: Collection[] = [];
|
||||
const response: Domain.Collection[] = [];
|
||||
for (const id in collections) {
|
||||
if (collections.hasOwnProperty(id)) {
|
||||
response.push(new Collection(collections[id]));
|
||||
response.push(new Domain.Collection(collections[id]));
|
||||
}
|
||||
}
|
||||
return response;
|
||||
|
||||
@@ -1,10 +1,6 @@
|
||||
import * as forge from 'node-forge';
|
||||
|
||||
import { Abstractions, Enums, Response, Services } from '@bitwarden/jslib';
|
||||
|
||||
import { CipherString } from '../models/domain/cipherString';
|
||||
import EncryptedObject from '../models/domain/encryptedObject';
|
||||
import SymmetricCryptoKey from '../models/domain/symmetricCryptoKey';
|
||||
import { Abstractions, Domain, Enums, Response, Services } from '@bitwarden/jslib';
|
||||
|
||||
import ConstantsService from './constants.service';
|
||||
|
||||
@@ -31,18 +27,18 @@ const Crypto = window.crypto;
|
||||
const Subtle = Crypto.subtle;
|
||||
|
||||
export default class CryptoService implements CryptoServiceInterface {
|
||||
private key: SymmetricCryptoKey;
|
||||
private encKey: SymmetricCryptoKey;
|
||||
private legacyEtmKey: SymmetricCryptoKey;
|
||||
private key: Domain.SymmetricCryptoKey;
|
||||
private encKey: Domain.SymmetricCryptoKey;
|
||||
private legacyEtmKey: Domain.SymmetricCryptoKey;
|
||||
private keyHash: string;
|
||||
private privateKey: ArrayBuffer;
|
||||
private orgKeys: Map<string, SymmetricCryptoKey>;
|
||||
private orgKeys: Map<string, Domain.SymmetricCryptoKey>;
|
||||
|
||||
constructor(private storageService: Abstractions.StorageService,
|
||||
private secureStorageService: Abstractions.StorageService) {
|
||||
}
|
||||
|
||||
async setKey(key: SymmetricCryptoKey): Promise<any> {
|
||||
async setKey(key: Domain.SymmetricCryptoKey): Promise<any> {
|
||||
this.key = key;
|
||||
|
||||
const option = await this.storageService.get<number>(ConstantsService.lockOptionKey);
|
||||
@@ -85,7 +81,7 @@ export default class CryptoService implements CryptoServiceInterface {
|
||||
return this.storageService.save(Keys.encOrgKeys, orgKeys);
|
||||
}
|
||||
|
||||
async getKey(): Promise<SymmetricCryptoKey> {
|
||||
async getKey(): Promise<Domain.SymmetricCryptoKey> {
|
||||
if (this.key != null) {
|
||||
return this.key;
|
||||
}
|
||||
@@ -97,7 +93,7 @@ export default class CryptoService implements CryptoServiceInterface {
|
||||
|
||||
const key = await this.secureStorageService.get<string>(Keys.key);
|
||||
if (key) {
|
||||
this.key = new SymmetricCryptoKey(key, true);
|
||||
this.key = new Domain.SymmetricCryptoKey(key, true);
|
||||
}
|
||||
|
||||
return key == null ? null : this.key;
|
||||
@@ -111,7 +107,7 @@ export default class CryptoService implements CryptoServiceInterface {
|
||||
return this.storageService.get<string>(Keys.keyHash);
|
||||
}
|
||||
|
||||
async getEncKey(): Promise<SymmetricCryptoKey> {
|
||||
async getEncKey(): Promise<Domain.SymmetricCryptoKey> {
|
||||
if (this.encKey != null) {
|
||||
return this.encKey;
|
||||
}
|
||||
@@ -126,12 +122,12 @@ export default class CryptoService implements CryptoServiceInterface {
|
||||
return null;
|
||||
}
|
||||
|
||||
const decEncKey = await this.decrypt(new CipherString(encKey), key, 'raw');
|
||||
const decEncKey = await this.decrypt(new Domain.CipherString(encKey), key, 'raw');
|
||||
if (decEncKey == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
this.encKey = new SymmetricCryptoKey(decEncKey);
|
||||
this.encKey = new Domain.SymmetricCryptoKey(decEncKey);
|
||||
return this.encKey;
|
||||
}
|
||||
|
||||
@@ -145,13 +141,13 @@ export default class CryptoService implements CryptoServiceInterface {
|
||||
return null;
|
||||
}
|
||||
|
||||
const privateKey = await this.decrypt(new CipherString(encPrivateKey), null, 'raw');
|
||||
const privateKey = await this.decrypt(new Domain.CipherString(encPrivateKey), null, 'raw');
|
||||
const privateKeyB64 = forge.util.encode64(privateKey);
|
||||
this.privateKey = Services.UtilsService.fromB64ToArray(privateKeyB64).buffer;
|
||||
return this.privateKey;
|
||||
}
|
||||
|
||||
async getOrgKeys(): Promise<Map<string, SymmetricCryptoKey>> {
|
||||
async getOrgKeys(): Promise<Map<string, Domain.SymmetricCryptoKey>> {
|
||||
if (this.orgKeys != null && this.orgKeys.size > 0) {
|
||||
return this.orgKeys;
|
||||
}
|
||||
@@ -162,7 +158,7 @@ export default class CryptoService implements CryptoServiceInterface {
|
||||
return null;
|
||||
}
|
||||
|
||||
const orgKeys: Map<string, SymmetricCryptoKey> = new Map<string, SymmetricCryptoKey>();
|
||||
const orgKeys: Map<string, Domain.SymmetricCryptoKey> = new Map<string, Domain.SymmetricCryptoKey>();
|
||||
let setKey = false;
|
||||
|
||||
for (const orgId in encOrgKeys) {
|
||||
@@ -171,7 +167,7 @@ export default class CryptoService implements CryptoServiceInterface {
|
||||
}
|
||||
|
||||
const decValueB64 = await this.rsaDecrypt(encOrgKeys[orgId]);
|
||||
orgKeys.set(orgId, new SymmetricCryptoKey(decValueB64, true));
|
||||
orgKeys.set(orgId, new Domain.SymmetricCryptoKey(decValueB64, true));
|
||||
setKey = true;
|
||||
}
|
||||
|
||||
@@ -182,7 +178,7 @@ export default class CryptoService implements CryptoServiceInterface {
|
||||
return this.orgKeys;
|
||||
}
|
||||
|
||||
async getOrgKey(orgId: string): Promise<SymmetricCryptoKey> {
|
||||
async getOrgKey(orgId: string): Promise<Domain.SymmetricCryptoKey> {
|
||||
if (orgId == null) {
|
||||
return null;
|
||||
}
|
||||
@@ -252,13 +248,13 @@ export default class CryptoService implements CryptoServiceInterface {
|
||||
await this.setKey(key);
|
||||
}
|
||||
|
||||
makeKey(password: string, salt: string): SymmetricCryptoKey {
|
||||
makeKey(password: string, salt: string): Domain.SymmetricCryptoKey {
|
||||
const keyBytes: string = (forge as any).pbkdf2(forge.util.encodeUtf8(password), forge.util.encodeUtf8(salt),
|
||||
5000, 256 / 8, 'sha256');
|
||||
return new SymmetricCryptoKey(keyBytes);
|
||||
return new Domain.SymmetricCryptoKey(keyBytes);
|
||||
}
|
||||
|
||||
async hashPassword(password: string, key: SymmetricCryptoKey): Promise<string> {
|
||||
async hashPassword(password: string, key: Domain.SymmetricCryptoKey): Promise<string> {
|
||||
const storedKey = await this.getKey();
|
||||
key = key || storedKey;
|
||||
if (!password || !key) {
|
||||
@@ -269,14 +265,14 @@ export default class CryptoService implements CryptoServiceInterface {
|
||||
return forge.util.encode64(hashBits);
|
||||
}
|
||||
|
||||
makeEncKey(key: SymmetricCryptoKey): Promise<CipherString> {
|
||||
makeEncKey(key: Domain.SymmetricCryptoKey): Promise<Domain.CipherString> {
|
||||
const bytes = new Uint8Array(512 / 8);
|
||||
Crypto.getRandomValues(bytes);
|
||||
return this.encrypt(bytes, key, 'raw');
|
||||
}
|
||||
|
||||
async encrypt(plainValue: string | Uint8Array, key?: SymmetricCryptoKey,
|
||||
plainValueEncoding: string = 'utf8'): Promise<CipherString> {
|
||||
async encrypt(plainValue: string | Uint8Array, key?: Domain.SymmetricCryptoKey,
|
||||
plainValueEncoding: string = 'utf8'): Promise<Domain.CipherString> {
|
||||
if (!plainValue) {
|
||||
return Promise.resolve(null);
|
||||
}
|
||||
@@ -292,10 +288,10 @@ export default class CryptoService implements CryptoServiceInterface {
|
||||
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);
|
||||
return new Domain.CipherString(encValue.key.encType, iv, ct, mac);
|
||||
}
|
||||
|
||||
async encryptToBytes(plainValue: ArrayBuffer, key?: SymmetricCryptoKey): Promise<ArrayBuffer> {
|
||||
async encryptToBytes(plainValue: ArrayBuffer, key?: Domain.SymmetricCryptoKey): Promise<ArrayBuffer> {
|
||||
const encValue = await this.aesEncrypt(plainValue, key);
|
||||
let macLen = 0;
|
||||
if (encValue.mac) {
|
||||
@@ -313,7 +309,7 @@ export default class CryptoService implements CryptoServiceInterface {
|
||||
return encBytes.buffer;
|
||||
}
|
||||
|
||||
async decrypt(cipherString: CipherString, key?: SymmetricCryptoKey,
|
||||
async decrypt(cipherString: Domain.CipherString, key?: Domain.SymmetricCryptoKey,
|
||||
outputEncoding: string = 'utf8'): Promise<string> {
|
||||
const ivBytes: string = forge.util.decode64(cipherString.initializationVector);
|
||||
const ctBytes: string = forge.util.decode64(cipherString.cipherText);
|
||||
@@ -330,7 +326,7 @@ export default class CryptoService implements CryptoServiceInterface {
|
||||
}
|
||||
}
|
||||
|
||||
async decryptFromBytes(encBuf: ArrayBuffer, key: SymmetricCryptoKey): Promise<ArrayBuffer> {
|
||||
async decryptFromBytes(encBuf: ArrayBuffer, key: Domain.SymmetricCryptoKey): Promise<ArrayBuffer> {
|
||||
if (!encBuf) {
|
||||
throw new Error('no encBuf.');
|
||||
}
|
||||
@@ -448,8 +444,8 @@ export default class CryptoService implements CryptoServiceInterface {
|
||||
|
||||
// Helpers
|
||||
|
||||
private async aesEncrypt(plainValue: ArrayBuffer, key: SymmetricCryptoKey): Promise<EncryptedObject> {
|
||||
const obj = new EncryptedObject();
|
||||
private async aesEncrypt(plainValue: ArrayBuffer, key: Domain.SymmetricCryptoKey): Promise<Domain.EncryptedObject> {
|
||||
const obj = new Domain.EncryptedObject();
|
||||
obj.key = await this.getKeyForEncryption(key);
|
||||
const keyBuf = obj.key.getBuffers();
|
||||
|
||||
@@ -472,7 +468,7 @@ export default class CryptoService implements CryptoServiceInterface {
|
||||
}
|
||||
|
||||
private async aesDecrypt(encType: Enums.EncryptionType, ctBytes: string, ivBytes: string, macBytes: string,
|
||||
key: SymmetricCryptoKey): Promise<any> {
|
||||
key: Domain.SymmetricCryptoKey): Promise<any> {
|
||||
const keyForEnc = await this.getKeyForEncryption(key);
|
||||
const theKey = this.resolveLegacyKey(encType, keyForEnc);
|
||||
|
||||
@@ -501,7 +497,7 @@ export default class CryptoService implements CryptoServiceInterface {
|
||||
}
|
||||
|
||||
private async aesDecryptWC(encType: Enums.EncryptionType, ctBuf: ArrayBuffer, ivBuf: ArrayBuffer,
|
||||
macBuf: ArrayBuffer, key: SymmetricCryptoKey): Promise<ArrayBuffer> {
|
||||
macBuf: ArrayBuffer, key: Domain.SymmetricCryptoKey): Promise<ArrayBuffer> {
|
||||
const theKey = await this.getKeyForEncryption(key);
|
||||
const keyBuf = theKey.getBuffers();
|
||||
const encKey = await Subtle.importKey('raw', keyBuf.encKey, AesAlgorithm, false, ['decrypt']);
|
||||
@@ -577,7 +573,7 @@ export default class CryptoService implements CryptoServiceInterface {
|
||||
return true;
|
||||
}
|
||||
|
||||
private async getKeyForEncryption(key?: SymmetricCryptoKey): Promise<SymmetricCryptoKey> {
|
||||
private async getKeyForEncryption(key?: Domain.SymmetricCryptoKey): Promise<Domain.SymmetricCryptoKey> {
|
||||
if (key) {
|
||||
return key;
|
||||
}
|
||||
@@ -586,12 +582,12 @@ export default class CryptoService implements CryptoServiceInterface {
|
||||
return encKey || (await this.getKey());
|
||||
}
|
||||
|
||||
private resolveLegacyKey(encType: Enums.EncryptionType, key: SymmetricCryptoKey): SymmetricCryptoKey {
|
||||
private resolveLegacyKey(encType: Enums.EncryptionType, key: Domain.SymmetricCryptoKey): Domain.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, Enums.EncryptionType.AesCbc128_HmacSha256_B64);
|
||||
new Domain.SymmetricCryptoKey(key.key, false, Enums.EncryptionType.AesCbc128_HmacSha256_B64);
|
||||
return this.legacyEtmKey;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
import ApiService from './api.service';
|
||||
import ConstantsService from './constants.service';
|
||||
|
||||
import { Abstractions } from '@bitwarden/jslib';
|
||||
|
||||
import EnvironmentUrls from '../models/domain/environmentUrls';
|
||||
import { Abstractions, Domain } from '@bitwarden/jslib';
|
||||
|
||||
export default class EnvironmentService {
|
||||
baseUrl: string;
|
||||
@@ -25,7 +23,7 @@ export default class EnvironmentService {
|
||||
webVault: null,
|
||||
};
|
||||
|
||||
const envUrls = new EnvironmentUrls();
|
||||
const envUrls = new Domain.EnvironmentUrls();
|
||||
|
||||
if (urls.base) {
|
||||
this.baseUrl = envUrls.base = urls.base;
|
||||
@@ -61,7 +59,7 @@ export default class EnvironmentService {
|
||||
this.identityUrl = urls.identity;
|
||||
this.iconsUrl = urls.icons;
|
||||
|
||||
const envUrls = new EnvironmentUrls();
|
||||
const envUrls = new Domain.EnvironmentUrls();
|
||||
if (this.baseUrl) {
|
||||
envUrls.base = this.baseUrl;
|
||||
} else {
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
import { CipherString } from '../models/domain/cipherString';
|
||||
|
||||
import ApiService from './api.service';
|
||||
import CryptoService from './crypto.service';
|
||||
import UserService from './user.service';
|
||||
|
||||
@@ -1,9 +1,6 @@
|
||||
import { CipherString } from '../models/domain/cipherString';
|
||||
import PasswordHistory from '../models/domain/passwordHistory';
|
||||
|
||||
import CryptoService from './crypto.service';
|
||||
|
||||
import { Abstractions, Services } from '@bitwarden/jslib';
|
||||
import { Abstractions, Domain, Services } from '@bitwarden/jslib';
|
||||
|
||||
const DefaultOptions = {
|
||||
length: 14,
|
||||
@@ -144,10 +141,10 @@ export default class PasswordGenerationService {
|
||||
}
|
||||
|
||||
optionsCache: any;
|
||||
history: PasswordHistory[] = [];
|
||||
history: Domain.PasswordHistory[] = [];
|
||||
|
||||
constructor(private cryptoService: CryptoService, private storageService: Abstractions.StorageService) {
|
||||
storageService.get<PasswordHistory[]>(Keys.history).then((encrypted) => {
|
||||
storageService.get<Domain.PasswordHistory[]>(Keys.history).then((encrypted) => {
|
||||
return this.decryptHistory(encrypted);
|
||||
}).then((history) => {
|
||||
this.history = history;
|
||||
@@ -177,7 +174,7 @@ export default class PasswordGenerationService {
|
||||
}
|
||||
|
||||
getHistory() {
|
||||
return this.history || new Array<PasswordHistory>();
|
||||
return this.history || new Array<Domain.PasswordHistory>();
|
||||
}
|
||||
|
||||
async addHistory(password: string): Promise<any> {
|
||||
@@ -186,7 +183,7 @@ export default class PasswordGenerationService {
|
||||
return;
|
||||
}
|
||||
|
||||
this.history.push(new PasswordHistory(password, Date.now()));
|
||||
this.history.push(new Domain.PasswordHistory(password, Date.now()));
|
||||
|
||||
// Remove old items.
|
||||
if (this.history.length > MaxPasswordsInHistory) {
|
||||
@@ -202,27 +199,27 @@ export default class PasswordGenerationService {
|
||||
return await this.storageService.remove(Keys.history);
|
||||
}
|
||||
|
||||
private async encryptHistory(): Promise<PasswordHistory[]> {
|
||||
private async encryptHistory(): Promise<Domain.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 PasswordHistory(encrypted.encryptedString, item.date);
|
||||
return new Domain.PasswordHistory(encrypted.encryptedString, item.date);
|
||||
});
|
||||
|
||||
return await Promise.all(promises);
|
||||
}
|
||||
|
||||
private async decryptHistory(history: PasswordHistory[]): Promise<PasswordHistory[]> {
|
||||
private async decryptHistory(history: Domain.PasswordHistory[]): Promise<Domain.PasswordHistory[]> {
|
||||
if (history == null || history.length === 0) {
|
||||
return Promise.resolve([]);
|
||||
}
|
||||
|
||||
const promises = history.map(async (item) => {
|
||||
const decrypted = await this.cryptoService.decrypt(new CipherString(item.password));
|
||||
return new PasswordHistory(decrypted, item.date);
|
||||
const decrypted = await this.cryptoService.decrypt(new Domain.CipherString(item.password));
|
||||
return new Domain.PasswordHistory(decrypted, item.date);
|
||||
});
|
||||
|
||||
return await Promise.all(promises);
|
||||
|
||||
Reference in New Issue
Block a user