Skip to content

Commit

Permalink
fix: breaking changes in the keycloak admin client
Browse files Browse the repository at this point in the history
  • Loading branch information
andrejpetras committed Aug 22, 2024
1 parent 7f0519f commit 4eab537
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -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 {

}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -35,6 +37,9 @@ public class KeycloakAdminService {
@Inject
KCClientConfig kcClientConfig;

@Inject
ClientRepresentationMapper mapper;

@ActivateRequestContext
public CreateClientResponse createClient(KeycloakClient keycloakClient) {
var spec = keycloakClient.getSpec();
Expand Down Expand Up @@ -72,15 +77,17 @@ 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 {
// do update
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)
Expand Down

0 comments on commit 4eab537

Please sign in to comment.