mirror of
https://github.com/bitwarden/browser
synced 2025-12-22 11:13:46 +00:00
[PM-12423] Migrate Cipher Decryption to Use SDK (#14206)
* Created mappings for client domain object to SDK * Add abstract decrypt observable * Added todo for future consideration * Added implementation to cipher service * Added adapter and unit tests * Created cipher encryption abstraction and service * Register cipher encryption service * Added tests for the cipher encryption service * changed signature * Updated feature flag name * added new function to be used for decrypting ciphers * Added new encryptedKey field * added new function to be used for decrypting ciphers * Manually set fields * Added encrypted key in attachment view * Fixed test * Updated references to use decrypt with feature flag * Added dependency * updated package.json * lint fix * fixed tests * Fixed small mapping issues * Fixed test * Added function to decrypt fido2 key value * Added function to decrypt fido2 key value and updated test * updated to use sdk function without prociding the key * updated localdata sdk type change * decrypt attachment content using sdk * Fixed dependency issues * updated package.json * Refactored service to handle getting decrypted buffer using the legacy and sdk implementations * updated services and component to use refactored version * Updated decryptCiphersWithSdk to use decryptManyLegacy for batch decryption, ensuring the SDK is only called once per batch * Fixed merge conflicts * Fixed merge conflicts * Fixed merge conflicts * Fixed lint issues * Moved getDecryptedAttachmentBuffer to cipher service * Moved getDecryptedAttachmentBuffer to cipher service * ensure CipherView properties are null instead of undefined * Fixed test * ensure AttachmentView properties are null instead of undefined * Linked ticket in comment * removed unused orgKey
This commit is contained in:
@@ -1,4 +1,7 @@
|
||||
import { AttachmentView as SdkAttachmentView } from "@bitwarden/sdk-internal";
|
||||
|
||||
import { mockFromJson } from "../../../../spec";
|
||||
import { EncString } from "../../../platform/models/domain/enc-string";
|
||||
import { SymmetricCryptoKey } from "../../../platform/models/domain/symmetric-crypto-key";
|
||||
|
||||
import { AttachmentView } from "./attachment.view";
|
||||
@@ -15,4 +18,56 @@ describe("AttachmentView", () => {
|
||||
|
||||
expect(actual.key).toEqual("encKeyB64_fromJSON");
|
||||
});
|
||||
|
||||
describe("fromSdkAttachmentView", () => {
|
||||
it("should return undefined when the input is null", () => {
|
||||
const result = AttachmentView.fromSdkAttachmentView(null as unknown as any);
|
||||
expect(result).toBeUndefined();
|
||||
});
|
||||
|
||||
it("should return an AttachmentView from an SdkAttachmentView", () => {
|
||||
const sdkAttachmentView = {
|
||||
id: "id",
|
||||
url: "url",
|
||||
size: "size",
|
||||
sizeName: "sizeName",
|
||||
fileName: "fileName",
|
||||
key: "encKeyB64_fromString",
|
||||
} as SdkAttachmentView;
|
||||
|
||||
const result = AttachmentView.fromSdkAttachmentView(sdkAttachmentView);
|
||||
|
||||
expect(result).toMatchObject({
|
||||
id: "id",
|
||||
url: "url",
|
||||
size: "size",
|
||||
sizeName: "sizeName",
|
||||
fileName: "fileName",
|
||||
key: null,
|
||||
encryptedKey: new EncString(sdkAttachmentView.key as string),
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("toSdkAttachmentView", () => {
|
||||
it("should convert AttachmentView to SdkAttachmentView", () => {
|
||||
const attachmentView = new AttachmentView();
|
||||
attachmentView.id = "id";
|
||||
attachmentView.url = "url";
|
||||
attachmentView.size = "size";
|
||||
attachmentView.sizeName = "sizeName";
|
||||
attachmentView.fileName = "fileName";
|
||||
attachmentView.encryptedKey = new EncString("encKeyB64");
|
||||
|
||||
const result = attachmentView.toSdkAttachmentView();
|
||||
expect(result).toEqual({
|
||||
id: "id",
|
||||
url: "url",
|
||||
size: "size",
|
||||
sizeName: "sizeName",
|
||||
fileName: "fileName",
|
||||
key: "encKeyB64",
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -2,7 +2,10 @@
|
||||
// @ts-strict-ignore
|
||||
import { Jsonify } from "type-fest";
|
||||
|
||||
import { AttachmentView as SdkAttachmentView } from "@bitwarden/sdk-internal";
|
||||
|
||||
import { View } from "../../../models/view/view";
|
||||
import { EncString } from "../../../platform/models/domain/enc-string";
|
||||
import { SymmetricCryptoKey } from "../../../platform/models/domain/symmetric-crypto-key";
|
||||
import { Attachment } from "../domain/attachment";
|
||||
|
||||
@@ -13,6 +16,10 @@ export class AttachmentView implements View {
|
||||
sizeName: string = null;
|
||||
fileName: string = null;
|
||||
key: SymmetricCryptoKey = null;
|
||||
/**
|
||||
* The SDK returns an encrypted key for the attachment.
|
||||
*/
|
||||
encryptedKey: EncString | undefined;
|
||||
|
||||
constructor(a?: Attachment) {
|
||||
if (!a) {
|
||||
@@ -40,4 +47,37 @@ export class AttachmentView implements View {
|
||||
const key = obj.key == null ? null : SymmetricCryptoKey.fromJSON(obj.key);
|
||||
return Object.assign(new AttachmentView(), obj, { key: key });
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the AttachmentView to a SDK AttachmentView.
|
||||
*/
|
||||
toSdkAttachmentView(): SdkAttachmentView {
|
||||
return {
|
||||
id: this.id,
|
||||
url: this.url,
|
||||
size: this.size,
|
||||
sizeName: this.sizeName,
|
||||
fileName: this.fileName,
|
||||
key: this.encryptedKey?.toJSON(),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the SDK AttachmentView to a AttachmentView.
|
||||
*/
|
||||
static fromSdkAttachmentView(obj: SdkAttachmentView): AttachmentView | undefined {
|
||||
if (!obj) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const view = new AttachmentView();
|
||||
view.id = obj.id ?? null;
|
||||
view.url = obj.url ?? null;
|
||||
view.size = obj.size ?? null;
|
||||
view.sizeName = obj.sizeName ?? null;
|
||||
view.fileName = obj.fileName ?? null;
|
||||
view.encryptedKey = new EncString(obj.key);
|
||||
|
||||
return view;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
// @ts-strict-ignore
|
||||
import { Jsonify } from "type-fest";
|
||||
|
||||
import { CardView as SdkCardView } from "@bitwarden/sdk-internal";
|
||||
|
||||
import { normalizeExpiryYearFormat } from "../../../autofill/utils";
|
||||
import { CardLinkedId as LinkedId } from "../../enums";
|
||||
import { linkedFieldOption } from "../../linked-field-option.decorator";
|
||||
@@ -146,4 +148,15 @@ export class CardView extends ItemView {
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts an SDK CardView to a CardView.
|
||||
*/
|
||||
static fromSdkCardView(obj: SdkCardView): CardView | undefined {
|
||||
if (obj == null) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return Object.assign(new CardView(), obj);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,16 @@
|
||||
import { mockFromJson } from "../../../../spec";
|
||||
import {
|
||||
CipherView as SdkCipherView,
|
||||
CipherType as SdkCipherType,
|
||||
CipherRepromptType as SdkCipherRepromptType,
|
||||
AttachmentView as SdkAttachmentView,
|
||||
LoginUriView as SdkLoginUriView,
|
||||
LoginView as SdkLoginView,
|
||||
FieldView as SdkFieldView,
|
||||
FieldType as SdkFieldType,
|
||||
} from "@bitwarden/sdk-internal";
|
||||
|
||||
import { mockFromJson, mockFromSdk } from "../../../../spec";
|
||||
import { CipherRepromptType } from "../../enums";
|
||||
import { CipherType } from "../../enums/cipher-type";
|
||||
|
||||
import { AttachmentView } from "./attachment.view";
|
||||
@@ -9,6 +21,7 @@ import { IdentityView } from "./identity.view";
|
||||
import { LoginView } from "./login.view";
|
||||
import { PasswordHistoryView } from "./password-history.view";
|
||||
import { SecureNoteView } from "./secure-note.view";
|
||||
import { SshKeyView } from "./ssh-key.view";
|
||||
|
||||
jest.mock("../../models/view/login.view");
|
||||
jest.mock("../../models/view/attachment.view");
|
||||
@@ -73,4 +86,121 @@ describe("CipherView", () => {
|
||||
expect(actual).toMatchObject(expected);
|
||||
});
|
||||
});
|
||||
|
||||
describe("fromSdkCipherView", () => {
|
||||
let sdkCipherView: SdkCipherView;
|
||||
|
||||
beforeEach(() => {
|
||||
jest.spyOn(CardView, "fromSdkCardView").mockImplementation(mockFromSdk);
|
||||
jest.spyOn(IdentityView, "fromSdkIdentityView").mockImplementation(mockFromSdk);
|
||||
jest.spyOn(LoginView, "fromSdkLoginView").mockImplementation(mockFromSdk);
|
||||
jest.spyOn(SecureNoteView, "fromSdkSecureNoteView").mockImplementation(mockFromSdk);
|
||||
jest.spyOn(SshKeyView, "fromSdkSshKeyView").mockImplementation(mockFromSdk);
|
||||
jest.spyOn(AttachmentView, "fromSdkAttachmentView").mockImplementation(mockFromSdk);
|
||||
jest.spyOn(FieldView, "fromSdkFieldView").mockImplementation(mockFromSdk);
|
||||
|
||||
sdkCipherView = {
|
||||
id: "id",
|
||||
organizationId: "orgId",
|
||||
folderId: "folderId",
|
||||
collectionIds: ["collectionId"],
|
||||
key: undefined,
|
||||
name: "name",
|
||||
notes: undefined,
|
||||
type: SdkCipherType.Login,
|
||||
favorite: true,
|
||||
edit: true,
|
||||
reprompt: SdkCipherRepromptType.None,
|
||||
organizationUseTotp: false,
|
||||
viewPassword: true,
|
||||
localData: undefined,
|
||||
permissions: undefined,
|
||||
attachments: [{ id: "attachmentId", url: "attachmentUrl" } as SdkAttachmentView],
|
||||
login: {
|
||||
username: "username",
|
||||
password: "password",
|
||||
uris: [{ uri: "bitwarden.com" } as SdkLoginUriView],
|
||||
totp: "totp",
|
||||
autofillOnPageLoad: true,
|
||||
} as SdkLoginView,
|
||||
identity: undefined,
|
||||
card: undefined,
|
||||
secureNote: undefined,
|
||||
sshKey: undefined,
|
||||
fields: [
|
||||
{
|
||||
name: "fieldName",
|
||||
value: "fieldValue",
|
||||
type: SdkFieldType.Linked,
|
||||
linkedId: 100,
|
||||
} as SdkFieldView,
|
||||
],
|
||||
passwordHistory: undefined,
|
||||
creationDate: "2022-01-01T12:00:00.000Z",
|
||||
revisionDate: "2022-01-02T12:00:00.000Z",
|
||||
deletedDate: undefined,
|
||||
};
|
||||
});
|
||||
|
||||
it("returns undefined when input is null", () => {
|
||||
expect(CipherView.fromSdkCipherView(null as unknown as SdkCipherView)).toBeUndefined();
|
||||
});
|
||||
|
||||
it("maps properties correctly", () => {
|
||||
const result = CipherView.fromSdkCipherView(sdkCipherView);
|
||||
|
||||
expect(result).toMatchObject({
|
||||
id: "id",
|
||||
organizationId: "orgId",
|
||||
folderId: "folderId",
|
||||
collectionIds: ["collectionId"],
|
||||
name: "name",
|
||||
notes: null,
|
||||
type: CipherType.Login,
|
||||
favorite: true,
|
||||
edit: true,
|
||||
reprompt: CipherRepromptType.None,
|
||||
organizationUseTotp: false,
|
||||
viewPassword: true,
|
||||
localData: undefined,
|
||||
permissions: undefined,
|
||||
attachments: [
|
||||
{
|
||||
id: "attachmentId",
|
||||
url: "attachmentUrl",
|
||||
__fromSdk: true,
|
||||
},
|
||||
],
|
||||
login: {
|
||||
username: "username",
|
||||
password: "password",
|
||||
uris: [
|
||||
{
|
||||
uri: "bitwarden.com",
|
||||
},
|
||||
],
|
||||
totp: "totp",
|
||||
autofillOnPageLoad: true,
|
||||
__fromSdk: true,
|
||||
},
|
||||
identity: new IdentityView(),
|
||||
card: new CardView(),
|
||||
secureNote: new SecureNoteView(),
|
||||
sshKey: new SshKeyView(),
|
||||
fields: [
|
||||
{
|
||||
name: "fieldName",
|
||||
value: "fieldValue",
|
||||
type: SdkFieldType.Linked,
|
||||
linkedId: 100,
|
||||
__fromSdk: true,
|
||||
},
|
||||
],
|
||||
passwordHistory: null,
|
||||
creationDate: new Date("2022-01-01T12:00:00.000Z"),
|
||||
revisionDate: new Date("2022-01-02T12:00:00.000Z"),
|
||||
deletedDate: null,
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
// FIXME: Update this file to be type safe and remove this and next line
|
||||
// @ts-strict-ignore
|
||||
import { CipherView as SdkCipherView } from "@bitwarden/sdk-internal";
|
||||
|
||||
import { View } from "../../../models/view/view";
|
||||
import { InitializerMetadata } from "../../../platform/interfaces/initializer-metadata.interface";
|
||||
import { InitializerKey } from "../../../platform/services/cryptography/initializer-key";
|
||||
@@ -110,7 +112,7 @@ export class CipherView implements View, InitializerMetadata {
|
||||
get hasOldAttachments(): boolean {
|
||||
if (this.hasAttachments) {
|
||||
for (let i = 0; i < this.attachments.length; i++) {
|
||||
if (this.attachments[i].key == null) {
|
||||
if (this.attachments[i].key == null && this.attachments[i].encryptedKey == null) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -222,4 +224,68 @@ export class CipherView implements View, InitializerMetadata {
|
||||
|
||||
return view;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a CipherView from the SDK CipherView.
|
||||
*/
|
||||
static fromSdkCipherView(obj: SdkCipherView): CipherView | undefined {
|
||||
if (obj == null) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const cipherView = new CipherView();
|
||||
cipherView.id = obj.id ?? null;
|
||||
cipherView.organizationId = obj.organizationId ?? null;
|
||||
cipherView.folderId = obj.folderId ?? null;
|
||||
cipherView.name = obj.name;
|
||||
cipherView.notes = obj.notes ?? null;
|
||||
cipherView.type = obj.type;
|
||||
cipherView.favorite = obj.favorite;
|
||||
cipherView.organizationUseTotp = obj.organizationUseTotp;
|
||||
cipherView.permissions = CipherPermissionsApi.fromSdkCipherPermissions(obj.permissions);
|
||||
cipherView.edit = obj.edit;
|
||||
cipherView.viewPassword = obj.viewPassword;
|
||||
cipherView.localData = obj.localData
|
||||
? {
|
||||
lastUsedDate: obj.localData.lastUsedDate
|
||||
? new Date(obj.localData.lastUsedDate).getTime()
|
||||
: undefined,
|
||||
lastLaunched: obj.localData.lastLaunched
|
||||
? new Date(obj.localData.lastLaunched).getTime()
|
||||
: undefined,
|
||||
}
|
||||
: undefined;
|
||||
cipherView.attachments =
|
||||
obj.attachments?.map((a) => AttachmentView.fromSdkAttachmentView(a)) ?? null;
|
||||
cipherView.fields = obj.fields?.map((f) => FieldView.fromSdkFieldView(f)) ?? null;
|
||||
cipherView.passwordHistory =
|
||||
obj.passwordHistory?.map((ph) => PasswordHistoryView.fromSdkPasswordHistoryView(ph)) ?? null;
|
||||
cipherView.collectionIds = obj.collectionIds ?? null;
|
||||
cipherView.revisionDate = obj.revisionDate == null ? null : new Date(obj.revisionDate);
|
||||
cipherView.creationDate = obj.creationDate == null ? null : new Date(obj.creationDate);
|
||||
cipherView.deletedDate = obj.deletedDate == null ? null : new Date(obj.deletedDate);
|
||||
cipherView.reprompt = obj.reprompt ?? CipherRepromptType.None;
|
||||
|
||||
switch (obj.type) {
|
||||
case CipherType.Card:
|
||||
cipherView.card = CardView.fromSdkCardView(obj.card);
|
||||
break;
|
||||
case CipherType.Identity:
|
||||
cipherView.identity = IdentityView.fromSdkIdentityView(obj.identity);
|
||||
break;
|
||||
case CipherType.Login:
|
||||
cipherView.login = LoginView.fromSdkLoginView(obj.login);
|
||||
break;
|
||||
case CipherType.SecureNote:
|
||||
cipherView.secureNote = SecureNoteView.fromSdkSecureNoteView(obj.secureNote);
|
||||
break;
|
||||
case CipherType.SshKey:
|
||||
cipherView.sshKey = SshKeyView.fromSdkSshKeyView(obj.sshKey);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return cipherView;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
// @ts-strict-ignore
|
||||
import { Jsonify } from "type-fest";
|
||||
|
||||
import { Fido2CredentialView as SdkFido2CredentialView } from "@bitwarden/sdk-internal";
|
||||
|
||||
import { ItemView } from "./item.view";
|
||||
|
||||
export class Fido2CredentialView extends ItemView {
|
||||
@@ -29,4 +31,29 @@ export class Fido2CredentialView extends ItemView {
|
||||
creationDate,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the SDK Fido2CredentialView to a Fido2CredentialView.
|
||||
*/
|
||||
static fromSdkFido2CredentialView(obj: SdkFido2CredentialView): Fido2CredentialView | undefined {
|
||||
if (!obj) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const view = new Fido2CredentialView();
|
||||
view.credentialId = obj.credentialId;
|
||||
view.keyType = obj.keyType as "public-key";
|
||||
view.keyAlgorithm = obj.keyAlgorithm as "ECDSA";
|
||||
view.keyCurve = obj.keyCurve as "P-256";
|
||||
view.rpId = obj.rpId;
|
||||
view.userHandle = obj.userHandle;
|
||||
view.userName = obj.userName;
|
||||
view.counter = parseInt(obj.counter);
|
||||
view.rpName = obj.rpName;
|
||||
view.userDisplayName = obj.userDisplayName;
|
||||
view.discoverable = obj.discoverable?.toLowerCase() === "true" ? true : false;
|
||||
view.creationDate = obj.creationDate ? new Date(obj.creationDate) : null;
|
||||
|
||||
return view;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
// @ts-strict-ignore
|
||||
import { Jsonify } from "type-fest";
|
||||
|
||||
import { FieldView as SdkFieldView } from "@bitwarden/sdk-internal";
|
||||
|
||||
import { View } from "../../../models/view/view";
|
||||
import { FieldType, LinkedIdType } from "../../enums";
|
||||
import { Field } from "../domain/field";
|
||||
@@ -31,4 +33,21 @@ export class FieldView implements View {
|
||||
static fromJSON(obj: Partial<Jsonify<FieldView>>): FieldView {
|
||||
return Object.assign(new FieldView(), obj);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the SDK FieldView to a FieldView.
|
||||
*/
|
||||
static fromSdkFieldView(obj: SdkFieldView): FieldView | undefined {
|
||||
if (!obj) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const view = new FieldView();
|
||||
view.name = obj.name;
|
||||
view.value = obj.value;
|
||||
view.type = obj.type;
|
||||
view.linkedId = obj.linkedId as unknown as LinkedIdType;
|
||||
|
||||
return view;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
// @ts-strict-ignore
|
||||
import { Jsonify } from "type-fest";
|
||||
|
||||
import { IdentityView as SdkIdentityView } from "@bitwarden/sdk-internal";
|
||||
|
||||
import { Utils } from "../../../platform/misc/utils";
|
||||
import { IdentityLinkedId as LinkedId } from "../../enums";
|
||||
import { linkedFieldOption } from "../../linked-field-option.decorator";
|
||||
@@ -158,4 +160,15 @@ export class IdentityView extends ItemView {
|
||||
static fromJSON(obj: Partial<Jsonify<IdentityView>>): IdentityView {
|
||||
return Object.assign(new IdentityView(), obj);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the SDK IdentityView to an IdentityView.
|
||||
*/
|
||||
static fromSdkIdentityView(obj: SdkIdentityView): IdentityView | undefined {
|
||||
if (obj == null) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return Object.assign(new IdentityView(), obj);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import { LoginUriView as SdkLoginUriView, UriMatchType } from "@bitwarden/sdk-internal";
|
||||
|
||||
import { UriMatchStrategy, UriMatchStrategySetting } from "../../../models/domain/domain-service";
|
||||
import { Utils } from "../../../platform/misc/utils";
|
||||
|
||||
@@ -184,6 +186,26 @@ describe("LoginUriView", () => {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("fromSdkLoginUriView", () => {
|
||||
it("should return undefined when the input is null", () => {
|
||||
const result = LoginUriView.fromSdkLoginUriView(null as unknown as SdkLoginUriView);
|
||||
expect(result).toBeUndefined();
|
||||
});
|
||||
|
||||
it("should create a LoginUriView from a SdkLoginUriView", () => {
|
||||
const sdkLoginUriView = {
|
||||
uri: "https://example.com",
|
||||
match: UriMatchType.Host,
|
||||
} as SdkLoginUriView;
|
||||
|
||||
const loginUriView = LoginUriView.fromSdkLoginUriView(sdkLoginUriView);
|
||||
|
||||
expect(loginUriView).toBeInstanceOf(LoginUriView);
|
||||
expect(loginUriView!.uri).toBe(sdkLoginUriView.uri);
|
||||
expect(loginUriView!.match).toBe(sdkLoginUriView.match);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
function uriFactory(match: UriMatchStrategySetting, uri: string) {
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
// @ts-strict-ignore
|
||||
import { Jsonify } from "type-fest";
|
||||
|
||||
import { LoginUriView as SdkLoginUriView } from "@bitwarden/sdk-internal";
|
||||
|
||||
import { UriMatchStrategy, UriMatchStrategySetting } from "../../../models/domain/domain-service";
|
||||
import { View } from "../../../models/view/view";
|
||||
import { SafeUrls } from "../../../platform/misc/safe-urls";
|
||||
@@ -112,6 +114,21 @@ export class LoginUriView implements View {
|
||||
return Object.assign(new LoginUriView(), obj);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a LoginUriView object from the SDK to a LoginUriView object.
|
||||
*/
|
||||
static fromSdkLoginUriView(obj: SdkLoginUriView): LoginUriView | undefined {
|
||||
if (obj == null) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const view = new LoginUriView();
|
||||
view.uri = obj.uri;
|
||||
view.match = obj.match;
|
||||
|
||||
return view;
|
||||
}
|
||||
|
||||
matchesUri(
|
||||
targetUri: string,
|
||||
equivalentDomains: Set<string>,
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
import { mockFromJson } from "../../../../spec";
|
||||
import { LoginView as SdkLoginView } from "@bitwarden/sdk-internal";
|
||||
|
||||
import { mockFromJson, mockFromSdk } from "../../../../spec";
|
||||
|
||||
import { LoginUriView } from "./login-uri.view";
|
||||
import { LoginView } from "./login.view";
|
||||
@@ -25,4 +27,35 @@ describe("LoginView", () => {
|
||||
uris: ["uri1_fromJSON", "uri2_fromJSON", "uri3_fromJSON"],
|
||||
});
|
||||
});
|
||||
|
||||
describe("fromSdkLoginView", () => {
|
||||
it("should return undefined when the input is null", () => {
|
||||
const result = LoginView.fromSdkLoginView(null as unknown as SdkLoginView);
|
||||
expect(result).toBeUndefined();
|
||||
});
|
||||
|
||||
it("should return a LoginView from an SdkLoginView", () => {
|
||||
jest.spyOn(LoginUriView, "fromSdkLoginUriView").mockImplementation(mockFromSdk);
|
||||
|
||||
const sdkLoginView = {
|
||||
username: "username",
|
||||
password: "password",
|
||||
passwordRevisionDate: "2025-01-01T01:06:40.441Z",
|
||||
uris: [{ uri: "bitwarden.com" } as any],
|
||||
totp: "totp",
|
||||
autofillOnPageLoad: true,
|
||||
} as SdkLoginView;
|
||||
|
||||
const result = LoginView.fromSdkLoginView(sdkLoginView);
|
||||
|
||||
expect(result).toMatchObject({
|
||||
username: "username",
|
||||
password: "password",
|
||||
passwordRevisionDate: new Date("2025-01-01T01:06:40.441Z"),
|
||||
uris: [expect.objectContaining({ uri: "bitwarden.com", __fromSdk: true })],
|
||||
totp: "totp",
|
||||
autofillOnPageLoad: true,
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
// FIXME: Update this file to be type safe and remove this and next line
|
||||
// @ts-strict-ignore
|
||||
import { LoginView as SdkLoginView } from "@bitwarden/sdk-internal";
|
||||
|
||||
import { UriMatchStrategySetting } from "../../../models/domain/domain-service";
|
||||
import { Utils } from "../../../platform/misc/utils";
|
||||
import { DeepJsonify } from "../../../types/deep-jsonify";
|
||||
@@ -100,4 +102,27 @@ export class LoginView extends ItemView {
|
||||
fido2Credentials,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the SDK LoginView to a LoginView.
|
||||
*
|
||||
* Note: FIDO2 credentials remain encrypted at this stage.
|
||||
* Unlike other fields that are decrypted as part of the LoginView, the SDK maintains
|
||||
* the FIDO2 credentials in encrypted form. We can decrypt them later using a separate
|
||||
* call to client.vault().ciphers().decrypt_fido2_credentials().
|
||||
*/
|
||||
static fromSdkLoginView(obj: SdkLoginView): LoginView | undefined {
|
||||
if (obj == null) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const passwordRevisionDate =
|
||||
obj.passwordRevisionDate == null ? null : new Date(obj.passwordRevisionDate);
|
||||
const uris = obj.uris?.map((uri) => LoginUriView.fromSdkLoginUriView(uri));
|
||||
|
||||
return Object.assign(new LoginView(), obj, {
|
||||
passwordRevisionDate,
|
||||
uris,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import { PasswordHistoryView as SdkPasswordHistoryView } from "@bitwarden/sdk-internal";
|
||||
|
||||
import { PasswordHistoryView } from "./password-history.view";
|
||||
|
||||
describe("PasswordHistoryView", () => {
|
||||
@@ -10,4 +12,25 @@ describe("PasswordHistoryView", () => {
|
||||
|
||||
expect(actual.lastUsedDate).toEqual(lastUsedDate);
|
||||
});
|
||||
|
||||
describe("fromSdkPasswordHistoryView", () => {
|
||||
it("should return undefined when the input is null", () => {
|
||||
const result = PasswordHistoryView.fromSdkPasswordHistoryView(null as unknown as any);
|
||||
expect(result).toBeUndefined();
|
||||
});
|
||||
|
||||
it("should return a PasswordHistoryView from an SdkPasswordHistoryView", () => {
|
||||
const sdkPasswordHistoryView = {
|
||||
password: "password",
|
||||
lastUsedDate: "2023-10-01T00:00:00Z",
|
||||
} as SdkPasswordHistoryView;
|
||||
|
||||
const result = PasswordHistoryView.fromSdkPasswordHistoryView(sdkPasswordHistoryView);
|
||||
|
||||
expect(result).toMatchObject({
|
||||
password: "password",
|
||||
lastUsedDate: new Date("2023-10-01T00:00:00Z"),
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
// @ts-strict-ignore
|
||||
import { Jsonify } from "type-fest";
|
||||
|
||||
import { PasswordHistoryView as SdkPasswordHistoryView } from "@bitwarden/sdk-internal";
|
||||
|
||||
import { View } from "../../../models/view/view";
|
||||
import { Password } from "../domain/password";
|
||||
|
||||
@@ -24,4 +26,19 @@ export class PasswordHistoryView implements View {
|
||||
lastUsedDate: lastUsedDate,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the SDK PasswordHistoryView to a PasswordHistoryView.
|
||||
*/
|
||||
static fromSdkPasswordHistoryView(obj: SdkPasswordHistoryView): PasswordHistoryView | undefined {
|
||||
if (!obj) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const view = new PasswordHistoryView();
|
||||
view.password = obj.password;
|
||||
view.lastUsedDate = obj.lastUsedDate == null ? null : new Date(obj.lastUsedDate);
|
||||
|
||||
return view;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
// @ts-strict-ignore
|
||||
import { Jsonify } from "type-fest";
|
||||
|
||||
import { SecureNoteView as SdkSecureNoteView } from "@bitwarden/sdk-internal";
|
||||
|
||||
import { SecureNoteType } from "../../enums";
|
||||
import { SecureNote } from "../domain/secure-note";
|
||||
|
||||
@@ -26,4 +28,15 @@ export class SecureNoteView extends ItemView {
|
||||
static fromJSON(obj: Partial<Jsonify<SecureNoteView>>): SecureNoteView {
|
||||
return Object.assign(new SecureNoteView(), obj);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the SDK SecureNoteView to a SecureNoteView.
|
||||
*/
|
||||
static fromSdkSecureNoteView(obj: SdkSecureNoteView): SecureNoteView | undefined {
|
||||
if (!obj) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return Object.assign(new SecureNoteView(), obj);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
// @ts-strict-ignore
|
||||
import { Jsonify } from "type-fest";
|
||||
|
||||
import { SshKeyView as SdkSshKeyView } from "@bitwarden/sdk-internal";
|
||||
|
||||
import { SshKey } from "../domain/ssh-key";
|
||||
|
||||
import { ItemView } from "./item.view";
|
||||
@@ -44,4 +46,19 @@ export class SshKeyView extends ItemView {
|
||||
static fromJSON(obj: Partial<Jsonify<SshKeyView>>): SshKeyView {
|
||||
return Object.assign(new SshKeyView(), obj);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the SDK SshKeyView to a SshKeyView.
|
||||
*/
|
||||
static fromSdkSshKeyView(obj: SdkSshKeyView): SshKeyView | undefined {
|
||||
if (!obj) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const keyFingerprint = obj.fingerprint;
|
||||
|
||||
return Object.assign(new SshKeyView(), obj, {
|
||||
keyFingerprint,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user