summaryrefslogtreecommitdiffhomepage
path: root/packages/browser/src
diff options
context:
space:
mode:
Diffstat (limited to 'packages/browser/src')
-rw-r--r--packages/browser/src/helpers/__jest__/generateCustomError.ts17
-rw-r--r--packages/browser/src/helpers/identifyAuthenticationError.ts16
-rw-r--r--packages/browser/src/helpers/identifyRegistrationError.ts28
-rw-r--r--packages/browser/src/helpers/structs.ts4
-rw-r--r--packages/browser/src/helpers/toPublicKeyCredentialDescriptor.ts6
-rw-r--r--packages/browser/src/methods/startAuthentication.test.ts52
-rw-r--r--packages/browser/src/methods/startRegistration.test.ts97
-rw-r--r--packages/browser/src/setupTests.ts61
8 files changed, 164 insertions, 117 deletions
diff --git a/packages/browser/src/helpers/__jest__/generateCustomError.ts b/packages/browser/src/helpers/__jest__/generateCustomError.ts
new file mode 100644
index 0000000..f4b3250
--- /dev/null
+++ b/packages/browser/src/helpers/__jest__/generateCustomError.ts
@@ -0,0 +1,17 @@
+/**
+ * Create "custom errors" to help emulate WebAuthn API errors
+*/
+type WebAuthnErrorName =
+ 'AbortError'
+ | 'ConstraintError'
+ | 'InvalidStateError'
+ | 'NotAllowedError'
+ | 'NotSupportedError'
+ | 'SecurityError'
+ | 'UnknownError';
+
+export function generateCustomError(name: WebAuthnErrorName): Error {
+ const customError = new Error();
+ customError.name = name;
+ return customError;
+}
diff --git a/packages/browser/src/helpers/identifyAuthenticationError.ts b/packages/browser/src/helpers/identifyAuthenticationError.ts
index 7f9bd82..c994947 100644
--- a/packages/browser/src/helpers/identifyAuthenticationError.ts
+++ b/packages/browser/src/helpers/identifyAuthenticationError.ts
@@ -20,38 +20,42 @@ export function identifyAuthenticationError({
if (error.name === 'AbortError') {
if (options.signal === new AbortController().signal) {
// https://www.w3.org/TR/webauthn-2/#sctn-createCredential (Step 16)
- return new WebAuthnError('Authentication ceremony was sent an abort signal (AbortError)');
+ return new WebAuthnError('Authentication ceremony was sent an abort signal', 'AbortError');
}
} else if (error.name === 'NotAllowedError') {
if (publicKey.allowCredentials?.length) {
// https://www.w3.org/TR/webauthn-2/#sctn-discover-from-external-source (Step 17)
// https://www.w3.org/TR/webauthn-2/#sctn-op-get-assertion (Step 6)
return new WebAuthnError(
- 'No available authenticator recognized any of the allowed credentials (NotAllowedError)',
+ 'No available authenticator recognized any of the allowed credentials',
+ 'NotAllowedError',
);
}
// https://www.w3.org/TR/webauthn-2/#sctn-discover-from-external-source (Step 18)
// https://www.w3.org/TR/webauthn-2/#sctn-op-get-assertion (Step 7)
return new WebAuthnError(
- 'User clicked cancel, or the authentication ceremony timed out (NotAllowedError)',
+ 'User clicked cancel, or the authentication ceremony timed out',
+ 'NotAllowedError',
);
} else if (error.name === 'SecurityError') {
const effectiveDomain = window.location.hostname;
if (!isValidDomain(effectiveDomain)) {
// https://www.w3.org/TR/webauthn-2/#sctn-discover-from-external-source (Step 5)
- return new WebAuthnError(`${window.location.hostname} is an invalid domain (SecurityError)`);
+ return new WebAuthnError(`${window.location.hostname} is an invalid domain`, 'SecurityError');
} else if (publicKey.rpId !== effectiveDomain) {
// https://www.w3.org/TR/webauthn-2/#sctn-discover-from-external-source (Step 6)
return new WebAuthnError(
- `The RP ID "${publicKey.rpId}" is invalid for this domain (SecurityError)`,
+ `The RP ID "${publicKey.rpId}" is invalid for this domain`,
+ 'SecurityError',
);
}
} else if (error.name === 'UnknownError') {
// https://www.w3.org/TR/webauthn-2/#sctn-op-get-assertion (Step 1)
// https://www.w3.org/TR/webauthn-2/#sctn-op-get-assertion (Step 12)
return new WebAuthnError(
- 'The authenticator was unable to process the specified options, or could not create a new assertion signature (UnknownError)',
+ 'The authenticator was unable to process the specified options, or could not create a new assertion signature',
+ 'UnknownError',
);
}
diff --git a/packages/browser/src/helpers/identifyRegistrationError.ts b/packages/browser/src/helpers/identifyRegistrationError.ts
index 544953b..5b560e1 100644
--- a/packages/browser/src/helpers/identifyRegistrationError.ts
+++ b/packages/browser/src/helpers/identifyRegistrationError.ts
@@ -20,29 +20,31 @@ export function identifyRegistrationError({
if (error.name === 'AbortError') {
if (options.signal === new AbortController().signal) {
// https://www.w3.org/TR/webauthn-2/#sctn-createCredential (Step 16)
- return new WebAuthnError('Registration ceremony was sent an abort signal');
+ return new WebAuthnError('Registration ceremony was sent an abort signal', 'AbortError');
}
} else if (error.name === 'ConstraintError') {
if (publicKey.authenticatorSelection?.requireResidentKey === true) {
// https://www.w3.org/TR/webauthn-2/#sctn-op-make-cred (Step 4)
return new WebAuthnError(
- 'Discoverable credentials were required but no available authenticator supported it (ConstraintError)',
+ 'Discoverable credentials were required but no available authenticator supported it',
+ 'ConstraintError'
);
} else if (publicKey.authenticatorSelection?.userVerification === 'required') {
// https://www.w3.org/TR/webauthn-2/#sctn-op-make-cred (Step 5)
return new WebAuthnError(
- 'User verification was required but no available authenticator supported it (ConstraintError)',
+ 'User verification was required but no available authenticator supported it',
+ 'ConstraintError'
);
}
} else if (error.name === 'InvalidStateError') {
// https://www.w3.org/TR/webauthn-2/#sctn-createCredential (Step 20)
// https://www.w3.org/TR/webauthn-2/#sctn-op-make-cred (Step 3)
- return new WebAuthnError('The authenticator was previously registered (InvalidStateError)');
+ return new WebAuthnError('The authenticator was previously registered', 'InvalidStateError');
} else if (error.name === 'NotAllowedError') {
// https://www.w3.org/TR/webauthn-2/#sctn-createCredential (Step 20)
// https://www.w3.org/TR/webauthn-2/#sctn-createCredential (Step 21)
return new WebAuthnError(
- 'User clicked cancel, or the registration ceremony timed out (NotAllowedError)',
+ 'User clicked cancel, or the registration ceremony timed out', 'NotAllowedError'
);
} else if (error.name === 'NotSupportedError') {
const validPubKeyCredParams = publicKey.pubKeyCredParams.filter(
@@ -52,35 +54,39 @@ export function identifyRegistrationError({
if (validPubKeyCredParams.length === 0) {
// https://www.w3.org/TR/webauthn-2/#sctn-createCredential (Step 10)
return new WebAuthnError(
- 'No entry in pubKeyCredParams was of type "public-key" (NotSupportedError)',
+ 'No entry in pubKeyCredParams was of type "public-key"',
+ 'NotSupportedError'
);
}
// https://www.w3.org/TR/webauthn-2/#sctn-op-make-cred (Step 2)
return new WebAuthnError(
- 'No available authenticator supported any of the specified pubKeyCredParams algorithms (NotSupportedError)',
+ 'No available authenticator supported any of the specified pubKeyCredParams algorithms',
+ 'NotSupportedError'
);
} else if (error.name === 'SecurityError') {
const effectiveDomain = window.location.hostname;
if (!isValidDomain(effectiveDomain)) {
// https://www.w3.org/TR/webauthn-2/#sctn-createCredential (Step 7)
- return new WebAuthnError(`${window.location.hostname} is an invalid domain (SecurityError)`);
+ return new WebAuthnError(`${window.location.hostname} is an invalid domain`, 'SecurityError');
} else if (publicKey.rp.id !== effectiveDomain) {
// https://www.w3.org/TR/webauthn-2/#sctn-createCredential (Step 8)
return new WebAuthnError(
- `The RP ID "${publicKey.rp.id}" is invalid for this domain (SecurityError)`,
+ `The RP ID "${publicKey.rp.id}" is invalid for this domain`,
+ 'SecurityError'
);
}
} else if (error.name === 'TypeError') {
if (publicKey.user.id.byteLength < 1 || publicKey.user.id.byteLength > 64) {
// https://www.w3.org/TR/webauthn-2/#sctn-createCredential (Step 5)
- return new WebAuthnError('User ID was not between 1 and 64 characters (TypeError)');
+ return new WebAuthnError('User ID was not between 1 and 64 characters', 'TypeError');
}
} else if (error.name === 'UnknownError') {
// https://www.w3.org/TR/webauthn-2/#sctn-op-make-cred (Step 1)
// https://www.w3.org/TR/webauthn-2/#sctn-op-make-cred (Step 8)
return new WebAuthnError(
- 'The authenticator was unable to process the specified options, or could not create a new credential (UnknownError)',
+ 'The authenticator was unable to process the specified options, or could not create a new credential',
+ 'UnknownError'
);
}
diff --git a/packages/browser/src/helpers/structs.ts b/packages/browser/src/helpers/structs.ts
index 66b6d63..8ae01b7 100644
--- a/packages/browser/src/helpers/structs.ts
+++ b/packages/browser/src/helpers/structs.ts
@@ -16,8 +16,8 @@
* scenarios a given error would be raised.
*/
export class WebAuthnError extends Error {
- constructor(message: string) {
+ constructor(message: string, name = 'WebAuthnError') {
super(message);
- this.name = 'WebAuthnError';
+ this.name = name;
}
}
diff --git a/packages/browser/src/helpers/toPublicKeyCredentialDescriptor.ts b/packages/browser/src/helpers/toPublicKeyCredentialDescriptor.ts
index 8fad78b..82b6ac1 100644
--- a/packages/browser/src/helpers/toPublicKeyCredentialDescriptor.ts
+++ b/packages/browser/src/helpers/toPublicKeyCredentialDescriptor.ts
@@ -10,5 +10,11 @@ export default function toPublicKeyCredentialDescriptor(
return {
...descriptor,
id: base64URLStringToBuffer(id),
+ /**
+ * `descriptor.transports` is an array of our `AuthenticatorTransport` that includes newer
+ * transports that TypeScript's DOM lib is ignorant of. Convince TS that our list of transports
+ * are fine to pass to WebAuthn since browsers will recognize the new value.
+ */
+ transports: descriptor.transports as AuthenticatorTransport[],
};
}
diff --git a/packages/browser/src/methods/startAuthentication.test.ts b/packages/browser/src/methods/startAuthentication.test.ts
index 4be0ad6..658b67c 100644
--- a/packages/browser/src/methods/startAuthentication.test.ts
+++ b/packages/browser/src/methods/startAuthentication.test.ts
@@ -9,6 +9,7 @@ import { browserSupportsWebauthn } from '../helpers/browserSupportsWebauthn';
import utf8StringToBuffer from '../helpers/utf8StringToBuffer';
import bufferToBase64URLString from '../helpers/bufferToBase64URLString';
import { WebAuthnError } from '../helpers/structs';
+import { generateCustomError } from '../helpers/__jest__/generateCustomError';
import startAuthentication from './startAuthentication';
@@ -201,8 +202,27 @@ test('should include extension results when no extensions specified', async () =
expect(response.clientExtensionResults).toEqual({});
});
+test('should support "cable" transport', async () => {
+ const opts: PublicKeyCredentialRequestOptionsJSON = {
+ ...goodOpts1,
+ allowCredentials: [
+ {
+ ...goodOpts1.allowCredentials![0],
+ transports: ["cable"],
+ },
+ ]
+ };
+
+ await startAuthentication(opts);
+
+ expect(mockNavigatorGet.mock.calls[0][0].publicKey.allowCredentials[0].transports[0])
+ .toEqual("cable");
+});
+
describe('WebAuthnError', () => {
describe('AbortError', () => {
+ const AbortError = generateCustomError('AbortError');
+
/**
* We can't actually test this because nothing in startAuthentication() propagates the abort
* signal. But if you invoked WebAuthn via this and then manually sent an abort signal I guess
@@ -211,27 +231,29 @@ describe('WebAuthnError', () => {
* As a matter of fact I couldn't actually get any browser to respect the abort signal...
*/
test.skip('should identify abort signal', async () => {
- mockNavigatorGet.mockRejectedValueOnce(new AbortError());
+ mockNavigatorGet.mockRejectedValueOnce(AbortError);
const rejected = await expect(startAuthentication(goodOpts1)).rejects;
rejected.toThrow(WebAuthnError);
rejected.toThrow(/abort signal/i);
- rejected.toThrow(/AbortError/);
+ rejected.toHaveProperty('name', 'AbortError');
});
});
describe('NotAllowedError', () => {
+ const NotAllowedError = generateCustomError('NotAllowedError');
+
test('should identify unrecognized allowed credentials', async () => {
- mockNavigatorGet.mockRejectedValueOnce(new NotAllowedError());
+ mockNavigatorGet.mockRejectedValueOnce(NotAllowedError);
const rejected = await expect(startAuthentication(goodOpts1)).rejects;
rejected.toThrow(WebAuthnError);
rejected.toThrow(/allowed credentials/i);
- rejected.toThrow(/NotAllowedError/);
+ rejected.toHaveProperty('name', 'NotAllowedError');
});
test('should identify cancellation or timeout', async () => {
- mockNavigatorGet.mockRejectedValueOnce(new NotAllowedError());
+ mockNavigatorGet.mockRejectedValueOnce(NotAllowedError);
const opts = {
...goodOpts1,
@@ -242,11 +264,13 @@ describe('WebAuthnError', () => {
rejected.toThrow(WebAuthnError);
rejected.toThrow(/cancel/i);
rejected.toThrow(/timed out/i);
- rejected.toThrow(/NotAllowedError/);
+ rejected.toHaveProperty('name', 'NotAllowedError');
});
});
describe('SecurityError', () => {
+ const SecurityError = generateCustomError('SecurityError');
+
let _originalHostName: string;
beforeEach(() => {
@@ -260,38 +284,40 @@ describe('WebAuthnError', () => {
test('should identify invalid domain', async () => {
window.location.hostname = '1.2.3.4';
- mockNavigatorGet.mockRejectedValueOnce(new SecurityError());
+ mockNavigatorGet.mockRejectedValueOnce(SecurityError);
const rejected = await expect(startAuthentication(goodOpts1)).rejects;
rejected.toThrowError(WebAuthnError);
rejected.toThrow(/1\.2\.3\.4/);
rejected.toThrow(/invalid domain/i);
- rejected.toThrow(/SecurityError/);
+ rejected.toHaveProperty('name', 'SecurityError');
});
test('should identify invalid RP ID', async () => {
window.location.hostname = 'simplewebauthn.com';
- mockNavigatorGet.mockRejectedValueOnce(new SecurityError());
+ mockNavigatorGet.mockRejectedValueOnce(SecurityError);
const rejected = await expect(startAuthentication(goodOpts1)).rejects;
rejected.toThrowError(WebAuthnError);
rejected.toThrow(goodOpts1.rpId);
rejected.toThrow(/invalid for this domain/i);
- rejected.toThrow(/SecurityError/);
+ rejected.toHaveProperty('name', 'SecurityError');
});
});
describe('UnknownError', () => {
+ const UnknownError = generateCustomError('UnknownError');
+
test('should identify potential authenticator issues', async () => {
- mockNavigatorGet.mockRejectedValueOnce(new UnknownError());
+ mockNavigatorGet.mockRejectedValueOnce(UnknownError);
const rejected = await expect(startAuthentication(goodOpts1)).rejects;
rejected.toThrow(WebAuthnError);
rejected.toThrow(/authenticator/i);
rejected.toThrow(/unable to process the specified options/i);
- rejected.toThrow(/could not create a new assertion signature /i);
- rejected.toThrow(/UnknownError/);
+ rejected.toThrow(/could not create a new assertion signature/i);
+ rejected.toHaveProperty('name', 'UnknownError');
});
});
});
diff --git a/packages/browser/src/methods/startRegistration.test.ts b/packages/browser/src/methods/startRegistration.test.ts
index 78b0157..fcd4a2c 100644
--- a/packages/browser/src/methods/startRegistration.test.ts
+++ b/packages/browser/src/methods/startRegistration.test.ts
@@ -1,15 +1,16 @@
import {
- RegistrationCredential,
AuthenticationExtensionsClientInputs,
AuthenticationExtensionsClientOutputs,
PublicKeyCredentialCreationOptionsJSON,
+ RegistrationCredential,
} from '@simplewebauthn/typescript-types';
-
-import utf8StringToBuffer from '../helpers/utf8StringToBuffer';
+import { generateCustomError } from '../helpers/__jest__/generateCustomError';
import { browserSupportsWebauthn } from '../helpers/browserSupportsWebauthn';
import bufferToBase64URLString from '../helpers/bufferToBase64URLString';
import { WebAuthnError } from '../helpers/structs';
+import utf8StringToBuffer from '../helpers/utf8StringToBuffer';
+
import startRegistration from './startRegistration';
jest.mock('../helpers/browserSupportsWebauthn');
@@ -104,7 +105,7 @@ test('should return base64url-encoded response values', async () => {
expect(response.response.clientDataJSON).toEqual('bW9ja0NsaWU');
});
-test("should throw error if WebAuthn isn't supported", async () => {
+test('should throw error if WebAuthn isn\'t supported', async () => {
mockSupportsWebauthn.mockReturnValue(false);
await expect(startRegistration(goodOpts1)).rejects.toThrow(
@@ -175,8 +176,44 @@ test('should include extension results when no extensions specified', async () =
expect(response.clientExtensionResults).toEqual({});
});
+test('should support "cable" transport in excludeCredentials', async () => {
+ const opts: PublicKeyCredentialCreationOptionsJSON = {
+ ...goodOpts1,
+ excludeCredentials: [
+ {
+ ...goodOpts1.excludeCredentials![0],
+ transports: ["cable"],
+ },
+ ]
+ };
+
+ await startRegistration(opts);
+
+ expect(mockNavigatorCreate.mock.calls[0][0].publicKey.excludeCredentials[0].transports[0])
+ .toEqual("cable");
+});
+
+test('should return "cable" transport from response', async () => {
+ mockNavigatorCreate.mockResolvedValue({
+ id: 'foobar',
+ rawId: utf8StringToBuffer('foobar'),
+ response: {
+ attestationObject: Buffer.from(mockAttestationObject, 'ascii'),
+ clientDataJSON: Buffer.from(mockClientDataJSON, 'ascii'),
+ getTransports: () => (["cable"]),
+ },
+ getClientExtensionResults: () => ({}),
+ type: 'webauthn.create',
+ });
+
+ const response = await startRegistration(goodOpts1);
+
+ expect(response.transports).toEqual(["cable"]);
+});
+
describe('WebAuthnError', () => {
describe('AbortError', () => {
+ const AbortError = generateCustomError('AbortError');
/**
* We can't actually test this because nothing in startRegistration() propagates the abort
* signal. But if you invoked WebAuthn via this and then manually sent an abort signal I guess
@@ -185,7 +222,7 @@ describe('WebAuthnError', () => {
* As a matter of fact I couldn't actually get any browser to respect the abort signal...
*/
test.skip('should identify abort signal', async () => {
- mockNavigatorCreate.mockRejectedValueOnce(new AbortError());
+ mockNavigatorCreate.mockRejectedValueOnce(AbortError);
const rejected = await expect(startRegistration(goodOpts1)).rejects;
rejected.toThrow(WebAuthnError);
@@ -195,8 +232,10 @@ describe('WebAuthnError', () => {
});
describe('ConstraintError', () => {
+ const ConstraintError = generateCustomError('ConstraintError');
+
test('should identify unsupported discoverable credentials', async () => {
- mockNavigatorCreate.mockRejectedValueOnce(new ConstraintError());
+ mockNavigatorCreate.mockRejectedValueOnce(ConstraintError);
const opts: PublicKeyCredentialCreationOptionsJSON = {
...goodOpts1,
@@ -210,11 +249,11 @@ describe('WebAuthnError', () => {
rejected.toThrow(WebAuthnError);
rejected.toThrow(/discoverable credentials were required/i);
rejected.toThrow(/no available authenticator supported/i);
- rejected.toThrow(/ConstraintError/);
+ rejected.toHaveProperty('name', 'ConstraintError');
});
test('should identify unsupported user verification', async () => {
- mockNavigatorCreate.mockRejectedValueOnce(new ConstraintError());
+ mockNavigatorCreate.mockRejectedValueOnce(ConstraintError);
const opts: PublicKeyCredentialCreationOptionsJSON = {
...goodOpts1,
@@ -227,37 +266,43 @@ describe('WebAuthnError', () => {
rejected.toThrow(WebAuthnError);
rejected.toThrow(/user verification was required/i);
rejected.toThrow(/no available authenticator supported/i);
- rejected.toThrow(/ConstraintError/);
+ rejected.toHaveProperty('name', 'ConstraintError');
});
});
describe('InvalidStateError', () => {
+ const InvalidStateError = generateCustomError('InvalidStateError');
+
test('should identify re-registration attempt', async () => {
- mockNavigatorCreate.mockRejectedValueOnce(new InvalidStateError());
+ mockNavigatorCreate.mockRejectedValueOnce(InvalidStateError);
const rejected = await expect(startRegistration(goodOpts1)).rejects;
rejected.toThrow(WebAuthnError);
rejected.toThrow(/authenticator/i);
rejected.toThrow(/previously registered/i);
- rejected.toThrow(/InvalidStateError/);
+ rejected.toHaveProperty('name', 'InvalidStateError');
});
});
describe('NotAllowedError', () => {
+ const NotAllowedError = generateCustomError('NotAllowedError');
+
test('should identify cancellation or timeout', async () => {
- mockNavigatorCreate.mockRejectedValueOnce(new NotAllowedError());
+ mockNavigatorCreate.mockRejectedValueOnce(NotAllowedError);
const rejected = await expect(startRegistration(goodOpts1)).rejects;
rejected.toThrow(WebAuthnError);
rejected.toThrow(/cancel/i);
rejected.toThrow(/timed out/i);
- rejected.toThrow(/NotAllowedError/);
+ rejected.toHaveProperty('name', 'NotAllowedError');
});
});
describe('NotSupportedError', () => {
+ const NotSupportedError = generateCustomError('NotSupportedError');
+
test('should identify missing "public-key" entries in pubKeyCredParams', async () => {
- mockNavigatorCreate.mockRejectedValueOnce(new NotSupportedError());
+ mockNavigatorCreate.mockRejectedValueOnce(NotSupportedError);
const opts = {
...goodOpts1,
@@ -268,11 +313,11 @@ describe('WebAuthnError', () => {
rejected.toThrow(WebAuthnError);
rejected.toThrow(/pubKeyCredParams/i);
rejected.toThrow(/public-key/i);
- rejected.toThrow(/NotSupportedError/);
+ rejected.toHaveProperty('name', 'NotSupportedError');
});
test('should identify no authenticator supports algs in pubKeyCredParams', async () => {
- mockNavigatorCreate.mockRejectedValueOnce(new NotSupportedError());
+ mockNavigatorCreate.mockRejectedValueOnce(NotSupportedError);
const opts: PublicKeyCredentialCreationOptionsJSON = {
...goodOpts1,
@@ -283,11 +328,13 @@ describe('WebAuthnError', () => {
rejected.toThrow(WebAuthnError);
rejected.toThrow(/No available authenticator/i);
rejected.toThrow(/pubKeyCredParams/i);
- rejected.toThrow(/NotSupportedError/);
+ rejected.toHaveProperty('name', 'NotSupportedError');
});
});
describe('SecurityError', () => {
+ const SecurityError = generateCustomError('SecurityError');
+
let _originalHostName: string;
beforeEach(() => {
@@ -301,25 +348,25 @@ describe('WebAuthnError', () => {
test('should identify invalid domain', async () => {
window.location.hostname = '1.2.3.4';
- mockNavigatorCreate.mockRejectedValueOnce(new SecurityError());
+ mockNavigatorCreate.mockRejectedValueOnce(SecurityError);
const rejected = await expect(startRegistration(goodOpts1)).rejects;
rejected.toThrowError(WebAuthnError);
rejected.toThrow(/1\.2\.3\.4/);
rejected.toThrow(/invalid domain/i);
- rejected.toThrow(/SecurityError/);
+ rejected.toHaveProperty('name', 'SecurityError');
});
test('should identify invalid RP ID', async () => {
window.location.hostname = 'simplewebauthn.com';
- mockNavigatorCreate.mockRejectedValueOnce(new SecurityError());
+ mockNavigatorCreate.mockRejectedValueOnce(SecurityError);
const rejected = await expect(startRegistration(goodOpts1)).rejects;
rejected.toThrowError(WebAuthnError);
rejected.toThrow(goodOpts1.rp.id);
rejected.toThrow(/invalid for this domain/i);
- rejected.toThrow(/SecurityError/);
+ rejected.toHaveProperty('name', 'SecurityError');
});
});
@@ -339,20 +386,22 @@ describe('WebAuthnError', () => {
rejected.toThrowError(WebAuthnError);
rejected.toThrow(/user id/i);
rejected.toThrow(/not between 1 and 64 characters/i);
- rejected.toThrow(/TypeError/);
+ rejected.toHaveProperty('name', 'TypeError');
});
});
describe('UnknownError', () => {
+ const UnknownError = generateCustomError('UnknownError');
+
test('should identify potential authenticator issues', async () => {
- mockNavigatorCreate.mockRejectedValueOnce(new UnknownError());
+ mockNavigatorCreate.mockRejectedValueOnce(UnknownError);
const rejected = await expect(startRegistration(goodOpts1)).rejects;
rejected.toThrow(WebAuthnError);
rejected.toThrow(/authenticator/i);
rejected.toThrow(/unable to process the specified options/i);
rejected.toThrow(/could not create a new credential/i);
- rejected.toThrow(/UnknownError/);
+ rejected.toHaveProperty('name', 'UnknownError');
});
});
});
diff --git a/packages/browser/src/setupTests.ts b/packages/browser/src/setupTests.ts
index fda8840..5b6efcf 100644
--- a/packages/browser/src/setupTests.ts
+++ b/packages/browser/src/setupTests.ts
@@ -24,64 +24,3 @@ Object.defineProperty(window, 'location', {
hostname: '',
},
});
-
-/**
- * Define WebAuthn's custom API errors
- */
-
-class AbortError extends Error {
- constructor() {
- super();
- this.name = 'AbortError';
- }
-}
-
-class ConstraintError extends Error {
- constructor() {
- super();
- this.name = 'ConstraintError';
- }
-}
-
-class InvalidStateError extends Error {
- constructor() {
- super();
- this.name = 'InvalidStateError';
- }
-}
-
-class NotAllowedError extends Error {
- constructor() {
- super();
- this.name = 'NotAllowedError';
- }
-}
-
-class NotSupportedError extends Error {
- constructor() {
- super();
- this.name = 'NotSupportedError';
- }
-}
-
-class SecurityError extends Error {
- constructor() {
- super();
- this.name = 'SecurityError';
- }
-}
-
-class UnknownError extends Error {
- constructor() {
- super();
- this.name = 'UnknownError';
- }
-}
-
-Object.defineProperty(global, 'AbortError', { value: AbortError });
-Object.defineProperty(global, 'ConstraintError', { value: ConstraintError });
-Object.defineProperty(global, 'InvalidStateError', { value: InvalidStateError });
-Object.defineProperty(global, 'NotAllowedError', { value: NotAllowedError });
-Object.defineProperty(global, 'NotSupportedError', { value: NotSupportedError });
-Object.defineProperty(global, 'SecurityError', { value: SecurityError });
-Object.defineProperty(global, 'UnknownError', { value: UnknownError });