1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-15 07:43:35 +00:00

[EC-582] Add domain object serialization (#3623)

This commit is contained in:
Thomas Rittson
2022-10-04 06:50:43 +10:00
committed by GitHub
parent cb7b8313a4
commit 162db0b600
25 changed files with 513 additions and 16 deletions

View File

@@ -1,3 +1,5 @@
import { Jsonify } from "type-fest";
import { Utils } from "../../misc/utils";
import { AttachmentData } from "../data/attachmentData";
import { AttachmentView } from "../view/attachmentView";
@@ -90,4 +92,18 @@ export class Attachment extends Domain {
);
return a;
}
static fromJSON(obj: Partial<Jsonify<Attachment>>): Attachment {
if (obj == null) {
return null;
}
const key = EncString.fromJSON(obj.key);
const fileName = EncString.fromJSON(obj.fileName);
return Object.assign(new Attachment(), obj, {
key,
fileName,
});
}
}

View File

@@ -1,3 +1,5 @@
import { Jsonify } from "type-fest";
import { CardData } from "../data/cardData";
import { CardView } from "../view/cardView";
@@ -62,4 +64,25 @@ export class Card extends Domain {
});
return c;
}
static fromJSON(obj: Partial<Jsonify<Card>>): Card {
if (obj == null) {
return null;
}
const cardholderName = EncString.fromJSON(obj.cardholderName);
const brand = EncString.fromJSON(obj.brand);
const number = EncString.fromJSON(obj.number);
const expMonth = EncString.fromJSON(obj.expMonth);
const expYear = EncString.fromJSON(obj.expYear);
const code = EncString.fromJSON(obj.code);
return Object.assign(new Card(), obj, {
cardholderName,
brand,
number,
expMonth,
expYear,
code,
});
}
}

View File

@@ -1,3 +1,5 @@
import { Jsonify } from "type-fest";
import { CipherRepromptType } from "../../enums/cipherRepromptType";
import { CipherType } from "../../enums/cipherType";
import { CipherData } from "../data/cipherData";
@@ -234,4 +236,48 @@ export class Cipher extends Domain {
}
return c;
}
static fromJSON(obj: Jsonify<Cipher>) {
if (obj == null) {
return null;
}
const domain = new Cipher();
const name = EncString.fromJSON(obj.name);
const notes = EncString.fromJSON(obj.notes);
const revisionDate = obj.revisionDate == null ? null : new Date(obj.revisionDate);
const deletedDate = obj.deletedDate == null ? null : new Date(obj.deletedDate);
const attachments = obj.attachments?.map((a: any) => Attachment.fromJSON(a));
const fields = obj.fields?.map((f: any) => Field.fromJSON(f));
const passwordHistory = obj.passwordHistory?.map((ph: any) => Password.fromJSON(ph));
Object.assign(domain, obj, {
name,
notes,
revisionDate,
deletedDate,
attachments,
fields,
passwordHistory,
});
switch (obj.type) {
case CipherType.Card:
domain.card = Card.fromJSON(obj.card);
break;
case CipherType.Identity:
domain.identity = Identity.fromJSON(obj.identity);
break;
case CipherType.Login:
domain.login = Login.fromJSON(obj.login);
break;
case CipherType.SecureNote:
domain.secureNote = SecureNote.fromJSON(obj.secureNote);
break;
default:
break;
}
return domain;
}
}

View File

@@ -44,6 +44,10 @@ export class EncString implements IEncrypted {
}
static fromJSON(obj: Jsonify<EncString>): EncString {
if (obj == null) {
return null;
}
return new EncString(obj);
}

View File

@@ -1,3 +1,5 @@
import { Jsonify } from "type-fest";
import { FieldType } from "../../enums/fieldType";
import { LinkedIdType } from "../../enums/linkedIdType";
import { FieldData } from "../data/fieldData";
@@ -59,4 +61,18 @@ export class Field extends Domain {
);
return f;
}
static fromJSON(obj: Partial<Jsonify<Field>>): Field {
if (obj == null) {
return null;
}
const name = EncString.fromJSON(obj.name);
const value = EncString.fromJSON(obj.value);
return Object.assign(new Field(), obj, {
name,
value,
});
}
}

View File

@@ -1,3 +1,5 @@
import { Jsonify } from "type-fest";
import { IdentityData } from "../data/identityData";
import { IdentityView } from "../view/identityView";
@@ -110,4 +112,50 @@ export class Identity extends Domain {
});
return i;
}
static fromJSON(obj: Jsonify<Identity>): Identity {
if (obj == null) {
return null;
}
const title = EncString.fromJSON(obj.title);
const firstName = EncString.fromJSON(obj.firstName);
const middleName = EncString.fromJSON(obj.middleName);
const lastName = EncString.fromJSON(obj.lastName);
const address1 = EncString.fromJSON(obj.address1);
const address2 = EncString.fromJSON(obj.address2);
const address3 = EncString.fromJSON(obj.address3);
const city = EncString.fromJSON(obj.city);
const state = EncString.fromJSON(obj.state);
const postalCode = EncString.fromJSON(obj.postalCode);
const country = EncString.fromJSON(obj.country);
const company = EncString.fromJSON(obj.company);
const email = EncString.fromJSON(obj.email);
const phone = EncString.fromJSON(obj.phone);
const ssn = EncString.fromJSON(obj.ssn);
const username = EncString.fromJSON(obj.username);
const passportNumber = EncString.fromJSON(obj.passportNumber);
const licenseNumber = EncString.fromJSON(obj.licenseNumber);
return Object.assign(new Identity(), obj, {
title,
firstName,
middleName,
lastName,
address1,
address2,
address3,
city,
state,
postalCode,
country,
company,
email,
phone,
ssn,
username,
passportNumber,
licenseNumber,
});
}
}

View File

@@ -1,3 +1,5 @@
import { Jsonify } from "type-fest";
import { LoginData } from "../data/loginData";
import { LoginView } from "../view/loginView";
@@ -85,4 +87,25 @@ export class Login extends Domain {
return l;
}
static fromJSON(obj: Partial<Jsonify<Login>>): Login {
if (obj == null) {
return null;
}
const username = EncString.fromJSON(obj.username);
const password = EncString.fromJSON(obj.password);
const totp = EncString.fromJSON(obj.totp);
const passwordRevisionDate =
obj.passwordRevisionDate == null ? null : new Date(obj.passwordRevisionDate);
const uris = obj.uris?.map((uri: any) => LoginUri.fromJSON(uri));
return Object.assign(new Login(), obj, {
username,
password,
totp,
passwordRevisionDate: passwordRevisionDate,
uris: uris,
});
}
}

View File

@@ -1,3 +1,5 @@
import { Jsonify } from "type-fest";
import { UriMatchType } from "../../enums/uriMatchType";
import { LoginUriData } from "../data/loginUriData";
import { LoginUriView } from "../view/loginUriView";
@@ -51,4 +53,15 @@ export class LoginUri extends Domain {
);
return u;
}
static fromJSON(obj: Jsonify<LoginUri>): LoginUri {
if (obj == null) {
return null;
}
const uri = EncString.fromJSON(obj.uri);
return Object.assign(new LoginUri(), obj, {
uri,
});
}
}

View File

@@ -1,3 +1,5 @@
import { Jsonify } from "type-fest";
import { PasswordHistoryData } from "../data/passwordHistoryData";
import { PasswordHistoryView } from "../view/passwordHistoryView";
@@ -40,4 +42,18 @@ export class Password extends Domain {
});
return ph;
}
static fromJSON(obj: Partial<Jsonify<Password>>): Password {
if (obj == null) {
return null;
}
const password = EncString.fromJSON(obj.password);
const lastUsedDate = obj.lastUsedDate == null ? null : new Date(obj.lastUsedDate);
return Object.assign(new Password(), obj, {
password,
lastUsedDate,
});
}
}

View File

@@ -1,3 +1,5 @@
import { Jsonify } from "type-fest";
import { SecureNoteType } from "../../enums/secureNoteType";
import { SecureNoteData } from "../data/secureNoteData";
import { SecureNoteView } from "../view/secureNoteView";
@@ -26,4 +28,12 @@ export class SecureNote extends Domain {
n.type = this.type;
return n;
}
static fromJSON(obj: Jsonify<SecureNote>): SecureNote {
if (obj == null) {
return null;
}
return Object.assign(new SecureNote(), obj);
}
}