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: 9.47
Show newest version
package com.nimbusds.jose.proc;


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

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


/**
 * A {@link JWSKeySelector} that always returns the same {@link Key}.
 *
 * @author Josh Cummings
 * @version 2024-04-20
 */
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) {
		this.singletonKeyList = Collections.singletonList(Objects.requireNonNull(key));
		this.expectedJWSAlg = Objects.requireNonNull(expectedJWSAlg);
	}

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy