@@ -14,6 +14,9 @@ See the License for the specific language governing permissions and
1414limitations under the License.
1515*/
1616
17+ import { hasOptionalStringProperty , hasRequiredStringProperty , isRecord } from "../@types/type-guards.ts" ;
18+ import { HTTPError } from "../http-api/errors.ts" ;
19+
1720/**
1821 * Errors expected to be encountered during OAuth2 discovery, client registration, and authentication.
1922 * Not intended to be displayed directly to the user.
@@ -32,3 +35,69 @@ export enum OAuth2Error {
3235 RevokeTokenFailed = "Failed to revoke token" ,
3336 DeviceAuthorizationGrantFailed = "Failed to perform device authorization grant" ,
3437}
38+
39+ /**
40+ * An error response from an OAuth 2.0 endpoint,
41+ * as specified in https://datatracker.ietf.org/doc/html/rfc6749#section-5.2
42+ */
43+ export interface OAuth2ErrorResponse {
44+ /** A single ASCII error code, e.g. `invalid_grant`. */
45+ error : string ;
46+ /** Human-readable ASCII text providing additional information about the error. */
47+ error_description ?: string ;
48+ /** A URI identifying a human-readable web page with information about the error. */
49+ error_uri ?: string ;
50+ }
51+
52+ /**
53+ * Check whether the given (JSON-parsed) response body is an OAuth 2.0 error response
54+ * as specified in https://datatracker.ietf.org/doc/html/rfc6749#section-5.2
55+ * @param response - the parsed response body to check
56+ * @returns whether the response is a valid {@link OAuth2ErrorResponse}
57+ */
58+ export function isOAuth2ErrorResponse ( response : unknown ) : response is OAuth2ErrorResponse {
59+ return (
60+ isRecord ( response ) &&
61+ hasRequiredStringProperty ( response , "error" ) &&
62+ hasOptionalStringProperty ( response , "error_description" ) &&
63+ hasOptionalStringProperty ( response , "error_uri" )
64+ ) ;
65+ }
66+
67+ /**
68+ * An error thrown when a request to an OAuth 2.0 endpoint fails with a body matching the error
69+ * response format specified in [RFC 6749 section 5.2](https://datatracker.ietf.org/doc/html/rfc6749#section-5.2).
70+ */
71+ export class OAuth2HTTPError extends HTTPError implements OAuth2ErrorResponse {
72+ /**
73+ * RFC 6749 section 5.2 error code, e.g. `invalid_grant`
74+ *
75+ * IANA matains a registry of valid values at
76+ * https://www.iana.org/assignments/oauth-parameters/oauth-parameters.xhtml#extensions-error
77+ */
78+ public error : string ;
79+
80+ /**
81+ * RFC 6749 section 5.2 human-readable ASCII text providing additional information about the error.
82+ * This field is optional and may be omitted by the endpoint.
83+ */
84+ public error_description ?: string ;
85+
86+ /**
87+ * RFC 6749 section 5.2 URI identifying a human-readable web page with information about the error.
88+ * This field is optional and may be omitted by the endpoint.
89+ */
90+ public error_uri ?: string ;
91+
92+ public constructor (
93+ msg : string ,
94+ httpStatus : number | undefined ,
95+ httpHeaders : Headers | undefined ,
96+ { error, error_description, error_uri } : OAuth2ErrorResponse ,
97+ ) {
98+ super ( msg , httpStatus , httpHeaders ) ;
99+ this . error = error ;
100+ this . error_description = error_description ;
101+ this . error_uri = error_uri ;
102+ }
103+ }
0 commit comments