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