Search in sources :

Example 6 with PublicJsonWebKey

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

the class OidcPrivateKeyJwtAuthenticatorTests method verifyAction.

@Test
public void verifyAction() throws Exception {
    val auth = new OidcPrivateKeyJwtAuthenticator(servicesManager, registeredServiceAccessStrategyEnforcer, ticketRegistry, webApplicationServiceFactory, casProperties, applicationContext);
    val request = new MockHttpServletRequest();
    val response = new MockHttpServletResponse();
    val context = new JEEContext(request, response);
    val audience = casProperties.getServer().getPrefix().concat('/' + OidcConstants.BASE_OIDC_URL + '/' + OidcConstants.ACCESS_TOKEN_URL);
    val registeredService = getOidcRegisteredService();
    registeredService.setClientId(UUID.randomUUID().toString());
    val file = File.createTempFile("jwks-service", ".jwks");
    val core = casProperties.getAuthn().getOidc().getJwks().getCore();
    val jsonWebKey = OidcJsonWebKeyStoreUtils.generateJsonWebKey(core.getJwksType(), core.getJwksKeySize(), OidcJsonWebKeyUsage.SIGNING);
    jsonWebKey.setKeyId("cas-kid");
    val jsonWebKeySet = new JsonWebKeySet(jsonWebKey);
    val data = jsonWebKeySet.toJson(JsonWebKey.OutputControlLevel.INCLUDE_PRIVATE);
    FileUtils.write(file, data, StandardCharsets.UTF_8);
    registeredService.setJwks("file://" + file.getAbsolutePath());
    servicesManager.save(registeredService);
    val claims = getClaims(registeredService.getClientId(), registeredService.getClientId(), registeredService.getClientId(), audience);
    val webKeys = oidcServiceJsonWebKeystoreCache.get(new OidcJsonWebKeyCacheKey(registeredService, OidcJsonWebKeyUsage.SIGNING)).get();
    val key = (PublicJsonWebKey) webKeys.getJsonWebKeys().get(0);
    val jwt = EncodingUtils.signJwsRSASha512(key.getPrivateKey(), claims.toJson().getBytes(StandardCharsets.UTF_8), Map.of());
    val credentials = getCredential(request, OAuth20Constants.CLIENT_ASSERTION_TYPE_JWT_BEARER, new String(jwt, StandardCharsets.UTF_8), registeredService.getClientId());
    auth.validate(credentials, context, JEESessionStore.INSTANCE);
    assertNotNull(credentials.getUserProfile());
}
Also used : lombok.val(lombok.val) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) JEEContext(org.pac4j.core.context.JEEContext) JsonWebKeySet(org.jose4j.jwk.JsonWebKeySet) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) OidcJsonWebKeyCacheKey(org.apereo.cas.oidc.jwks.OidcJsonWebKeyCacheKey) PublicJsonWebKey(org.jose4j.jwk.PublicJsonWebKey) Test(org.junit.jupiter.api.Test)

Example 7 with PublicJsonWebKey

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

the class OAuthBearerTest method createEcJwk.

protected PublicJsonWebKey createEcJwk() throws JoseException {
    PublicJsonWebKey jwk = PublicJsonWebKey.Factory.newPublicJwk("{" + "  \"kty\": \"EC\"," + "  \"d\": \"Tk7qzHNnSBMioAU7NwZ9JugFWmWbUCyzeBRjVcTp_so\"," + "  \"use\": \"sig\"," + "  \"crv\": \"P-256\"," + "  \"kid\": \"key-1\"," + "  \"x\": \"qqeGjWmYZU5M5bBrRw1zqZcbPunoFVxsfaa9JdA0R5I\"," + "  \"y\": \"wnoj0YjheNP80XYh1SEvz1-wnKByEoHvb6KrDcjMuWc\"" + "}");
    jwk.setKeyId("key-1");
    return jwk;
}
Also used : PublicJsonWebKey(org.jose4j.jwk.PublicJsonWebKey)

Example 8 with PublicJsonWebKey

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

the class ValidatorAccessTokenValidatorTest method testInvalidEncryptionAlgorithm.

@Test
public void testInvalidEncryptionAlgorithm() throws Exception {
    PublicJsonWebKey jwk = createRsaJwk();
    assertThrowsWithMessage(InvalidAlgorithmException.class, () -> testEncryptionAlgorithm(jwk, "fake"), "fake is an unknown, unsupported or unavailable alg algorithm");
}
Also used : PublicJsonWebKey(org.jose4j.jwk.PublicJsonWebKey) Test(org.junit.jupiter.api.Test)

Example 9 with PublicJsonWebKey

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

the class ValidatorAccessTokenValidatorTest method testEcdsaEncryptionAlgorithm.

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

Example 10 with PublicJsonWebKey

use of org.jose4j.jwk.PublicJsonWebKey in project oxAuth by GluuFederation.

the class CrossEncryptionTest method testDecryptWithJose4J.

public boolean testDecryptWithJose4J(String jwe) {
    try {
        PublicJsonWebKey jwk = PublicJsonWebKey.Factory.newPublicJwk(recipientJwkJson);
        JsonWebEncryption receiverJwe = new JsonWebEncryption();
        AlgorithmConstraints algConstraints = new AlgorithmConstraints(ConstraintType.WHITELIST, KeyManagementAlgorithmIdentifiers.RSA_OAEP);
        receiverJwe.setAlgorithmConstraints(algConstraints);
        AlgorithmConstraints encConstraints = new AlgorithmConstraints(ConstraintType.WHITELIST, ContentEncryptionAlgorithmIdentifiers.AES_128_GCM);
        receiverJwe.setContentEncryptionAlgorithmConstraints(encConstraints);
        receiverJwe.setKey(jwk.getPrivateKey());
        receiverJwe.setCompactSerialization(jwe);
        final String decryptedPayload = new String(Base64Util.base64urldecode(receiverJwe.getPlaintextString()));
        System.out.println("Jose4j decrypt succeed: " + decryptedPayload);
        if (isJsonEqual(decryptedPayload, PAYLOAD)) {
            return true;
        }
    } catch (Exception e) {
        System.out.println("Jose4j decrypt failed: " + e.getMessage());
        e.printStackTrace();
    }
    return false;
}
Also used : JsonWebEncryption(org.jose4j.jwe.JsonWebEncryption) JSONException(org.json.JSONException) ParseException(java.text.ParseException) InvalidJwtException(org.gluu.oxauth.model.exception.InvalidJwtException) IOException(java.io.IOException) InvalidJweException(org.gluu.oxauth.model.exception.InvalidJweException) PublicJsonWebKey(org.jose4j.jwk.PublicJsonWebKey) AlgorithmConstraints(org.jose4j.jwa.AlgorithmConstraints)

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