KEYCLOAK-4911 charset not specified by Java client registration API

This commit is contained in:
Daan de Wit 2017-05-22 15:17:03 +02:00
parent 29c5d683d0
commit ef6b303bfe
3 changed files with 59 additions and 18 deletions

View file

@ -17,8 +17,11 @@
package org.keycloak.client.registration; package org.keycloak.client.registration;
import com.fasterxml.jackson.annotation.JsonInclude; import static java.nio.charset.StandardCharsets.UTF_8;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.io.InputStream;
import org.apache.http.client.HttpClient; import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.client.HttpClients;
import org.keycloak.representations.adapters.config.AdapterConfig; 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.representations.oidc.OIDCClientRepresentation;
import org.keycloak.util.JsonSerialization; import org.keycloak.util.JsonSerialization;
import java.io.IOException; import com.fasterxml.jackson.annotation.JsonInclude;
import java.io.InputStream; import com.fasterxml.jackson.databind.ObjectMapper;
/** /**
* @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a> * @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a>
@ -73,7 +76,7 @@ public class ClientRegistration {
public ClientRepresentation create(ClientRepresentation client) throws ClientRegistrationException { public ClientRepresentation create(ClientRepresentation client) throws ClientRegistrationException {
String content = serialize(client); 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); return deserialize(resultStream, ClientRepresentation.class);
} }
@ -89,7 +92,7 @@ public class ClientRegistration {
public ClientRepresentation update(ClientRepresentation client) throws ClientRegistrationException { public ClientRepresentation update(ClientRepresentation client) throws ClientRegistrationException {
String content = serialize(client); 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; return resultStream != null ? deserialize(resultStream, ClientRepresentation.class) : null;
} }
@ -129,7 +132,7 @@ public class ClientRegistration {
public OIDCClientRepresentation create(OIDCClientRepresentation client) throws ClientRegistrationException { public OIDCClientRepresentation create(OIDCClientRepresentation client) throws ClientRegistrationException {
String content = serialize(client); 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); return deserialize(resultStream, OIDCClientRepresentation.class);
} }
@ -140,7 +143,7 @@ public class ClientRegistration {
public OIDCClientRepresentation update(OIDCClientRepresentation client) throws ClientRegistrationException { public OIDCClientRepresentation update(OIDCClientRepresentation client) throws ClientRegistrationException {
String content = serialize(client); 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; return resultStream != null ? deserialize(resultStream, OIDCClientRepresentation.class) : null;
} }
@ -157,7 +160,7 @@ public class ClientRegistration {
public class SAMLClientRegistration { public class SAMLClientRegistration {
public ClientRepresentation create(String entityDescriptor) throws ClientRegistrationException { 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); return deserialize(resultStream, ClientRepresentation.class);
} }

View file

@ -31,6 +31,7 @@ import org.keycloak.common.util.StreamUtil;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.nio.charset.Charset;
/** /**
* @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a> * @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a>
@ -52,13 +53,13 @@ class HttpUtil {
this.auth = auth; 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 { try {
HttpPost request = new HttpPost(getUrl(baseUri, path)); 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.setHeader(HttpHeaders.ACCEPT, acceptType);
request.setEntity(new StringEntity(content)); request.setEntity(new StringEntity(content, charset));
addAuth(request); addAuth(request);
@ -77,6 +78,10 @@ class HttpUtil {
throw new ClientRegistrationException("Failed to send request", e); 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 { InputStream doGet(String acceptType, String... path) throws ClientRegistrationException {
try { 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 { try {
HttpPut request = new HttpPut(getUrl(baseUri, path)); 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.setHeader(HttpHeaders.ACCEPT, acceptType);
request.setEntity(new StringEntity(content)); request.setEntity(new StringEntity(content, charset));
addAuth(request); addAuth(request);

View file

@ -41,11 +41,19 @@ public class ClientRegistrationTest extends AbstractClientRegistrationTest {
private static final String CLIENT_ID = "test-client"; private static final String CLIENT_ID = "test-client";
private static final String CLIENT_SECRET = "test-client-secret"; private static final String CLIENT_SECRET = "test-client-secret";
private ClientRepresentation registerClient() throws ClientRegistrationException { private ClientRepresentation buildClient() {
ClientRepresentation client = new ClientRepresentation(); ClientRepresentation client = new ClientRepresentation();
client.setClientId(CLIENT_ID); client.setClientId(CLIENT_ID);
client.setSecret(CLIENT_SECRET); 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); ClientRepresentation createdClient = reg.create(client);
assertEquals(CLIENT_ID, createdClient.getClientId()); 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 @Test
public void getClientAsAdmin() throws ClientRegistrationException { public void getClientAsAdmin() throws ClientRegistrationException {
registerClientAsAdmin(); 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 { private void deleteClient(ClientRepresentation client) throws ClientRegistrationException {
reg.delete(CLIENT_ID); reg.delete(CLIENT_ID);
try { try {