@@ -4,12 +4,15 @@ import { OAuthErrorResponse } from "../../shared/auth.js";
4
4
* Base class for all OAuth errors
5
5
*/
6
6
export class OAuthError extends Error {
7
+ static errorCode : string ;
8
+ public errorCode : string ;
9
+
7
10
constructor (
8
- public readonly errorCode : string ,
9
11
message : string ,
10
12
public readonly errorUri ?: string
11
13
) {
12
14
super ( message ) ;
15
+ this . errorCode = ( this . constructor as typeof OAuthError ) . errorCode
13
16
this . name = this . constructor . name ;
14
17
}
15
18
@@ -36,19 +39,15 @@ export class OAuthError extends Error {
36
39
* or is otherwise malformed.
37
40
*/
38
41
export class InvalidRequestError extends OAuthError {
39
- constructor ( message : string , errorUri ?: string ) {
40
- super ( "invalid_request" , message , errorUri ) ;
41
- }
42
+ static errorCode = "invalid_request" ;
42
43
}
43
44
44
45
/**
45
46
* Invalid client error - Client authentication failed (e.g., unknown client, no client
46
47
* authentication included, or unsupported authentication method).
47
48
*/
48
49
export class InvalidClientError extends OAuthError {
49
- constructor ( message : string , errorUri ?: string ) {
50
- super ( "invalid_client" , message , errorUri ) ;
51
- }
50
+ static errorCode = "invalid_client" ;
52
51
}
53
52
54
53
/**
@@ -57,135 +56,139 @@ export class InvalidClientError extends OAuthError {
57
56
* authorization request, or was issued to another client.
58
57
*/
59
58
export class InvalidGrantError extends OAuthError {
60
- constructor ( message : string , errorUri ?: string ) {
61
- super ( "invalid_grant" , message , errorUri ) ;
62
- }
59
+ static errorCode = "invalid_grant" ;
63
60
}
64
61
65
62
/**
66
63
* Unauthorized client error - The authenticated client is not authorized to use
67
64
* this authorization grant type.
68
65
*/
69
66
export class UnauthorizedClientError extends OAuthError {
70
- constructor ( message : string , errorUri ?: string ) {
71
- super ( "unauthorized_client" , message , errorUri ) ;
72
- }
67
+ static errorCode = "unauthorized_client" ;
73
68
}
74
69
75
70
/**
76
71
* Unsupported grant type error - The authorization grant type is not supported
77
72
* by the authorization server.
78
73
*/
79
74
export class UnsupportedGrantTypeError extends OAuthError {
80
- constructor ( message : string , errorUri ?: string ) {
81
- super ( "unsupported_grant_type" , message , errorUri ) ;
82
- }
75
+ static errorCode = "unsupported_grant_type" ;
83
76
}
84
77
85
78
/**
86
79
* Invalid scope error - The requested scope is invalid, unknown, malformed, or
87
80
* exceeds the scope granted by the resource owner.
88
81
*/
89
82
export class InvalidScopeError extends OAuthError {
90
- constructor ( message : string , errorUri ?: string ) {
91
- super ( "invalid_scope" , message , errorUri ) ;
92
- }
83
+ static errorCode = "invalid_scope" ;
93
84
}
94
85
95
86
/**
96
87
* Access denied error - The resource owner or authorization server denied the request.
97
88
*/
98
89
export class AccessDeniedError extends OAuthError {
99
- constructor ( message : string , errorUri ?: string ) {
100
- super ( "access_denied" , message , errorUri ) ;
101
- }
90
+ static errorCode = "access_denied" ;
102
91
}
103
92
104
93
/**
105
94
* Server error - The authorization server encountered an unexpected condition
106
95
* that prevented it from fulfilling the request.
107
96
*/
108
97
export class ServerError extends OAuthError {
109
- constructor ( message : string , errorUri ?: string ) {
110
- super ( "server_error" , message , errorUri ) ;
111
- }
98
+ static errorCode = "server_error" ;
112
99
}
113
100
114
101
/**
115
102
* Temporarily unavailable error - The authorization server is currently unable to
116
103
* handle the request due to a temporary overloading or maintenance of the server.
117
104
*/
118
105
export class TemporarilyUnavailableError extends OAuthError {
119
- constructor ( message : string , errorUri ?: string ) {
120
- super ( "temporarily_unavailable" , message , errorUri ) ;
121
- }
106
+ static errorCode = "temporarily_unavailable" ;
122
107
}
123
108
124
109
/**
125
110
* Unsupported response type error - The authorization server does not support
126
111
* obtaining an authorization code using this method.
127
112
*/
128
113
export class UnsupportedResponseTypeError extends OAuthError {
129
- constructor ( message : string , errorUri ?: string ) {
130
- super ( "unsupported_response_type" , message , errorUri ) ;
131
- }
114
+ static errorCode = "unsupported_response_type" ;
132
115
}
133
116
134
117
/**
135
118
* Unsupported token type error - The authorization server does not support
136
119
* the requested token type.
137
120
*/
138
121
export class UnsupportedTokenTypeError extends OAuthError {
139
- constructor ( message : string , errorUri ?: string ) {
140
- super ( "unsupported_token_type" , message , errorUri ) ;
141
- }
122
+ static errorCode = "unsupported_token_type" ;
142
123
}
143
124
144
125
/**
145
126
* Invalid token error - The access token provided is expired, revoked, malformed,
146
127
* or invalid for other reasons.
147
128
*/
148
129
export class InvalidTokenError extends OAuthError {
149
- constructor ( message : string , errorUri ?: string ) {
150
- super ( "invalid_token" , message , errorUri ) ;
151
- }
130
+ static errorCode = "invalid_token" ;
152
131
}
153
132
154
133
/**
155
134
* Method not allowed error - The HTTP method used is not allowed for this endpoint.
156
135
* (Custom, non-standard error)
157
136
*/
158
137
export class MethodNotAllowedError extends OAuthError {
159
- constructor ( message : string , errorUri ?: string ) {
160
- super ( "method_not_allowed" , message , errorUri ) ;
161
- }
138
+ static errorCode = "method_not_allowed" ;
162
139
}
163
140
164
141
/**
165
142
* Too many requests error - Rate limit exceeded.
166
143
* (Custom, non-standard error based on RFC 6585)
167
144
*/
168
145
export class TooManyRequestsError extends OAuthError {
169
- constructor ( message : string , errorUri ?: string ) {
170
- super ( "too_many_requests" , message , errorUri ) ;
171
- }
146
+ static errorCode = "too_many_requests" ;
172
147
}
173
148
174
149
/**
175
150
* Invalid client metadata error - The client metadata is invalid.
176
151
* (Custom error for dynamic client registration - RFC 7591)
177
152
*/
178
153
export class InvalidClientMetadataError extends OAuthError {
179
- constructor ( message : string , errorUri ?: string ) {
180
- super ( "invalid_client_metadata" , message , errorUri ) ;
181
- }
154
+ static errorCode = "invalid_client_metadata" ;
182
155
}
183
156
184
157
/**
185
158
* Insufficient scope error - The request requires higher privileges than provided by the access token.
186
159
*/
187
160
export class InsufficientScopeError extends OAuthError {
188
- constructor ( message : string , errorUri ?: string ) {
189
- super ( "insufficient_scope" , message , errorUri ) ;
161
+ static errorCode = "insufficient_scope" ;
162
+ }
163
+
164
+ /**
165
+ * A utility class for defining one-off error codes
166
+ */
167
+ export class CustomOAuthError extends OAuthError {
168
+ constructor ( errorCode : string , message : string , errorUri ?: string ) {
169
+ super ( message , errorUri ) ;
170
+ this . errorCode = errorCode
190
171
}
191
172
}
173
+
174
+ /**
175
+ * A full list of all OAuthErrors, enabling parsing from error responses
176
+ */
177
+ export const OAUTH_ERRORS = {
178
+ [ InvalidRequestError . errorCode ] : InvalidRequestError ,
179
+ [ InvalidClientError . errorCode ] : InvalidClientError ,
180
+ [ InvalidGrantError . errorCode ] : InvalidGrantError ,
181
+ [ UnauthorizedClientError . errorCode ] : UnauthorizedClientError ,
182
+ [ UnsupportedGrantTypeError . errorCode ] : UnsupportedGrantTypeError ,
183
+ [ InvalidScopeError . errorCode ] : InvalidScopeError ,
184
+ [ AccessDeniedError . errorCode ] : AccessDeniedError ,
185
+ [ ServerError . errorCode ] : ServerError ,
186
+ [ TemporarilyUnavailableError . errorCode ] : TemporarilyUnavailableError ,
187
+ [ UnsupportedResponseTypeError . errorCode ] : UnsupportedResponseTypeError ,
188
+ [ UnsupportedTokenTypeError . errorCode ] : UnsupportedTokenTypeError ,
189
+ [ InvalidTokenError . errorCode ] : InvalidTokenError ,
190
+ [ MethodNotAllowedError . errorCode ] : MethodNotAllowedError ,
191
+ [ TooManyRequestsError . errorCode ] : TooManyRequestsError ,
192
+ [ InvalidClientMetadataError . errorCode ] : InvalidClientMetadataError ,
193
+ [ InsufficientScopeError . errorCode ] : InsufficientScopeError ,
194
+ } as const ;
0 commit comments