001package com.nimbusds.jose.proc; 002 003 004import com.nimbusds.jose.JWSAlgorithm; 005import com.nimbusds.jose.JWSHeader; 006 007import java.security.Key; 008import java.util.Collections; 009import java.util.List; 010import java.util.Objects; 011 012 013/** 014 * A {@link JWSKeySelector} that always returns the same {@link Key}. 015 * 016 * @author Josh Cummings 017 * @version 2024-04-20 018 */ 019public class SingleKeyJWSKeySelector<C extends SecurityContext> implements JWSKeySelector<C> { 020 021 022 private final List<Key> singletonKeyList; 023 024 private final JWSAlgorithm expectedJWSAlg; 025 026 027 /** 028 * Creates a new single-key JWS key selector. 029 * 030 * @param expectedJWSAlg The expected JWS algorithm for the JWS 031 * objects to be verified. Must not be 032 * {@code null}. 033 * @param key The key to always return. Must not be 034 * {@code null}. 035 */ 036 public SingleKeyJWSKeySelector(final JWSAlgorithm expectedJWSAlg, final Key key) { 037 this.singletonKeyList = Collections.singletonList(Objects.requireNonNull(key)); 038 this.expectedJWSAlg = Objects.requireNonNull(expectedJWSAlg); 039 } 040 041 042 @Override 043 public List<? extends Key> selectJWSKeys(final JWSHeader header, final C context) { 044 045 if (! this.expectedJWSAlg.equals(header.getAlgorithm())) { 046 return Collections.emptyList(); 047 } 048 049 return this.singletonKeyList; 050 } 051}