diff options
author | Antoine Cormouls <contact.antoine.cormouls@gmail.com> | 2020-12-07 09:11:40 +0100 |
---|---|---|
committer | Antoine Cormouls <contact.antoine.cormouls@gmail.com> | 2020-12-07 09:11:40 +0100 |
commit | 106e05f4294aabed133a26efb31252628fffec93 (patch) | |
tree | 596112b13f0ae40521d8e589fbae7e47e1814ef2 /packages/browser/src | |
parent | e2f91c5facf90a43c3c017ace1200a6c5eeb0ae6 (diff) |
review fixes
Diffstat (limited to 'packages/browser/src')
-rw-r--r-- | packages/browser/src/methods/startAssertion.test.ts | 50 | ||||
-rw-r--r-- | packages/browser/src/methods/startAssertion.ts | 14 |
2 files changed, 46 insertions, 18 deletions
diff --git a/packages/browser/src/methods/startAssertion.test.ts b/packages/browser/src/methods/startAssertion.test.ts index 656c419..0a8a16f 100644 --- a/packages/browser/src/methods/startAssertion.test.ts +++ b/packages/browser/src/methods/startAssertion.test.ts @@ -45,6 +45,12 @@ const goodOpts3: PublicKeyCredentialRequestOptionsJSON = { timeout: 1, }; +const goodOpts4: PublicKeyCredentialRequestOptionsJSON = { + challenge: bufferToBase64URLString(toUint8Array('fizz')), + timeout: 1, + allowCredentials: [], +}; + beforeEach(() => { mockNavigatorGet.mockReset(); mockSupportsWebauthn.mockReset(); @@ -65,24 +71,44 @@ test('should convert options before passing to navigator.credentials.get(...)', }, ); - const checkWithOpts = async (opts: PublicKeyCredentialRequestOptionsJSON) => { - await startAssertion(opts); - - const argsPublicKey = mockNavigatorGet.mock.calls[0][0].publicKey; - const credId = argsPublicKey.allowCredentials[0].id; + await startAssertion(goodOpts1); - expect(new Uint8Array(argsPublicKey.challenge)).toEqual(new Uint8Array([102, 105, 122, 122])); - // Make sure the credential ID is an ArrayBuffer with a length of 64 - expect(credId instanceof ArrayBuffer).toEqual(true); - expect(credId.byteLength).toEqual(64); - }; + const argsPublicKey = mockNavigatorGet.mock.calls[0][0].publicKey; + const credId = argsPublicKey.allowCredentials[0].id; - await checkWithOpts(goodOpts1); - await checkWithOpts(goodOpts3); + expect(new Uint8Array(argsPublicKey.challenge)).toEqual(new Uint8Array([102, 105, 122, 122])); + // Make sure the credential ID is an ArrayBuffer with a length of 64 + expect(credId instanceof ArrayBuffer).toEqual(true); + expect(credId.byteLength).toEqual(64); done(); }); +test('should support optional allowCredential', async () => { + mockSupportsWebauthn.mockReturnValue(true); + + // Stub out a response so the method won't throw + mockNavigatorGet.mockImplementation( + (): Promise<any> => { + return new Promise(resolve => { + resolve({ + response: {}, + getClientExtensionResults: () => ({}), + }); + }); + }, + ); + + await startAssertion(goodOpts3); + let allowCredentials = mockNavigatorGet.mock.calls[0][0].allowCredentials; + expect(allowCredentials).toEqual(undefined); + + // Should convert empty array to undefined + await startAssertion(goodOpts4); + allowCredentials = mockNavigatorGet.mock.calls[1][0].allowCredentials; + expect(allowCredentials).toEqual(undefined); +}); + test('should return base64url-encoded response values', async done => { mockSupportsWebauthn.mockReturnValue(true); diff --git a/packages/browser/src/methods/startAssertion.ts b/packages/browser/src/methods/startAssertion.ts index fbc6f59..e02e577 100644 --- a/packages/browser/src/methods/startAssertion.ts +++ b/packages/browser/src/methods/startAssertion.ts @@ -21,16 +21,18 @@ export default async function startAssertion( throw new Error('WebAuthn is not supported in this browser'); } + // We need to avoid passing empty array to avoid blocking retrieval + // of public key + let allowCredentials; + if (requestOptionsJSON.allowCredentials?.length !== 0) { + allowCredentials = requestOptionsJSON.allowCredentials?.map(toPublicKeyCredentialDescriptor); + } + // We need to convert some values to Uint8Arrays before passing the credentials to the navigator const publicKey: PublicKeyCredentialRequestOptions = { ...requestOptionsJSON, challenge: base64URLStringToBuffer(requestOptionsJSON.challenge), - // We need to avoid passing empty array to avoid blocking retrieval - // of public key - allowCredentials: - requestOptionsJSON.allowCredentials?.length === 0 - ? undefined - : requestOptionsJSON.allowCredentials?.map(toPublicKeyCredentialDescriptor), + allowCredentials, }; // Wait for the user to complete assertion |