diff options
author | Matthew Miller <matthew@millerti.me> | 2022-06-17 22:46:03 -0700 |
---|---|---|
committer | Matthew Miller <matthew@millerti.me> | 2022-06-17 22:46:03 -0700 |
commit | 0c62c20f32514079e904f2925d73bf46d670cf20 (patch) | |
tree | b6a4c27f8523686b84c1ea6e5adbc82172750b2f /packages/browser/src/methods | |
parent | d11de5a7240cd81739d6f3deae25ec9966cde90c (diff) |
Configure options for conditional UI
Diffstat (limited to 'packages/browser/src/methods')
-rw-r--r-- | packages/browser/src/methods/startAuthentication.ts | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/packages/browser/src/methods/startAuthentication.ts b/packages/browser/src/methods/startAuthentication.ts index a08e3a3..a12c8fd 100644 --- a/packages/browser/src/methods/startAuthentication.ts +++ b/packages/browser/src/methods/startAuthentication.ts @@ -8,6 +8,7 @@ import bufferToBase64URLString from '../helpers/bufferToBase64URLString'; import base64URLStringToBuffer from '../helpers/base64URLStringToBuffer'; import bufferToUTF8String from '../helpers/bufferToUTF8String'; import { browserSupportsWebauthn } from '../helpers/browserSupportsWebauthn'; +import { browserSupportsWebAuthnAutofill } from '../helpers/browserSupportsConditionalMediation'; import toPublicKeyCredentialDescriptor from '../helpers/toPublicKeyCredentialDescriptor'; import { identifyAuthenticationError } from '../helpers/identifyAuthenticationError'; @@ -42,6 +43,30 @@ export default async function startAuthentication( const options: CredentialRequestOptions = { publicKey }; + /** + * Set up the page to prompt the user to select a credential for authentication via the browser's + * input autofill mechanism. + */ + if (supportBrowserAutofill) { + if (!(await browserSupportsWebAuthnAutofill())) { + 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']"); + + // WebAuthn autofill requires at least one valid input + if (eligibleInputs.length < 1) { + 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; + // Massage options into a suitable structure + delete options.publicKey?.allowCredentials; + } + // Wait for the user to complete assertion let credential; try { |