8000 Add client type support to Admin API v2 by arnabnandy7 · Pull Request #50895 · 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
5 changes: 5 additions & 0 deletions js/libs/keycloak-admin-client/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@
"maxLength" : 255,
"description" : "ID uniquely identifying this client. Validation: must not be blank; size must be between 1 and 255"
},
"type" : {
"type" : "string",
"maxLength" : 255,
"description" : "Client type. Validation: size must be between 0 and 255; on update/on patch: type cannot be changed for an existing client"
},
"displayName" : {
"type" : "string",
"maxLength" : 255,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@
affectedFieldNames = {"protocol"},
message = "protocol cannot be changed for an existing client",
groups = {PutClient.class, PatchClient.class})
@ServerManagedFieldUnmodified(
affectedFieldNames = {"type"},
message = "type cannot be changed for an existing client",
groups = {PutClient.class, PatchClient.class})
@ValidRedirectUris
public class BaseClientRepresentation extends BaseRepresentation {
public static final String DISCRIMINATOR_FIELD = "protocol";
Expand All @@ -52,6 +56,10 @@ public class BaseClientRepresentation extends BaseRepresentation {
@JsonPropertyDescription("ID uniquely identifying this client")
protected String clientId;

@Size(max = 255)
@JsonPropertyDescription("Client type")
private String type;

@Size(max = 255)
@JsonPropertyDescription("Human readable name of the client")
private String displayName;
Expand Down Expand Up @@ -105,11 +113,21 @@ public void setClientId(String clientId) {
this.clientId = clientId;
}

public String getType() {
return type;
}

public void setType(String type) {
markFieldAsExplicitlySet("type");
this.type = type;
}
Comment thread
arnabnandy7 marked this conversation as resolved.
Resolved

public String getDisplayName() {
return displayName;
}

public void setDisplayName(String displayName) {
markFieldAsExplicitlySet("displayName");
this.displayName = displayName;
}

Expand All @@ -118,6 +136,7 @@ public String getDescription() {
}

public void setDescription(String description) {
markFieldAsExplicitlySet("description");
this.description = description;
}

Expand All @@ -126,6 +145,7 @@ public Boolean getEnabled() {
}

public void setEnabled(Boolean enabled) {
markFieldAsExplicitlySet("enabled");
this.enabled = enabled;
}

Expand All @@ -134,6 +154,7 @@ public String getAppUrl() {
}

public void setAppUrl(String appUrl) {
markFieldAsExplicitlySet("appUrl");
this.appUrl = appUrl;
}

Expand All @@ -142,15 +163,17 @@ public Set<String> getRedirectUris() {
}

public void setRedirectUris(Set<String> redirectUris) {
this.redirectUris = redirectUris;
markFieldAsExplicitlySet("redirectUris");
this.redirectUris = redirectUris == null ? new LinkedHashSet<>() : new LinkedHashSet<>(redirectUris);
}

Comment thread
arnabnandy7 marked this conversation as resolved.
Resolved
public Set<String> getRoles() {
return roles;
}

public void setRoles(Set<String> roles) {
this.roles = roles;
markFieldAsExplicitlySet("roles");
this.roles = roles == null ? new LinkedHashSet<>() : new LinkedHashSet<>(roles);
}

public String getProtocol() {
Expand Down Expand Up @@ -183,19 +206,20 @@ public boolean equals(Object o) {
return false;
}
BaseClientRepresentation that = (BaseClientRepresentation)o;
return Objects.equals(uuid, that.uuid) && Objects.equals(clientId, that.clientId) && Objects.equals(displayName, that.displayName) && Objects.equals(description, that.description) && Objects.equals(enabled, that.enabled) && Objects.equals(appUrl, that.appUrl) && Objects.equals(redirectUris, that.redirectUris) && Objects.equals(roles, that.roles) && Objects.equals(protocol, that.protocol);
return Objects.equals(uuid, that.uuid) && Objects.equals(clientId, that.clientId) && Objects.equals(type, that.type) && Objects.equals(displayName, that.displayName) && Objects.equals(description, that.description) && Objects.equals(enabled, that.enabled) && Objects.equals(appUrl, that.appUrl) && Objects.equals(redirectUris, that.redirectUris) && Objects.equals(roles, that.roles) && Objects.equals(protocol, that.protocol);
}

@Override
public int hashCode() {
return Objects.hash(uuid, clientId, displayName, description, enabled, appUrl, redirectUris, roles, protocol);
return Objects.hash(uuid, clientId, type, displayName, description, enabled, appUrl, redirectUris, roles, protocol);
}

@Override
public String toString() {
return getClass().getSimpleName() + "{" + "protocol=" + getProtocol()
+ ", uuid=" + uuid
+ ", clientId=" + clientId
+ ", type=" + type
+ ", displayName=" + displayName
+ ", description=" + description
+ ", enabled=" + enabled
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package org.keycloak.representations.admin.v2;

import java.util.Linked 8BD0 HashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;

import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
Expand All @@ -15,6 +17,9 @@ public class BaseRepresentation {
@JsonIgnore
protected Map<String, Object> additionalFields = new LinkedHashMap<String, Object>();

@JsonIgnore
private final Set<String> explicitlySetFields = new LinkedHashSet<>();

@JsonAnyGetter
public Map<String, Object> getAdditionalFields() {
return additionalFields;
Expand All @@ -28,4 +33,17 @@ public void setAdditionalField(String name, Object value) {
public void setAdditionalFields(Map<String, Object> additionalFields) {
this.additionalFields = additionalFields;
}

protected void markFieldAsExplicitlySet(String fieldName) {
explicitlySetFields.add(fieldName);
}

@JsonIgnore
public boolean isFieldExplicitlySet(String fieldName) {
return explicitlySetFields.contains(fieldName);
}

public void clearExplicitlySetFields() {
explicitlySetFields.clear();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,16 @@ public Set<Flow> getLoginFlows() {
}

public void setLoginFlows(Set<Flow> loginFlows) {
this.loginFlows = loginFlows;
markFieldAsExplicitlySet("loginFlows");
this.loginFlows = loginFlows == null ? new LinkedHashSet<>() : new LinkedHashSet<>(loginFlows);
}
Comment thread
arnabnandy7 marked this conversation as resolved.
Resolved

public Auth getAuth() {
return auth;
}

public void setAuth(Auth auth) {
markFieldAsExplicitlySet("auth");
this.auth = auth;
}

Expand All @@ -87,15 +89,17 @@ public Set<String> getWebOrigins() {
}

public void setWebOrigins(Set<String> webOrigins) {
this.webOrigins = webOrigins;
markFieldAsExplicitlySet("webOrigins");
this.webOrigins = webOrigins == null ? new LinkedHashSet<>() : new LinkedHashSet<>(webOrigins);
}
Comment thread
arnabnandy7 marked this conversation as resolved.
Resolved

public Set<String> getServiceAccountRoles() {
return serviceAccountRoles;
}

public void setServiceAccountRoles(Set<String> serviceAccountRoles) {
this.serviceAccountRoles = serviceAccountRoles;
markFieldAsExplicitlySet("serviceAccountRoles");
this.serviceAccountRoles = serviceAccountRoles == null ? new LinkedHashSet<>() : new LinkedHashSet<>(serviceAccountRoles);
}

@ClientSecretNotBlank(groups = PutClient.class, affectedFieldNames = {"secret"})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ public Boolean getForceNameIdFormat() {
}

public void setForceNameIdFormat(Boolean forceNameIdFormat) {
markFieldAsExplicitlySet("forceNameIdFormat");
this.forceNameIdFormat = forceNameIdFormat;
}

Expand All @@ -122,6 +123,7 @@ public Boolean getIncludeAuthnStatement() {
}

public void setIncludeAuthnStatement(Boolean includeAuthnStatement) {
markFieldAsExplicitlySet("includeAuthnStatement");
this.includeAuthnStatement = includeAuthnStatement;
}

Expand All @@ -130,6 +132,7 @@ public Boolean getSignDocuments() {
}

public void setSignDocuments(Boolean signDocuments) {
markFieldAsExplicitlySet("signDocuments");
this.signDocuments = signDocuments;
}

Expand All @@ -138,6 +141,7 @@ public Boolean getSignAssertions() {
}

public void setSignAssertions(Boolean signAssertions) {
markFieldAsExplicitlySet("signAssertions");
this.signAssertions = signAssertions;
}

Expand All @@ -146,6 +150,7 @@ public Boolean getClientSignatureRequired() {
}

public void setClientSignatureRequired(Boolean clientSignatureRequired) {
markFieldAsExplicitlySet("clientSignatureRequired");
this.clientSignatureRequired = clientSignatureRequired;
}

Expand All @@ -154,6 +159,7 @@ public Boolean getForcePostBinding() {
}

public void setForcePostBinding(Boolean forcePostBinding) {
markFieldAsExplicitlySet("forcePostBinding");
this.forcePostBinding = forcePostBinding;
}

Expand All @@ -162,6 +168,7 @@ public Boolean getFrontChannelLogout() {
}

public void setFrontChannelLogout(Boolean frontChannelLogout) {
markFieldAsExplicitlySet("frontChannelLogout");
this.frontChannelLogout = frontChannelLogout;
}

Expand All @@ -170,6 +177,7 @@ public SignatureAlgorithm getSignatureAlgorithm() {
}

public void setSignatureAlgorithm(SignatureAlgorithm signatureAlgorithm) {
markFieldAsExplicitlySet("signatureAlgorithm");
this.signatureAlgorithm = signatureAlgorithm;
}

Expand All @@ -178,6 +186,7 @@ public String getSignatureCanonicalizationMethod() {
}

public void setSignatureCanonicalizationMethod(String signatureCanonicalizationMethod) {
markFieldAsExplicitlySet("signatureCanonicalizationMethod");
this.signatureCanonicalizationMethod = signatureCanonicalizationMethod;
}

Expand All @@ -186,6 +195,7 @@ public String getSigningCertificate() {
}

public void setSigningCertificate(String signingCertificate) {
markFieldAsExplicitlySet("signingCertificate");
this.signingCertificate = signingCertificate;
}

Expand All @@ -194,6 +204,7 @@ public Boolean getAllowEcpFlow() {
}

public void setAllowEcpFlow(Boolean allowEcpFlow) {
markFieldAsExplicitlySet("allowEcpFlow");
this.allowEcpFlow = allowEcpFlow;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

public class ServiceException extends RuntimeException {
private Response.Status suggestedHttpResponseStatus;
private Object[] parameters;

public ServiceException(String message) {
super(message);
Expand All @@ -21,6 +22,11 @@ public ServiceException(String message, Response.Status suggestedStatus) {
this.suggestedHttpResponseStatus = suggestedStatus;
}

public ServiceException(String message, Object[] parameters, Response.Status suggestedStatus) {
this(message, suggestedStatus);
this.parameters = parameters == null ? null : parameters.clone();
}
Comment thread
arnabnandy7 marked this conversation as resolved.
Resolved

public ServiceException(Response.Status suggestedStatus) {
super();
this.suggestedHttpResponseStatus = suggestedStatus;
Expand All @@ -30,6 +36,10 @@ public Optional<Response.Status> getSuggestedResponseStatus() {
return Optional.ofNullable(suggestedHttpResponseStatus);
}

public Optional<Object[]> getParameters() {
return Optional.ofNullable(parameters == null ? null : parameters.clone());
}
Comment thread
arnabnandy7 marked this conversation as resolved.
Resolved

public WebApplicationException toWebApplicationException() {
return toWebApplicationException(Response.Status.BAD_REQUEST);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
package org.keycloak.services;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Optional;

import jakarta.ws.rs.core.Context;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.ext.ExceptionMapper;
import jakarta.ws.rs.ext.Provider;

import org.keycloak.models.KeycloakSession;
import org.keycloak.representations.idm.ErrorRepresentation;
import org.keycloak.representations.idm.OAuth2ErrorRepresentation;
import org.keycloak.services.error.KeycloakErrorHandler;

/**
Expand All @@ -21,6 +28,40 @@ public class ServiceExceptionMapper implements ExceptionMapper<ServiceException>

@Override
public Response toResponse(ServiceException exception) {
return KeycloakErrorHandler.getResponse(session, exception.toWebApplicationException());
Response response = KeycloakErrorHandler.getResponse(session, exception.toWebApplicationException());
Optional<Object[]> parameters = exception.getParameters();
if (exception.getMessage() != null && parameters.isPresent()
&& response.getMediaType() != null && MediaType.APPLICATION_JSON_TYPE.isCompatible(response.getMediaType())) {
return Response.fromResponse(response)
.entity(addParameters(response.getEntity(), exception.getMessage(), parameters.get()))
.build();
}
return response;
}

static Object addParameters(Object entity, String message, Object[] parameters) {
Object[] stableParameters = parameters.clone();
if (entity instanceof ErrorRepresentation error) {
if (error.getErrorMessage() == null) {
error.setErrorMessage(message);
}
error.setParams(stableParameters);
return error;
}
if (entity instanceof OAuth2ErrorRepresentation error) {
Map<String, Object> enriched = new LinkedHashMap<>();
enriched.put("error", error.getError());
if (error.getErrorDescription() != null) {
enriched.put("error_description", error.getErrorDescription());
}
enriched.put("errorMessage", message);
enriched.put("params", stableParameters);
return enriched;
}

ErrorRepresentation error = new ErrorRepresentation();
error.setErrorMessage(message);
error.setParams(stableParameters);
return error;
}
Comment thread
arnabnandy7 marked this conversation as resolved.
Resolved
}
Loading
Loading
0