summaryrefslogtreecommitdiffhomepage
path: root/packages/browser/src
diff options
context:
space:
mode:
authorMatthew Miller <matthew@millerti.me>2020-12-08 10:11:33 -0800
committerGitHub <noreply@github.com>2020-12-08 10:11:33 -0800
commit58369389fa1de782d7e3764a9844124c9fcf12eb (patch)
tree6b0cddef05f798366b7154caa8d24a00e3a8c230 /packages/browser/src
parent4f1a0ab1225a34788d3c1f9bf4a9c0b7cc31b17b (diff)
parentf5a8b982440774710ee8e2ac87d7b782ba98f4f6 (diff)
Merge pull request #79 from Moumouls/moumouls/allowCredentials
Optional Allow Credential
Diffstat (limited to 'packages/browser/src')
-rw-r--r--packages/browser/src/methods/startAssertion.test.ts46
-rw-r--r--packages/browser/src/methods/startAssertion.ts9
2 files changed, 54 insertions, 1 deletions
diff --git a/packages/browser/src/methods/startAssertion.test.ts b/packages/browser/src/methods/startAssertion.test.ts
index 996f66a..c74b88f 100644
--- a/packages/browser/src/methods/startAssertion.test.ts
+++ b/packages/browser/src/methods/startAssertion.test.ts
@@ -72,6 +72,52 @@ test('should convert options before passing to navigator.credentials.get(...)',
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({
+ challenge: bufferToBase64URLString(toUint8Array('fizz')),
+ timeout: 1,
+ });
+
+ expect(mockNavigatorGet.mock.calls[0][0].allowCredentials).toEqual(undefined);
+});
+
+test('should convert allow allowCredential to undefined when empty', 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({
+ challenge: bufferToBase64URLString(toUint8Array('fizz')),
+ timeout: 1,
+ allowCredentials: [],
+ });
+ expect(mockNavigatorGet.mock.calls[0][0].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 09e416f..e02e577 100644
--- a/packages/browser/src/methods/startAssertion.ts
+++ b/packages/browser/src/methods/startAssertion.ts
@@ -21,11 +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),
- allowCredentials: requestOptionsJSON.allowCredentials.map(toPublicKeyCredentialDescriptor),
+ allowCredentials,
};
// Wait for the user to complete assertion