Skip to content

Commit 9fe007e

Browse files
authored
Merge pull request #582 from thomas-schofield-fr/webauthn-additional-fix
fix: for issues with _allowCredentials and device registration
2 parents c4b719e + ad9026a commit 9fe007e

2 files changed

Lines changed: 43 additions & 2 deletions

File tree

packages/javascript-sdk/src/fr-webauthn/fr-webauthn.test.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff 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
});

packages/javascript-sdk/src/fr-webauthn/index.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff 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
}

0 commit comments

Comments
 (0)