From 4eab53732f4f5da26eea888ba6432037d995c866 Mon Sep 17 00:00:00 2001 From: Andrej Petras Date: Thu, 22 Aug 2024 14:26:15 +0200 Subject: [PATCH] fix: breaking changes in the keycloak admin client --- .../operator/models/ClientRepresentationMapper.java | 10 ++++++++++ .../operator/models/ExtClientRepresentation.java | 10 ++++++++++ .../client/operator/service/KeycloakAdminService.java | 11 +++++++++-- 3 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 src/main/java/org/tkit/onecx/iam/kc/client/operator/models/ClientRepresentationMapper.java create mode 100644 src/main/java/org/tkit/onecx/iam/kc/client/operator/models/ExtClientRepresentation.java diff --git a/src/main/java/org/tkit/onecx/iam/kc/client/operator/models/ClientRepresentationMapper.java b/src/main/java/org/tkit/onecx/iam/kc/client/operator/models/ClientRepresentationMapper.java new file mode 100644 index 0000000..1c55ca4 --- /dev/null +++ b/src/main/java/org/tkit/onecx/iam/kc/client/operator/models/ClientRepresentationMapper.java @@ -0,0 +1,10 @@ +package org.tkit.onecx.iam.kc.client.operator.models; + +import org.keycloak.representations.idm.ClientRepresentation; +import org.mapstruct.Mapper; + +@Mapper +public interface ClientRepresentationMapper { + + ExtClientRepresentation create(ClientRepresentation data); +} diff --git a/src/main/java/org/tkit/onecx/iam/kc/client/operator/models/ExtClientRepresentation.java b/src/main/java/org/tkit/onecx/iam/kc/client/operator/models/ExtClientRepresentation.java new file mode 100644 index 0000000..7972d56 --- /dev/null +++ b/src/main/java/org/tkit/onecx/iam/kc/client/operator/models/ExtClientRepresentation.java @@ -0,0 +1,10 @@ +package org.tkit.onecx.iam.kc.client.operator.models; + +import org.keycloak.representations.idm.ClientRepresentation; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; + +@JsonIgnoreProperties({ "type" }) +public class ExtClientRepresentation extends ClientRepresentation { + +} diff --git a/src/main/java/org/tkit/onecx/iam/kc/client/operator/service/KeycloakAdminService.java b/src/main/java/org/tkit/onecx/iam/kc/client/operator/service/KeycloakAdminService.java index 34629bd..b390454 100644 --- a/src/main/java/org/tkit/onecx/iam/kc/client/operator/service/KeycloakAdminService.java +++ b/src/main/java/org/tkit/onecx/iam/kc/client/operator/service/KeycloakAdminService.java @@ -18,6 +18,8 @@ import org.tkit.onecx.iam.kc.client.operator.KeycloakClient; import org.tkit.onecx.iam.kc.client.operator.config.KCClientConfig; import org.tkit.onecx.iam.kc.client.operator.config.KCDefaultConfig; +import org.tkit.onecx.iam.kc.client.operator.models.ClientRepresentationMapper; +import org.tkit.onecx.iam.kc.client.operator.models.ExtClientRepresentation; @ApplicationScoped public class KeycloakAdminService { @@ -35,6 +37,9 @@ public class KeycloakAdminService { @Inject KCClientConfig kcClientConfig; + @Inject + ClientRepresentationMapper mapper; + @ActivateRequestContext public CreateClientResponse createClient(KeycloakClient keycloakClient) { var spec = keycloakClient.getSpec(); @@ -72,7 +77,8 @@ public CreateClientResponse createClient(KeycloakClient keycloakClient) { if (clients.isEmpty()) { // do create - try (var resp = keycloak.realm(realm).clients().create(client)) { + ExtClientRepresentation extClient = mapper.create(client); + try (var resp = keycloak.realm(realm).clients().create(extClient)) { return CreateClientResponse.of(resp.getStatus(), resp.readEntity(String.class)); } } else { @@ -80,7 +86,8 @@ public CreateClientResponse createClient(KeycloakClient keycloakClient) { var defaultClientScopeNames = client.getDefaultClientScopes(); var optionalClientScopeNames = client.getOptionalClientScopes(); var clientToUpdate = keycloak.realm(realm).clients().get(clients.get(0).getId()); - clientToUpdate.update(client); + ExtClientRepresentation extClient = mapper.create(client); + clientToUpdate.update(extClient); // update default client scopes var toRemove = clientToUpdate.getDefaultClientScopes().stream() .filter(rep -> !defaultClientScopeNames.contains(rep.getName())).map(ClientScopeRepresentation::getId)