-
Notifications
You must be signed in to change notification settings - Fork 4
/
errors.ts
207 lines (192 loc) · 5.52 KB
/
errors.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import {
HttpError,
HttpErrorOptions,
isHttpError,
optionsFromArgs,
} from "./deps.ts";
export interface OAuth2ErrorOptions extends HttpErrorOptions {
/** An ASCII error code. */
code?: string;
/** A URI identifying a human readable web page with information about the error. */
uri?: string;
}
export class OAuth2Error extends HttpError {
/** An ASCII error code. */
code: string;
/** A URI identifying a human readable web page with information about the error. */
uri?: string;
constructor(
status?: number,
message?: string,
options?: OAuth2ErrorOptions,
);
constructor(status?: number, options?: OAuth2ErrorOptions);
constructor(message?: string, options?: OAuth2ErrorOptions);
constructor(options?: OAuth2ErrorOptions);
constructor(
statusOrMessageOrOptions?: number | string | OAuth2ErrorOptions,
messageOrOptions?: string | OAuth2ErrorOptions,
options?: OAuth2ErrorOptions,
) {
const init: OAuth2ErrorOptions = optionsFromArgs(
statusOrMessageOrOptions,
messageOrOptions,
options,
);
super({ name: "OAuth2Error", ...init });
const { code, uri } = init;
this.code = code ?? "server_error";
if (uri) this.uri = uri;
}
}
export function isOAuth2Error(value: unknown): value is OAuth2Error {
return isHttpError(value) && typeof (value as OAuth2Error).code === "string";
}
/**
* The request is missing a required parameter, includes an unsupported parameter value,
* repeats a parameter, includes multiple credentials, utilizes more than one mechanism
* for authenticating the client, or is otherwise malformed.
*/
export class InvalidRequestError extends OAuth2Error {
constructor(message?: string, options?: ErrorOptions) {
super({
message,
name: "InvalidRequestError",
code: "invalid_request",
status: 400,
cause: options?.cause,
});
}
}
/** Client authentication failed. */
export class InvalidClientError extends OAuth2Error {
constructor(message?: string, options?: ErrorOptions) {
super({
message,
name: "InvalidClientError",
code: "invalid_client",
status: 401,
cause: options?.cause,
});
}
}
/**
* The provided authorization grant or refresh token is invalid, expired, revoked,
* does not match the redirection URI used in the authorization request, or was issued to another client.
*/
export class InvalidGrantError extends OAuth2Error {
constructor(message?: string, options?: ErrorOptions) {
super({
message,
name: "InvalidGrantError",
code: "invalid_grant",
status: 400,
cause: options?.cause,
});
}
}
/** The authenticated client is not authorized to use this authorization grant type. */
export class UnauthorizedClientError extends OAuth2Error {
constructor(message?: string, options?: ErrorOptions) {
super({
message,
name: "UnauthorizedClientError",
code: "unauthorized_client",
status: 401,
cause: options?.cause,
} as OAuth2ErrorOptions);
}
}
/** The token type is not supported by the authorization server. */
export class UnsupportedTokenTypeError extends OAuth2Error {
constructor(message?: string, options?: ErrorOptions) {
super({
message,
name: "UnsupportedTokenTypeError",
code: "unsupported_token_type",
status: 400,
cause: options?.cause,
});
}
}
/** The authorization grant type is not supported by the authorization server. */
export class UnsupportedGrantTypeError extends OAuth2Error {
constructor(message?: string, options?: ErrorOptions) {
super({
message,
name: "UnsupportedGrantTypeError",
code: "unsupported_grant_type",
status: 400,
cause: options?.cause,
});
}
}
/** The resource owner or authorization server denied the request. */
export class AccessDeniedError extends OAuth2Error {
constructor(message?: string, options?: ErrorOptions) {
super({
message,
name: "AccessDeniedError",
code: "access_denied",
status: 401,
cause: options?.cause,
});
}
}
/**
* The authorization server does not support obtaining
* an authorization code using this method.
*/
export class UnsupportedResponseTypeError extends OAuth2Error {
constructor(message?: string, options?: ErrorOptions) {
super({
message,
name: "UnsupportedResponseTypeError",
code: "unsupported_response_type",
status: 400,
cause: options?.cause,
});
}
}
/** The requested scope is invalid, unknown, or malformed. */
export class InvalidScopeError extends OAuth2Error {
constructor(message?: string, options?: ErrorOptions) {
super({
message,
name: "InvalidScopeError",
code: "invalid_scope",
status: 400,
cause: options?.cause,
});
}
}
/**
* The authorization server encountered an unexpected condition that
* prevented it from fulfilling the request.
*/
export class ServerError extends OAuth2Error {
constructor(message?: string, options?: ErrorOptions) {
super({
message,
name: "ServerError",
code: "server_error",
status: 500,
cause: options?.cause,
});
}
}
/**
* The authorization server is currently unable to handle the request due to
* a temporary overloading or maintenance of the server.
*/
export class TemporarilyUnavailableError extends OAuth2Error {
constructor(message?: string, options?: ErrorOptions) {
super({
message,
name: "TemporarilyUnavailableError",
code: "temporarily_unavailable",
status: 503,
cause: options?.cause,
});
}
}