Search in sources :

Example 1 with JsonWebKeySet

use of org.jose4j.jwk.JsonWebKeySet in project cas by apereo.

the class OidcServiceJsonWebKeystoreCacheLoader method load.

@Override
public Optional<RsaJsonWebKey> load(final OidcRegisteredService svc) throws Exception {
    final Optional<JsonWebKeySet> jwks = buildJsonWebKeySet(svc);
    if (!jwks.isPresent() || jwks.get().getJsonWebKeys().isEmpty()) {
        return Optional.empty();
    }
    final RsaJsonWebKey key = getJsonSigningWebKeyFromJwks(jwks.get());
    if (key == null) {
        return Optional.empty();
    }
    return Optional.of(key);
}
Also used : RsaJsonWebKey(org.jose4j.jwk.RsaJsonWebKey) JsonWebKeySet(org.jose4j.jwk.JsonWebKeySet)

Example 2 with JsonWebKeySet

use of org.jose4j.jwk.JsonWebKeySet in project cas by apereo.

the class OidcDefaultJsonWebKeystoreCacheLoader method buildJsonWebKeySet.

private static JsonWebKeySet buildJsonWebKeySet(final String json) throws Exception {
    final JsonWebKeySet jsonWebKeySet = new JsonWebKeySet(json);
    final RsaJsonWebKey webKey = getJsonSigningWebKeyFromJwks(jsonWebKeySet);
    if (webKey == null || webKey.getPrivateKey() == null) {
        LOGGER.warn("JSON web key retrieved [{}] is not found or has no associated private key", webKey);
        return null;
    }
    return jsonWebKeySet;
}
Also used : RsaJsonWebKey(org.jose4j.jwk.RsaJsonWebKey) JsonWebKeySet(org.jose4j.jwk.JsonWebKeySet)

Example 3 with JsonWebKeySet

use of org.jose4j.jwk.JsonWebKeySet in project cas by apereo.

the class OidcServiceJsonWebKeystoreCacheLoader method buildJsonWebKeySet.

private static JsonWebKeySet buildJsonWebKeySet(final String json) throws Exception {
    final JsonWebKeySet jsonWebKeySet = new JsonWebKeySet(json);
    final RsaJsonWebKey webKey = getJsonSigningWebKeyFromJwks(jsonWebKeySet);
    if (webKey == null || webKey.getPublicKey() == null) {
        LOGGER.warn("JSON web key retrieved [{}] is not found or has no associated public key", webKey);
        return null;
    }
    return jsonWebKeySet;
}
Also used : RsaJsonWebKey(org.jose4j.jwk.RsaJsonWebKey) JsonWebKeySet(org.jose4j.jwk.JsonWebKeySet)

Example 4 with JsonWebKeySet

use of org.jose4j.jwk.JsonWebKeySet in project kafka by apache.

the class JwksFileVerificationKeyResolver method init.

@Override
public void init() throws IOException {
    log.debug("Starting creation of new VerificationKeyResolver from {}", jwksFile);
    String json = Utils.readFileAsString(jwksFile.toFile().getPath());
    JsonWebKeySet jwks;
    try {
        jwks = new JsonWebKeySet(json);
    } catch (JoseException e) {
        throw new IOException(e);
    }
    delegate = new JwksVerificationKeyResolver(jwks.getJsonWebKeys());
}
Also used : JoseException(org.jose4j.lang.JoseException) IOException(java.io.IOException) JwksVerificationKeyResolver(org.jose4j.keys.resolvers.JwksVerificationKeyResolver) JsonWebKeySet(org.jose4j.jwk.JsonWebKeySet)

Example 5 with JsonWebKeySet

use of org.jose4j.jwk.JsonWebKeySet in project tomee by apache.

the class PublicKeyResolver method parseJwks.

private Map<String, Key> parseJwks(final String publicKey) {
    final JsonObject jwks;
    try {
        jwks = Json.createReader(new StringReader(publicKey)).readObject();
    } catch (final JsonParsingException e) {
        return Collections.emptyMap();
    }
    try {
        final JsonArray keys = jwks.getJsonArray(JWK_SET_MEMBER_NAME);
        for (final JsonValue key : keys) {
            validateJwk(key.asJsonObject());
        }
    } catch (final Exception e) {
        throw new DeploymentException("MicroProfile Public Key JWKS invalid format.");
    }
    try {
        final JsonWebKeySet keySet = new JsonWebKeySet(publicKey);
        final Map<String, Key> keys = keySet.getJsonWebKeys().stream().collect(Collectors.toMap(JsonWebKey::getKeyId, JsonWebKey::getKey));
        return Collections.unmodifiableMap(keys);
    } catch (final JoseException e) {
        throw new DeploymentException(JWTAuthConfigurationProperties.PUBLIC_KEY_ERROR + " JWK.", e);
    }
}
Also used : JsonArray(javax.json.JsonArray) JoseException(org.jose4j.lang.JoseException) StringReader(java.io.StringReader) JsonValue(javax.json.JsonValue) JsonObject(javax.json.JsonObject) DeploymentException(javax.enterprise.inject.spi.DeploymentException) JsonWebKeySet(org.jose4j.jwk.JsonWebKeySet) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) URISyntaxException(java.net.URISyntaxException) DeploymentException(javax.enterprise.inject.spi.DeploymentException) IOException(java.io.IOException) JoseException(org.jose4j.lang.JoseException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) JsonParsingException(javax.json.stream.JsonParsingException) JsonWebKey(org.jose4j.jwk.JsonWebKey) Key(java.security.Key) JsonParsingException(javax.json.stream.JsonParsingException)

Aggregations

JsonWebKeySet (org.jose4j.jwk.JsonWebKeySet)35 lombok.val (lombok.val)24 Test (org.junit.jupiter.api.Test)14 StringUtils (org.apache.commons.lang3.StringUtils)7 RsaJsonWebKey (org.jose4j.jwk.RsaJsonWebKey)7 Optional (java.util.Optional)6 Slf4j (lombok.extern.slf4j.Slf4j)6 JsonWebKey (org.jose4j.jwk.JsonWebKey)6 PublicJsonWebKey (org.jose4j.jwk.PublicJsonWebKey)6 Resource (org.springframework.core.io.Resource)6 StandardCharsets (java.nio.charset.StandardCharsets)4 IOUtils (org.apache.commons.io.IOUtils)4 CacheLoader (com.github.benmanes.caffeine.cache.CacheLoader)3 RequiredArgsConstructor (lombok.RequiredArgsConstructor)3 SneakyThrows (lombok.SneakyThrows)3 OidcRegisteredService (org.apereo.cas.services.OidcRegisteredService)3 ResponseEntity (org.springframework.http.ResponseEntity)3 GetMapping (org.springframework.web.bind.annotation.GetMapping)3 IOException (java.io.IOException)2 Key (java.security.Key)2