Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { TextEncoder } from "util";
import { mock, MockProxy } from "jest-mock-extended";
import { BehaviorSubject, of } from "rxjs";

import { PureCrypto } from "@bitwarden/sdk-internal";

import { mockAccountServiceWith, mockAccountInfoWith } from "../../../../spec";
import { Account } from "../../../auth/abstractions/account.service";
import { CipherId, UserId } from "../../../types/guid";
Expand All @@ -25,6 +27,7 @@ import {
Fido2UserInterfaceSession,
NewCredentialParams,
} from "../../abstractions/fido2/fido2-user-interface.service.abstraction";
import { SdkLoadService } from "../../abstractions/sdk/sdk-load.service";
import { Utils } from "../../misc/utils";

import { CBOR } from "./cbor";
Expand Down Expand Up @@ -70,6 +73,11 @@ describe("FidoAuthenticatorService", () => {
);
windowReference = Utils.newGuid();
accountService.activeAccount$ = activeAccountSubject;

// PureCrypto is backed by WASM and is not initialized in jest. stub the
// GUID generator so createKeyView() can run without loading the module.
(SdkLoadService as any).Ready = jest.fn().mockResolvedValue(true);
jest.spyOn(PureCrypto, "new_guid").mockImplementation(() => Utils.newGuid());
});

describe("makeCredential", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// @ts-strict-ignore
import { filter, firstValueFrom, map, timeout } from "rxjs";

import { PureCrypto } from "@bitwarden/sdk-internal";

import { AccountService } from "../../../auth/abstractions/account.service";
import { getUserId } from "../../../auth/services/account.service";
import { CipherId } from "../../../types/guid";
Expand All @@ -25,6 +27,7 @@ import {
} from "../../abstractions/fido2/fido2-authenticator.service.abstraction";
import { Fido2UserInterfaceService } from "../../abstractions/fido2/fido2-user-interface.service.abstraction";
import { LogService } from "../../abstractions/log.service";
import { SdkLoadService } from "../../abstractions/sdk/sdk-load.service";
import { Utils } from "../../misc/utils";

import { CBOR } from "./cbor";
Expand Down Expand Up @@ -485,9 +488,11 @@ async function createKeyView(
throw new Fido2AuthenticatorError(Fido2AuthenticatorErrorCode.Unknown);
}

await SdkLoadService.Ready; // Required for PureCrypto.new_guid()

const pkcs8Key = new Uint8Array(await crypto.subtle.exportKey("pkcs8", keyValue));
const fido2Credential = new Fido2CredentialView();
fido2Credential.credentialId = Utils.newGuid();
fido2Credential.credentialId = PureCrypto.new_guid();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably do await SdkLoadService.Ready; before we use purecrypto to ensure that the SDK is initialized on time

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed! 88d336b

fido2Credential.keyType = "public-key";
fido2Credential.keyAlgorithm = "ECDSA";
fido2Credential.keyCurve = "P-256";
Expand Down
Loading