8000 experimentation: Requested/Runtime Representations and defaults by slaskawi · Pull Request #39636 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -19,30 +22,36 @@ 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;

@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<String> appRedirectUrls;
private Set<String> appRedirectUrls = new LinkedHashSet<String>();

@JsonInclude(JsonInclude.Include.NON_EMPTY)
@JsonPropertyDescription("Login flows that are enabled for this client")
private Set<String> loginFlows;
private Set<String> loginFlows = new LinkedHashSet<String>();

@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<String> webOrigins;
private Set<String> webOrigins = new LinkedHashSet<String>();

@JsonInclude(JsonInclude.Include.NON_EMPTY)
@JsonPropertyDescription("Roles associated with this client")
private Set<String> roles;
private Set<String> roles = new LinkedHashSet<String>();

@JsonInclude(JsonInclude.Include.NON_EMPTY)
@JsonPropertyDescription("Service account configuration for this client")
private ServiceAccount serviceAccount;

Expand Down Expand Up @@ -81,6 +90,9 @@ public String getProtocol() {
}

public void setProtocol(String protocol) {
if (protocol == null) {
protocol = OIDC;
}
this.protocol = protocol;
}

Expand Down Expand Up @@ -148,7 +160,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")
Expand Down Expand Up @@ -196,14 +208,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<String> roles;
private Set<String> roles = new LinkedHashSet<String>();

public Boolean getEnabled() {
return enabled;
Expand Down
10BC8
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.keycloak.admin.api.client;

import jakarta.ws.rs.DefaultValue;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.QueryParam;
Expand All @@ -10,5 +11,5 @@ public interface ClientApi {

@GET
@Produces(MediaType.APPLICATION_JSON)
ClientRepresentation getClient(@QueryParam("runtime") Boolean isRuntime);
ClientRepresentation getClient(@QueryParam("runtime") @DefaultValue("false") boolean runtime);
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package org.keycloak.admin.api.client;

import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.DefaultValue;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.PUT;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.QueryParam;
import jakarta.ws.rs.core.MediaType;
import org.keycloak.provider.Provider;
import org.keycloak.representations.admin.v2.ClientRepresentation;
Expand All @@ -18,7 +20,7 @@ public interface ClientsApi extends Provider {
@GET
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
Stream<ClientRepresentation> getClients();
Stream<ClientRepresentation> getClients(@QueryParam("runtime") @DefaultValue("false") boolean runtime);

@PUT
@Consumes(MediaType.APPLICATION_JSON)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.keycloak.admin.api.client;

import jakarta.ws.rs.DefaultValue;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.NotFoundException;
import jakarta.ws.rs.Produces;
Expand All @@ -26,8 +27,14 @@ 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(@QueryParam("runtime") @DefaultValue("false") boolean runtime) {
ClientService.ClientSearchOptions searchOptions = ClientService.ClientSearchOptions.byClientId(clientId);
ClientService.ClientProjectionOptions projectionOptions = ClientService.ClientProjectionOptions.DEFAULT;
if (runtime) {
projectionOptions = ClientService.ClientProjectionOptions.FULL_REPRESENTATION;
}

return clientService.getClient(realm, projectionOptions, searchOptions)
.orElseThrow(() -> new NotFoundException("Cannot find the specified client"));
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package org.keycloak.admin.api.client;

import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.DefaultValue;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.PUT;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.QueryParam;
import jakarta.ws.rs.WebApplicationException;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
Expand Down Expand Up @@ -35,8 +37,15 @@ public DefaultClientsApi(KeycloakSession session, RealmModel realm) {
@GET
@Produces(MediaType.APPLICATION_JSON)
@Override
public Stream<ClientRepresentation> getClients() {
return clientService.getClients(realm);
public Stream<ClientRepresentation> getClients(@QueryParam("runtime") @DefaultValue("false") boolean runtime) {
ClientService.ClientSearchOptions searchOptions = ClientService.ClientSearchOptions.any();
ClientService.ClientProjectionOptions projectionOptions = ClientService.ClientProjectionOptions.DEFAULT;

if (runtime) {
projectionOptions = ClientService.ClientProjectionOptions.FULL_REPRESENTATION;
}

return clientService.getClients(realm, projectionOptions, searchOptions);
}

@PUT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ public interface ClientModelMapper {

ClientRepresentation fromModel(ClientModel model);

// ClientModel toModel(ClientModel baseModel, ClientRepresentation representation);
ClientModel toModel(ClientRepresentation representation);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,55 @@
import org.keycloak.services.ServiceException;

import java.util.Optional;
import java.util.function.Predicate;
import java.util.stream.Stream;

public interface ClientService extends Service {

Optional<ClientRepresentation> getClient(RealmModel realm, String clientId);
abstract class ClientSearchOptions {

Optional<ClientRepresentation> getClient(RealmModel realm, String clientId, Boolean fullRepresentation);
public static ClientSearchOptions any() {
return new ClientSearchOptions() {
@Override
public Predicate<ClientRepresentation> filter() {
return client -> true;
}
};
}

Stream<ClientRepresentation> getClients(RealmModel realm);
public static ClientSearchOptions byClientId(String clientId) {
return new ClientSearchOptions(){
@Override
public Predicate<ClientRepresentation> filter() {
return client -> client.getClientId().equals(clientId);
}
};
}

public abstract Predicate<ClientRepresentation> filter();
}

enum ClientProjectionOptions {
DEFAULT(false),
FULL_REPRESENTATION (true);

Boolean fullRepresentation;

ClientProjectionOptions(Boolean fullRepresentation) {
this.fullRepresentation = fullRepresentation;
}
}

Optional<ClientRepresentation> getClient(RealmModel realm, ClientProjectionOptions projectionOptions, ClientSearchOptions searchOptions);

Stream<ClientRepresentation> getClients(RealmModel realm, ClientProjectionOptions projectionOptions, ClientSearchOptions searchOptions);

ClientRepresentation deleteClient(RealmModel realm, String clientId);

Stream<ClientRepresentation> deleteClients(RealmModel realm, ClientSearchOptions searchOptions);

ClientRepresentation createOrUpdateClient(RealmModel realm, ClientRepresentation client) throws ServiceException;

ClientRepresentation createClient(RealmModel realm, ClientRepresentation client) throws ServiceException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,7 @@ default Set<String> getServiceAccountRoles(Stream<RoleModel> stream) {
.map(RoleModel::getName)
.collect(Collectors.toSet());
}

@Override
ClientModel toModel(ClientRepresentation representation);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,66 @@
import java.util.Optional;
import java.util.stream.Stream;

// TODO
public class DefaultClientService implements ClientService {
private final KeycloakSession session;
private final ClientModelMapper mapper;

private final MockJPAClientRepresentationProvider mockJPAClientRepresentationProvider = new MockJPAClientRepresentationProvider();
private final Defaults defaults = new Defaults();

public DefaultClientService(KeycloakSession session) {
this.session = session;
this.mapper = session.getProvider(ModelMapper.class).clients();
}

@Override
public Optional<ClientRepresentation> getClient(RealmModel realm, String clientId) {
return Optional.ofNullable(realm.getClientByClientId(clientId)).map(mapper::fromModel);
public Optional<ClientRepresentation> getClient(RealmModel realm, ClientProjectionOptions projectionOptions, ClientSearchOptions searchOptions) {
// 1. Depending on the projection options, we either need to search for runtime representations or requested ones.
// The runtime representation is nothing more than ClientModel from the database.
// The requested representation needs to be taken from the ClientRepresentationProvider
Stream<ClientRepresentation> stream = null;
if (projectionOptions == ClientProjectionOptions.FULL_REPRESENTATION) {
stream = mockJPAClientRepresentationProvider.stream();
} else {
stream = realm.getClientsStream()
.map(client -> mapper.fromModel(client));
}

// 2. Returning a collection or a single result is just a results of proper filtering.
stream.filter(searchOptions.filter());

// 3. Finally, we need to return the result as a stream.
return stream.findAny();
}

@Override
public Stream<ClientRepresentation> getClients(RealmModel realm, ClientProjectionOptions projectionOptions, ClientSearchOptions searchOptions) {
return realm.getClientsStream()
.map(client -> projectionOptions != null && Boolean.TRUE.equals(projectionOptions.fullRepresentation)
? mapper.fromModel(client)
: getReducedClientRepresentation(client.getClientId()));
}

@Override
public Optional<ClientRepresentation> getClient(RealmModel realm, String clientId, Boolean fullRepresentation) {
// TODO reduced client rep
return fullRepresentation != null && fullRepresentation ? getClient(realm, clientId) : Optional.of(getTestReducedClientRep(clientId));
public ClientRepresentation deleteClient(RealmModel realm, String clientId) {
var client = realm.getClientByClientId(clientId);
if (client == null) {
throw new ServiceException("Client not found", Response.Status.NOT_FOUND);
}
realm.removeClient(client.getId());
return mapper.fromModel(client);
}

@Override
public Stream<ClientRepresentation> getClients(RealmModel realm) {
return realm.getClientsStream().map(mapper::fromModel);
public Stream<ClientRepresentation> deleteClients(RealmModel realm, ClientSearchOptions searchOptions) {
var clients = realm.getClientsStream().toList();
clients.forEach(client -> realm.removeClient(client.getId()));
return clients.stream().map(mapper::fromModel);
}

@Override
public ClientRepresentation createOrUpdateClient(RealmModel realm, ClientRepresentation client) throws ServiceException {
return null; // TODO
throw new UnsupportedOperationException("Not implemented");
}

@Override
Expand All @@ -48,17 +79,33 @@ public ClientRepresentation createClient(RealmModel realm, ClientRepresentation
throw new ServiceException("Client already exists", Response.Status.CONFLICT);
}

var model = realm.addClient(client.getClientId());
return mapper.fromModel(model);
// 1. At first, we store the Client Representation as it was sent to us. This is the Requested Client Representation
// from https://github.com/keycloak/keycloak/discussions/38551
this.mockJPAClientRepresentationProvider.addClient(client);

// 2. Next, we apply defaults to it. Since the #getClient method returns what was actually requested,
// this needs to happen after persisting the clients to the "requested" Client Representations database table.
var clientWithDefaults = defaults.applyDefaults(client);

// 3. Finally, we create the Client model and persist it in the database. End of the day all Client Representations
// need to be pushed there to maintain backwards compatibility and play nicely with other Keycloak features
var clientModel = mapper.toModel(clientWithDefaults);

// We need a new method. Calling realm.addClient(client.getClientId()) and relying that Hibernates tracks
// the returned value is a leaky abstraction. This should be corrected.
//var model = realm.addClient(clientModel);

return client;
}

@Override
public void close() {

// No resources to close
}

// TODO tested reduced client representation
private static ClientRepresentation getTestReducedClientRep(String clientId) {
private static ClientRepresentation getReducedClientRepresentation(String clientId) {
return new ClientRepresentation(clientId);
}
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.keycloak.services.client;

import org.keycloak.representations.admin.v2.ClientRepresentation;

public class Defaults {

public ClientRepresentation applyDefaults(ClientRepresentation client) {
// More clever and comprehensive logic goes here.
if (client.getEnabled() == null) {
client.setEnabled(true);
}
return client;
}

}
Loading
0