diff options
author | Matthew Miller <matthew@millerti.me> | 2022-12-27 19:22:08 -0800 |
---|---|---|
committer | Matthew Miller <matthew@millerti.me> | 2022-12-27 19:22:08 -0800 |
commit | 1c0128c483812aae7ab2b1a22198d762825788b1 (patch) | |
tree | 086d3ffce194e0da329dd559ae83b1b4ca55c82c /packages/browser/src | |
parent | 3170379c67a9cf33374d9f9ab89dafed16ba7edf (diff) |
Update startAuthentication to use new values
Diffstat (limited to 'packages/browser/src')
-rw-r--r-- | packages/browser/src/methods/startAuthentication.ts | 34 |
1 files changed, 25 insertions, 9 deletions
diff --git a/packages/browser/src/methods/startAuthentication.ts b/packages/browser/src/methods/startAuthentication.ts index 761a96c..8c9564f 100644 --- a/packages/browser/src/methods/startAuthentication.ts +++ b/packages/browser/src/methods/startAuthentication.ts @@ -1,7 +1,8 @@ import { PublicKeyCredentialRequestOptionsJSON, AuthenticationCredential, - AuthenticationCredentialJSON, + AuthenticationResponseJSON, + PublicKeyCredentialFuture, } from '@simplewebauthn/typescript-types'; import { bufferToBase64URLString } from '../helpers/bufferToBase64URLString'; @@ -12,22 +13,26 @@ import { browserSupportsWebAuthnAutofill } from '../helpers/browserSupportsWebAu 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 * - * @param requestOptionsJSON Output from **@simplewebauthn/server**'s generateAssertionOptions(...) + * @param requestOptionsJSON Output from **@simplewebauthn/server**'s `generateAuthenticationOptions(...)` * @param useBrowserAutofill Initialize conditional UI to enable logging in via browser * autofill prompts */ export async function startAuthentication( requestOptionsJSON: PublicKeyCredentialRequestOptionsJSON, useBrowserAutofill = false, -): Promise<AuthenticationCredentialJSON> { +): Promise<AuthenticationResponseJSON> { if (!browserSupportsWebAuthn()) { throw new Error('WebAuthn is not supported in this browser'); } + const globalPublicKeyCredential = + window.PublicKeyCredential as unknown as PublicKeyCredentialFuture; + // We need to avoid passing empty array to avoid blocking retrieval // of public key let allowCredentials; @@ -36,11 +41,16 @@ export async function startAuthentication( } // We need to convert some values to Uint8Arrays before passing the credentials to the navigator - const publicKey: PublicKeyCredentialRequestOptions = { - ...requestOptionsJSON, - challenge: base64URLStringToBuffer(requestOptionsJSON.challenge), - allowCredentials, - }; + let publicKey: PublicKeyCredentialRequestOptions; + if (typeof globalPublicKeyCredential.parseRequestOptionsFromJSON === 'function') { + publicKey = globalPublicKeyCredential.parseRequestOptionsFromJSON(requestOptionsJSON); + } else { + publicKey = { + ...requestOptionsJSON, + challenge: base64URLStringToBuffer(requestOptionsJSON.challenge), + allowCredentials, + }; + } // Prepare options for `.get()` const options: CredentialRequestOptions = {}; @@ -86,6 +96,12 @@ export async function startAuthentication( throw new Error('Authentication was not completed'); } + // Use toJSON() if it's available in the browser + if (typeof credential.toJSON === 'function') { + return credential.toJSON() as AuthenticationResponseJSON; + } + + // Manually construct an instance of AuthenticationResponseJSON const { id, rawId, response, type } = credential; let userHandle = undefined; @@ -105,6 +121,6 @@ export async function startAuthentication( }, type, clientExtensionResults: credential.getClientExtensionResults(), - authenticatorAttachment: credential.authenticatorAttachment, + authenticatorAttachment: toAuthenticatorAttachment(credential.authenticatorAttachment), }; } |