All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.nimbusds.jose.proc.SingleKeyJWSKeySelector Maven / Gradle / Ivy

Go to download

Java library for Javascript Object Signing and Encryption (JOSE) and JSON Web Tokens (JWT)

There is a newer version: 10.0.1
Show newest version
package com.nimbusds.jose.proc;


import java.security.Key;
import java.util.Collections;
import java.util.List;

import com.nimbusds.jose.JWSAlgorithm;
import com.nimbusds.jose.JWSHeader;


/**
 * A {@link JWSKeySelector} that always returns the same {@link Key}.
 *
 * @author Josh Cummings
 */
public class SingleKeyJWSKeySelector implements JWSKeySelector {
	
	
	private final List singletonKeyList;
	
	private final JWSAlgorithm expectedJWSAlg;
	

	/**
	 * Creates a new single-key JWS key selector.
	 *
	 * @param expectedJWSAlg The expected JWS algorithm for the JWS
	 *                       objects to be verified. Must not be
	 *                       {@code null}.
	 * @param key            The key to always return. Must not be
	 *                       {@code null}.
	 */
	public SingleKeyJWSKeySelector(final JWSAlgorithm expectedJWSAlg, final Key key) {
		if (expectedJWSAlg == null) {
			throw new IllegalArgumentException("The expected JWS algorithm cannot be null");
		}
		if (key == null) {
			throw new IllegalArgumentException("The key cannot be null");
		}
		this.singletonKeyList = Collections.singletonList(key);
		this.expectedJWSAlg = expectedJWSAlg;
	}

	
	@Override
	public List selectJWSKeys(final JWSHeader header, final C context) {
		
		if (! this.expectedJWSAlg.equals(header.getAlgorithm())) {
			return Collections.emptyList();
		}
		
		return this.singletonKeyList;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy