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.jose;
018
019import org.jspecify.annotations.NonNull;
020import javax.annotation.concurrent.Immutable;
021import static java.util.Objects.requireNonNull;
022
023/**
024 * The outcome of parsing a JSON Web Key Set document with the default size and key-count limits.
025 * <p>
026 * Applications receive these outcomes from the corresponding operation; they cannot construct them.
027 * Value accessors exist only on the variant that owns a value. Diagnostic text contains no input or result data.
028 * A later release may add variants; consuming switches should include a rejecting default branch.
029 *
030 * @author <a href="https://www.revetkn.com">Mark Allen</a>
031 * @since 1.0.0
032 */
033@Immutable
034public abstract sealed class JwksParseResult permits JwksParseResult.Succeeded, JwksParseResult.Rejected {
035        JwksParseResult() { }
036
037        static @NonNull JwksParseResult fromJwks(@NonNull Jwks jwks) {
038                return new Succeeded(requireNonNull(jwks));
039        }
040
041        static @NonNull JwksParseResult fromReason(JoseException.@NonNull Reason reason) {
042                return new Rejected(requireNonNull(reason));
043        }
044
045        /**
046         * Returns the outcome kind with all values redacted.
047         * @return redacted description
048         * @since 1.0.0
049         */
050        @Override
051        public final @NonNull String toString() {
052                return "JwksParseResult{outcome=" + getClass().getSimpleName() + ", data=<redacted>}";
053        }
054
055        /**
056         * A JSON Web Key Set document passed the structural and resource-limit checks. Unusable individual keys were skipped.
057         * @author <a href="https://www.revetkn.com">Mark Allen</a>
058         * @since 1.0.0
059         */
060        @Immutable
061        public static final class Succeeded extends JwksParseResult {
062                private final @NonNull Jwks jwks;
063                private Succeeded(@NonNull Jwks jwks) { this.jwks = jwks; }
064
065                /**
066                 * Returns the parsed set of usable public verification keys.
067                 * @return key set
068                 * @since 1.0.0
069                 */
070                public @NonNull Jwks getJwks() { return this.jwks; }
071        }
072
073        /**
074         * The input was rejected; this variant contains only a fixed reason.
075         * @author <a href="https://www.revetkn.com">Mark Allen</a>
076         * @since 1.0.0
077         */
078        @Immutable
079        public static final class Rejected extends JwksParseResult {
080                private final JoseException.@NonNull Reason reason;
081                private Rejected(JoseException.@NonNull Reason reason) { this.reason = reason; }
082
083                /**
084                 * Returns the fixed JOSE rejection reason.
085                 * @return reason
086                 * @since 1.0.0
087                 */
088                public JoseException.@NonNull Reason getReason() { return this.reason; }
089        }
090}