diff options
Diffstat (limited to 'packages/browser/src/methods/startRegistration.ts')
-rw-r--r-- | packages/browser/src/methods/startRegistration.ts | 53 |
1 files changed, 30 insertions, 23 deletions
diff --git a/packages/browser/src/methods/startRegistration.ts b/packages/browser/src/methods/startRegistration.ts index bc30707..14419ca 100644 --- a/packages/browser/src/methods/startRegistration.ts +++ b/packages/browser/src/methods/startRegistration.ts @@ -1,7 +1,8 @@ import { PublicKeyCredentialCreationOptionsJSON, RegistrationCredential, - RegistrationCredentialJSON, + RegistrationResponseJSON, + PublicKeyCredentialFuture, } from '@simplewebauthn/typescript-types'; import { utf8StringToBuffer } from '../helpers/utf8StringToBuffer'; @@ -11,6 +12,7 @@ import { browserSupportsWebAuthn } from '../helpers/browserSupportsWebAuthn'; import { toPublicKeyCredentialDescriptor } from '../helpers/toPublicKeyCredentialDescriptor'; import { identifyRegistrationError } from '../helpers/identifyRegistrationError'; import { webauthnAbortService } from '../helpers/webAuthnAbortService'; +import { toAuthenticatorAttachment } from '../helpers/toAuthenticatorAttachment'; /** * Begin authenticator "registration" via WebAuthn attestation @@ -19,21 +21,29 @@ import { webauthnAbortService } from '../helpers/webAuthnAbortService'; */ export async function startRegistration( creationOptionsJSON: PublicKeyCredentialCreationOptionsJSON, -): Promise<RegistrationCredentialJSON> { +): Promise<RegistrationResponseJSON> { if (!browserSupportsWebAuthn()) { throw new Error('WebAuthn is not supported in this browser'); } + const globalPublicKeyCredential = + window.PublicKeyCredential as unknown as PublicKeyCredentialFuture; + + let publicKey: PublicKeyCredentialCreationOptions; // We need to convert some values to Uint8Arrays before passing the credentials to the navigator - const publicKey: PublicKeyCredentialCreationOptions = { - ...creationOptionsJSON, - challenge: base64URLStringToBuffer(creationOptionsJSON.challenge), - user: { - ...creationOptionsJSON.user, - id: utf8StringToBuffer(creationOptionsJSON.user.id), - }, - excludeCredentials: creationOptionsJSON.excludeCredentials.map(toPublicKeyCredentialDescriptor), - }; + if (typeof globalPublicKeyCredential.parseCreationOptionsFromJSON === 'function') { + publicKey = globalPublicKeyCredential.parseCreationOptionsFromJSON(creationOptionsJSON); + } else { + publicKey = { + ...creationOptionsJSON, + challenge: base64URLStringToBuffer(creationOptionsJSON.challenge), + user: { + ...creationOptionsJSON.user, + id: utf8StringToBuffer(creationOptionsJSON.user.id), + }, + excludeCredentials: creationOptionsJSON.excludeCredentials?.map(toPublicKeyCredentialDescriptor), + }; + } // Finalize options const options: CredentialCreationOptions = { publicKey }; @@ -52,27 +62,24 @@ export async function startRegistration( throw new Error('Registration was not completed'); } + // Use toJSON() if it's available in the browser + if (typeof credential.toJSON === 'function') { + return credential.toJSON() as RegistrationResponseJSON; + } + + // Manually construct an instance of RegistrationResponseJSON const { id, rawId, response, type } = credential; - // Convert values to base64 to make it easier to send back to the server - const credentialJSON: RegistrationCredentialJSON = { + return { id, rawId: bufferToBase64URLString(rawId), response: { attestationObject: bufferToBase64URLString(response.attestationObject), clientDataJSON: bufferToBase64URLString(response.clientDataJSON), + transports: response.getTransports(), }, type, clientExtensionResults: credential.getClientExtensionResults(), - authenticatorAttachment: credential.authenticatorAttachment, + authenticatorAttachment: toAuthenticatorAttachment(credential.authenticatorAttachment), }; - - /** - * Include the authenticator's transports if the browser supports querying for them - */ - if (typeof response.getTransports === 'function') { - credentialJSON.transports = response.getTransports(); - } - - return credentialJSON; } |