89 lines
4.0 KiB
JavaScript
89 lines
4.0 KiB
JavaScript
import _defineProperty from "@babel/runtime/helpers/defineProperty";
|
|
/*
|
|
Copyright 2023 The Matrix.org Foundation C.I.C.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
import { hasOptionalStringProperty, hasRequiredStringProperty, isRecord } from "../@types/type-guards.js";
|
|
import { HTTPError } from "../http-api/errors.js";
|
|
|
|
/**
|
|
* Errors expected to be encountered during OAuth2 discovery, client registration, and authentication.
|
|
* Not intended to be displayed directly to the user.
|
|
*/
|
|
export let OAuth2Error = /*#__PURE__*/function (OAuth2Error) {
|
|
OAuth2Error["General"] = "Something went wrong with OAuth2 discovery";
|
|
OAuth2Error["OpSupport"] = "Configured OAuth2 OP does not support required functions";
|
|
OAuth2Error["DynamicRegistrationNotSupported"] = "Dynamic registration not supported";
|
|
OAuth2Error["DynamicRegistrationFailed"] = "Dynamic registration failed";
|
|
OAuth2Error["DynamicRegistrationInvalid"] = "Dynamic registration invalid response";
|
|
OAuth2Error["CodeExchangeFailed"] = "Failed to exchange code for token";
|
|
OAuth2Error["InvalidBearerTokenResponse"] = "Invalid bearer token response";
|
|
OAuth2Error["InvalidDeviceAuthorizationResponse"] = "Invalid device authorization response";
|
|
OAuth2Error["MissingOrInvalidStoredState"] = "State required to finish logging in is not found in storage.";
|
|
OAuth2Error["RefreshTokenFailed"] = "Failed to refresh token";
|
|
OAuth2Error["RevokeTokenFailed"] = "Failed to revoke token";
|
|
OAuth2Error["DeviceAuthorizationGrantFailed"] = "Failed to perform device authorization grant";
|
|
return OAuth2Error;
|
|
}({});
|
|
|
|
/**
|
|
* An error response from an OAuth 2.0 endpoint,
|
|
* as specified in https://datatracker.ietf.org/doc/html/rfc6749#section-5.2
|
|
*/
|
|
|
|
/**
|
|
* Check whether the given (JSON-parsed) response body is an OAuth 2.0 error response
|
|
* as specified in https://datatracker.ietf.org/doc/html/rfc6749#section-5.2
|
|
* @param response - the parsed response body to check
|
|
* @returns whether the response is a valid {@link OAuth2ErrorResponse}
|
|
*/
|
|
export function isOAuth2ErrorResponse(response) {
|
|
return isRecord(response) && hasRequiredStringProperty(response, "error") && hasOptionalStringProperty(response, "error_description") && hasOptionalStringProperty(response, "error_uri");
|
|
}
|
|
|
|
/**
|
|
* An error thrown when a request to an OAuth 2.0 endpoint fails with a body matching the error
|
|
* response format specified in [RFC 6749 section 5.2](https://datatracker.ietf.org/doc/html/rfc6749#section-5.2).
|
|
*/
|
|
export class OAuth2HTTPError extends HTTPError {
|
|
constructor(msg, httpStatus, httpHeaders, {
|
|
error,
|
|
error_description,
|
|
error_uri
|
|
}) {
|
|
super(msg, httpStatus, httpHeaders);
|
|
/**
|
|
* RFC 6749 section 5.2 error code, e.g. `invalid_grant`
|
|
*
|
|
* IANA matains a registry of valid values at
|
|
* https://www.iana.org/assignments/oauth-parameters/oauth-parameters.xhtml#extensions-error
|
|
*/
|
|
_defineProperty(this, "error", void 0);
|
|
/**
|
|
* RFC 6749 section 5.2 human-readable ASCII text providing additional information about the error.
|
|
* This field is optional and may be omitted by the endpoint.
|
|
*/
|
|
_defineProperty(this, "error_description", void 0);
|
|
/**
|
|
* RFC 6749 section 5.2 URI identifying a human-readable web page with information about the error.
|
|
* This field is optional and may be omitted by the endpoint.
|
|
*/
|
|
_defineProperty(this, "error_uri", void 0);
|
|
this.error = error;
|
|
this.error_description = error_description;
|
|
this.error_uri = error_uri;
|
|
}
|
|
}
|
|
//# sourceMappingURL=error.js.map
|