Search in sources :

Example 1 with JWKMatcher

use of com.nimbusds.jose.jwk.JWKMatcher in project spring-security by spring-projects.

the class JwtDecoderProviderConfigurationUtils method getJWSAlgorithms.

static <C extends SecurityContext> Set<JWSAlgorithm> getJWSAlgorithms(JWKSource<C> jwkSource) {
    JWKMatcher jwkMatcher = new JWKMatcher.Builder().publicOnly(true).keyUses(KeyUse.SIGNATURE, null).keyTypes(KeyType.RSA, KeyType.EC).build();
    Set<JWSAlgorithm> jwsAlgorithms = new HashSet<>();
    try {
        List<? extends JWK> jwks = jwkSource.get(new JWKSelector(jwkMatcher), null);
        for (JWK jwk : jwks) {
            if (jwk.getAlgorithm() != null) {
                JWSAlgorithm jwsAlgorithm = JWSAlgorithm.parse(jwk.getAlgorithm().getName());
                jwsAlgorithms.add(jwsAlgorithm);
            } else {
                if (jwk.getKeyType() == KeyType.RSA) {
                    jwsAlgorithms.addAll(JWSAlgorithm.Family.RSA);
                } else if (jwk.getKeyType() == KeyType.EC) {
                    jwsAlgorithms.addAll(JWSAlgorithm.Family.EC);
                }
            }
        }
    } catch (KeySourceException ex) {
        throw new IllegalStateException(ex);
    }
    Assert.notEmpty(jwsAlgorithms, "Failed to find any algorithms from the JWK set");
    return jwsAlgorithms;
}
Also used : JWKSelector(com.nimbusds.jose.jwk.JWKSelector) UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) JWSAlgorithm(com.nimbusds.jose.JWSAlgorithm) KeySourceException(com.nimbusds.jose.KeySourceException) JWKMatcher(com.nimbusds.jose.jwk.JWKMatcher) HashSet(java.util.HashSet) JWK(com.nimbusds.jose.jwk.JWK)

Aggregations

JWSAlgorithm (com.nimbusds.jose.JWSAlgorithm)1 KeySourceException (com.nimbusds.jose.KeySourceException)1 JWK (com.nimbusds.jose.jwk.JWK)1 JWKMatcher (com.nimbusds.jose.jwk.JWKMatcher)1 JWKSelector (com.nimbusds.jose.jwk.JWKSelector)1 HashSet (java.util.HashSet)1 UriComponentsBuilder (org.springframework.web.util.UriComponentsBuilder)1