File tree Expand file tree Collapse file tree
packages/javascript-sdk/src/fr-webauthn Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -210,4 +210,38 @@ describe('Test FRWebAuthn class with Conditional UI', () => {
210210 } ) ,
211211 ) ;
212212 } ) ;
213+
214+ it ( 'should throw NotSupportedError if WebAuthn is not supported' , async ( ) => {
215+ // Mock WebAuthn not supported
216+ const spy = vi . spyOn ( FRWebAuthn , 'isWebAuthnSupported' ) . mockReturnValue ( false ) ;
217+
218+ await expect ( FRWebAuthn . getRegistrationCredential ( { } as any ) ) . rejects . toThrow (
219+ 'PublicKeyCredential not supported by this browser' ,
220+ ) ;
221+
222+ spy . mockRestore ( ) ;
223+ } ) ;
224+
225+ it ( 'should correctly convert _allowCredentials id to Int8Array' , ( ) => {
226+ const metadata : any = {
227+ _action : 'webauthn_authentication' ,
228+ challenge : 'JEisuqkVMhI490jM0/iEgrRz+j94OoGc7gdY4gYicSk=' ,
229+ relyingPartyId : '' ,
230+ _allowCredentials : [
231+ {
232+ type : 'public-key' ,
233+ id : [ 1 , 2 , 3 , 4 ] ,
234+ transports : [ 'usb' ] ,
235+ } ,
236+ ] ,
237+ timeout : 60000 ,
238+ } ;
239+
240+ const publicKey = FRWebAuthn . createAuthenticationPublicKey ( metadata ) ;
241+
242+ expect ( publicKey . allowCredentials ) . toBeDefined ( ) ;
243+ expect ( publicKey . allowCredentials ! [ 0 ] . id ) . toBeInstanceOf ( Int8Array ) ;
244+ const idArray = publicKey . allowCredentials ! [ 0 ] . id as Int8Array ;
245+ expect ( Array . from ( idArray ) ) . toEqual ( [ 1 , 2 , 3 , 4 ] ) ;
246+ } ) ;
213247} ) ;
Original file line number Diff line number Diff line change @@ -469,7 +469,7 @@ abstract class FRWebAuthn {
469469 options : PublicKeyCredentialCreationOptions ,
470470 ) : Promise < PublicKeyCredential | null > {
471471 // Feature check before we attempt registering a device
472- if ( this . isWebAuthnSupported ( ) ) {
472+ if ( ! this . isWebAuthnSupported ( ) ) {
473473 const e = new Error ( 'PublicKeyCredential not supported by this browser' ) ;
474474 e . name = WebAuthnOutcomeType . NotSupportedError ;
475475 throw e ;
@@ -534,7 +534,14 @@ abstract class FRWebAuthn {
534534 // Use the structured _allowCredentials if available, otherwise parse the string format
535535 let allowCredentialsValue : PublicKeyCredentialDescriptor [ ] | undefined ;
536536 if ( _allowCredentials && Array . isArray ( _allowCredentials ) ) {
537- allowCredentialsValue = _allowCredentials ;
537+ // The incoming _allowCredentials entries have an `id` property of type `Array`, which is rejected by `navigator.credentials.get()`.
538+ // Converting it to a TypedArray here to meet the spec (https://developer.mozilla.org/en-US/docs/Web/API/PublicKeyCredentialRequestOptions#id).
539+ allowCredentialsValue = _allowCredentials . map ( ( cred ) => {
540+ return {
541+ ...cred ,
542+ id : new Int8Array ( cred . id as unknown as number [ ] ) ,
543+ } ;
544+ } ) ;
538545 } else {
539546 allowCredentialsValue = parseCredentials ( allowCredentials || acceptableCredentials || '' ) ;
540547 }
You can’t perform that action at this time.
0 commit comments