Finish de/serialization methods initial implementation

This commit is contained in:
Thomas Rittson 2022-06-24 12:36:43 +10:00
parent 8ab017ed55
commit 483673b03f
8 changed files with 115 additions and 83 deletions

View File

@ -5,6 +5,14 @@ import { SymmetricCryptoKey } from "../domain/symmetricCryptoKey";
import { View } from "./view";
const propertyMap: any = {
id: null,
url: null,
size: null,
sizeName: null,
filename: null,
};
export class AttachmentView implements View {
id: string = null;
url: string = null;
@ -36,34 +44,15 @@ export class AttachmentView implements View {
}
toJSON(): string {
const obj = Utils.copyToNewObject(this, {
id: null,
url: null,
size: null,
sizeName: null,
filename: null,
});
const obj = Utils.copyToNewObject(this, propertyMap);
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
);
const view = Utils.copyToNewObject(obj, propertyMap, AttachmentView);
view.key = obj.key == null ? null : SymmetricCryptoKey.fromJSON(obj.key);
return view;
}
}

View File

@ -5,6 +5,14 @@ import { linkedFieldOption } from "../../misc/linkedFieldOption.decorator";
import { ItemView } from "./itemView";
const propertyMap: any = {
cardholderName: null,
brand: null,
number: null,
expMonth: null,
expYear: null,
code: null,
};
export class CardView extends ItemView {
@linkedFieldOption(LinkedId.CardholderName)
cardholderName: string = null;
@ -83,30 +91,11 @@ export class CardView extends ItemView {
}
toJSON(): string {
const obj = Utils.copyToNewObject(this, {
cardholderName: null,
brand: null,
number: null,
expMonth: null,
expYear: null,
code: null,
});
const obj = Utils.copyToNewObject(this, propertyMap);
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
);
return Utils.copyToNewObject(obj, propertyMap, CardView);
}
}

View File

@ -14,6 +14,22 @@ import { PasswordHistoryView } from "./passwordHistoryView";
import { SecureNoteView } from "./secureNoteView";
import { View } from "./view";
const propertyMap: any = {
id: null,
corganizationId: null,
folderId: null,
name: null,
notes: null,
type: null,
favorite: null,
organizationUseTotp: null,
edit: null,
viewPassword: null,
localData: null,
collectionIds: null,
reprompt: null,
};
export class CipherView implements View {
id: string = null;
organizationId: string = null;
@ -135,40 +151,26 @@ export class CipherView implements View {
}
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,
});
const obj = Utils.copyToNewObject(this, propertyMap);
// Dates
// obj.revisionDate = this.revisionDate?.toISOString();
// obj.deletedDate = this.deletedDate?.toISOString();
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);
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);
obj.identity = JSON.stringify(this.identity);
break;
case CipherType.Login:
// obj.login = JSON.stringify(this.login);
obj.login = JSON.stringify(this.login);
break;
case CipherType.SecureNote:
obj.secureNote = JSON.stringify(this.secureNote);
@ -181,24 +183,7 @@ export class CipherView implements View {
}
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
);
const view = Utils.copyToNewObject(obj, propertyMap, CipherView);
// Dates
view.revisionDate = new Date(obj.revisionDate);
@ -214,10 +199,10 @@ export class CipherView implements View {
view.card = CardView.fromJSON(obj.card);
break;
case CipherType.Identity:
// view.identity = IdentityView.fromJSON(obj.identity);
view.identity = IdentityView.fromJSON(obj.identity);
break;
case CipherType.Login:
// view.login = LoginView.fromJSON(obj.login);
view.login = LoginView.fromJSON(obj.login);
break;
case CipherType.SecureNote:
view.secureNote = SecureNoteView.fromJSON(obj.secureNote);

View File

@ -15,6 +15,7 @@ const propertyMap: any = {
showCount: null,
linkedId: null,
};
export class FieldView implements View {
name: string = null;
value: string = null;

View File

@ -4,6 +4,27 @@ import { Utils } from "../../misc/utils";
import { ItemView } from "./itemView";
const propertyMap: any = {
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,
};
export class IdentityView extends ItemView {
@linkedFieldOption(LinkedId.Title)
title: string = null;
@ -139,4 +160,13 @@ export class IdentityView extends ItemView {
addressPart2 += ", " + postalCode;
return addressPart2;
}
toJSON(): string {
const obj = Utils.copyToNewObject(this, propertyMap);
return JSON.stringify(obj);
}
static fromJSON(obj: any): IdentityView {
return Utils.copyToNewObject(obj, propertyMap, IdentityView);
}
}

View File

@ -1,3 +1,5 @@
import { stringify } from "querystring";
import { UriMatchType } from "../../enums/uriMatchType";
import { Utils } from "../../misc/utils";
import { LoginUri } from "../domain/loginUri";
@ -20,6 +22,11 @@ const CanLaunchWhitelist = [
"androidapp://",
];
const propertyMap: any = {
match: null,
uri: null,
};
export class LoginUriView implements View {
match: UriMatchType = null;
@ -124,4 +131,13 @@ export class LoginUriView implements View {
? "http://" + this.uri
: this.uri;
}
toJSON(): string {
const obj = Utils.copyToNewObject(this, propertyMap);
return JSON.stringify(obj);
}
static fromJSON(obj: any) {
return Utils.copyToNewObject(obj, propertyMap, LoginUriView);
}
}

View File

@ -6,6 +6,13 @@ import { Login } from "../domain/login";
import { ItemView } from "./itemView";
import { LoginUriView } from "./loginUriView";
const propertyMap: any = {
username: null,
password: null,
totp: null,
autofillOnPageLoad: null,
};
export class LoginView extends ItemView {
@linkedFieldOption(LinkedId.Username)
username: string = null;
@ -60,4 +67,19 @@ export class LoginView extends ItemView {
get hasUris(): boolean {
return this.uris != null && this.uris.length > 0;
}
toJSON(): string {
const obj = Utils.copyToNewObject(this, propertyMap);
obj.passwordRevisionDate = this.passwordRevisionDate?.toISOString();
obj.uris = this.uris == null ? null : JSON.stringify(this.uris);
return JSON.stringify(obj);
}
static fromJSON(obj: any): LoginView {
const view = Utils.copyToNewObject(obj, propertyMap, LoginView);
view.passwordRevisionDate =
obj.passwordRevisionDate == null ? null : new Date(obj.passwordRevisionDate);
view.uris = obj.uris?.map((uri: any) => LoginUriView.fromJSON(uri));
return view;
}
}

View File

@ -25,7 +25,7 @@ export class PasswordHistoryView implements View {
return JSON.stringify(obj);
}
fromJSON(obj: any): PasswordHistoryView {
static fromJSON(obj: any): PasswordHistoryView {
return Utils.copyToNewObject(obj, propertyMap, PasswordHistoryView);
}
}