Skip to content

Commit ec22c1f

Browse files
committed
refactor: unify error code constants in CredentialsManagerError and WebAuthError
1 parent 92b3637 commit ec22c1f

File tree

2 files changed

+119
-59
lines changed

2 files changed

+119
-59
lines changed
Lines changed: 68 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { AuthError } from './AuthError';
22

3-
const ERROR_CODE_MAP: Record<string, string> = {
3+
/**
4+
* Public constants exposing all possible CredentialsManager error codes.
5+
*/
6+
export const CredentialsManagerErrorCodes = {
47
INVALID_CREDENTIALS: 'INVALID_CREDENTIALS',
58
NO_CREDENTIALS: 'NO_CREDENTIALS',
69
NO_REFRESH_TOKEN: 'NO_REFRESH_TOKEN',
@@ -12,43 +15,72 @@ const ERROR_CODE_MAP: Record<string, string> = {
1215
BIOMETRICS_FAILED: 'BIOMETRICS_FAILED',
1316
NO_NETWORK: 'NO_NETWORK',
1417
API_ERROR: 'API_ERROR',
18+
INCOMPATIBLE_DEVICE: 'INCOMPATIBLE_DEVICE',
19+
CRYPTO_EXCEPTION: 'CRYPTO_EXCEPTION',
20+
UNKNOWN_ERROR: 'UNKNOWN_ERROR',
21+
} as const;
22+
23+
const ERROR_CODE_MAP: Record<string, string> = {
24+
INVALID_CREDENTIALS: CredentialsManagerErrorCodes.INVALID_CREDENTIALS,
25+
NO_CREDENTIALS: CredentialsManagerErrorCodes.NO_CREDENTIALS,
26+
NO_REFRESH_TOKEN: CredentialsManagerErrorCodes.NO_REFRESH_TOKEN,
27+
RENEW_FAILED: CredentialsManagerErrorCodes.RENEW_FAILED,
28+
STORE_FAILED: CredentialsManagerErrorCodes.STORE_FAILED,
29+
REVOKE_FAILED: CredentialsManagerErrorCodes.REVOKE_FAILED,
30+
LARGE_MIN_TTL: CredentialsManagerErrorCodes.LARGE_MIN_TTL,
31+
CREDENTIAL_MANAGER_ERROR:
32+
CredentialsManagerErrorCodes.CREDENTIAL_MANAGER_ERROR,
33+
BIOMETRICS_FAILED: CredentialsManagerErrorCodes.BIOMETRICS_FAILED,
34+
NO_NETWORK: CredentialsManagerErrorCodes.NO_NETWORK,
35+
API_ERROR: CredentialsManagerErrorCodes.API_ERROR,
1536

1637
// --- Web (@auth0/auth0-spa-js) mappings ---
17-
login_required: 'NO_CREDENTIALS',
18-
consent_required: 'RENEW_FAILED',
19-
mfa_required: 'RENEW_FAILED',
20-
invalid_grant: 'RENEW_FAILED',
21-
invalid_refresh_token: 'RENEW_FAILED',
22-
missing_refresh_token: 'NO_REFRESH_TOKEN',
38+
login_required: CredentialsManagerErrorCodes.NO_CREDENTIALS,
39+
consent_required: CredentialsManagerErrorCodes.RENEW_FAILED,
40+
mfa_required: CredentialsManagerErrorCodes.RENEW_FAILED,
41+
invalid_grant: CredentialsManagerErrorCodes.RENEW_FAILED,
42+
invalid_refresh_token: CredentialsManagerErrorCodes.RENEW_FAILED,
43+
missing_refresh_token: CredentialsManagerErrorCodes.NO_REFRESH_TOKEN,
2344

2445
// --- Many-to-one mapping for granular Android Biometric errors ---
25-
INCOMPATIBLE_DEVICE: 'INCOMPATIBLE_DEVICE',
26-
CRYPTO_EXCEPTION: 'CRYPTO_EXCEPTION',
27-
BIOMETRIC_NO_ACTIVITY: 'BIOMETRICS_FAILED',
28-
BIOMETRIC_ERROR_STATUS_UNKNOWN: 'BIOMETRICS_FAILED',
29-
BIOMETRIC_ERROR_UNSUPPORTED: 'BIOMETRICS_FAILED',
30-
BIOMETRIC_ERROR_HW_UNAVAILABLE: 'BIOMETRICS_FAILED',
31-
BIOMETRIC_ERROR_NONE_ENROLLED: 'BIOMETRICS_FAILED',
32-
BIOMETRIC_ERROR_NO_HARDWARE: 'BIOMETRICS_FAILED',
33-
BIOMETRIC_ERROR_SECURITY_UPDATE_REQUIRED: 'BIOMETRICS_FAILED',
34-
BIOMETRIC_AUTHENTICATION_CHECK_FAILED: 'BIOMETRICS_FAILED',
35-
BIOMETRIC_ERROR_DEVICE_CREDENTIAL_NOT_AVAILABLE: 'BIOMETRICS_FAILED',
46+
INCOMPATIBLE_DEVICE: CredentialsManagerErrorCodes.INCOMPATIBLE_DEVICE,
47+
CRYPTO_EXCEPTION: CredentialsManagerErrorCodes.CRYPTO_EXCEPTION,
48+
BIOMETRIC_NO_ACTIVITY: CredentialsManagerErrorCodes.BIOMETRICS_FAILED,
49+
BIOMETRIC_ERROR_STATUS_UNKNOWN:
50+
CredentialsManagerErrorCodes.BIOMETRICS_FAILED,
51+
BIOMETRIC_ERROR_UNSUPPORTED: CredentialsManagerErrorCodes.BIOMETRICS_FAILED,
52+
BIOMETRIC_ERROR_HW_UNAVAILABLE:
53+
CredentialsManagerErrorCodes.BIOMETRICS_FAILED,
54+
BIOMETRIC_ERROR_NONE_ENROLLED: CredentialsManagerErrorCodes.BIOMETRICS_FAILED,
55+
BIOMETRIC_ERROR_NO_HARDWARE: CredentialsManagerErrorCodes.BIOMETRICS_FAILED,
56+
BIOMETRIC_ERROR_SECURITY_UPDATE_REQUIRED:
57+
CredentialsManagerErrorCodes.BIOMETRICS_FAILED,
58+
BIOMETRIC_AUTHENTICATION_CHECK_FAILED:
59+
CredentialsManagerErrorCodes.BIOMETRICS_FAILED,
60+
BIOMETRIC_ERROR_DEVICE_CREDENTIAL_NOT_AVAILABLE:
61+
CredentialsManagerErrorCodes.BIOMETRICS_FAILED,
3662
BIOMETRIC_ERROR_STRONG_AND_DEVICE_CREDENTIAL_NOT_AVAILABLE:
37-
'BIOMETRICS_FAILED',
38-
BIOMETRIC_ERROR_NO_DEVICE_CREDENTIAL: 'BIOMETRICS_FAILED',
39-
BIOMETRIC_ERROR_NEGATIVE_BUTTON: 'BIOMETRICS_FAILED',
40-
BIOMETRIC_ERROR_HW_NOT_PRESENT: 'BIOMETRICS_FAILED',
41-
BIOMETRIC_ERROR_NO_BIOMETRICS: 'BIOMETRICS_FAILED',
42-
BIOMETRIC_ERROR_USER_CANCELED: 'BIOMETRICS_FAILED',
43-
BIOMETRIC_ERROR_LOCKOUT_PERMANENT: 'BIOMETRICS_FAILED',
44-
BIOMETRIC_ERROR_VENDOR: 'BIOMETRICS_FAILED',
45-
BIOMETRIC_ERROR_LOCKOUT: 'BIOMETRICS_FAILED',
46-
BIOMETRIC_ERROR_CANCELED: 'BIOMETRICS_FAILED',
47-
BIOMETRIC_ERROR_NO_SPACE: 'BIOMETRICS_FAILED',
48-
BIOMETRIC_ERROR_TIMEOUT: 'BIOMETRICS_FAILED',
49-
BIOMETRIC_ERROR_UNABLE_TO_PROCESS: 'BIOMETRICS_FAILED',
50-
BIOMETRICS_INVALID_USER: 'BIOMETRICS_FAILED',
51-
BIOMETRIC_AUTHENTICATION_FAILED: 'BIOMETRICS_FAILED',
63+
CredentialsManagerErrorCodes.BIOMETRICS_FAILED,
64+
BIOMETRIC_ERROR_NO_DEVICE_CREDENTIAL:
65+
CredentialsManagerErrorCodes.BIOMETRICS_FAILED,
66+
BIOMETRIC_ERROR_NEGATIVE_BUTTON:
67+
CredentialsManagerErrorCodes.BIOMETRICS_FAILED,
68+
BIOMETRIC_ERROR_HW_NOT_PRESENT:
69+
CredentialsManagerErrorCodes.BIOMETRICS_FAILED,
70+
BIOMETRIC_ERROR_NO_BIOMETRICS: CredentialsManagerErrorCodes.BIOMETRICS_FAILED,
71+
BIOMETRIC_ERROR_USER_CANCELED: CredentialsManagerErrorCodes.BIOMETRICS_FAILED,
72+
BIOMETRIC_ERROR_LOCKOUT_PERMANENT:
73+
CredentialsManagerErrorCodes.BIOMETRICS_FAILED,
74+
BIOMETRIC_ERROR_VENDOR: CredentialsManagerErrorCodes.BIOMETRICS_FAILED,
75+
BIOMETRIC_ERROR_LOCKOUT: CredentialsManagerErrorCodes.BIOMETRICS_FAILED,
76+
BIOMETRIC_ERROR_CANCELED: CredentialsManagerErrorCodes.BIOMETRICS_FAILED,
77+
BIOMETRIC_ERROR_NO_SPACE: CredentialsManagerErrorCodes.BIOMETRICS_FAILED,
78+
BIOMETRIC_ERROR_TIMEOUT: CredentialsManagerErrorCodes.BIOMETRICS_FAILED,
79+
BIOMETRIC_ERROR_UNABLE_TO_PROCESS:
80+
CredentialsManagerErrorCodes.BIOMETRICS_FAILED,
81+
BIOMETRICS_INVALID_USER: CredentialsManagerErrorCodes.BIOMETRICS_FAILED,
82+
BIOMETRIC_AUTHENTICATION_FAILED:
83+
CredentialsManagerErrorCodes.BIOMETRICS_FAILED,
5284
};
5385

5486
export class CredentialsManagerError extends AuthError {
@@ -61,6 +93,8 @@ export class CredentialsManagerError extends AuthError {
6193
json: originalError.json,
6294
});
6395

64-
this.type = ERROR_CODE_MAP[originalError.code] || 'UNKNOWN_ERROR';
96+
this.type =
97+
ERROR_CODE_MAP[originalError.code] ||
98+
CredentialsManagerErrorCodes.UNKNOWN_ERROR;
6599
}
66100
}

src/core/models/WebAuthError.ts

Lines changed: 51 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,63 @@
11
import { AuthError } from './AuthError';
22

3+
/**
4+
* Public constants exposing all possible WebAuth error codes.
5+
*/
6+
export const WebAuthErrorCodes = {
7+
USER_CANCELLED: 'USER_CANCELLED',
8+
ACCESS_DENIED: 'ACCESS_DENIED',
9+
NETWORK_ERROR: 'NETWORK_ERROR',
10+
ID_TOKEN_VALIDATION_FAILED: 'ID_TOKEN_VALIDATION_FAILED',
11+
BIOMETRICS_CONFIGURATION_ERROR: 'BIOMETRICS_CONFIGURATION_ERROR',
12+
BROWSER_NOT_AVAILABLE: 'BROWSER_NOT_AVAILABLE',
13+
FAILED_TO_LOAD_URL: 'FAILED_TO_LOAD_URL',
14+
BROWSER_TERMINATED: 'BROWSER_TERMINATED',
15+
NO_BUNDLE_IDENTIFIER: 'NO_BUNDLE_IDENTIFIER',
16+
TRANSACTION_ACTIVE_ALREADY: 'TRANSACTION_ACTIVE_ALREADY',
17+
NO_AUTHORIZATION_CODE: 'NO_AUTHORIZATION_CODE',
18+
PKCE_NOT_ALLOWED: 'PKCE_NOT_ALLOWED',
19+
INVALID_INVITATION_URL: 'INVALID_INVITATION_URL',
20+
INVALID_STATE: 'INVALID_STATE',
21+
TIMEOUT_ERROR: 'TIMEOUT_ERROR',
22+
CONSENT_REQUIRED: 'CONSENT_REQUIRED',
23+
INVALID_CONFIGURATION: 'INVALID_CONFIGURATION',
24+
UNKNOWN_ERROR: 'UNKNOWN_ERROR',
25+
} as const;
26+
327
const ERROR_CODE_MAP: Record<string, string> = {
428
// --- Common Codes ---
5-
'a0.session.user_cancelled': 'USER_CANCELLED',
6-
'USER_CANCELLED': 'USER_CANCELLED',
7-
'access_denied': 'ACCESS_DENIED',
8-
'a0.network_error': 'NETWORK_ERROR',
9-
'a0.session.invalid_idtoken': 'ID_TOKEN_VALIDATION_FAILED',
10-
'ID_TOKEN_VALIDATION_FAILED': 'ID_TOKEN_VALIDATION_FAILED',
11-
'BIOMETRICS_CONFIGURATION_ERROR': 'BIOMETRICS_CONFIGURATION_ERROR',
29+
'a0.session.user_cancelled': WebAuthErrorCodes.USER_CANCELLED,
30+
'USER_CANCELLED': WebAuthErrorCodes.USER_CANCELLED,
31+
'access_denied': WebAuthErrorCodes.ACCESS_DENIED,
32+
'a0.network_error': WebAuthErrorCodes.NETWORK_ERROR,
33+
'a0.session.invalid_idtoken': WebAuthErrorCodes.ID_TOKEN_VALIDATION_FAILED,
34+
'ID_TOKEN_VALIDATION_FAILED': WebAuthErrorCodes.ID_TOKEN_VALIDATION_FAILED,
35+
'BIOMETRICS_CONFIGURATION_ERROR':
36+
WebAuthErrorCodes.BIOMETRICS_CONFIGURATION_ERROR,
1237

1338
// --- Android-specific mappings ---
14-
'a0.browser_not_available': 'BROWSER_NOT_AVAILABLE',
15-
'a0.session.failed_load': 'FAILED_TO_LOAD_URL',
16-
'a0.session.browser_terminated': 'BROWSER_TERMINATED',
39+
'a0.browser_not_available': WebAuthErrorCodes.BROWSER_NOT_AVAILABLE,
40+
'a0.session.failed_load': WebAuthErrorCodes.FAILED_TO_LOAD_URL,
41+
'a0.session.browser_terminated': WebAuthErrorCodes.BROWSER_TERMINATED,
1742

1843
// --- iOS-specific mappings ---
19-
'NO_BUNDLE_IDENTIFIER': 'NO_BUNDLE_IDENTIFIER',
20-
'TRANSACTION_ACTIVE_ALREADY': 'TRANSACTION_ACTIVE_ALREADY',
21-
'NO_AUTHORIZATION_CODE': 'NO_AUTHORIZATION_CODE',
22-
'PKCE_NOT_ALLOWED': 'PKCE_NOT_ALLOWED',
23-
'INVALID_INVITATION_URL': 'INVALID_INVITATION_URL',
44+
'NO_BUNDLE_IDENTIFIER': WebAuthErrorCodes.NO_BUNDLE_IDENTIFIER,
45+
'TRANSACTION_ACTIVE_ALREADY': WebAuthErrorCodes.TRANSACTION_ACTIVE_ALREADY,
46+
'NO_AUTHORIZATION_CODE': WebAuthErrorCodes.NO_AUTHORIZATION_CODE,
47+
'PKCE_NOT_ALLOWED': WebAuthErrorCodes.PKCE_NOT_ALLOWED,
48+
'INVALID_INVITATION_URL': WebAuthErrorCodes.INVALID_INVITATION_URL,
2449

2550
// --- Web (@auth0/auth0-spa-js) mappings ---
26-
'cancelled': 'USER_CANCELLED',
27-
'state_mismatch': 'INVALID_STATE',
28-
'login_required': 'ACCESS_DENIED',
29-
'timeout': 'TIMEOUT_ERROR',
30-
'consent_required': 'CONSENT_REQUIRED',
51+
'cancelled': WebAuthErrorCodes.USER_CANCELLED,
52+
'state_mismatch': WebAuthErrorCodes.INVALID_STATE,
53+
'login_required': WebAuthErrorCodes.ACCESS_DENIED,
54+
'timeout': WebAuthErrorCodes.TIMEOUT_ERROR,
55+
'consent_required': WebAuthErrorCodes.CONSENT_REQUIRED,
3156

3257
// --- Generic Fallbacks ---
33-
'a0.invalid_configuration': 'INVALID_CONFIGURATION',
34-
'UNKNOWN': 'UNKNOWN_ERROR',
35-
'OTHER': 'UNKNOWN_ERROR',
58+
'a0.invalid_configuration': WebAuthErrorCodes.INVALID_CONFIGURATION,
59+
'UNKNOWN': WebAuthErrorCodes.UNKNOWN_ERROR,
60+
'OTHER': WebAuthErrorCodes.UNKNOWN_ERROR,
3661
};
3762

3863
export class WebAuthError extends AuthError {
@@ -49,9 +74,10 @@ export class WebAuthError extends AuthError {
4974
originalError.message.includes('state is invalid') ||
5075
originalError.code === 'state_mismatch'
5176
) {
52-
this.type = 'INVALID_STATE';
77+
this.type = WebAuthErrorCodes.INVALID_STATE;
5378
} else {
54-
this.type = ERROR_CODE_MAP[originalError.code] || 'UNKNOWN_ERROR';
79+
this.type =
80+
ERROR_CODE_MAP[originalError.code] || WebAuthErrorCodes.UNKNOWN_ERROR;
5581
}
5682
}
5783
}

0 commit comments

Comments
 (0)