diff --git a/integration/client-registration/src/main/java/org/keycloak/client/registration/ClientRegistration.java b/integration/client-registration/src/main/java/org/keycloak/client/registration/ClientRegistration.java index b4ee78afb3..15fe4b9fba 100644 --- a/integration/client-registration/src/main/java/org/keycloak/client/registration/ClientRegistration.java +++ b/integration/client-registration/src/main/java/org/keycloak/client/registration/ClientRegistration.java @@ -17,8 +17,11 @@ package org.keycloak.client.registration; -import com.fasterxml.jackson.annotation.JsonInclude; -import com.fasterxml.jackson.databind.ObjectMapper; +import static java.nio.charset.StandardCharsets.UTF_8; + +import java.io.IOException; +import java.io.InputStream; + import org.apache.http.client.HttpClient; import org.apache.http.impl.client.HttpClients; import org.keycloak.representations.adapters.config.AdapterConfig; @@ -26,8 +29,8 @@ import org.keycloak.representations.idm.ClientRepresentation; import org.keycloak.representations.oidc.OIDCClientRepresentation; import org.keycloak.util.JsonSerialization; -import java.io.IOException; -import java.io.InputStream; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.databind.ObjectMapper; /** * @author Stian Thorgersen @@ -73,7 +76,7 @@ public class ClientRegistration { public ClientRepresentation create(ClientRepresentation client) throws ClientRegistrationException { String content = serialize(client); - InputStream resultStream = httpUtil.doPost(content, JSON, JSON, DEFAULT); + InputStream resultStream = httpUtil.doPost(content, JSON, UTF_8, JSON, DEFAULT); return deserialize(resultStream, ClientRepresentation.class); } @@ -89,7 +92,7 @@ public class ClientRegistration { public ClientRepresentation update(ClientRepresentation client) throws ClientRegistrationException { String content = serialize(client); - InputStream resultStream = httpUtil.doPut(content, JSON, JSON, DEFAULT, client.getClientId()); + InputStream resultStream = httpUtil.doPut(content, JSON, UTF_8, JSON, DEFAULT, client.getClientId()); return resultStream != null ? deserialize(resultStream, ClientRepresentation.class) : null; } @@ -129,7 +132,7 @@ public class ClientRegistration { public OIDCClientRepresentation create(OIDCClientRepresentation client) throws ClientRegistrationException { String content = serialize(client); - InputStream resultStream = httpUtil.doPost(content, JSON, JSON, OIDC); + InputStream resultStream = httpUtil.doPost(content, JSON, UTF_8, JSON, OIDC); return deserialize(resultStream, OIDCClientRepresentation.class); } @@ -140,7 +143,7 @@ public class ClientRegistration { public OIDCClientRepresentation update(OIDCClientRepresentation client) throws ClientRegistrationException { String content = serialize(client); - InputStream resultStream = httpUtil.doPut(content, JSON, JSON, OIDC, client.getClientId()); + InputStream resultStream = httpUtil.doPut(content, JSON, UTF_8, JSON, OIDC, client.getClientId()); return resultStream != null ? deserialize(resultStream, OIDCClientRepresentation.class) : null; } @@ -157,7 +160,7 @@ public class ClientRegistration { public class SAMLClientRegistration { public ClientRepresentation create(String entityDescriptor) throws ClientRegistrationException { - InputStream resultStream = httpUtil.doPost(entityDescriptor, XML, JSON, SAML); + InputStream resultStream = httpUtil.doPost(entityDescriptor, XML, UTF_8, JSON, SAML); return deserialize(resultStream, ClientRepresentation.class); } diff --git a/integration/client-registration/src/main/java/org/keycloak/client/registration/HttpUtil.java b/integration/client-registration/src/main/java/org/keycloak/client/registration/HttpUtil.java index 7ca1361011..f6e65e629e 100644 --- a/integration/client-registration/src/main/java/org/keycloak/client/registration/HttpUtil.java +++ b/integration/client-registration/src/main/java/org/keycloak/client/registration/HttpUtil.java @@ -31,6 +31,7 @@ import org.keycloak.common.util.StreamUtil; import java.io.IOException; import java.io.InputStream; +import java.nio.charset.Charset; /** * @author Stian Thorgersen @@ -52,13 +53,13 @@ class HttpUtil { this.auth = auth; } - InputStream doPost(String content, String contentType, String acceptType, String... path) throws ClientRegistrationException { + InputStream doPost(String content, String contentType, Charset charset, String acceptType, String... path) throws ClientRegistrationException { try { HttpPost request = new HttpPost(getUrl(baseUri, path)); - request.setHeader(HttpHeaders.CONTENT_TYPE, contentType); + request.setHeader(HttpHeaders.CONTENT_TYPE, contentType(contentType, charset)); request.setHeader(HttpHeaders.ACCEPT, acceptType); - request.setEntity(new StringEntity(content)); + request.setEntity(new StringEntity(content, charset)); addAuth(request); @@ -77,6 +78,10 @@ class HttpUtil { throw new ClientRegistrationException("Failed to send request", e); } } + + private String contentType(String contentType, Charset charset) { + return contentType + ";charset=" + charset.name(); + } InputStream doGet(String acceptType, String... path) throws ClientRegistrationException { try { @@ -105,13 +110,13 @@ class HttpUtil { } } - InputStream doPut(String content, String contentType, String acceptType, String... path) throws ClientRegistrationException { + InputStream doPut(String content, String contentType, Charset charset, String acceptType, String... path) throws ClientRegistrationException { try { HttpPut request = new HttpPut(getUrl(baseUri, path)); - request.setHeader(HttpHeaders.CONTENT_TYPE, contentType); + request.setHeader(HttpHeaders.CONTENT_TYPE, contentType(contentType, charset)); request.setHeader(HttpHeaders.ACCEPT, acceptType); - request.setEntity(new StringEntity(content)); + request.setEntity(new StringEntity(content, charset)); addAuth(request); diff --git a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/client/ClientRegistrationTest.java b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/client/ClientRegistrationTest.java index 08cfb7e42a..d5903220d1 100644 --- a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/client/ClientRegistrationTest.java +++ b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/client/ClientRegistrationTest.java @@ -41,11 +41,19 @@ public class ClientRegistrationTest extends AbstractClientRegistrationTest { private static final String CLIENT_ID = "test-client"; private static final String CLIENT_SECRET = "test-client-secret"; - private ClientRepresentation registerClient() throws ClientRegistrationException { - ClientRepresentation client = new ClientRepresentation(); + private ClientRepresentation buildClient() { + ClientRepresentation client = new ClientRepresentation(); client.setClientId(CLIENT_ID); client.setSecret(CLIENT_SECRET); - + + return client; + } + + private ClientRepresentation registerClient() throws ClientRegistrationException { + return registerClient(buildClient()); + } + + private ClientRepresentation registerClient(ClientRepresentation client) throws ClientRegistrationException { ClientRepresentation createdClient = reg.create(client); assertEquals(CLIENT_ID, createdClient.getClientId()); @@ -98,6 +106,17 @@ public class ClientRegistrationTest extends AbstractClientRegistrationTest { } } + @Test + public void registerClientWithNonAsciiChars() throws ClientRegistrationException { + authCreateClients(); + ClientRepresentation client = buildClient(); + String name = "Cli\u00EBnt"; + client.setName(name); + + ClientRepresentation createdClient = registerClient(client); + assertEquals(name, createdClient.getName()); + } + @Test public void getClientAsAdmin() throws ClientRegistrationException { registerClientAsAdmin(); @@ -204,6 +223,20 @@ public class ClientRegistrationTest extends AbstractClientRegistrationTest { } } + @Test + public void updateClientWithNonAsciiChars() throws ClientRegistrationException { + authCreateClients(); + registerClient(); + + authManageClients(); + ClientRepresentation client = reg.get(CLIENT_ID); + String name = "Cli\u00EBnt"; + client.setName(name); + + ClientRepresentation updatedClient = reg.update(client); + assertEquals(name, updatedClient.getName()); + } + private void deleteClient(ClientRepresentation client) throws ClientRegistrationException { reg.delete(CLIENT_ID); try {