Skip to content

Commit

Permalink
add conditional check
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewkmin committed Oct 11, 2023
1 parent 1452f06 commit d69624b
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion packages/http/src/webauthn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,15 @@ export async function getWebAuthnAssertion(
payload: string,
options?: TurnkeyCredentialRequestOptions
): Promise<string> {
const webAuthnSupported = await hasWebAuthnSupport();

if (!webAuthnSupported) {
throw new Error("webauthn is not supported by this browser");
}

const signingOptions = await getCredentialRequestOptions(payload, options);
const clientGetResult = await webauthnCredentialGet(signingOptions);
const assertion = clientGetResult.toJSON();

const stamp: TWebAuthnStamp = {
authenticatorData: assertion.response.authenticatorData,
clientDataJson: assertion.response.clientDataJSON,
Expand All @@ -131,7 +136,27 @@ export async function getWebAuthnAssertion(
export async function getWebAuthnAttestation(
options: TurnkeyCredentialCreationOptions
): Promise<TAttestation> {
const webAuthnSupported = await hasWebAuthnSupport();

if (!webAuthnSupported) {
throw new Error("webauthn is not supported by this browser");
}

const res = await webauthnCredentialCreate(options);

return toInternalAttestation(res.toJSON());
}

// For additional details see https://web.dev/articles/passkey-form-autofill#feature-detection, https://github.com/w3c/webauthn/wiki/Explainer:-WebAuthn-Conditional-UI
async function hasWebAuthnSupport(): Promise<boolean> {
if (
window.PublicKeyCredential &&
PublicKeyCredential.isConditionalMediationAvailable
) {
const isCMA = await PublicKeyCredential.isConditionalMediationAvailable();

return isCMA;
}

return false;
}

0 comments on commit d69624b

Please sign in to comment.