Search in sources :

Example 16 with JSONWebKeySet

use of org.keycloak.jose.jwk.JSONWebKeySet in project keycloak by keycloak.

the class OIDCJwksClientRegistrationTest method createClientWithManuallySetKid.

private OIDCClientRepresentation createClientWithManuallySetKid(String kid) throws Exception {
    OIDCClientRepresentation clientRep = createRep();
    clientRep.setGrantTypes(Collections.singletonList(OAuth2Constants.CLIENT_CREDENTIALS));
    clientRep.setTokenEndpointAuthMethod(OIDCLoginProtocol.PRIVATE_KEY_JWT);
    // Generate keys for client
    TestOIDCEndpointsApplicationResource oidcClientEndpointsResource = testingClient.testApp().oidcClientEndpoints();
    oidcClientEndpointsResource.generateKeys("RS256");
    JSONWebKeySet keySet = oidcClientEndpointsResource.getJwks();
    // Override kid with custom value
    keySet.getKeys()[0].setKeyId(kid);
    clientRep.setJwks(keySet);
    return reg.oidc().create(clientRep);
}
Also used : TestOIDCEndpointsApplicationResource(org.keycloak.testsuite.client.resources.TestOIDCEndpointsApplicationResource) OIDCClientRepresentation(org.keycloak.representations.oidc.OIDCClientRepresentation) JSONWebKeySet(org.keycloak.jose.jwk.JSONWebKeySet)

Example 17 with JSONWebKeySet

use of org.keycloak.jose.jwk.JSONWebKeySet in project keycloak by keycloak.

the class ClientAuthSignedJWTTest method setupJwks.

private KeyPair setupJwks(String algorithm, ClientRepresentation clientRepresentation, ClientResource clientResource) throws Exception {
    // generate and register client keypair
    TestOIDCEndpointsApplicationResource oidcClientEndpointsResource = testingClient.testApp().oidcClientEndpoints();
    oidcClientEndpointsResource.generateKeys(algorithm);
    Map<String, String> generatedKeys = oidcClientEndpointsResource.getKeysAsBase64();
    KeyPair keyPair = getKeyPairFromGeneratedBase64(generatedKeys, algorithm);
    // use and set JWKS
    OIDCAdvancedConfigWrapper.fromClientRepresentation(clientRepresentation).setUseJwksString(true);
    JSONWebKeySet keySet = oidcClientEndpointsResource.getJwks();
    OIDCAdvancedConfigWrapper.fromClientRepresentation(clientRepresentation).setJwksString(JsonSerialization.writeValueAsString(keySet));
    clientResource.update(clientRepresentation);
    // set time offset, so that new keys are downloaded
    setTimeOffset(20);
    return keyPair;
}
Also used : KeyPair(java.security.KeyPair) TestOIDCEndpointsApplicationResource(org.keycloak.testsuite.client.resources.TestOIDCEndpointsApplicationResource) JSONWebKeySet(org.keycloak.jose.jwk.JSONWebKeySet)

Example 18 with JSONWebKeySet

use of org.keycloak.jose.jwk.JSONWebKeySet in project keycloak by keycloak.

the class OAuthClient method getRealmPublicKey.

private KeyWrapper getRealmPublicKey(String realm, String algoritm, String kid) {
    boolean loadedKeysFromServer = false;
    JSONWebKeySet jsonWebKeySet = publicKeys.get(realm);
    if (jsonWebKeySet == null) {
        jsonWebKeySet = getRealmKeys(realm);
        publicKeys.put(realm, jsonWebKeySet);
        loadedKeysFromServer = true;
    }
    KeyWrapper key = findKey(jsonWebKeySet, algoritm, kid);
    if (key == null && !loadedKeysFromServer) {
        jsonWebKeySet = getRealmKeys(realm);
        publicKeys.put(realm, jsonWebKeySet);
        key = findKey(jsonWebKeySet, algoritm, kid);
    }
    if (key == null) {
        throw new RuntimeException("Public key for realm:" + realm + ", algorithm: " + algoritm + " not found");
    }
    return key;
}
Also used : KeyWrapper(org.keycloak.crypto.KeyWrapper) JSONWebKeySet(org.keycloak.jose.jwk.JSONWebKeySet)

Aggregations

JSONWebKeySet (org.keycloak.jose.jwk.JSONWebKeySet)18 PublicKey (java.security.PublicKey)7 Test (org.junit.Test)5 JWK (org.keycloak.jose.jwk.JWK)5 OIDCConfigurationRepresentation (org.keycloak.protocol.oidc.representations.OIDCConfigurationRepresentation)4 OIDCClientRepresentation (org.keycloak.representations.oidc.OIDCClientRepresentation)4 KeyWrapper (org.keycloak.crypto.KeyWrapper)3 CertificateRepresentation (org.keycloak.representations.idm.CertificateRepresentation)3 TestOIDCEndpointsApplicationResource (org.keycloak.testsuite.client.resources.TestOIDCEndpointsApplicationResource)3 IOException (java.io.IOException)2 KeyPair (java.security.KeyPair)2 X509Certificate (java.security.cert.X509Certificate)2 List (java.util.List)2 GET (javax.ws.rs.GET)2 NotFoundException (javax.ws.rs.NotFoundException)2 Path (javax.ws.rs.Path)2 Produces (javax.ws.rs.Produces)2 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)2 NoCache (org.jboss.resteasy.annotations.cache.NoCache)2 OIDCAdvancedConfigWrapper (org.keycloak.protocol.oidc.OIDCAdvancedConfigWrapper)2