
com.nimbusds.jose.proc.JWSVerificationKeySelector Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of nimbus-jose-jwt Show documentation
Show all versions of nimbus-jose-jwt Show documentation
Java library for Javascript Object Signing and Encryption (JOSE) and
JSON Web Tokens (JWT)
package com.nimbusds.jose.proc;
import java.security.Key;
import java.security.PublicKey;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import javax.crypto.SecretKey;
import com.nimbusds.jose.JWSAlgorithm;
import com.nimbusds.jose.JWSHeader;
import com.nimbusds.jose.KeySourceException;
import com.nimbusds.jose.jwk.*;
import com.nimbusds.jose.jwk.source.JWKSource;
import net.jcip.annotations.ThreadSafe;
/**
* Key selector for verifying JWS objects, where the key candidates are
* retrieved from a {@link JWKSource JSON Web Key (JWK) source}.
*
* @author Vladimir Dzhuvinov
* @version 2016-06-21
*/
@ThreadSafe
public class JWSVerificationKeySelector extends AbstractJWKSelectorWithSource implements JWSKeySelector {
/**
* The expected JWS algorithm.
*/
private final JWSAlgorithm jwsAlg;
/**
* Creates a new JWS verification key selector.
*
* @param jwsAlg The expected JWS algorithm for the objects to be
* verified. Must not be {@code null}.
* @param jwkSource The JWK source. Must not be {@code null}.
*/
public JWSVerificationKeySelector(final JWSAlgorithm jwsAlg, final JWKSource jwkSource) {
super(jwkSource);
if (jwsAlg == null) {
throw new IllegalArgumentException("The JWS algorithm must not be null");
}
this.jwsAlg = jwsAlg;
}
/**
* Returns the expected JWS algorithm.
*
* @return The expected JWS algorithm.
*/
public JWSAlgorithm getExpectedJWSAlgorithm() {
return jwsAlg;
}
/**
* Creates a JWK matcher for the expected JWS algorithm and the
* specified JWS header.
*
* @param jwsHeader The JWS header. Must not be {@code null}.
*
* @return The JWK matcher, {@code null} if none could be created.
*/
protected JWKMatcher createJWKMatcher(final JWSHeader jwsHeader) {
if (! getExpectedJWSAlgorithm().equals(jwsHeader.getAlgorithm())) {
// Unexpected JWS alg
return null;
} else if (JWSAlgorithm.Family.RSA.contains(getExpectedJWSAlgorithm()) || JWSAlgorithm.Family.EC.contains(getExpectedJWSAlgorithm())) {
// RSA or EC key matcher
return new JWKMatcher.Builder()
.keyType(KeyType.forAlgorithm(getExpectedJWSAlgorithm()))
.keyID(jwsHeader.getKeyID())
.keyUses(KeyUse.SIGNATURE, null)
.algorithms(getExpectedJWSAlgorithm(), null)
.build();
} else if (JWSAlgorithm.Family.HMAC_SHA.contains(getExpectedJWSAlgorithm())) {
// HMAC secret matcher
return new JWKMatcher.Builder()
.keyType(KeyType.forAlgorithm(getExpectedJWSAlgorithm()))
.keyID(jwsHeader.getKeyID())
.privateOnly(true)
.algorithms(getExpectedJWSAlgorithm(), null)
.build();
} else {
return null; // Unsupported algorithm
}
}
@Override
public List selectJWSKeys(final JWSHeader jwsHeader, final C context)
throws KeySourceException {
if (! jwsAlg.equals(jwsHeader.getAlgorithm())) {
// Unexpected JWS alg
return Collections.emptyList();
}
JWKMatcher jwkMatcher = createJWKMatcher(jwsHeader);
if (jwkMatcher == null) {
return Collections.emptyList();
}
List jwkMatches = getJWKSource().get(new JWKSelector(jwkMatcher), context);
List sanitizedKeyList = new LinkedList<>();
for (Key key: KeyConverter.toJavaKeys(jwkMatches)) {
if (key instanceof PublicKey || key instanceof SecretKey) {
sanitizedKeyList.add(key);
} // skip asymmetric private keys
}
return sanitizedKeyList;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy