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

Create and use safeGetString() instead of instanceof checks to determine type (#8906)

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

Co-authored-by: Daniel James Smith <djsmith85@users.noreply.github.com>
This commit is contained in:
Daniel James Smith
2024-04-24 23:41:35 +02:00
committed by GitHub
parent a6755f5f20
commit dba910d0b9
11 changed files with 82 additions and 137 deletions

View File

@@ -2,6 +2,8 @@ import { EncString } from "../../platform/models/domain/enc-string";
import { Card as CardDomain } from "../../vault/models/domain/card"; import { Card as CardDomain } from "../../vault/models/domain/card";
import { CardView } from "../../vault/models/view/card.view"; import { CardView } from "../../vault/models/view/card.view";
import { safeGetString } from "./utils";
export class CardExport { export class CardExport {
static template(): CardExport { static template(): CardExport {
const req = new CardExport(); const req = new CardExport();
@@ -46,20 +48,11 @@ export class CardExport {
return; return;
} }
if (o instanceof CardView) { this.cardholderName = safeGetString(o.cardholderName);
this.cardholderName = o.cardholderName; this.brand = safeGetString(o.brand);
this.brand = o.brand; this.number = safeGetString(o.number);
this.number = o.number; this.expMonth = safeGetString(o.expMonth);
this.expMonth = o.expMonth; this.expYear = safeGetString(o.expYear);
this.expYear = o.expYear; this.code = safeGetString(o.code);
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;
}
} }
} }

View File

@@ -10,6 +10,7 @@ import { IdentityExport } from "./identity.export";
import { LoginExport } from "./login.export"; import { LoginExport } from "./login.export";
import { PasswordHistoryExport } from "./password-history.export"; import { PasswordHistoryExport } from "./password-history.export";
import { SecureNoteExport } from "./secure-note.export"; import { SecureNoteExport } from "./secure-note.export";
import { safeGetString } from "./utils";
export class CipherExport { export class CipherExport {
static template(): CipherExport { static template(): CipherExport {
@@ -145,23 +146,16 @@ export class CipherExport {
this.type = o.type; this.type = o.type;
this.reprompt = o.reprompt; this.reprompt = o.reprompt;
if (o instanceof CipherView) { this.name = safeGetString(o.name);
this.name = o.name; this.notes = safeGetString(o.notes);
this.notes = o.notes; if ("key" in o) {
} else {
this.name = o.name?.encryptedString;
this.notes = o.notes?.encryptedString;
this.key = o.key?.encryptedString; this.key = o.key?.encryptedString;
} }
this.favorite = o.favorite; this.favorite = o.favorite;
if (o.fields != null) { if (o.fields != null) {
if (o instanceof CipherView) {
this.fields = o.fields.map((f) => new FieldExport(f)); this.fields = o.fields.map((f) => new FieldExport(f));
} else {
this.fields = o.fields.map((f) => new FieldExport(f));
}
} }
switch (o.type) { switch (o.type) {
@@ -180,11 +174,7 @@ export class CipherExport {
} }
if (o.passwordHistory != null) { if (o.passwordHistory != null) {
if (o instanceof CipherView) {
this.passwordHistory = o.passwordHistory.map((ph) => new PasswordHistoryExport(ph)); this.passwordHistory = o.passwordHistory.map((ph) => new PasswordHistoryExport(ph));
} else {
this.passwordHistory = o.passwordHistory.map((ph) => new PasswordHistoryExport(ph));
}
} }
this.creationDate = o.creationDate; this.creationDate = o.creationDate;

View File

@@ -2,6 +2,8 @@ import { EncString } from "../../platform/models/domain/enc-string";
import { Collection as CollectionDomain } from "../../vault/models/domain/collection"; import { Collection as CollectionDomain } from "../../vault/models/domain/collection";
import { CollectionView } from "../../vault/models/view/collection.view"; import { CollectionView } from "../../vault/models/view/collection.view";
import { safeGetString } from "./utils";
export class CollectionExport { export class CollectionExport {
static template(): CollectionExport { static template(): CollectionExport {
const req = new CollectionExport(); const req = new CollectionExport();
@@ -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 // Use build method instead of ctor so that we can control order of JSON stringify for pretty print
build(o: CollectionView | CollectionDomain) { build(o: CollectionView | CollectionDomain) {
this.organizationId = o.organizationId; this.organizationId = o.organizationId;
if (o instanceof CollectionView) { this.name = safeGetString(o.name);
this.name = o.name;
} else {
this.name = o.name?.encryptedString;
}
this.externalId = o.externalId; this.externalId = o.externalId;
} }
} }

View File

@@ -2,6 +2,8 @@ import { EncString } from "../../platform/models/domain/enc-string";
import { Fido2Credential } from "../../vault/models/domain/fido2-credential"; import { Fido2Credential } from "../../vault/models/domain/fido2-credential";
import { Fido2CredentialView } from "../../vault/models/view/fido2-credential.view"; import { Fido2CredentialView } from "../../vault/models/view/fido2-credential.view";
import { safeGetString } from "./utils";
/** /**
* Represents format of Fido2 Credentials in JSON exports. * Represents format of Fido2 Credentials in JSON exports.
*/ */
@@ -99,33 +101,18 @@ export class Fido2CredentialExport {
return; return;
} }
if (o instanceof Fido2CredentialView) { this.credentialId = safeGetString(o.credentialId);
this.credentialId = o.credentialId; this.keyType = safeGetString(o.keyType);
this.keyType = o.keyType; this.keyAlgorithm = safeGetString(o.keyAlgorithm);
this.keyAlgorithm = o.keyAlgorithm; this.keyCurve = safeGetString(o.keyCurve);
this.keyCurve = o.keyCurve; this.keyValue = safeGetString(o.keyValue);
this.keyValue = o.keyValue; this.rpId = safeGetString(o.rpId);
this.rpId = o.rpId; this.userHandle = safeGetString(o.userHandle);
this.userHandle = o.userHandle; this.userName = safeGetString(o.userName);
this.userName = o.userName; this.counter = safeGetString(String(o.counter));
this.counter = String(o.counter); this.rpName = safeGetString(o.rpName);
this.rpName = o.rpName; this.userDisplayName = safeGetString(o.userDisplayName);
this.userDisplayName = o.userDisplayName; this.discoverable = safeGetString(String(o.discoverable));
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.creationDate = o.creationDate; this.creationDate = o.creationDate;
} }
} }

View File

@@ -3,6 +3,8 @@ import { FieldType, LinkedIdType } from "../../vault/enums";
import { Field as FieldDomain } from "../../vault/models/domain/field"; import { Field as FieldDomain } from "../../vault/models/domain/field";
import { FieldView } from "../../vault/models/view/field.view"; import { FieldView } from "../../vault/models/view/field.view";
import { safeGetString } from "./utils";
export class FieldExport { export class FieldExport {
static template(): FieldExport { static template(): FieldExport {
const req = new FieldExport(); const req = new FieldExport();
@@ -38,13 +40,8 @@ export class FieldExport {
return; return;
} }
if (o instanceof FieldView) { this.name = safeGetString(o.name);
this.name = o.name; this.value = safeGetString(o.value);
this.value = o.value;
} else {
this.name = o.name?.encryptedString;
this.value = o.value?.encryptedString;
}
this.type = o.type; this.type = o.type;
this.linkedId = o.linkedId; this.linkedId = o.linkedId;
} }

View File

@@ -2,6 +2,8 @@ import { EncString } from "../../platform/models/domain/enc-string";
import { Folder as FolderDomain } from "../../vault/models/domain/folder"; import { Folder as FolderDomain } from "../../vault/models/domain/folder";
import { FolderView } from "../../vault/models/view/folder.view"; import { FolderView } from "../../vault/models/view/folder.view";
import { safeGetString } from "./utils";
export class FolderExport { export class FolderExport {
static template(): FolderExport { static template(): FolderExport {
const req = new FolderExport(); const req = new FolderExport();
@@ -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 // Use build method instead of ctor so that we can control order of JSON stringify for pretty print
build(o: FolderView | FolderDomain) { build(o: FolderView | FolderDomain) {
if (o instanceof FolderView) { this.name = safeGetString(o.name);
this.name = o.name;
} else {
this.name = o.name?.encryptedString;
}
} }
} }

View File

@@ -2,6 +2,8 @@ import { EncString } from "../../platform/models/domain/enc-string";
import { Identity as IdentityDomain } from "../../vault/models/domain/identity"; import { Identity as IdentityDomain } from "../../vault/models/domain/identity";
import { IdentityView } from "../../vault/models/view/identity.view"; import { IdentityView } from "../../vault/models/view/identity.view";
import { safeGetString } from "./utils";
export class IdentityExport { export class IdentityExport {
static template(): IdentityExport { static template(): IdentityExport {
const req = new IdentityExport(); const req = new IdentityExport();
@@ -94,44 +96,23 @@ export class IdentityExport {
return; return;
} }
if (o instanceof IdentityView) { this.title = safeGetString(o.title);
this.title = o.title; this.firstName = safeGetString(o.firstName);
this.firstName = o.firstName; this.middleName = safeGetString(o.middleName);
this.middleName = o.middleName; this.lastName = safeGetString(o.lastName);
this.lastName = o.lastName; this.address1 = safeGetString(o.address1);
this.address1 = o.address1; this.address2 = safeGetString(o.address2);
this.address2 = o.address2; this.address3 = safeGetString(o.address3);
this.address3 = o.address3; this.city = safeGetString(o.city);
this.city = o.city; this.state = safeGetString(o.state);
this.state = o.state; this.postalCode = safeGetString(o.postalCode);
this.postalCode = o.postalCode; this.country = safeGetString(o.country);
this.country = o.country; this.company = safeGetString(o.company);
this.company = o.company; this.email = safeGetString(o.email);
this.email = o.email; this.phone = safeGetString(o.phone);
this.phone = o.phone; this.ssn = safeGetString(o.ssn);
this.ssn = o.ssn; this.username = safeGetString(o.username);
this.username = o.username; this.passportNumber = safeGetString(o.passportNumber);
this.passportNumber = o.passportNumber; this.licenseNumber = safeGetString(o.licenseNumber);
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;
}
} }
} }

View File

@@ -3,6 +3,8 @@ import { EncString } from "../../platform/models/domain/enc-string";
import { LoginUri as LoginUriDomain } from "../../vault/models/domain/login-uri"; import { LoginUri as LoginUriDomain } from "../../vault/models/domain/login-uri";
import { LoginUriView } from "../../vault/models/view/login-uri.view"; import { LoginUriView } from "../../vault/models/view/login-uri.view";
import { safeGetString } from "./utils";
export class LoginUriExport { export class LoginUriExport {
static template(): LoginUriExport { static template(): LoginUriExport {
const req = new LoginUriExport(); const req = new LoginUriExport();
@@ -33,10 +35,8 @@ export class LoginUriExport {
return; return;
} }
if (o instanceof LoginUriView) { this.uri = safeGetString(o.uri);
this.uri = o.uri; if ("uriChecksum" in o) {
} else {
this.uri = o.uri?.encryptedString;
this.uriChecksum = o.uriChecksum?.encryptedString; this.uriChecksum = o.uriChecksum?.encryptedString;
} }
this.match = o.match; this.match = o.match;

View File

@@ -4,6 +4,7 @@ import { LoginView } from "../../vault/models/view/login.view";
import { Fido2CredentialExport } from "./fido2-credential.export"; import { Fido2CredentialExport } from "./fido2-credential.export";
import { LoginUriExport } from "./login-uri.export"; import { LoginUriExport } from "./login-uri.export";
import { safeGetString } from "./utils";
export class LoginExport { export class LoginExport {
static template(): LoginExport { static template(): LoginExport {
@@ -53,25 +54,15 @@ export class LoginExport {
} }
if (o.uris != null) { if (o.uris != null) {
if (o instanceof LoginView) {
this.uris = o.uris.map((u) => new LoginUriExport(u)); this.uris = o.uris.map((u) => new LoginUriExport(u));
} else {
this.uris = o.uris.map((u) => new LoginUriExport(u));
}
} }
if (o.fido2Credentials != null) { if (o.fido2Credentials != null) {
this.fido2Credentials = o.fido2Credentials.map((key) => new Fido2CredentialExport(key)); this.fido2Credentials = o.fido2Credentials.map((key) => new Fido2CredentialExport(key));
} }
if (o instanceof LoginView) { this.username = safeGetString(o.username);
this.username = o.username; this.password = safeGetString(o.password);
this.password = o.password; this.totp = safeGetString(o.totp);
this.totp = o.totp;
} else {
this.username = o.username?.encryptedString;
this.password = o.password?.encryptedString;
this.totp = o.totp?.encryptedString;
}
} }
} }

View File

@@ -2,6 +2,8 @@ import { EncString } from "../../platform/models/domain/enc-string";
import { Password } from "../../vault/models/domain/password"; import { Password } from "../../vault/models/domain/password";
import { PasswordHistoryView } from "../../vault/models/view/password-history.view"; import { PasswordHistoryView } from "../../vault/models/view/password-history.view";
import { safeGetString } from "./utils";
export class PasswordHistoryExport { export class PasswordHistoryExport {
static template(): PasswordHistoryExport { static template(): PasswordHistoryExport {
const req = new PasswordHistoryExport(); const req = new PasswordHistoryExport();
@@ -30,11 +32,7 @@ export class PasswordHistoryExport {
return; return;
} }
if (o instanceof PasswordHistoryView) { this.password = safeGetString(o.password);
this.password = o.password;
} else {
this.password = o.password?.encryptedString;
}
this.lastUsedDate = o.lastUsedDate; this.lastUsedDate = o.lastUsedDate;
} }
} }

View File

@@ -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;
}