From e790db404ed9db4a30f4e9caba179e736e214dfe Mon Sep 17 00:00:00 2001 From: Steve Hawkins Date: Wed, 7 May 2025 15:34:00 -0400 Subject: [PATCH] refinements related to service methods, serdes and adding a default to representation Signed-off-by: Steve Hawkins --- .../admin/v2/ClientRepresentation.java | 28 ++++++++---- .../keycloak/admin/api/client/ClientApi.java | 2 +- .../admin/api/client/DefaultClientApi.java | 4 +- .../admin/api/client/DefaultClientsApi.java | 4 +- .../services/client/ClientService.java | 41 +++++++++++++++-- .../services/client/DefaultClientService.java | 45 ++++++++++++------- 6 files changed, 91 insertions(+), 33 deletions(-) diff --git a/core/src/main/java/org/keycloak/representations/admin/v2/ClientRepresentation.java b/core/src/main/java/org/keycloak/representations/admin/v2/ClientRepresentation.java index 139a093274a..b59b45e5f07 100644 --- a/core/src/main/java/org/keycloak/representations/admin/v2/ClientRepresentation.java +++ b/core/src/main/java/org/keycloak/representations/admin/v2/ClientRepresentation.java @@ -4,11 +4,14 @@ import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonPropertyDescription; +import java.util.LinkedHashSet; import java.util.Set; -@JsonInclude(JsonInclude.Include.NON_EMPTY) +@JsonInclude(JsonInclude.Include.NON_ABSENT) public class ClientRepresentation { + public static final String OIDC = "openid-connect"; + @JsonProperty(required = true) @JsonPropertyDescription("ID uniquely identifying this client") private String clientId; @@ -19,8 +22,9 @@ public class ClientRepresentation { @JsonPropertyDescription("Human readable description of the client") private String description; + @JsonProperty(defaultValue = OIDC) @JsonPropertyDescription("The protocol used to communicate with the client") - private String protocol; + private String protocol = OIDC; @JsonPropertyDescription("Whether this client is enabled") private Boolean enabled; @@ -28,21 +32,26 @@ public class ClientRepresentation { @JsonPropertyDescription("URL to the application's homepage that is represented by this client") private String appUrl; + @JsonInclude(JsonInclude.Include.NON_EMPTY) @JsonPropertyDescription("URLs that the browser can redirect to after login") - private Set appRedirectUrls; + private Set appRedirectUrls = new LinkedHashSet(); + @JsonInclude(JsonInclude.Include.NON_EMPTY) @JsonPropertyDescription("Login flows that are enabled for this client") - private Set loginFlows; + private Set loginFlows = new LinkedHashSet(); @JsonPropertyDescription("Authentication configuration for this client") private Auth auth; + @JsonInclude(JsonInclude.Include.NON_EMPTY) @JsonPropertyDescription("Web origins that are allowed to make requests to this client") - private Set webOrigins; + private Set webOrigins = new LinkedHashSet(); + @JsonInclude(JsonInclude.Include.NON_EMPTY) @JsonPropertyDescription("Roles associated with this client") - private Set roles; + private Set roles = new LinkedHashSet(); + @JsonInclude(JsonInclude.Include.NON_EMPTY) @JsonPropertyDescription("Service account configuration for this client") private ServiceAccount serviceAccount; @@ -148,7 +157,7 @@ public void setServiceAccount(ServiceAccount serviceAccount) { this.serviceAccount = serviceAccount; } - @JsonInclude(JsonInclude.Include.NON_EMPTY) + @JsonInclude(JsonInclude.Include.NON_ABSENT) public static class Auth { @JsonPropertyDescription("Whether authentication is enabled for this client") @@ -196,14 +205,15 @@ public void setCertificate(String certificate) { } } - @JsonInclude(JsonInclude.Include.NON_EMPTY) + @JsonInclude(JsonInclude.Include.NON_ABSENT) public static class ServiceAccount { @JsonPropertyDescription("Whether the service account is enabled") private Boolean enabled; + @JsonInclude(JsonInclude.Include.NON_EMPTY) @JsonPropertyDescription("Roles assigned to the service account") - private Set roles; + private Set roles = new LinkedHashSet(); public Boolean getEnabled() { return enabled; diff --git a/rest/admin-api/src/main/java/org/keycloak/admin/api/client/ClientApi.java b/rest/admin-api/src/main/java/org/keycloak/admin/api/client/ClientApi.java index 1a40411246e..c1c3a25b334 100644 --- a/rest/admin-api/src/main/java/org/keycloak/admin/api/client/ClientApi.java +++ b/rest/admin-api/src/main/java/org/keycloak/admin/api/client/ClientApi.java @@ -10,5 +10,5 @@ public interface ClientApi { @GET @Produces(MediaType.APPLICATION_JSON) - ClientRepresentation getClient(@QueryParam("runtime") Boolean isRuntime); + ClientRepresentation getClient(); } diff --git a/rest/admin-api/src/main/java/org/keycloak/admin/api/client/DefaultClientApi.java b/rest/admin-api/src/main/java/org/keycloak/admin/api/client/DefaultClientApi.java index 234a1b1dc69..469550964e4 100644 --- a/rest/admin-api/src/main/java/org/keycloak/admin/api/client/DefaultClientApi.java +++ b/rest/admin-api/src/main/java/org/keycloak/admin/api/client/DefaultClientApi.java @@ -26,8 +26,8 @@ public DefaultClientApi(KeycloakSession session, String clientId) { @GET @Produces(MediaType.APPLICATION_JSON) @Override - public ClientRepresentation getClient(@QueryParam("runtime") Boolean isRuntime) { - return clientService.getClient(realm, clientId, isRuntime) + public ClientRepresentation getClient() { + return clientService.getClient(realm, clientId, null) .orElseThrow(() -> new NotFoundException("Cannot find the specified client")); } } diff --git a/rest/admin-api/src/main/java/org/keycloak/admin/api/client/DefaultClientsApi.java b/rest/admin-api/src/main/java/org/keycloak/admin/api/client/DefaultClientsApi.java index 742159bd303..244fd1aab82 100644 --- a/rest/admin-api/src/main/java/org/keycloak/admin/api/client/DefaultClientsApi.java +++ b/rest/admin-api/src/main/java/org/keycloak/admin/api/client/DefaultClientsApi.java @@ -36,9 +36,10 @@ public DefaultClientsApi(KeycloakSession session, RealmModel realm) { @Produces(MediaType.APPLICATION_JSON) @Override public Stream getClients() { - return clientService.getClients(realm); + return clientService.getClients(realm, null, null, null); } + @Override @PUT @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) @@ -52,6 +53,7 @@ public ClientRepresentation createOrUpdateClient(ClientRepresentation client) { } } + @Override @POST @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) diff --git a/server-spi/src/main/java/org/keycloak/services/client/ClientService.java b/server-spi/src/main/java/org/keycloak/services/client/ClientService.java index 2951c09cd22..ac260a40b61 100644 --- a/server-spi/src/main/java/org/keycloak/services/client/ClientService.java +++ b/server-spi/src/main/java/org/keycloak/services/client/ClientService.java @@ -5,18 +5,51 @@ import org.keycloak.services.Service; import org.keycloak.services.ServiceException; +import jakarta.ws.rs.core.Response.Status; + import java.util.Optional; import java.util.stream.Stream; public interface ClientService extends Service { - Optional getClient(RealmModel realm, String clientId); + public static class ClientSearchOptions { + // TODO + } + + public static class ClientProjectionOptions { + // TODO + } + + public static class ClientSortAndSliceOptions { + // order by + // offset + // limit + // NOTE: this is not always the most desirable way to do pagination + } + + Optional getClient(RealmModel realm, String clientId, ClientProjectionOptions projectionOptions); - Optional getClient(RealmModel realm, String clientId, Boolean fullRepresentation); + Stream getClients(RealmModel realm, ClientProjectionOptions projectionOptions, ClientSearchOptions searchOptions, ClientSortAndSliceOptions sortAndSliceOptions); - Stream getClients(RealmModel realm); + ClientRepresentation deleteClient(RealmModel realm, String clientId); - ClientRepresentation createOrUpdateClient(RealmModel realm, ClientRepresentation client) throws ServiceException; + Stream deleteClients(RealmModel realm, ClientSearchOptions searchOptions); + + default ClientRepresentation createOrUpdateClient(RealmModel realm, ClientRepresentation client) throws ServiceException { + // If we can paramertize the services, it makes sense to define default handling like this + // but this duplicate the existence check + try { + return createClient(realm, client); + } catch (ServiceException e) { + if (!e.getSuggestedResponseStatus().filter(Status.CONFLICT::equals).isPresent()) { + throw e; + } + } + return updateClient(realm, client); + } + + ClientRepresentation updateClient(RealmModel realm, ClientRepresentation client) throws ServiceException; ClientRepresentation createClient(RealmModel realm, ClientRepresentation client) throws ServiceException; + } diff --git a/services/src/main/java/org/keycloak/services/client/DefaultClientService.java b/services/src/main/java/org/keycloak/services/client/DefaultClientService.java index 6bd93730700..eb7a8f3e599 100644 --- a/services/src/main/java/org/keycloak/services/client/DefaultClientService.java +++ b/services/src/main/java/org/keycloak/services/client/DefaultClientService.java @@ -1,6 +1,8 @@ package org.keycloak.services.client; import jakarta.ws.rs.core.Response; + +import org.keycloak.models.ClientModel; import org.keycloak.models.KeycloakSession; import org.keycloak.models.RealmModel; import org.keycloak.models.mapper.ClientModelMapper; @@ -22,33 +24,48 @@ public DefaultClientService(KeycloakSession session) { } @Override - public Optional getClient(RealmModel realm, String clientId) { + public Optional getClient(RealmModel realm, String clientId, + ClientProjectionOptions projectionOptions) { return Optional.ofNullable(realm.getClientByClientId(clientId)).map(mapper::fromModel); } @Override - public Optional getClient(RealmModel realm, String clientId, Boolean fullRepresentation) { - // TODO reduced client rep - return fullRepresentation != null && fullRepresentation ? getClient(realm, clientId) : Optional.of(getTestReducedClientRep(clientId)); + public Stream getClients(RealmModel realm, ClientProjectionOptions projectionOptions, + ClientSearchOptions searchOptions, ClientSortAndSliceOptions sortAndSliceOptions) { + return realm.getClientsStream().map(mapper::fromModel); } @Override - public Stream getClients(RealmModel realm) { - return realm.getClientsStream().map(mapper::fromModel); + public ClientRepresentation createClient(RealmModel realm, ClientRepresentation client) throws ServiceException { + if (realm.getClientByClientId(client.getClientId()) != null) { + throw new ServiceException("Client already exists", Response.Status.CONFLICT); + } + + var model = realm.addClient(client.getClientId()); + // TODO: overlay model + return mapper.fromModel(model); } @Override - public ClientRepresentation createOrUpdateClient(RealmModel realm, ClientRepresentation client) throws ServiceException { - return null; // TODO + public ClientRepresentation deleteClient(RealmModel realm, String clientId) { + // TODO Auto-generated method stub + return null; } @Override - public ClientRepresentation createClient(RealmModel realm, ClientRepresentation client) throws ServiceException { - if (realm.getClientByClientId(client.getClientId()) != null) { - throw new ServiceException("Client already exists", Response.Status.CONFLICT); + public Stream deleteClients(RealmModel realm, ClientSearchOptions searchOptions) { + // TODO Auto-generated method stub + return null; + } + + @Override + public ClientRepresentation updateClient(RealmModel realm, ClientRepresentation client) throws ServiceException { + ClientModel model = realm.getClientByClientId(client.getClientId()); + if (model == null) { + throw new ServiceException("Client does not exist", Response.Status.NOT_FOUND); } - var model = realm.addClient(client.getClientId()); + // TODO: overlay model return mapper.fromModel(model); } @@ -57,8 +74,4 @@ public void close() { } - // TODO tested reduced client representation - private static ClientRepresentation getTestReducedClientRep(String clientId) { - return new ClientRepresentation(clientId); - } }