summaryrefslogtreecommitdiffhomepage
path: root/packages/browser/src/helpers
diff options
context:
space:
mode:
authorFelix Mosheev <9304194+felixmosh@users.noreply.github.com>2022-04-12 09:01:39 +0300
committerFelix Mosheev <9304194+felixmosh@users.noreply.github.com>2022-04-12 09:01:39 +0300
commite4714e4b31e2eb88733d63f92c1bf61511d25e83 (patch)
tree736bd6de77e5e0153a71008aaf825ed853bb328e /packages/browser/src/helpers
parentf46a315898dd31af268f7b36b39f1a28720c0072 (diff)
Change error name to a specific type, fixes #190
Diffstat (limited to 'packages/browser/src/helpers')
-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
3 files changed, 29 insertions, 19 deletions
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;
}
}