WIP making View objects serializable

This commit is contained in:
Thomas Rittson 2022-06-24 10:38:55 +10:00
parent 3d240c05ce
commit 8ab017ed55
7 changed files with 222 additions and 1 deletions

View File

@ -1,5 +1,6 @@
import { Utils } from "@bitwarden/common/misc/utils";
import { EncryptionType } from "../../enums/encryptionType";
import { Utils } from "../../misc/utils";
export class SymmetricCryptoKey {
key: ArrayBuffer;
@ -54,4 +55,14 @@ export class SymmetricCryptoKey {
this.macKeyB64 = Utils.fromBufferToB64(this.macKey);
}
}
toJSON(): string {
const obj = Utils.copyToNewObject(this, { keyB64: null });
return JSON.stringify(obj);
}
static fromJSON(obj: any): SymmetricCryptoKey {
const arrayBuffer = Utils.fromB64ToArray(obj.keyB64);
return new SymmetricCryptoKey(arrayBuffer);
}
}

View File

@ -1,3 +1,5 @@
import { Utils } from "@bitwarden/common/misc/utils";
import { Attachment } from "../domain/attachment";
import { SymmetricCryptoKey } from "../domain/symmetricCryptoKey";
@ -32,4 +34,36 @@ export class AttachmentView implements View {
}
return 0;
}
toJSON(): string {
const obj = Utils.copyToNewObject(this, {
id: null,
url: null,
size: null,
sizeName: null,
filename: null,
});
obj.key = this.key == null ? null : JSON.stringify(this.key);
return JSON.stringify(obj);
}
static fromJSON(obj: any): AttachmentView {
const view = Utils.copyToNewObject(
obj,
{
id: null,
url: null,
size: null,
sizeName: null,
filename: null,
},
AttachmentView
);
view.key = obj.key == null ? null : SymmetricCryptoKey.fromJSON(obj.key);
return view;
}
}

View File

@ -1,3 +1,5 @@
import { Utils } from "@bitwarden/common/misc/utils";
import { CardLinkedId as LinkedId } from "../../enums/linkedIdType";
import { linkedFieldOption } from "../../misc/linkedFieldOption.decorator";
@ -79,4 +81,32 @@ export class CardView extends ItemView {
private formatYear(year: string): string {
return year.length === 2 ? "20" + year : year;
}
toJSON(): string {
const obj = Utils.copyToNewObject(this, {
cardholderName: null,
brand: null,
number: null,
expMonth: null,
expYear: null,
code: null,
});
return JSON.stringify(obj);
}
static fromJSON(obj: any): CardView {
return Utils.copyToNewObject(
obj,
{
cardholderName: null,
brand: null,
number: null,
expMonth: null,
expYear: null,
code: null,
},
CardView
);
}
}

View File

@ -1,3 +1,5 @@
import { Utils } from "@bitwarden/common/misc/utils";
import { CipherRepromptType } from "../../enums/cipherRepromptType";
import { CipherType } from "../../enums/cipherType";
import { LinkedIdType } from "../../enums/linkedIdType";
@ -131,4 +133,99 @@ export class CipherView implements View {
linkedFieldI18nKey(id: LinkedIdType): string {
return this.linkedFieldOptions.get(id)?.i18nKey;
}
toJSON(): string {
// localdata is not included because it has 'any' type
const obj = Utils.copyToNewObject(this, {
id: null,
organizationId: null,
folderId: null,
name: null,
notes: null,
type: null,
favorite: null,
organizationUseTotp: null,
edit: null,
viewPassword: null,
collectionIds: null,
reprompt: null,
});
// Dates
// obj.revisionDate = this.revisionDate?.toISOString();
// obj.deletedDate = this.deletedDate?.toISOString();
// Nested objects
obj.attachments = JSON.stringify(this.attachments);
obj.fields = JSON.stringify(this.fields);
// obj.passwordHistory = JSON.stringify(this.passwordHistory);
switch (this.type) {
case CipherType.Card:
obj.card = JSON.stringify(this.card);
break;
case CipherType.Identity:
// obj.identity = JSON.stringify(this.identity);
break;
case CipherType.Login:
// obj.login = JSON.stringify(this.login);
break;
case CipherType.SecureNote:
obj.secureNote = JSON.stringify(this.secureNote);
break;
default:
break;
}
return JSON.stringify(obj);
}
static fromJSON(obj: any): CipherView {
const view = Utils.copyToNewObject(
obj,
{
id: null,
corganizationId: null,
folderId: null,
name: null,
notes: null,
type: null,
favorite: null,
organizationUseTotp: null,
edit: null,
viewPassword: null,
collectionIds: null,
reprompt: null,
},
CipherView
);
// Dates
view.revisionDate = new Date(obj.revisionDate);
view.deletedDate = new Date(obj.deletedDate);
// Nested objects
view.attachments = obj.attachments?.map((a: any) => AttachmentView.fromJSON(a));
view.fields = obj.fields?.map((f: any) => FieldView.fromJSON(f));
view.passwordHistory = obj.passwordHistory?.map((ph: any) => PasswordHistoryView.fromJSON(ph));
switch (view.type) {
case CipherType.Card:
view.card = CardView.fromJSON(obj.card);
break;
case CipherType.Identity:
// view.identity = IdentityView.fromJSON(obj.identity);
break;
case CipherType.Login:
// view.login = LoginView.fromJSON(obj.login);
break;
case CipherType.SecureNote:
view.secureNote = SecureNoteView.fromJSON(obj.secureNote);
break;
default:
break;
}
return view;
}
}

View File

@ -1,9 +1,20 @@
import { Utils } from "@bitwarden/common/misc/utils";
import { FieldType } from "../../enums/fieldType";
import { LinkedIdType } from "../../enums/linkedIdType";
import { Field } from "../domain/field";
import { View } from "./view";
const propertyMap: any = {
name: null,
value: null,
type: null,
newField: null,
showValue: null,
showCount: null,
linkedId: null,
};
export class FieldView implements View {
name: string = null;
value: string = null;
@ -25,4 +36,13 @@ export class FieldView implements View {
get maskedValue(): string {
return this.value != null ? "••••••••" : null;
}
toJSON(): string {
const obj = Utils.copyToNewObject(this, propertyMap);
return JSON.stringify(obj);
}
static fromJSON(obj: any): FieldView {
return Utils.copyToNewObject(obj, propertyMap, FieldView);
}
}

View File

@ -1,7 +1,13 @@
import { Utils } from "@bitwarden/common/misc/utils";
import { Password } from "../domain/password";
import { View } from "./view";
const propertyMap: any = {
password: null,
lastUsedDate: null,
};
export class PasswordHistoryView implements View {
password: string = null;
lastUsedDate: Date = null;
@ -13,4 +19,13 @@ export class PasswordHistoryView implements View {
this.lastUsedDate = ph.lastUsedDate;
}
toJSON(): string {
const obj = Utils.copyToNewObject(this, propertyMap);
return JSON.stringify(obj);
}
fromJSON(obj: any): PasswordHistoryView {
return Utils.copyToNewObject(obj, propertyMap, PasswordHistoryView);
}
}

View File

@ -1,8 +1,13 @@
import { Utils } from "@bitwarden/common/misc/utils";
import { SecureNoteType } from "../../enums/secureNoteType";
import { SecureNote } from "../domain/secureNote";
import { ItemView } from "./itemView";
const propertyMap: any = {
type: null,
};
export class SecureNoteView extends ItemView {
type: SecureNoteType = null;
@ -18,4 +23,13 @@ export class SecureNoteView extends ItemView {
get subTitle(): string {
return null;
}
toJSON(): string {
const obj = Utils.copyToNewObject(this, propertyMap);
return JSON.stringify(obj);
}
static fromJSON(obj: any): SecureNoteView {
return Utils.copyToNewObject(obj, propertyMap, SecureNoteView);
}
}