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 8b4a6a0
Showing 1 changed file with 38 additions and 13 deletions.
51 changes: 38 additions & 13 deletions packages/http/src/webauthn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,24 +114,49 @@ export async function getWebAuthnAssertion(
payload: string,
options?: TurnkeyCredentialRequestOptions
): Promise<string> {
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,
credentialId: assertion.id,
signature: assertion.response.signature,
};
const supported = await isWebAuthnSupported();

if (supported) {
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,
credentialId: assertion.id,
signature: assertion.response.signature,
};

return JSON.stringify(stamp);
}

return JSON.stringify(stamp);
throw new Error("webauthn is not supported by this browser");
}

export async function getWebAuthnAttestation(
options: TurnkeyCredentialCreationOptions
): Promise<TAttestation> {
const res = await webauthnCredentialCreate(options);
const supported = await isWebAuthnSupported();

if (supported) {
const res = await webauthnCredentialCreate(options);

return toInternalAttestation(res.toJSON());
}

throw new Error("webauthn is not supported by this browser");
}

// For additional details see https://web.dev/articles/passkey-form-autofill#feature-detection
async function isWebAuthnSupported(): Promise<boolean> {
if (
window.PublicKeyCredential &&
PublicKeyCredential.isConditionalMediationAvailable
) {
const isCMA = await PublicKeyCredential.isConditionalMediationAvailable();

return isCMA;
}

return toInternalAttestation(res.toJSON());
return false;
}

0 comments on commit 8b4a6a0

Please sign in to comment.