forked from keycloak/keycloak
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
KEYCLOAK-16679 Add algorithm settings for client assertion signature …
…in OIDC identity broker
- Loading branch information
Showing
7 changed files
with
179 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
...test/java/org/keycloak/testsuite/broker/KcOidcBrokerClientSecretJwtCustomSignAlgTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package org.keycloak.testsuite.broker; | ||
|
||
import static org.keycloak.testsuite.broker.BrokerTestConstants.IDP_OIDC_ALIAS; | ||
import static org.keycloak.testsuite.broker.BrokerTestConstants.IDP_OIDC_PROVIDER_ID; | ||
import static org.keycloak.testsuite.broker.BrokerTestTools.createIdentityProvider; | ||
|
||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.UUID; | ||
|
||
import org.keycloak.authentication.authenticators.client.JWTClientSecretAuthenticator; | ||
import org.keycloak.crypto.Algorithm; | ||
import org.keycloak.models.IdentityProviderSyncMode; | ||
import org.keycloak.protocol.oidc.OIDCConfigAttributes; | ||
import org.keycloak.protocol.oidc.OIDCLoginProtocol; | ||
import org.keycloak.representations.idm.ClientRepresentation; | ||
import org.keycloak.representations.idm.IdentityProviderRepresentation; | ||
|
||
public class KcOidcBrokerClientSecretJwtCustomSignAlgTest extends AbstractBrokerTest { | ||
|
||
@Override | ||
protected BrokerConfiguration getBrokerConfiguration() { | ||
return new KcOidcBrokerConfigurationWithJWTAuthentication(); | ||
} | ||
|
||
private class KcOidcBrokerConfigurationWithJWTAuthentication extends KcOidcBrokerConfiguration { | ||
|
||
String clientSecret = UUID.randomUUID().toString(); | ||
String signAlg = Algorithm.HS384; | ||
|
||
@Override | ||
public List<ClientRepresentation> createProviderClients() { | ||
List<ClientRepresentation> clientsRepList = super.createProviderClients(); | ||
log.info("Update provider clients to accept JWT authentication"); | ||
for (ClientRepresentation client : clientsRepList) { | ||
if (client.getAttributes() == null) { | ||
client.setAttributes(new HashMap<String, String>()); | ||
} | ||
client.setClientAuthenticatorType(JWTClientSecretAuthenticator.PROVIDER_ID); | ||
client.setSecret(clientSecret); | ||
client.getAttributes().put(OIDCConfigAttributes.TOKEN_ENDPOINT_AUTH_SIGNING_ALG, signAlg); | ||
} | ||
return clientsRepList; | ||
} | ||
|
||
@Override | ||
public IdentityProviderRepresentation setUpIdentityProvider(IdentityProviderSyncMode syncMode) { | ||
IdentityProviderRepresentation idp = createIdentityProvider(IDP_OIDC_ALIAS, IDP_OIDC_PROVIDER_ID); | ||
Map<String, String> config = idp.getConfig(); | ||
applyDefaultConfiguration(config, syncMode); | ||
config.put("clientAuthMethod", OIDCLoginProtocol.CLIENT_SECRET_JWT); | ||
config.put("clientSecret", clientSecret); | ||
config.put("clientAssertionSigningAlg", signAlg); | ||
return idp; | ||
} | ||
} | ||
} |
92 changes: 92 additions & 0 deletions
92
...c/test/java/org/keycloak/testsuite/broker/KcOidcBrokerPrivateKeyJwtCustomSignAlgTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package org.keycloak.testsuite.broker; | ||
|
||
import org.keycloak.admin.client.Keycloak; | ||
import org.keycloak.authentication.authenticators.client.JWTClientAuthenticator; | ||
import org.keycloak.common.util.MultivaluedHashMap; | ||
import org.keycloak.crypto.Algorithm; | ||
import org.keycloak.keys.GeneratedEcdsaKeyProviderFactory; | ||
import org.keycloak.keys.KeyProvider; | ||
import org.keycloak.models.IdentityProviderSyncMode; | ||
import org.keycloak.protocol.oidc.OIDCConfigAttributes; | ||
import org.keycloak.protocol.oidc.OIDCLoginProtocol; | ||
import org.keycloak.representations.idm.ClientRepresentation; | ||
import org.keycloak.representations.idm.ComponentRepresentation; | ||
import org.keycloak.representations.idm.IdentityProviderRepresentation; | ||
import org.keycloak.testsuite.util.TokenSignatureUtil; | ||
|
||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import javax.ws.rs.core.Response; | ||
|
||
import static org.keycloak.testsuite.broker.BrokerTestConstants.IDP_OIDC_ALIAS; | ||
import static org.keycloak.testsuite.broker.BrokerTestConstants.IDP_OIDC_PROVIDER_ID; | ||
import static org.keycloak.testsuite.broker.BrokerTestConstants.REALM_CONS_NAME; | ||
import static org.keycloak.testsuite.broker.BrokerTestTools.createIdentityProvider; | ||
import static org.keycloak.testsuite.broker.BrokerTestTools.getConsumerRoot; | ||
|
||
public class KcOidcBrokerPrivateKeyJwtCustomSignAlgTest extends AbstractBrokerTest { | ||
|
||
@Override | ||
protected BrokerConfiguration getBrokerConfiguration() { | ||
return new KcOidcBrokerConfigurationWithJWTAuthentication(); | ||
} | ||
|
||
private class KcOidcBrokerConfigurationWithJWTAuthentication extends KcOidcBrokerConfiguration { | ||
|
||
String signAlg = Algorithm.ES256; | ||
|
||
@Override | ||
public List<ClientRepresentation> createProviderClients() { | ||
List<ClientRepresentation> clientsRepList = super.createProviderClients(); | ||
log.info("Update provider clients to accept JWT authentication"); | ||
for (ClientRepresentation client: clientsRepList) { | ||
client.setClientAuthenticatorType(JWTClientAuthenticator.PROVIDER_ID); | ||
if (client.getAttributes() == null) { | ||
client.setAttributes(new HashMap<String, String>()); | ||
} | ||
client.getAttributes().put(OIDCConfigAttributes.TOKEN_ENDPOINT_AUTH_SIGNING_ALG, signAlg); | ||
client.getAttributes().put(OIDCConfigAttributes.USE_JWKS_URL, "true"); | ||
client.getAttributes().put(OIDCConfigAttributes.JWKS_URL, getConsumerRoot() + | ||
"/auth/realms/" + REALM_CONS_NAME + "/protocol/openid-connect/certs"); | ||
} | ||
return clientsRepList; | ||
} | ||
|
||
@Override | ||
public IdentityProviderRepresentation setUpIdentityProvider(IdentityProviderSyncMode syncMode) { | ||
generateEcdsaKeyProvider("valid", signAlg, REALM_CONS_NAME, adminClient); | ||
IdentityProviderRepresentation idp = createIdentityProvider(IDP_OIDC_ALIAS, IDP_OIDC_PROVIDER_ID); | ||
Map<String, String> config = idp.getConfig(); | ||
applyDefaultConfiguration(config, syncMode); | ||
config.put("clientSecret", null); | ||
config.put("clientAuthMethod", OIDCLoginProtocol.PRIVATE_KEY_JWT); | ||
config.put("clientAssertionSigningAlg", signAlg); | ||
return idp; | ||
} | ||
|
||
private void generateEcdsaKeyProvider(String name, String alg, String realmName, Keycloak adminClient) { | ||
ComponentRepresentation rep = createRep(name, | ||
adminClient.realm(realmName).toRepresentation().getId(), GeneratedEcdsaKeyProviderFactory.ID); | ||
long priority = System.currentTimeMillis(); | ||
rep.getConfig().putSingle("priority", Long.toString(priority)); | ||
rep.getConfig().putSingle("active", "true"); | ||
rep.getConfig().putSingle("enabled", "true"); | ||
rep.getConfig().putSingle("ecdsaEllipticCurveKey", | ||
TokenSignatureUtil.convertAlgorithmToECDomainParamNistRep(alg)); | ||
Response response = adminClient.realm(realmName).components().add(rep); | ||
response.close(); | ||
} | ||
|
||
protected ComponentRepresentation createRep(String name, String realmId, String providerId) { | ||
ComponentRepresentation rep = new ComponentRepresentation(); | ||
rep.setName(name); | ||
rep.setParentId(realmId); | ||
rep.setProviderId(providerId); | ||
rep.setProviderType(KeyProvider.class.getName()); | ||
rep.setConfig(new MultivaluedHashMap<>()); | ||
return rep; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters