Skip to content

Commit

Permalink
Create and use safeGetString() instead of instanceof checks to de…
Browse files Browse the repository at this point in the history
…termine type (#8906)

`safeGetString` takes a `string` or `EncString` and return the appropiate value based on it's type

Co-authored-by: Daniel James Smith <[email protected]>
  • Loading branch information
djsmith85 and djsmith85 authored Apr 24, 2024
1 parent a6755f5 commit dba910d
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 137 deletions.
23 changes: 8 additions & 15 deletions libs/common/src/models/export/card.export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { EncString } from "../../platform/models/domain/enc-string";
import { Card as CardDomain } from "../../vault/models/domain/card";
import { CardView } from "../../vault/models/view/card.view";

import { safeGetString } from "./utils";

export class CardExport {
static template(): CardExport {
const req = new CardExport();
Expand Down Expand Up @@ -46,20 +48,11 @@ export class CardExport {
return;
}

if (o instanceof CardView) {
this.cardholderName = o.cardholderName;
this.brand = o.brand;
this.number = o.number;
this.expMonth = o.expMonth;
this.expYear = o.expYear;
this.code = o.code;
} else {
this.cardholderName = o.cardholderName?.encryptedString;
this.brand = o.brand?.encryptedString;
this.number = o.number?.encryptedString;
this.expMonth = o.expMonth?.encryptedString;
this.expYear = o.expYear?.encryptedString;
this.code = o.code?.encryptedString;
}
this.cardholderName = safeGetString(o.cardholderName);
this.brand = safeGetString(o.brand);
this.number = safeGetString(o.number);
this.expMonth = safeGetString(o.expMonth);
this.expYear = safeGetString(o.expYear);
this.code = safeGetString(o.code);
}
}
22 changes: 6 additions & 16 deletions libs/common/src/models/export/cipher.export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { IdentityExport } from "./identity.export";
import { LoginExport } from "./login.export";
import { PasswordHistoryExport } from "./password-history.export";
import { SecureNoteExport } from "./secure-note.export";
import { safeGetString } from "./utils";

export class CipherExport {
static template(): CipherExport {
Expand Down Expand Up @@ -145,23 +146,16 @@ export class CipherExport {
this.type = o.type;
this.reprompt = o.reprompt;

if (o instanceof CipherView) {
this.name = o.name;
this.notes = o.notes;
} else {
this.name = o.name?.encryptedString;
this.notes = o.notes?.encryptedString;
this.name = safeGetString(o.name);
this.notes = safeGetString(o.notes);
if ("key" in o) {
this.key = o.key?.encryptedString;
}

this.favorite = o.favorite;

if (o.fields != null) {
if (o instanceof CipherView) {
this.fields = o.fields.map((f) => new FieldExport(f));
} else {
this.fields = o.fields.map((f) => new FieldExport(f));
}
this.fields = o.fields.map((f) => new FieldExport(f));
}

switch (o.type) {
Expand All @@ -180,11 +174,7 @@ export class CipherExport {
}

if (o.passwordHistory != null) {
if (o instanceof CipherView) {
this.passwordHistory = o.passwordHistory.map((ph) => new PasswordHistoryExport(ph));
} else {
this.passwordHistory = o.passwordHistory.map((ph) => new PasswordHistoryExport(ph));
}
this.passwordHistory = o.passwordHistory.map((ph) => new PasswordHistoryExport(ph));
}

this.creationDate = o.creationDate;
Expand Down
8 changes: 3 additions & 5 deletions libs/common/src/models/export/collection.export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { EncString } from "../../platform/models/domain/enc-string";
import { Collection as CollectionDomain } from "../../vault/models/domain/collection";
import { CollectionView } from "../../vault/models/view/collection.view";

import { safeGetString } from "./utils";

export class CollectionExport {
static template(): CollectionExport {
const req = new CollectionExport();
Expand Down Expand Up @@ -36,11 +38,7 @@ export class CollectionExport {
// Use build method instead of ctor so that we can control order of JSON stringify for pretty print
build(o: CollectionView | CollectionDomain) {
this.organizationId = o.organizationId;
if (o instanceof CollectionView) {
this.name = o.name;
} else {
this.name = o.name?.encryptedString;
}
this.name = safeGetString(o.name);
this.externalId = o.externalId;
}
}
41 changes: 14 additions & 27 deletions libs/common/src/models/export/fido2-credential.export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { EncString } from "../../platform/models/domain/enc-string";
import { Fido2Credential } from "../../vault/models/domain/fido2-credential";
import { Fido2CredentialView } from "../../vault/models/view/fido2-credential.view";

import { safeGetString } from "./utils";

/**
* Represents format of Fido2 Credentials in JSON exports.
*/
Expand Down Expand Up @@ -99,33 +101,18 @@ export class Fido2CredentialExport {
return;
}

if (o instanceof Fido2CredentialView) {
this.credentialId = o.credentialId;
this.keyType = o.keyType;
this.keyAlgorithm = o.keyAlgorithm;
this.keyCurve = o.keyCurve;
this.keyValue = o.keyValue;
this.rpId = o.rpId;
this.userHandle = o.userHandle;
this.userName = o.userName;
this.counter = String(o.counter);
this.rpName = o.rpName;
this.userDisplayName = o.userDisplayName;
this.discoverable = String(o.discoverable);
} else {
this.credentialId = o.credentialId?.encryptedString;
this.keyType = o.keyType?.encryptedString;
this.keyAlgorithm = o.keyAlgorithm?.encryptedString;
this.keyCurve = o.keyCurve?.encryptedString;
this.keyValue = o.keyValue?.encryptedString;
this.rpId = o.rpId?.encryptedString;
this.userHandle = o.userHandle?.encryptedString;
this.userName = o.userName?.encryptedString;
this.counter = o.counter?.encryptedString;
this.rpName = o.rpName?.encryptedString;
this.userDisplayName = o.userDisplayName?.encryptedString;
this.discoverable = o.discoverable?.encryptedString;
}
this.credentialId = safeGetString(o.credentialId);
this.keyType = safeGetString(o.keyType);
this.keyAlgorithm = safeGetString(o.keyAlgorithm);
this.keyCurve = safeGetString(o.keyCurve);
this.keyValue = safeGetString(o.keyValue);
this.rpId = safeGetString(o.rpId);
this.userHandle = safeGetString(o.userHandle);
this.userName = safeGetString(o.userName);
this.counter = safeGetString(String(o.counter));
this.rpName = safeGetString(o.rpName);
this.userDisplayName = safeGetString(o.userDisplayName);
this.discoverable = safeGetString(String(o.discoverable));
this.creationDate = o.creationDate;
}
}
11 changes: 4 additions & 7 deletions libs/common/src/models/export/field.export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { FieldType, LinkedIdType } from "../../vault/enums";
import { Field as FieldDomain } from "../../vault/models/domain/field";
import { FieldView } from "../../vault/models/view/field.view";

import { safeGetString } from "./utils";

export class FieldExport {
static template(): FieldExport {
const req = new FieldExport();
Expand Down Expand Up @@ -38,13 +40,8 @@ export class FieldExport {
return;
}

if (o instanceof FieldView) {
this.name = o.name;
this.value = o.value;
} else {
this.name = o.name?.encryptedString;
this.value = o.value?.encryptedString;
}
this.name = safeGetString(o.name);
this.value = safeGetString(o.value);
this.type = o.type;
this.linkedId = o.linkedId;
}
Expand Down
8 changes: 3 additions & 5 deletions libs/common/src/models/export/folder.export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { EncString } from "../../platform/models/domain/enc-string";
import { Folder as FolderDomain } from "../../vault/models/domain/folder";
import { FolderView } from "../../vault/models/view/folder.view";

import { safeGetString } from "./utils";

export class FolderExport {
static template(): FolderExport {
const req = new FolderExport();
Expand All @@ -23,10 +25,6 @@ export class FolderExport {

// Use build method instead of ctor so that we can control order of JSON stringify for pretty print
build(o: FolderView | FolderDomain) {
if (o instanceof FolderView) {
this.name = o.name;
} else {
this.name = o.name?.encryptedString;
}
this.name = safeGetString(o.name);
}
}
59 changes: 20 additions & 39 deletions libs/common/src/models/export/identity.export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { EncString } from "../../platform/models/domain/enc-string";
import { Identity as IdentityDomain } from "../../vault/models/domain/identity";
import { IdentityView } from "../../vault/models/view/identity.view";

import { safeGetString } from "./utils";

export class IdentityExport {
static template(): IdentityExport {
const req = new IdentityExport();
Expand Down Expand Up @@ -94,44 +96,23 @@ export class IdentityExport {
return;
}

if (o instanceof IdentityView) {
this.title = o.title;
this.firstName = o.firstName;
this.middleName = o.middleName;
this.lastName = o.lastName;
this.address1 = o.address1;
this.address2 = o.address2;
this.address3 = o.address3;
this.city = o.city;
this.state = o.state;
this.postalCode = o.postalCode;
this.country = o.country;
this.company = o.company;
this.email = o.email;
this.phone = o.phone;
this.ssn = o.ssn;
this.username = o.username;
this.passportNumber = o.passportNumber;
this.licenseNumber = o.licenseNumber;
} else {
this.title = o.title?.encryptedString;
this.firstName = o.firstName?.encryptedString;
this.middleName = o.middleName?.encryptedString;
this.lastName = o.lastName?.encryptedString;
this.address1 = o.address1?.encryptedString;
this.address2 = o.address2?.encryptedString;
this.address3 = o.address3?.encryptedString;
this.city = o.city?.encryptedString;
this.state = o.state?.encryptedString;
this.postalCode = o.postalCode?.encryptedString;
this.country = o.country?.encryptedString;
this.company = o.company?.encryptedString;
this.email = o.email?.encryptedString;
this.phone = o.phone?.encryptedString;
this.ssn = o.ssn?.encryptedString;
this.username = o.username?.encryptedString;
this.passportNumber = o.passportNumber?.encryptedString;
this.licenseNumber = o.licenseNumber?.encryptedString;
}
this.title = safeGetString(o.title);
this.firstName = safeGetString(o.firstName);
this.middleName = safeGetString(o.middleName);
this.lastName = safeGetString(o.lastName);
this.address1 = safeGetString(o.address1);
this.address2 = safeGetString(o.address2);
this.address3 = safeGetString(o.address3);
this.city = safeGetString(o.city);
this.state = safeGetString(o.state);
this.postalCode = safeGetString(o.postalCode);
this.country = safeGetString(o.country);
this.company = safeGetString(o.company);
this.email = safeGetString(o.email);
this.phone = safeGetString(o.phone);
this.ssn = safeGetString(o.ssn);
this.username = safeGetString(o.username);
this.passportNumber = safeGetString(o.passportNumber);
this.licenseNumber = safeGetString(o.licenseNumber);
}
}
8 changes: 4 additions & 4 deletions libs/common/src/models/export/login-uri.export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { EncString } from "../../platform/models/domain/enc-string";
import { LoginUri as LoginUriDomain } from "../../vault/models/domain/login-uri";
import { LoginUriView } from "../../vault/models/view/login-uri.view";

import { safeGetString } from "./utils";

export class LoginUriExport {
static template(): LoginUriExport {
const req = new LoginUriExport();
Expand Down Expand Up @@ -33,10 +35,8 @@ export class LoginUriExport {
return;
}

if (o instanceof LoginUriView) {
this.uri = o.uri;
} else {
this.uri = o.uri?.encryptedString;
this.uri = safeGetString(o.uri);
if ("uriChecksum" in o) {
this.uriChecksum = o.uriChecksum?.encryptedString;
}
this.match = o.match;
Expand Down
19 changes: 5 additions & 14 deletions libs/common/src/models/export/login.export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { LoginView } from "../../vault/models/view/login.view";

import { Fido2CredentialExport } from "./fido2-credential.export";
import { LoginUriExport } from "./login-uri.export";
import { safeGetString } from "./utils";

export class LoginExport {
static template(): LoginExport {
Expand Down Expand Up @@ -53,25 +54,15 @@ export class LoginExport {
}

if (o.uris != null) {
if (o instanceof LoginView) {
this.uris = o.uris.map((u) => new LoginUriExport(u));
} else {
this.uris = o.uris.map((u) => new LoginUriExport(u));
}
this.uris = o.uris.map((u) => new LoginUriExport(u));
}

if (o.fido2Credentials != null) {
this.fido2Credentials = o.fido2Credentials.map((key) => new Fido2CredentialExport(key));
}

if (o instanceof LoginView) {
this.username = o.username;
this.password = o.password;
this.totp = o.totp;
} else {
this.username = o.username?.encryptedString;
this.password = o.password?.encryptedString;
this.totp = o.totp?.encryptedString;
}
this.username = safeGetString(o.username);
this.password = safeGetString(o.password);
this.totp = safeGetString(o.totp);
}
}
8 changes: 3 additions & 5 deletions libs/common/src/models/export/password-history.export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { EncString } from "../../platform/models/domain/enc-string";
import { Password } from "../../vault/models/domain/password";
import { PasswordHistoryView } from "../../vault/models/view/password-history.view";

import { safeGetString } from "./utils";

export class PasswordHistoryExport {
static template(): PasswordHistoryExport {
const req = new PasswordHistoryExport();
Expand Down Expand Up @@ -30,11 +32,7 @@ export class PasswordHistoryExport {
return;
}

if (o instanceof PasswordHistoryView) {
this.password = o.password;
} else {
this.password = o.password?.encryptedString;
}
this.password = safeGetString(o.password);
this.lastUsedDate = o.lastUsedDate;
}
}
12 changes: 12 additions & 0 deletions libs/common/src/models/export/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { EncString } from "@bitwarden/common/platform/models/domain/enc-string";

export function safeGetString(value: string | EncString) {
if (value == null) {
return null;
}

if (typeof value == "string") {
return value;
}
return value?.encryptedString;
}

0 comments on commit dba910d

Please sign in to comment.