1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-16 08:13:42 +00:00

convert domain models to jslib

This commit is contained in:
Kyle Spearrin
2018-01-08 15:23:36 -05:00
parent 5d39030e05
commit e68f7a1141
26 changed files with 90 additions and 884 deletions

View File

@@ -1,43 +0,0 @@
import { Data } from '@bitwarden/jslib';
import { CipherString } from './cipherString';
import Domain from './domain';
class Attachment extends Domain {
id: string;
url: string;
size: number;
sizeName: string;
fileName: CipherString;
constructor(obj?: Data.Attachment, alreadyEncrypted: boolean = false) {
super();
if (obj == null) {
return;
}
this.size = obj.size;
this.buildDomainModel(this, obj, {
id: null,
url: null,
sizeName: null,
fileName: null,
}, alreadyEncrypted, ['id', 'url', 'sizeName']);
}
decrypt(orgId: string): Promise<any> {
const model = {
id: this.id,
size: this.size,
sizeName: this.sizeName,
url: this.url,
};
return this.decryptObj(model, {
fileName: null,
}, orgId);
}
}
export { Attachment };
(window as any).Attachment = Attachment;

View File

@@ -1,43 +0,0 @@
import { Data } from '@bitwarden/jslib';
import { CipherString } from './cipherString';
import Domain from './domain';
class Card extends Domain {
cardholderName: CipherString;
brand: CipherString;
number: CipherString;
expMonth: CipherString;
expYear: CipherString;
code: CipherString;
constructor(obj?: Data.Card, alreadyEncrypted: boolean = false) {
super();
if (obj == null) {
return;
}
this.buildDomainModel(this, obj, {
cardholderName: null,
brand: null,
number: null,
expMonth: null,
expYear: null,
code: null,
}, alreadyEncrypted, []);
}
decrypt(orgId: string): Promise<any> {
return this.decryptObj({}, {
cardholderName: null,
brand: null,
number: null,
expMonth: null,
expYear: null,
code: null,
}, orgId);
}
}
export { Card };
(window as any).Card = Card;

View File

@@ -1,188 +0,0 @@
import { Abstractions, Enums, Data } from '@bitwarden/jslib';
import { Attachment } from './attachment';
import { Card } from './card';
import { CipherString } from './cipherString';
import Domain from './domain';
import { Field } from './field';
import { Identity } from './identity';
import { Login } from './login';
import { SecureNote } from './secureNote';
class Cipher extends Domain {
id: string;
organizationId: string;
folderId: string;
name: CipherString;
notes: CipherString;
type: Enums.CipherType;
favorite: boolean;
organizationUseTotp: boolean;
edit: boolean;
localData: any;
login: Login;
identity: Identity;
card: Card;
secureNote: SecureNote;
attachments: Attachment[];
fields: Field[];
collectionIds: string[];
constructor(obj?: Data.Cipher, alreadyEncrypted: boolean = false, localData: any = null) {
super();
if (obj == null) {
return;
}
this.buildDomainModel(this, obj, {
id: null,
organizationId: null,
folderId: null,
name: null,
notes: null,
}, alreadyEncrypted, ['id', 'organizationId', 'folderId']);
this.type = obj.type;
this.favorite = obj.favorite;
this.organizationUseTotp = obj.organizationUseTotp;
this.edit = obj.edit;
this.collectionIds = obj.collectionIds;
this.localData = localData;
switch (this.type) {
case Enums.CipherType.Login:
this.login = new Login(obj.login, alreadyEncrypted);
break;
case Enums.CipherType.SecureNote:
this.secureNote = new SecureNote(obj.secureNote, alreadyEncrypted);
break;
case Enums.CipherType.Card:
this.card = new Card(obj.card, alreadyEncrypted);
break;
case Enums.CipherType.Identity:
this.identity = new Identity(obj.identity, alreadyEncrypted);
break;
default:
break;
}
if (obj.attachments != null) {
this.attachments = [];
obj.attachments.forEach((attachment) => {
this.attachments.push(new Attachment(attachment, alreadyEncrypted));
});
} else {
this.attachments = null;
}
if (obj.fields != null) {
this.fields = [];
obj.fields.forEach((field) => {
this.fields.push(new Field(field, alreadyEncrypted));
});
} else {
this.fields = null;
}
}
async decrypt(): Promise<any> {
const model = {
id: this.id,
organizationId: this.organizationId,
folderId: this.folderId,
favorite: this.favorite,
type: this.type,
localData: this.localData,
login: null as any,
card: null as any,
identity: null as any,
secureNote: null as any,
subTitle: null as string,
attachments: null as any[],
fields: null as any[],
collectionIds: this.collectionIds,
};
await this.decryptObj(model, {
name: null,
notes: null,
}, this.organizationId);
switch (this.type) {
case Enums.CipherType.Login:
model.login = await this.login.decrypt(this.organizationId);
model.subTitle = model.login.username;
if (model.login.uri) {
const containerService = (window as any).BitwardenContainerService;
if (containerService) {
const platformUtilsService: Abstractions.PlatformUtilsService =
containerService.getPlatformUtilsService();
model.login.domain = platformUtilsService.getDomain(model.login.uri);
} else {
throw new Error('window.BitwardenContainerService not initialized.');
}
}
break;
case Enums.CipherType.SecureNote:
model.secureNote = await this.secureNote.decrypt(this.organizationId);
model.subTitle = null;
break;
case Enums.CipherType.Card:
model.card = await this.card.decrypt(this.organizationId);
model.subTitle = model.card.brand;
if (model.card.number && model.card.number.length >= 4) {
if (model.subTitle !== '') {
model.subTitle += ', ';
}
model.subTitle += ('*' + model.card.number.substr(model.card.number.length - 4));
}
break;
case Enums.CipherType.Identity:
model.identity = await this.identity.decrypt(this.organizationId);
model.subTitle = '';
if (model.identity.firstName) {
model.subTitle = model.identity.firstName;
}
if (model.identity.lastName) {
if (model.subTitle !== '') {
model.subTitle += ' ';
}
model.subTitle += model.identity.lastName;
}
break;
default:
break;
}
const orgId = this.organizationId;
if (this.attachments != null && this.attachments.length > 0) {
const attachments: any[] = [];
await this.attachments.reduce((promise, attachment) => {
return promise.then(() => {
return attachment.decrypt(orgId);
}).then((decAttachment) => {
attachments.push(decAttachment);
});
}, Promise.resolve());
model.attachments = attachments;
}
if (this.fields != null && this.fields.length > 0) {
const fields: any[] = [];
await this.fields.reduce((promise, field) => {
return promise.then(() => {
return field.decrypt(orgId);
}).then((decField) => {
fields.push(decField);
});
}, Promise.resolve());
model.fields = fields;
}
return model;
}
}
export { Cipher };
(window as any).Cipher = Cipher;

View File

@@ -1,116 +0,0 @@
import { Enums } from '@bitwarden/jslib';
import { CryptoService } from '../../services/abstractions/crypto.service';
class CipherString {
encryptedString?: string;
encryptionType?: Enums.EncryptionType;
decryptedValue?: string;
cipherText?: string;
initializationVector?: string;
mac?: string;
constructor(encryptedStringOrType: string | Enums.EncryptionType, ct?: string, iv?: string, mac?: string) {
if (ct != null) {
// ct and header
const encType = encryptedStringOrType as Enums.EncryptionType;
this.encryptedString = encType + '.' + ct;
// iv
if (iv != null) {
this.encryptedString += ('|' + iv);
}
// mac
if (mac != null) {
this.encryptedString += ('|' + mac);
}
this.encryptionType = encType;
this.cipherText = ct;
this.initializationVector = iv;
this.mac = mac;
return;
}
this.encryptedString = encryptedStringOrType as string;
if (!this.encryptedString) {
return;
}
const headerPieces = this.encryptedString.split('.');
let encPieces: string[] = null;
if (headerPieces.length === 2) {
try {
this.encryptionType = parseInt(headerPieces[0], null);
encPieces = headerPieces[1].split('|');
} catch (e) {
return;
}
} else {
encPieces = this.encryptedString.split('|');
this.encryptionType = encPieces.length === 3 ? Enums.EncryptionType.AesCbc128_HmacSha256_B64 :
Enums.EncryptionType.AesCbc256_B64;
}
switch (this.encryptionType) {
case Enums.EncryptionType.AesCbc128_HmacSha256_B64:
case Enums.EncryptionType.AesCbc256_HmacSha256_B64:
if (encPieces.length !== 3) {
return;
}
this.initializationVector = encPieces[0];
this.cipherText = encPieces[1];
this.mac = encPieces[2];
break;
case Enums.EncryptionType.AesCbc256_B64:
if (encPieces.length !== 2) {
return;
}
this.initializationVector = encPieces[0];
this.cipherText = encPieces[1];
break;
case Enums.EncryptionType.Rsa2048_OaepSha256_B64:
case Enums.EncryptionType.Rsa2048_OaepSha1_B64:
if (encPieces.length !== 1) {
return;
}
this.cipherText = encPieces[0];
break;
default:
return;
}
}
decrypt(orgId: string): Promise<string> {
if (this.decryptedValue) {
return Promise.resolve(this.decryptedValue);
}
let cryptoService: CryptoService;
const containerService = (window as any).BitwardenContainerService;
if (containerService) {
cryptoService = containerService.getCryptoService();
} else {
throw new Error('window.BitwardenContainerService not initialized.');
}
return cryptoService.getOrgKey(orgId).then((orgKey: any) => {
return cryptoService.decrypt(this, orgKey);
}).then((decValue: any) => {
this.decryptedValue = decValue;
return this.decryptedValue;
}).catch(() => {
this.decryptedValue = '[error: cannot decrypt]';
return this.decryptedValue;
});
}
}
export { CipherString };
(window as any).CipherString = CipherString;

View File

@@ -1,37 +0,0 @@
import { Data } from '@bitwarden/jslib';
import { CipherString } from './cipherString';
import Domain from './domain';
class Collection extends Domain {
id: string;
organizationId: string;
name: CipherString;
constructor(obj?: Data.Collection, alreadyEncrypted: boolean = false) {
super();
if (obj == null) {
return;
}
this.buildDomainModel(this, obj, {
id: null,
organizationId: null,
name: null,
}, alreadyEncrypted, ['id', 'organizationId']);
}
decrypt(): Promise<any> {
const model = {
id: this.id,
organizationId: this.organizationId,
};
return this.decryptObj(model, {
name: null,
}, this.organizationId);
}
}
export { Collection };
(window as any).Collection = Collection;

View File

@@ -1,46 +0,0 @@
import { CipherString } from '../domain/cipherString';
export default abstract class Domain {
protected buildDomainModel(model: any, obj: any, map: any, alreadyEncrypted: boolean, notEncList: any[] = []) {
for (const prop in map) {
if (!map.hasOwnProperty(prop)) {
continue;
}
const objProp = obj[(map[prop] || prop)];
if (alreadyEncrypted === true || notEncList.indexOf(prop) > -1) {
model[prop] = objProp ? objProp : null;
} else {
model[prop] = objProp ? new CipherString(objProp) : null;
}
}
}
protected async decryptObj(model: any, map: any, orgId: string) {
const promises = [];
const self: any = this;
for (const prop in map) {
if (!map.hasOwnProperty(prop)) {
continue;
}
// tslint:disable-next-line
(function (theProp) {
const p = Promise.resolve().then(() => {
const mapProp = map[theProp] || theProp;
if (self[mapProp]) {
return self[mapProp].decrypt(orgId);
}
return null;
}).then((val: any) => {
model[theProp] = val;
});
promises.push(p);
})(prop);
}
await Promise.all(promises);
return model;
}
}

View File

@@ -1,8 +0,0 @@
import SymmetricCryptoKey from './symmetricCryptoKey';
export default class EncryptedObject {
iv: Uint8Array;
ct: Uint8Array;
mac: Uint8Array;
key: SymmetricCryptoKey;
}

View File

@@ -1,5 +0,0 @@
export default class EnvironmentUrls {
base: string;
api: string;
identity: string;
}

View File

@@ -1,37 +0,0 @@
import { Enums, Data } from '@bitwarden/jslib';
import { CipherString } from './cipherString';
import Domain from './domain';
class Field extends Domain {
name: CipherString;
vault: CipherString;
type: Enums.FieldType;
constructor(obj?: Data.Field, alreadyEncrypted: boolean = false) {
super();
if (obj == null) {
return;
}
this.type = obj.type;
this.buildDomainModel(this, obj, {
name: null,
value: null,
}, alreadyEncrypted, []);
}
decrypt(orgId: string): Promise<any> {
const model = {
type: this.type,
};
return this.decryptObj(model, {
name: null,
value: null,
}, orgId);
}
}
export { Field };
(window as any).Field = Field;

View File

@@ -1,79 +0,0 @@
import { Data } from '@bitwarden/jslib';
import { CipherString } from './cipherString';
import Domain from './domain';
class Identity extends Domain {
title: CipherString;
firstName: CipherString;
middleName: CipherString;
lastName: CipherString;
address1: CipherString;
address2: CipherString;
address3: CipherString;
city: CipherString;
state: CipherString;
postalCode: CipherString;
country: CipherString;
company: CipherString;
email: CipherString;
phone: CipherString;
ssn: CipherString;
username: CipherString;
passportNumber: CipherString;
licenseNumber: CipherString;
constructor(obj?: Data.Identity, alreadyEncrypted: boolean = false) {
super();
if (obj == null) {
return;
}
this.buildDomainModel(this, obj, {
title: null,
firstName: null,
middleName: null,
lastName: null,
address1: null,
address2: null,
address3: null,
city: null,
state: null,
postalCode: null,
country: null,
company: null,
email: null,
phone: null,
ssn: null,
username: null,
passportNumber: null,
licenseNumber: null,
}, alreadyEncrypted, []);
}
decrypt(orgId: string): Promise<any> {
return this.decryptObj({}, {
title: null,
firstName: null,
middleName: null,
lastName: null,
address1: null,
address2: null,
address3: null,
city: null,
state: null,
postalCode: null,
country: null,
company: null,
email: null,
phone: null,
ssn: null,
username: null,
passportNumber: null,
licenseNumber: null,
}, orgId);
}
}
export { Identity };
(window as any).Identity = Identity;

View File

@@ -1,37 +0,0 @@
import { Data } from '@bitwarden/jslib';
import { CipherString } from './cipherString';
import Domain from './domain';
class Login extends Domain {
uri: CipherString;
username: CipherString;
password: CipherString;
totp: CipherString;
constructor(obj?: Data.Login, alreadyEncrypted: boolean = false) {
super();
if (obj == null) {
return;
}
this.buildDomainModel(this, obj, {
uri: null,
username: null,
password: null,
totp: null,
}, alreadyEncrypted, []);
}
decrypt(orgId: string): Promise<any> {
return this.decryptObj({}, {
uri: null,
username: null,
password: null,
totp: null,
}, orgId);
}
}
export { Login };
(window as any).Login = Login;

View File

@@ -1,9 +0,0 @@
export default class PasswordHistory {
password: string;
date: number;
constructor(password: string, date: number) {
this.password = password;
this.date = date;
}
}

View File

@@ -1,25 +0,0 @@
import { Enums, Data } from '@bitwarden/jslib';
import Domain from './domain';
class SecureNote extends Domain {
type: Enums.SecureNoteType;
constructor(obj?: Data.SecureNote, alreadyEncrypted: boolean = false) {
super();
if (obj == null) {
return;
}
this.type = obj.type;
}
decrypt(orgId: string): any {
return {
type: this.type,
};
}
}
export { SecureNote };
(window as any).SecureNote = SecureNote;

View File

@@ -1,78 +0,0 @@
import * as forge from 'node-forge';
import { Enums, Services } from '@bitwarden/jslib';
import SymmetricCryptoKeyBuffers from './symmetricCryptoKeyBuffers';
export default class SymmetricCryptoKey {
key: string;
keyB64: string;
encKey: string;
macKey: string;
encType: Enums.EncryptionType;
keyBuf: SymmetricCryptoKeyBuffers;
constructor(keyBytes: string, b64KeyBytes?: boolean, encType?: Enums.EncryptionType) {
if (b64KeyBytes) {
keyBytes = forge.util.decode64(keyBytes);
}
if (!keyBytes) {
throw new Error('Must provide keyBytes');
}
const buffer = (forge as any).util.createBuffer(keyBytes);
if (!buffer || buffer.length() === 0) {
throw new Error('Couldn\'t make buffer');
}
const bufferLength: number = buffer.length();
if (encType == null) {
if (bufferLength === 32) {
encType = Enums.EncryptionType.AesCbc256_B64;
} else if (bufferLength === 64) {
encType = Enums.EncryptionType.AesCbc256_HmacSha256_B64;
} else {
throw new Error('Unable to determine encType.');
}
}
this.key = keyBytes;
this.keyB64 = forge.util.encode64(keyBytes);
this.encType = encType;
if (encType === Enums.EncryptionType.AesCbc256_B64 && bufferLength === 32) {
this.encKey = keyBytes;
this.macKey = null;
} else if (encType === Enums.EncryptionType.AesCbc128_HmacSha256_B64 && bufferLength === 32) {
this.encKey = buffer.getBytes(16); // first half
this.macKey = buffer.getBytes(16); // second half
} else if (encType === Enums.EncryptionType.AesCbc256_HmacSha256_B64 && bufferLength === 64) {
this.encKey = buffer.getBytes(32); // first half
this.macKey = buffer.getBytes(32); // second half
} else {
throw new Error('Unsupported encType/key length.');
}
}
getBuffers() {
if (this.keyBuf) {
return this.keyBuf;
}
const key = Services.UtilsService.fromB64ToArray(this.keyB64);
const keys = new SymmetricCryptoKeyBuffers(key.buffer);
if (this.macKey) {
keys.encKey = key.slice(0, key.length / 2).buffer;
keys.macKey = key.slice(key.length / 2).buffer;
} else {
keys.encKey = key.buffer;
keys.macKey = null;
}
this.keyBuf = keys;
return this.keyBuf;
}
}

View File

@@ -1,9 +0,0 @@
export default class SymmetricCryptoKeyBuffers {
key: ArrayBuffer;
encKey?: ArrayBuffer;
macKey?: ArrayBuffer;
constructor(key: ArrayBuffer) {
this.key = key;
}
}