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.server;
018
019import org.jspecify.annotations.NonNull;
020import org.jspecify.annotations.Nullable;
021import com.google.errorprone.annotations.CheckReturnValue;
022import javax.annotation.concurrent.Immutable;
023import java.util.Optional;
024import static java.util.Objects.requireNonNull;
025/**
026 * A library-created outcome. Applications cannot mint outcomes or proofs. Future variants require a rejecting
027 * switch default. Diagnostics disclose no interaction, credential, identity or claims. Infrastructure failures
028 * remain exceptions and cannot be reported as credential rejection.
029 * @since 1.0.0
030 */
031@Immutable
032public abstract sealed class OAuthAuthorizationResult permits OAuthAuthorizationResult.InteractionRequired, OAuthAuthorizationResult.Completed, OAuthAuthorizationResult.Denied, OAuthAuthorizationResult.Rejected {
033        OAuthAuthorizationResult() {}
034        static @NonNull OAuthAuthorizationResult fromInteractionRequired(@NonNull OAuthServerInteraction interaction) { return new InteractionRequired(requireNonNull(interaction)); }
035        static @NonNull OAuthAuthorizationResult fromCompleted(@NonNull OAuthServerResponse response) { return new Completed(requireNonNull(response)); }
036        static @NonNull OAuthAuthorizationResult fromDenied(@NonNull OAuthServerResponse response) { return new Denied(requireNonNull(response)); }
037        static @NonNull OAuthAuthorizationResult fromRejection(OAuthServerException.@NonNull Reason reason, @NonNull OAuthServerResponse response) {
038                requireNonNull(reason).requireKind(OAuthServerException.Kind.VALIDATION);
039                return new Rejected(reason,requireNonNull(response));
040        }
041        /** Returns a fixed redacted description.
042         * @return fixed redacted description
043         * @since 1.0.0
044         */
045        @Override public final @NonNull String toString() { return "OAuthAuthorizationResult{<redacted>}"; }
046        /** The library-created InteractionRequired outcome.
047         * @since 1.0.0
048         */
049        @Immutable public static final class InteractionRequired extends OAuthAuthorizationResult {
050                private final @NonNull OAuthServerInteraction interaction;
051                private InteractionRequired(@NonNull OAuthServerInteraction interaction) { this.interaction=interaction; }
052                /** Returns the restricted interaction.
053                 * @return interaction
054                 * @since 1.0.0
055                 */
056                @CheckReturnValue public @NonNull OAuthServerInteraction getInteraction() { return this.interaction; }
057        }
058        /** The library-created Completed outcome.
059         * @since 1.0.0
060         */
061        @Immutable public static final class Completed extends OAuthAuthorizationResult {
062                private final @NonNull OAuthServerResponse response;
063                private Completed(@NonNull OAuthServerResponse response) { this.response=response; }
064                /** Returns the restricted response.
065                 * @return response
066                 * @since 1.0.0
067                 */
068                @CheckReturnValue public @NonNull OAuthServerResponse getResponse() { return this.response; }
069        }
070        /** The library-created Denied outcome.
071         * @since 1.0.0
072         */
073        @Immutable public static final class Denied extends OAuthAuthorizationResult {
074                private final @NonNull OAuthServerResponse response;
075                private Denied(@NonNull OAuthServerResponse response) { this.response=response; }
076                /** Returns the restricted response.
077                 * @return response
078                 * @since 1.0.0
079                 */
080                @CheckReturnValue public @NonNull OAuthServerResponse getResponse() { return this.response; }
081        }
082        /** A fixed rejection with no proof, request, credential, identity, claims or exception object.
083         * @since 1.0.0
084         */
085        @Immutable public static final class Rejected extends OAuthAuthorizationResult {
086                private final OAuthServerException.@NonNull Reason reason;
087                private final @NonNull OAuthServerResponse response;
088                private Rejected(OAuthServerException.@NonNull Reason reason, @NonNull OAuthServerResponse response) { this.reason=reason; this.response=response; }
089                /** Returns the fixed issuer rejection reason, not an external error string.
090                 * @return fixed reason
091                 * @since 1.0.0
092                 */
093                @CheckReturnValue public OAuthServerException.@NonNull Reason getReason() { return this.reason; }
094                /** Returns the safe library-prepared local rejection response.
095                 * @return restricted response
096                 * @since 1.0.0
097                 */
098                @CheckReturnValue public @NonNull OAuthServerResponse getResponse() { return this.response; }
099        }
100}