Skip to content

Commit

Permalink
feat: fix client response breaking changes
Browse files Browse the repository at this point in the history
  • Loading branch information
andrejpetras committed Aug 22, 2024
1 parent f5a22ca commit cdeb343
Show file tree
Hide file tree
Showing 3 changed files with 28 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,7 @@
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;

@ApplicationScoped
public class KeycloakAdminService {
Expand All @@ -35,6 +36,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 +76,17 @@ public CreateClientResponse createClient(KeycloakClient keycloakClient) {

if (clients.isEmpty()) {
// do create
try (var resp = keycloak.realm(realm).clients().create(client)) {
var 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);
var 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 cdeb343

Please sign in to comment.