diff options
Diffstat (limited to 'packages/browser/src')
-rw-r--r-- | packages/browser/src/helpers/toPublicKeyCredentialDescriptor.ts | 16 | ||||
-rw-r--r-- | packages/browser/src/methods/startAssertion.ts | 15 |
2 files changed, 20 insertions, 11 deletions
diff --git a/packages/browser/src/helpers/toPublicKeyCredentialDescriptor.ts b/packages/browser/src/helpers/toPublicKeyCredentialDescriptor.ts new file mode 100644 index 0000000..ad166a5 --- /dev/null +++ b/packages/browser/src/helpers/toPublicKeyCredentialDescriptor.ts @@ -0,0 +1,16 @@ +import base64js from 'base64-js'; +import type { PublicKeyCredentialDescriptorJSON } from '@webauthntine/typescript-types'; + +export default function toPublicKeyCredentialDescriptor( + descriptor: PublicKeyCredentialDescriptorJSON, +): PublicKeyCredentialDescriptor { + // Make sure the Base64'd credential ID length is a multiple of 4 or else toByteArray will throw + const { id } = descriptor; + const padLength = 4 - (id.length % 4); + const paddedId = id.padEnd(id.length + padLength, '='); + + return { + ...descriptor, + id: base64js.toByteArray(paddedId), + }; +} diff --git a/packages/browser/src/methods/startAssertion.ts b/packages/browser/src/methods/startAssertion.ts index f504f19..826763a 100644 --- a/packages/browser/src/methods/startAssertion.ts +++ b/packages/browser/src/methods/startAssertion.ts @@ -3,11 +3,11 @@ import { AuthenticatorAssertionResponseJSON, AssertionCredential, } from '@webauthntine/typescript-types'; -import base64js from 'base64-js'; import toUint8Array from '../helpers/toUint8Array'; import toBase64String from '../helpers/toBase64String'; import supportsWebauthn from '../helpers/supportsWebauthn'; +import toPublicKeyCredentialDescriptor from '../helpers/toPublicKeyCredentialDescriptor'; /** * Begin authenticator "login" via WebAuthn assertion @@ -25,16 +25,9 @@ export default async function startAssertion( const publicKey: PublicKeyCredentialRequestOptions = { ...requestOptionsJSON.publicKey, challenge: toUint8Array(requestOptionsJSON.publicKey.challenge), - allowCredentials: requestOptionsJSON.publicKey.allowCredentials.map(cred => { - // Make sure the credential ID length is a multiple of 4 - const padLength = 4 - (cred.id.length % 4); - const id = cred.id.padEnd(cred.id.length + padLength, '='); - - return { - ...cred, - id: base64js.toByteArray(id), - }; - }), + allowCredentials: requestOptionsJSON.publicKey.allowCredentials.map( + toPublicKeyCredentialDescriptor, + ), }; // Wait for the user to complete assertion |