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 com.revetsec.internal.jose.VerificationKey;
020import org.jspecify.annotations.NonNull;
021
022import javax.annotation.concurrent.Immutable;
023import java.util.List;
024
025import static java.util.Objects.requireNonNull;
026
027/**
028 * A fixed set of verification keys, which never changes and is never fetched.
029 * <p>
030 * Use it for keys the application already holds, such as a key set configured with the application or fetched out of
031 * band. It does no I/O and needs no network settings. A token whose key is not in the set is rejected as
032 * {@link JoseException.Reason#UNKNOWN_KEY UNKNOWN_KEY}; nothing is refreshed.
033 * <p>
034 * A source holds no issuer: share one only among validators that expect the same issuer. Sources compare by
035 * reference.
036 *
037 * @author <a href="https://www.revetkn.com">Mark Allen</a>
038 * @since 1.0.0
039 */
040@Immutable
041public final class StaticJwksSource implements JwksSource {
042        @NonNull
043        private final Jwks jwks;
044
045        private StaticJwksSource(@NonNull Jwks jwks) {
046                this.jwks = jwks;
047        }
048
049        /**
050         * Returns a source that holds exactly the usable keys of {@code jwks}.
051         *
052         * @param jwks the key set
053         * @return the source
054         * @throws NullPointerException     if {@code jwks} is {@code null}
055         * @throws IllegalArgumentException if the key set holds no usable key, because such a source could verify
056         *                                  nothing
057         * @since 1.0.0
058         */
059        @NonNull
060        public static StaticJwksSource fromJwks(@NonNull Jwks jwks) {
061                requireNonNull(jwks);
062
063                if (jwks.getKeys().isEmpty())
064                        throw new IllegalArgumentException("A static JSON Web Key source needs at least one usable key.");
065
066                return new StaticJwksSource(jwks);
067        }
068
069        /**
070         * Returns the key set this source holds.
071         *
072         * @return the key set
073         * @since 1.0.0
074         */
075        @NonNull
076        public Jwks getJwks() {
077                return this.jwks;
078        }
079
080        /**
081         * The parsed keys, for key selection.
082         */
083        @NonNull
084        List<@NonNull VerificationKey> verificationKeys() {
085                return this.jwks.verificationKeys();
086        }
087
088        /**
089         * Describes the source by its keys' public facts.
090         *
091         * @return the description
092         * @since 1.0.0
093         */
094        @Override
095        @NonNull
096        public String toString() {
097                return getClass().getSimpleName() + "{jwks=" + this.jwks + "}";
098        }
099}