summaryrefslogtreecommitdiffhomepage
path: root/packages/browser/src/methods/startAuthentication.ts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/browser/src/methods/startAuthentication.ts')
-rw-r--r--packages/browser/src/methods/startAuthentication.ts49
1 files changed, 29 insertions, 20 deletions
diff --git a/packages/browser/src/methods/startAuthentication.ts b/packages/browser/src/methods/startAuthentication.ts
index cce28e7..5147232 100644
--- a/packages/browser/src/methods/startAuthentication.ts
+++ b/packages/browser/src/methods/startAuthentication.ts
@@ -1,18 +1,18 @@
import {
- PublicKeyCredentialRequestOptionsJSON,
AuthenticationCredential,
AuthenticationResponseJSON,
-} from '@simplewebauthn/typescript-types';
+ PublicKeyCredentialRequestOptionsJSON,
+} from "@simplewebauthn/typescript-types";
-import { bufferToBase64URLString } from '../helpers/bufferToBase64URLString';
-import { base64URLStringToBuffer } from '../helpers/base64URLStringToBuffer';
-import { bufferToUTF8String } from '../helpers/bufferToUTF8String';
-import { browserSupportsWebAuthn } from '../helpers/browserSupportsWebAuthn';
-import { browserSupportsWebAuthnAutofill } from '../helpers/browserSupportsWebAuthnAutofill';
-import { toPublicKeyCredentialDescriptor } from '../helpers/toPublicKeyCredentialDescriptor';
-import { identifyAuthenticationError } from '../helpers/identifyAuthenticationError';
-import { webauthnAbortService } from '../helpers/webAuthnAbortService';
-import { toAuthenticatorAttachment } from '../helpers/toAuthenticatorAttachment';
+import { bufferToBase64URLString } from "../helpers/bufferToBase64URLString";
+import { base64URLStringToBuffer } from "../helpers/base64URLStringToBuffer";
+import { bufferToUTF8String } from "../helpers/bufferToUTF8String";
+import { browserSupportsWebAuthn } from "../helpers/browserSupportsWebAuthn";
+import { browserSupportsWebAuthnAutofill } from "../helpers/browserSupportsWebAuthnAutofill";
+import { toPublicKeyCredentialDescriptor } from "../helpers/toPublicKeyCredentialDescriptor";
+import { identifyAuthenticationError } from "../helpers/identifyAuthenticationError";
+import { webauthnAbortService } from "../helpers/webAuthnAbortService";
+import { toAuthenticatorAttachment } from "../helpers/toAuthenticatorAttachment";
/**
* Begin authenticator "login" via WebAuthn assertion
@@ -26,14 +26,16 @@ export async function startAuthentication(
useBrowserAutofill = false,
): Promise<AuthenticationResponseJSON> {
if (!browserSupportsWebAuthn()) {
- throw new Error('WebAuthn is not supported in this browser');
+ throw new Error("WebAuthn is not supported in this browser");
}
// We need to avoid passing empty array to avoid blocking retrieval
// of public key
let allowCredentials;
if (requestOptionsJSON.allowCredentials?.length !== 0) {
- allowCredentials = requestOptionsJSON.allowCredentials?.map(toPublicKeyCredentialDescriptor);
+ allowCredentials = requestOptionsJSON.allowCredentials?.map(
+ toPublicKeyCredentialDescriptor,
+ );
}
// We need to convert some values to Uint8Arrays before passing the credentials to the navigator
@@ -52,20 +54,24 @@ export async function startAuthentication(
*/
if (useBrowserAutofill) {
if (!(await browserSupportsWebAuthnAutofill())) {
- throw Error('Browser does not support WebAuthn autofill');
+ throw Error("Browser does not support WebAuthn autofill");
}
// Check for an <input> with "webauthn" in its `autocomplete` attribute
- const eligibleInputs = document.querySelectorAll("input[autocomplete*='webauthn']");
+ const eligibleInputs = document.querySelectorAll(
+ "input[autocomplete*='webauthn']",
+ );
// WebAuthn autofill requires at least one valid input
if (eligibleInputs.length < 1) {
- throw Error('No <input> with `"webauthn"` in its `autocomplete` attribute was detected');
+ throw Error(
+ 'No <input> with `"webauthn"` in its `autocomplete` attribute was detected',
+ );
}
// `CredentialMediationRequirement` doesn't know about "conditional" yet as of
// typescript@4.6.3
- options.mediation = 'conditional' as CredentialMediationRequirement;
+ options.mediation = "conditional" as CredentialMediationRequirement;
// Conditional UI requires an empty allow list
publicKey.allowCredentials = [];
}
@@ -78,13 +84,14 @@ export async function startAuthentication(
// Wait for the user to complete assertion
let credential;
try {
- credential = (await navigator.credentials.get(options)) as AuthenticationCredential;
+ credential =
+ (await navigator.credentials.get(options)) as AuthenticationCredential;
} catch (err) {
throw identifyAuthenticationError({ error: err as Error, options });
}
if (!credential) {
- throw new Error('Authentication was not completed');
+ throw new Error("Authentication was not completed");
}
const { id, rawId, response, type } = credential;
@@ -106,6 +113,8 @@ export async function startAuthentication(
},
type,
clientExtensionResults: credential.getClientExtensionResults(),
- authenticatorAttachment: toAuthenticatorAttachment(credential.authenticatorAttachment),
+ authenticatorAttachment: toAuthenticatorAttachment(
+ credential.authenticatorAttachment,
+ ),
};
}