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;
020
021import javax.annotation.concurrent.Immutable;
022
023import static java.util.Objects.requireNonNull;
024
025/**
026 * The outcome of parsing a raw, unverified OAuth browser callback. A successful parse grants no identity;
027 * the configured OAuth or OpenID Connect client must complete the browser-bound transaction.
028 * <p>
029 * Only the producing operation constructs these outcomes. Value accessors exist only on the variant that
030 * owns a value, and diagnostic text contains no callback data. Later releases may add variants;
031 * consuming switches should include a rejecting default branch.
032 *
033 * @author <a href="https://www.revetkn.com">Mark Allen</a>
034 * @since 1.0.0
035 */
036@Immutable
037public abstract sealed class AuthorizationResponseParseResult
038                permits AuthorizationResponseParseResult.Succeeded, AuthorizationResponseParseResult.Rejected {
039        AuthorizationResponseParseResult() { }
040
041        static @NonNull AuthorizationResponseParseResult fromResponse(@NonNull AuthorizationResponse response) {
042                return new Succeeded(requireNonNull(response));
043        }
044
045        static @NonNull AuthorizationResponseParseResult fromRejected() {
046                return new Rejected();
047        }
048
049        /**
050         * Returns the outcome kind without callback data.
051         * @return redacted description
052         * @since 1.0.0
053         */
054        @Override
055        public final @NonNull String toString() {
056                return "AuthorizationResponseParseResult{outcome=" + getClass().getSimpleName() + ", data=<redacted>}";
057        }
058
059        /**
060         * A syntactically parsed callback that remains unverified until transaction completion.
061         * @author <a href="https://www.revetkn.com">Mark Allen</a>
062         * @since 1.0.0
063         */
064        @Immutable
065        public static final class Succeeded extends AuthorizationResponseParseResult {
066                private final @NonNull AuthorizationResponse response;
067                private Succeeded(@NonNull AuthorizationResponse response) { this.response = response; }
068
069                /**
070                 * Returns the raw, unverified callback. It grants no identity or permission.
071                 * @return the parsed callback
072                 * @since 1.0.0
073                 */
074                public @NonNull AuthorizationResponse getResponse() { return this.response; }
075        }
076
077        /**
078         * A malformed, duplicate, oversized or split-channel callback.
079         * @author <a href="https://www.revetkn.com">Mark Allen</a>
080         * @since 1.0.0
081         */
082        @Immutable
083        public static final class Rejected extends AuthorizationResponseParseResult {
084                private Rejected() { }
085
086                /**
087                 * Returns a fixed reason without attacker-controlled callback data.
088                 * @return malformed callback reason
089                 * @since 1.0.0
090                 */
091                public OAuthException.@NonNull Reason getReason() { return OAuthException.Reason.CALLBACK_MALFORMED; }
092        }
093}