summaryrefslogtreecommitdiffhomepage
path: root/packages/browser/src
diff options
context:
space:
mode:
Diffstat (limited to 'packages/browser/src')
-rw-r--r--packages/browser/src/helpers/__mocks__/browserSupportsWebauthn.ts (renamed from packages/browser/src/helpers/__mocks__/supportsWebauthn.ts)2
-rw-r--r--packages/browser/src/helpers/browserSupportsWebauthn.test.ts (renamed from packages/browser/src/helpers/supportsWebauthn.test.ts)8
-rw-r--r--packages/browser/src/helpers/browserSupportsWebauthn.ts (renamed from packages/browser/src/helpers/supportsWebauthn.ts)2
-rw-r--r--packages/browser/src/helpers/platformAuthenticatorIsAvailable.test.ts26
-rw-r--r--packages/browser/src/helpers/platformAuthenticatorIsAvailable.ts9
-rw-r--r--packages/browser/src/index.test.ts8
-rw-r--r--packages/browser/src/index.ts10
-rw-r--r--packages/browser/src/methods/startAuthentication.test.ts6
-rw-r--r--packages/browser/src/methods/startAuthentication.ts4
-rw-r--r--packages/browser/src/methods/startRegistration.test.ts6
-rw-r--r--packages/browser/src/methods/startRegistration.ts4
11 files changed, 65 insertions, 20 deletions
diff --git a/packages/browser/src/helpers/__mocks__/supportsWebauthn.ts b/packages/browser/src/helpers/__mocks__/browserSupportsWebauthn.ts
index b6b47d4..20d5d88 100644
--- a/packages/browser/src/helpers/__mocks__/supportsWebauthn.ts
+++ b/packages/browser/src/helpers/__mocks__/browserSupportsWebauthn.ts
@@ -1,2 +1,2 @@
// We just need a simple mock so we can control whether this returns `true` or `false`
-export default jest.fn();
+export const browserSupportsWebauthn = jest.fn();
diff --git a/packages/browser/src/helpers/supportsWebauthn.test.ts b/packages/browser/src/helpers/browserSupportsWebauthn.test.ts
index e11d77b..5308315 100644
--- a/packages/browser/src/helpers/supportsWebauthn.test.ts
+++ b/packages/browser/src/helpers/browserSupportsWebauthn.test.ts
@@ -1,4 +1,4 @@
-import supportsWebauthn from './supportsWebauthn';
+import { browserSupportsWebauthn } from './browserSupportsWebauthn';
beforeEach(() => {
// @ts-ignore 2741
@@ -6,12 +6,12 @@ beforeEach(() => {
});
test('should return true when browser supports WebAuthn', () => {
- expect(supportsWebauthn()).toBe(true);
+ expect(browserSupportsWebauthn()).toBe(true);
});
test('should return false when browser does not support WebAuthn', () => {
delete (window as any).PublicKeyCredential;
- expect(supportsWebauthn()).toBe(false);
+ expect(browserSupportsWebauthn()).toBe(false);
});
test('should return false when window is undefined', () => {
@@ -20,7 +20,7 @@ test('should return false when window is undefined', () => {
windowSpy.mockImplementation(() => undefined);
expect(window).toBe(undefined);
- expect(supportsWebauthn()).toBe(false);
+ expect(browserSupportsWebauthn()).toBe(false);
// Restore original window value.
windowSpy.mockRestore();
diff --git a/packages/browser/src/helpers/supportsWebauthn.ts b/packages/browser/src/helpers/browserSupportsWebauthn.ts
index b572080..030256f 100644
--- a/packages/browser/src/helpers/supportsWebauthn.ts
+++ b/packages/browser/src/helpers/browserSupportsWebauthn.ts
@@ -1,7 +1,7 @@
/**
* Determine if the browser is capable of Webauthn
*/
-export default function supportsWebauthn(): boolean {
+export function browserSupportsWebauthn(): boolean {
return (
window?.PublicKeyCredential !== undefined && typeof window.PublicKeyCredential === 'function'
);
diff --git a/packages/browser/src/helpers/platformAuthenticatorIsAvailable.test.ts b/packages/browser/src/helpers/platformAuthenticatorIsAvailable.test.ts
new file mode 100644
index 0000000..ba9f233
--- /dev/null
+++ b/packages/browser/src/helpers/platformAuthenticatorIsAvailable.test.ts
@@ -0,0 +1,26 @@
+import { platformAuthenticatorIsAvailable } from './platformAuthenticatorIsAvailable';
+
+const mockIsUVPAA = jest.fn();
+
+beforeEach(() => {
+ mockIsUVPAA.mockReset();
+
+ // @ts-ignore 2741
+ window.PublicKeyCredential = {
+ isUserVerifyingPlatformAuthenticatorAvailable: mockIsUVPAA.mockResolvedValue(true),
+ };
+});
+
+test('should return true when platform authenticator is available', async () => {
+ const isAvailable = await platformAuthenticatorIsAvailable();
+
+ expect(isAvailable).toEqual(true);
+});
+
+test('should return false when platform authenticator is unavailable', async () => {
+ mockIsUVPAA.mockResolvedValue(false);
+
+ const isAvailable = await platformAuthenticatorIsAvailable();
+
+ expect(isAvailable).toEqual(false);
+});
diff --git a/packages/browser/src/helpers/platformAuthenticatorIsAvailable.ts b/packages/browser/src/helpers/platformAuthenticatorIsAvailable.ts
new file mode 100644
index 0000000..10d84e3
--- /dev/null
+++ b/packages/browser/src/helpers/platformAuthenticatorIsAvailable.ts
@@ -0,0 +1,9 @@
+/**
+ * Determine whether the browser can communicate with a built-in authenticator, like
+ * Touch ID, Android fingerprint scanner, or Windows Hello.
+ *
+ * This method will _not_ be able to tell you the name of the platform authenticator.
+ */
+export async function platformAuthenticatorIsAvailable(): Promise<boolean> {
+ return PublicKeyCredential.isUserVerifyingPlatformAuthenticatorAvailable();
+}
diff --git a/packages/browser/src/index.test.ts b/packages/browser/src/index.test.ts
index ffd3b2b..e82191c 100644
--- a/packages/browser/src/index.test.ts
+++ b/packages/browser/src/index.test.ts
@@ -8,6 +8,10 @@ test('should export method `startAuthentication`', () => {
expect(index.startAuthentication).toBeDefined();
});
-test('should export method `supportsWebauthn`', () => {
- expect(index.supportsWebauthn).toBeDefined();
+test('should export method `browserSupportsWebauthn`', () => {
+ expect(index.browserSupportsWebauthn).toBeDefined();
+});
+
+test('should export method `platformAuthenticatorIsAvailable`', () => {
+ expect(index.browserSupportsWebauthn).toBeDefined();
});
diff --git a/packages/browser/src/index.ts b/packages/browser/src/index.ts
index 520af9a..4c83297 100644
--- a/packages/browser/src/index.ts
+++ b/packages/browser/src/index.ts
@@ -4,6 +4,12 @@
*/
import startRegistration from './methods/startRegistration';
import startAuthentication from './methods/startAuthentication';
-import supportsWebauthn from './helpers/supportsWebauthn';
+import { browserSupportsWebauthn } from './helpers/browserSupportsWebauthn';
+import { platformAuthenticatorIsAvailable } from './helpers/platformAuthenticatorIsAvailable';
-export { startRegistration, startAuthentication, supportsWebauthn };
+export {
+ startRegistration,
+ startAuthentication,
+ browserSupportsWebauthn,
+ platformAuthenticatorIsAvailable,
+};
diff --git a/packages/browser/src/methods/startAuthentication.test.ts b/packages/browser/src/methods/startAuthentication.test.ts
index 96b140c..acd97c3 100644
--- a/packages/browser/src/methods/startAuthentication.test.ts
+++ b/packages/browser/src/methods/startAuthentication.test.ts
@@ -5,16 +5,16 @@ import {
AuthenticationExtensionsClientOutputs,
} from '@simplewebauthn/typescript-types';
-import supportsWebauthn from '../helpers/supportsWebauthn';
+import { browserSupportsWebauthn } from '../helpers/browserSupportsWebauthn';
import utf8StringToBuffer from '../helpers/utf8StringToBuffer';
import bufferToBase64URLString from '../helpers/bufferToBase64URLString';
import startAuthentication from './startAuthentication';
-jest.mock('../helpers/supportsWebauthn');
+jest.mock('../helpers/browserSupportsWebauthn');
const mockNavigatorGet = window.navigator.credentials.get as jest.Mock;
-const mockSupportsWebauthn = supportsWebauthn as jest.Mock;
+const mockSupportsWebauthn = browserSupportsWebauthn as jest.Mock;
const mockAuthenticatorData = 'mockAuthenticatorData';
const mockClientDataJSON = 'mockClientDataJSON';
diff --git a/packages/browser/src/methods/startAuthentication.ts b/packages/browser/src/methods/startAuthentication.ts
index 1f431f5..277b8f0 100644
--- a/packages/browser/src/methods/startAuthentication.ts
+++ b/packages/browser/src/methods/startAuthentication.ts
@@ -7,7 +7,7 @@ import {
import bufferToBase64URLString from '../helpers/bufferToBase64URLString';
import base64URLStringToBuffer from '../helpers/base64URLStringToBuffer';
import bufferToUTF8String from '../helpers/bufferToUTF8String';
-import supportsWebauthn from '../helpers/supportsWebauthn';
+import { browserSupportsWebauthn } from '../helpers/browserSupportsWebauthn';
import toPublicKeyCredentialDescriptor from '../helpers/toPublicKeyCredentialDescriptor';
/**
@@ -18,7 +18,7 @@ import toPublicKeyCredentialDescriptor from '../helpers/toPublicKeyCredentialDes
export default async function startAuthentication(
requestOptionsJSON: PublicKeyCredentialRequestOptionsJSON,
): Promise<AuthenticationCredentialJSON> {
- if (!supportsWebauthn()) {
+ if (!browserSupportsWebauthn()) {
throw new Error('WebAuthn is not supported in this browser');
}
diff --git a/packages/browser/src/methods/startRegistration.test.ts b/packages/browser/src/methods/startRegistration.test.ts
index 3567b02..2569a99 100644
--- a/packages/browser/src/methods/startRegistration.test.ts
+++ b/packages/browser/src/methods/startRegistration.test.ts
@@ -6,15 +6,15 @@ import {
} from '@simplewebauthn/typescript-types';
import utf8StringToBuffer from '../helpers/utf8StringToBuffer';
-import supportsWebauthn from '../helpers/supportsWebauthn';
+import { browserSupportsWebauthn } from '../helpers/browserSupportsWebauthn';
import bufferToBase64URLString from '../helpers/bufferToBase64URLString';
import startRegistration from './startRegistration';
-jest.mock('../helpers/supportsWebauthn');
+jest.mock('../helpers/browserSupportsWebauthn');
const mockNavigatorCreate = window.navigator.credentials.create as jest.Mock;
-const mockSupportsWebauthn = supportsWebauthn as jest.Mock;
+const mockSupportsWebauthn = browserSupportsWebauthn as jest.Mock;
const mockAttestationObject = 'mockAtte';
const mockClientDataJSON = 'mockClie';
diff --git a/packages/browser/src/methods/startRegistration.ts b/packages/browser/src/methods/startRegistration.ts
index 1bb5c34..eec07c5 100644
--- a/packages/browser/src/methods/startRegistration.ts
+++ b/packages/browser/src/methods/startRegistration.ts
@@ -7,7 +7,7 @@ import {
import utf8StringToBuffer from '../helpers/utf8StringToBuffer';
import bufferToBase64URLString from '../helpers/bufferToBase64URLString';
import base64URLStringToBuffer from '../helpers/base64URLStringToBuffer';
-import supportsWebauthn from '../helpers/supportsWebauthn';
+import { browserSupportsWebauthn } from '../helpers/browserSupportsWebauthn';
import toPublicKeyCredentialDescriptor from '../helpers/toPublicKeyCredentialDescriptor';
/**
@@ -18,7 +18,7 @@ import toPublicKeyCredentialDescriptor from '../helpers/toPublicKeyCredentialDes
export default async function startRegistration(
creationOptionsJSON: PublicKeyCredentialCreationOptionsJSON,
): Promise<RegistrationCredentialJSON> {
- if (!supportsWebauthn()) {
+ if (!browserSupportsWebauthn()) {
throw new Error('WebAuthn is not supported in this browser');
}