1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-17 16:53:34 +00:00

view model types

This commit is contained in:
Kyle Spearrin
2018-01-24 11:33:15 -05:00
parent 5557c5b638
commit 7e1c883f03
19 changed files with 299 additions and 118 deletions

View File

@@ -3,6 +3,8 @@ import { AttachmentData } from '../data/attachmentData';
import { CipherString } from './cipherString';
import Domain from './domain';
import { AttachmentView } from '../view/attachmentView';
export class Attachment extends Domain {
id: string;
url: string;
@@ -25,15 +27,8 @@ export class Attachment extends Domain {
}, 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, {
decrypt(orgId: string): Promise<AttachmentView> {
return this.decryptObj(new AttachmentView(this), {
fileName: null,
}, orgId);
}

View File

@@ -3,6 +3,8 @@ import { CardData } from '../data/cardData';
import { CipherString } from './cipherString';
import Domain from './domain';
import { CardView } from '../view/cardView';
export class Card extends Domain {
cardholderName: CipherString;
brand: CipherString;
@@ -27,8 +29,8 @@ export class Card extends Domain {
}, alreadyEncrypted, []);
}
decrypt(orgId: string): Promise<any> {
return this.decryptObj({}, {
decrypt(orgId: string): Promise<CardView> {
return this.decryptObj(new CardView(this), {
cardholderName: null,
brand: null,
number: null,

View File

@@ -1,9 +1,9 @@
import { CipherType } from '../../enums/cipherType';
import { PlatformUtilsService } from '../../abstractions/platformUtils.service';
import { CipherData } from '../data/cipherData';
import { CipherView } from '../view/cipherView';
import { Attachment } from './attachment';
import { Card } from './card';
import { CipherString } from './cipherString';
@@ -89,23 +89,8 @@ export class Cipher extends Domain {
}
}
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,
};
async decrypt(): Promise<CipherView> {
const model = new CipherView(this);
await this.decryptObj(model, {
name: null,
@@ -115,44 +100,15 @@ export class Cipher extends Domain {
switch (this.type) {
case 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: PlatformUtilsService =
containerService.getPlatformUtilsService();
model.login.domain = platformUtilsService.getDomain(model.login.uri);
} else {
throw new Error('window.bitwardenContainerService not initialized.');
}
}
break;
case CipherType.SecureNote:
model.secureNote = await this.secureNote.decrypt(this.organizationId);
model.subTitle = null;
break;
case 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 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;

View File

@@ -1,22 +1,25 @@
import { CipherString } from '../domain/cipherString';
import { View } from '../view/view';
export default abstract class Domain {
protected buildDomainModel(model: any, obj: any, map: any, alreadyEncrypted: boolean, notEncList: any[] = []) {
protected buildDomainModel<D extends Domain>(domain: D, dataObj: any, map: any,
alreadyEncrypted: boolean, notEncList: any[] = []) {
for (const prop in map) {
if (!map.hasOwnProperty(prop)) {
continue;
}
const objProp = obj[(map[prop] || prop)];
const objProp = dataObj[(map[prop] || prop)];
if (alreadyEncrypted === true || notEncList.indexOf(prop) > -1) {
model[prop] = objProp ? objProp : null;
(domain as any)[prop] = objProp ? objProp : null;
} else {
model[prop] = objProp ? new CipherString(objProp) : null;
(domain as any)[prop] = objProp ? new CipherString(objProp) : null;
}
}
}
protected async decryptObj(model: any, map: any, orgId: string) {
protected async decryptObj<T extends View>(viewModel: T, map: any, orgId: string): Promise<T> {
const promises = [];
const self: any = this;
@@ -34,13 +37,13 @@ export default abstract class Domain {
}
return null;
}).then((val: any) => {
model[theProp] = val;
(viewModel as any)[theProp] = val;
});
promises.push(p);
})(prop);
}
await Promise.all(promises);
return model;
return viewModel;
}
}

View File

@@ -5,6 +5,8 @@ import { FieldData } from '../data/fieldData';
import { CipherString } from './cipherString';
import Domain from './domain';
import { FieldView } from '../view/fieldView';
export class Field extends Domain {
name: CipherString;
vault: CipherString;
@@ -23,12 +25,8 @@ export class Field extends Domain {
}, alreadyEncrypted, []);
}
decrypt(orgId: string): Promise<any> {
const model = {
type: this.type,
};
return this.decryptObj(model, {
decrypt(orgId: string): Promise<FieldView> {
return this.decryptObj(new FieldView(this), {
name: null,
value: null,
}, orgId);

View File

@@ -3,6 +3,8 @@ import { IdentityData } from '../data/identityData';
import { CipherString } from './cipherString';
import Domain from './domain';
import { IdentityView } from '../view/identityView';
export class Identity extends Domain {
title: CipherString;
firstName: CipherString;
@@ -51,8 +53,8 @@ export class Identity extends Domain {
}, alreadyEncrypted, []);
}
decrypt(orgId: string): Promise<any> {
return this.decryptObj({}, {
decrypt(orgId: string): Promise<IdentityView> {
return this.decryptObj(new IdentityView(this), {
title: null,
firstName: null,
middleName: null,

View File

@@ -1,5 +1,7 @@
import { LoginData } from '../data/loginData';
import { LoginView } from '../view/loginView';
import { CipherString } from './cipherString';
import Domain from './domain';
@@ -23,8 +25,8 @@ export class Login extends Domain {
}, alreadyEncrypted, []);
}
decrypt(orgId: string): Promise<any> {
return this.decryptObj({}, {
decrypt(orgId: string): Promise<LoginView> {
return this.decryptObj(new LoginView(this), {
uri: null,
username: null,
password: null,

View File

@@ -4,6 +4,8 @@ import { SecureNoteData } from '../data/secureNoteData';
import Domain from './domain';
import { SecureNoteView } from '../view/secureNoteView';
export class SecureNote extends Domain {
type: SecureNoteType;
@@ -16,9 +18,7 @@ export class SecureNote extends Domain {
this.type = obj.type;
}
decrypt(orgId: string): any {
return {
type: this.type,
};
decrypt(orgId: string): Promise<SecureNoteView> {
return Promise.resolve(new SecureNoteView(this));
}
}

View File

@@ -0,0 +1,18 @@
import { View } from './view';
import { Attachment } from '../domain/attachment';
export class AttachmentView implements View {
id: string;
url: string;
size: number;
sizeName: string;
fileName: string;
constructor(a: Attachment) {
this.id = a.id;
this.url = a.url;
this.size = a.size;
this.sizeName = a.sizeName;
}
}

View File

@@ -0,0 +1,16 @@
import { View } from './view';
import { Card } from '../domain/card';
export class CardView implements View {
cardholderName: string;
brand: string;
number: string;
expMonth: string;
expYear: string;
code: string;
constructor(c?: Card) {
// ctor
}
}

View File

@@ -0,0 +1,80 @@
import { CipherType } from '../../enums/cipherType';
import { Cipher } from '../domain/cipher';
import { AttachmentView } from './attachmentView';
import { CardView } from './cardView';
import { FieldView } from './fieldView';
import { IdentityView } from './identityView';
import { LoginView } from './loginView';
import { SecureNoteView } from './secureNoteView';
import { View } from './view';
export class CipherView implements View {
id: string;
organizationId: string;
folderId: string;
name: string;
notes: string;
type: CipherType;
favorite: boolean;
localData: any;
login: LoginView;
identity: IdentityView;
card: CardView;
secureNote: SecureNoteView;
attachments: AttachmentView[];
fields: FieldView[];
collectionIds: string[];
// tslint:disable-next-line
private _subTitle: string;
constructor(c: Cipher) {
this.id = c.id;
this.organizationId = c.organizationId;
this.folderId = c.folderId;
this.favorite = c.favorite;
this.type = c.type;
this.localData = c.localData;
this.collectionIds = c.collectionIds;
}
get subTitle(): string {
if (this._subTitle == null) {
switch (this.type) {
case CipherType.Login:
this._subTitle = this.login.username;
break;
case CipherType.SecureNote:
this._subTitle = null;
break;
case CipherType.Card:
this._subTitle = this.card.brand;
if (this.card.number != null && this.card.number.length >= 4) {
if (this._subTitle !== '') {
this._subTitle += ', ';
}
this._subTitle += ('*' + this.card.number.substr(this.card.number.length - 4));
}
break;
case CipherType.Identity:
this._subTitle = '';
if (this.identity.firstName != null) {
this._subTitle = this.identity.firstName;
}
if (this.identity.lastName != null) {
if (this._subTitle !== '') {
this._subTitle += ' ';
}
this._subTitle += this.identity.lastName;
}
break;
default:
break;
}
}
return this._subTitle;
}
}

View File

@@ -0,0 +1,15 @@
import { FieldType } from '../../enums/fieldType';
import { View } from './view';
import { Field } from '../domain/field';
export class FieldView implements View {
name: string;
vault: string;
type: FieldType;
constructor(f: Field) {
this.type = f.type;
}
}

View File

@@ -0,0 +1,28 @@
import { View } from './view';
import { Identity } from '../domain/identity';
export class IdentityView implements View {
title: string;
firstName: string;
middleName: string;
lastName: string;
address1: string;
address2: string;
address3: string;
city: string;
state: string;
postalCode: string;
country: string;
company: string;
email: string;
phone: string;
ssn: string;
username: string;
passportNumber: string;
licenseNumber: string;
constructor(i?: Identity) {
// ctor
}
}

8
src/models/view/index.ts Normal file
View File

@@ -0,0 +1,8 @@
export { AttachmentView } from './attachmentView';
export { CardView } from './cardView';
export { CipherView } from './cipherView';
export { FieldView } from './fieldView';
export { IdentityView } from './identityView';
export { LoginView } from './loginView';
export { SecureNoteView } from './secureNoteView';
export { View } from './view';

View File

@@ -0,0 +1,34 @@
import { View } from './view';
import { Login } from '../domain/login';
import { PlatformUtilsService } from '../../abstractions/platformUtils.service';
export class LoginView implements View {
uri: string;
username: string;
password: string;
maskedPassword: string;
totp: string;
// tslint:disable-next-line
private _domain: string;
constructor(l?: Login) {
// ctor
}
get domain(): string {
if (this._domain == null && this.uri != null) {
const containerService = (window as any).bitwardenContainerService;
if (containerService) {
const platformUtilsService: PlatformUtilsService = containerService.getPlatformUtilsService();
this._domain = platformUtilsService.getDomain(this.uri);
} else {
throw new Error('window.bitwardenContainerService not initialized.');
}
}
return this._domain;
}
}

View File

@@ -0,0 +1,13 @@
import { SecureNoteType } from '../../enums/secureNoteType';
import { View } from './view';
import { SecureNote } from '../domain/secureNote';
export class SecureNoteView implements View {
type: SecureNoteType;
constructor(n: SecureNote) {
this.type = n.type;
}
}

2
src/models/view/view.ts Normal file
View File

@@ -0,0 +1,2 @@
export class View {
}