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 org.jspecify.annotations.Nullable;
021
022import javax.annotation.concurrent.NotThreadSafe;
023import java.util.Optional;
024
025/**
026 * A browser authorization error, reported only after pending-state and issuer checks pass.
027 *
028 * @author <a href="https://www.revetkn.com">Mark Allen</a>
029 * @since 1.0.0
030 */
031@NotThreadSafe
032public final class AuthorizationErrorException extends OAuthException {
033        private static final long serialVersionUID = 1L;
034        /** Safe bounded authorization error code; null when the AS supplied no safe code. */
035        private final @Nullable String errorCode;
036
037        private AuthorizationErrorException(@Nullable String errorCode) {
038                super(Reason.AUTHORIZATION_ERROR, "temporarily_unavailable".equals(errorCode), null);
039                this.errorCode = errorCode;
040        }
041
042        @NonNull
043        static AuthorizationErrorException fromErrorCode(@NonNull String code) {
044                return new AuthorizationErrorException(OAuthSafeErrorCode.fromValue(code));
045        }
046
047        /**
048         * Returns a bounded, safe OAuth error code when one was supplied.
049         *
050         * @return the safe error code
051         * @since 1.0.0
052         */
053        public @NonNull Optional<@NonNull String> getErrorCode() {
054                return Optional.ofNullable(this.errorCode);
055        }
056}