001/*
002 * Copyright 2026 Revetware LLC.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package com.revetsec.oauth;
018
019import org.jspecify.annotations.NonNull;
020import javax.annotation.concurrent.Immutable;
021import static java.util.Objects.requireNonNull;
022
023/**
024 * Fixed OAuth and OpenID Connect protocol errors for endpoint and checked authorization rejections.
025 * Unknown extension codes share one fallback and never become diagnostic text. A later release may add constants;
026 * consuming switches should include a default branch.
027 * @author <a href="https://www.revetkn.com">Mark Allen</a>
028 * @since 1.0.0
029 */
030@Immutable
031public enum OAuthEndpointError {
032        /** The endpoint reported invalid_request. */
033        INVALID_REQUEST,
034        /** The endpoint reported unauthorized_client. */
035        UNAUTHORIZED_CLIENT,
036        /** The endpoint reported access_denied. */
037        ACCESS_DENIED,
038        /** The endpoint reported unsupported_response_type. */
039        UNSUPPORTED_RESPONSE_TYPE,
040        /** The endpoint reported invalid_scope. */
041        INVALID_SCOPE,
042        /** The endpoint reported server_error. */
043        SERVER_ERROR,
044        /** The endpoint reported temporarily_unavailable. */
045        TEMPORARILY_UNAVAILABLE,
046        /** The endpoint reported invalid_client. */
047        INVALID_CLIENT,
048        /** The endpoint reported invalid_grant. */
049        INVALID_GRANT,
050        /** The endpoint reported unsupported_grant_type. */
051        UNSUPPORTED_GRANT_TYPE,
052        /** The endpoint reported unsupported_token_type. */
053        UNSUPPORTED_TOKEN_TYPE,
054        /** The endpoint reported invalid_token. */
055        INVALID_TOKEN,
056        /** The endpoint reported insufficient_scope. */
057        INSUFFICIENT_SCOPE,
058        /** The endpoint reported login_required. */
059        LOGIN_REQUIRED,
060        /** The endpoint reported interaction_required. */
061        INTERACTION_REQUIRED,
062        /** The endpoint reported account_selection_required. */
063        ACCOUNT_SELECTION_REQUIRED,
064        /** The endpoint reported consent_required. */
065        CONSENT_REQUIRED,
066        /** The endpoint reported invalid_request_uri. */
067        INVALID_REQUEST_URI,
068        /** The endpoint reported invalid_request_object. */
069        INVALID_REQUEST_OBJECT,
070        /** The endpoint reported request_not_supported. */
071        REQUEST_NOT_SUPPORTED,
072        /** The endpoint reported request_uri_not_supported. */
073        REQUEST_URI_NOT_SUPPORTED,
074        /** The endpoint reported registration_not_supported. */
075        REGISTRATION_NOT_SUPPORTED,
076        /** The endpoint supplied an absent or unrecognized error code; no supplied text is retained. */
077        UNRECOGNIZED;
078
079        /**
080         * Interprets an exact wire error code without retaining supplied text.
081         * @param wireValue supplied protocol error code
082         * @return fixed error or the unrecognized fallback
083         * @throws NullPointerException if the code is null
084         * @since 1.0.0
085         */
086        public static @NonNull OAuthEndpointError fromWireValue(@NonNull String wireValue) {
087                return switch (requireNonNull(wireValue)) {
088                        case "invalid_request" -> INVALID_REQUEST;
089                        case "unauthorized_client" -> UNAUTHORIZED_CLIENT;
090                        case "access_denied" -> ACCESS_DENIED;
091                        case "unsupported_response_type" -> UNSUPPORTED_RESPONSE_TYPE;
092                        case "invalid_scope" -> INVALID_SCOPE;
093                        case "server_error" -> SERVER_ERROR;
094                        case "temporarily_unavailable" -> TEMPORARILY_UNAVAILABLE;
095                        case "invalid_client" -> INVALID_CLIENT;
096                        case "invalid_grant" -> INVALID_GRANT;
097                        case "unsupported_grant_type" -> UNSUPPORTED_GRANT_TYPE;
098                        case "unsupported_token_type" -> UNSUPPORTED_TOKEN_TYPE;
099                        case "invalid_token" -> INVALID_TOKEN;
100                        case "insufficient_scope" -> INSUFFICIENT_SCOPE;
101                        case "login_required" -> LOGIN_REQUIRED;
102                        case "interaction_required" -> INTERACTION_REQUIRED;
103                        case "account_selection_required" -> ACCOUNT_SELECTION_REQUIRED;
104                        case "consent_required" -> CONSENT_REQUIRED;
105                        case "invalid_request_uri" -> INVALID_REQUEST_URI;
106                        case "invalid_request_object" -> INVALID_REQUEST_OBJECT;
107                        case "request_not_supported" -> REQUEST_NOT_SUPPORTED;
108                        case "request_uri_not_supported" -> REQUEST_URI_NOT_SUPPORTED;
109                        case "registration_not_supported" -> REGISTRATION_NOT_SUPPORTED;
110                        default -> UNRECOGNIZED;
111                };
112        }
113}