Search in sources :

Example 1 with PublicJsonWebKey

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

the class ValidatorAccessTokenValidatorTest method testRsaEncryptionAlgorithm.

@Test
public void testRsaEncryptionAlgorithm() throws Exception {
    PublicJsonWebKey jwk = createRsaJwk();
    testEncryptionAlgorithm(jwk, AlgorithmIdentifiers.RSA_USING_SHA256);
}
Also used : PublicJsonWebKey(org.jose4j.jwk.PublicJsonWebKey) Test(org.junit.jupiter.api.Test)

Example 2 with PublicJsonWebKey

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

the class BaseStringCipherExecutor method configureSigningParameters.

private void configureSigningParameters(final String secretKeySigning) {
    var signingKeyToUse = secretKeySigning;
    if (StringUtils.isBlank(signingKeyToUse)) {
        LOGGER.warn("Secret key for signing is not defined for [{}]. CAS will attempt to auto-generate the signing key", getName());
        signingKeyToUse = EncodingUtils.generateJsonWebKey(this.signingKeySize);
        val prop = String.format("%s=%s", getSigningKeySetting(), signingKeyToUse);
        // CHECKSTYLE:OFF
        LOGGER.warn("Generated signing key [{}] of size [{}] for [{}]. The generated key MUST be added to CAS settings:\n\n\t{}\n\n", signingKeyToUse, this.signingKeySize, getName(), prop);
    // CHECKSTYLE:ON
    } else {
        try {
            val jwk = (PublicJsonWebKey) EncodingUtils.newJsonWebKey(signingKeyToUse);
            LOGGER.trace("Parsed signing key as a JSON web key for [{}] with kid [{}]", getName(), jwk.getKeyId());
            if (jwk.getPrivateKey() == null) {
                val msg = "Provided signing key as a JSON web key does not carry a private key";
                LOGGER.error(msg);
                throw new RuntimeException(msg);
            }
            setSigningKey(jwk.getPrivateKey());
        } catch (final Exception e) {
            LOGGER.trace("Unable to recognize signing key for [{}] as a JSON web key: [{}].", getSigningKeySetting(), e.getMessage());
            LOGGER.debug("Using pre-defined signing key to use for [{}]", getSigningKeySetting());
        }
    }
    configureSigningKey(signingKeyToUse);
}
Also used : lombok.val(lombok.val) PublicJsonWebKey(org.jose4j.jwk.PublicJsonWebKey)

Example 3 with PublicJsonWebKey

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

the class BaseOidcJsonWebKeyTokenSigningAndEncryptionService method getJsonWebKeySigningKey.

@Override
protected PublicJsonWebKey getJsonWebKeySigningKey() {
    val iss = issuerService.determineIssuer(Optional.empty());
    LOGGER.trace("Using issuer [{}] to locate JWK signing key", iss);
    val jwks = defaultJsonWebKeystoreCache.get(new OidcJsonWebKeyCacheKey(iss, OidcJsonWebKeyUsage.SIGNING));
    if (Objects.requireNonNull(jwks).isEmpty()) {
        throw new IllegalArgumentException("No signing key could be found for issuer " + iss);
    }
    return (PublicJsonWebKey) jwks.get().getJsonWebKeys().get(0);
}
Also used : lombok.val(lombok.val) OidcJsonWebKeyCacheKey(org.apereo.cas.oidc.jwks.OidcJsonWebKeyCacheKey) PublicJsonWebKey(org.jose4j.jwk.PublicJsonWebKey)

Example 4 with PublicJsonWebKey

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

the class OidcRegisteredServiceJwtAccessTokenCipherExecutor method getEncryptionKeyForDecryption.

private Key getEncryptionKeyForDecryption(final RegisteredService registeredService) {
    val svc = (OAuthRegisteredService) registeredService;
    if (svc instanceof OidcRegisteredService) {
        val jwks = Objects.requireNonNull(this.serviceJsonWebKeystoreCache.get(new OidcJsonWebKeyCacheKey(svc, OidcJsonWebKeyUsage.ENCRYPTION)));
        if (jwks.isEmpty()) {
            LOGGER.warn("Service " + svc.getServiceId() + " with client id " + svc.getClientId() + " is configured to encrypt tokens, yet no JSON web key is available");
            return null;
        }
        val jsonWebKey = (PublicJsonWebKey) jwks.get().getJsonWebKeys().get(0);
        LOGGER.debug("Found JSON web key to encrypt the token: [{}]", jsonWebKey);
        if (jsonWebKey.getPrivateKey() == null) {
            LOGGER.warn("JSON web key used to sign the token has no associated private key");
            return null;
        }
        return jsonWebKey.getPrivateKey();
    }
    return null;
}
Also used : lombok.val(lombok.val) OAuthRegisteredService(org.apereo.cas.support.oauth.services.OAuthRegisteredService) OidcRegisteredService(org.apereo.cas.services.OidcRegisteredService) OidcJsonWebKeyCacheKey(org.apereo.cas.oidc.jwks.OidcJsonWebKeyCacheKey) PublicJsonWebKey(org.jose4j.jwk.PublicJsonWebKey)

Example 5 with PublicJsonWebKey

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

the class BaseOidcJsonWebKeyTokenSigningAndEncryptionService method getJsonWebKeyForEncryption.

/**
 * Gets json web key for encryption.
 *
 * @param svc the svc
 * @return the json web key for encryption
 */
protected PublicJsonWebKey getJsonWebKeyForEncryption(final OAuthRegisteredService svc) {
    LOGGER.debug("Service [{}] is set to encrypt tokens", svc);
    val jwks = serviceJsonWebKeystoreCache.get(new OidcJsonWebKeyCacheKey(svc, OidcJsonWebKeyUsage.ENCRYPTION));
    if (Objects.requireNonNull(jwks).isEmpty()) {
        throw new IllegalArgumentException("Service " + svc.getServiceId() + " with client id " + svc.getClientId() + " is configured to encrypt tokens, yet no JSON web key is available to handle encryption");
    }
    val jsonWebKey = jwks.get().getJsonWebKeys().stream().filter(key -> OidcJsonWebKeystoreRotationService.JsonWebKeyLifecycleStates.getJsonWebKeyState(key).isCurrent()).min(Comparator.comparing(JsonWebKey::getKeyId)).orElseThrow(() -> new IllegalArgumentException("Unable to locate JSON web key for encryption that is marked as current"));
    LOGGER.debug("Found JSON web key to encrypt the token: [{}]", jsonWebKey);
    Objects.requireNonNull(jsonWebKey.getKey(), "JSON web key used to encrypt the token has no associated public key");
    return (PublicJsonWebKey) jsonWebKey;
}
Also used : lombok.val(lombok.val) PublicJsonWebKey(org.jose4j.jwk.PublicJsonWebKey) JsonWebKey(org.jose4j.jwk.JsonWebKey) OidcJsonWebKeyCacheKey(org.apereo.cas.oidc.jwks.OidcJsonWebKeyCacheKey) PublicJsonWebKey(org.jose4j.jwk.PublicJsonWebKey)

Aggregations

PublicJsonWebKey (org.jose4j.jwk.PublicJsonWebKey)10 lombok.val (lombok.val)5 OidcJsonWebKeyCacheKey (org.apereo.cas.oidc.jwks.OidcJsonWebKeyCacheKey)4 Test (org.junit.jupiter.api.Test)4 IOException (java.io.IOException)1 ParseException (java.text.ParseException)1 OidcRegisteredService (org.apereo.cas.services.OidcRegisteredService)1 OAuthRegisteredService (org.apereo.cas.support.oauth.services.OAuthRegisteredService)1 InvalidJweException (org.gluu.oxauth.model.exception.InvalidJweException)1 InvalidJwtException (org.gluu.oxauth.model.exception.InvalidJwtException)1 AlgorithmConstraints (org.jose4j.jwa.AlgorithmConstraints)1 JsonWebEncryption (org.jose4j.jwe.JsonWebEncryption)1 JsonWebKey (org.jose4j.jwk.JsonWebKey)1 JsonWebKeySet (org.jose4j.jwk.JsonWebKeySet)1 JSONException (org.json.JSONException)1 JEEContext (org.pac4j.core.context.JEEContext)1 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)1 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)1