From 20ac70d3fd095ece28a8a4bc2e7bf6009f76af28 Mon Sep 17 00:00:00 2001 From: Stian Thorgersen Date: Tue, 22 Aug 2017 08:07:36 +0200 Subject: [PATCH] KEYCLOAK-5119 (#4400) --- .../examples/rest/HelloResourceProvider.java | 3 +- .../keycloak/protocol/saml/SamlService.java | 4 +- .../services/resources/AccountService.java | 37 +++++++------------ .../java/org/keycloak/utils/MediaType.java | 4 ++ .../rest/TestApplicationResourceProvider.java | 15 ++++---- .../rest/resource/TestCacheResource.java | 5 +-- .../resources/TestApplicationResource.java | 6 +-- .../resources/TestingCacheResource.java | 12 +++--- .../client/resources/TestingResource.java | 9 ++--- 9 files changed, 40 insertions(+), 55 deletions(-) diff --git a/examples/providers/rest/src/main/java/org/keycloak/examples/rest/HelloResourceProvider.java b/examples/providers/rest/src/main/java/org/keycloak/examples/rest/HelloResourceProvider.java index aebd677221..ebd8d1c991 100644 --- a/examples/providers/rest/src/main/java/org/keycloak/examples/rest/HelloResourceProvider.java +++ b/examples/providers/rest/src/main/java/org/keycloak/examples/rest/HelloResourceProvider.java @@ -22,7 +22,6 @@ import org.keycloak.services.resource.RealmResourceProvider; import javax.ws.rs.GET; import javax.ws.rs.Produces; -import javax.ws.rs.core.MediaType; /** * @author Stian Thorgersen @@ -41,7 +40,7 @@ public class HelloResourceProvider implements RealmResourceProvider { } @GET - @Produces(MediaType.TEXT_PLAIN) + @Produces("text/plain; charset=utf-8") public String get() { String name = session.getContext().getRealm().getDisplayName(); if (name == null) { diff --git a/services/src/main/java/org/keycloak/protocol/saml/SamlService.java b/services/src/main/java/org/keycloak/protocol/saml/SamlService.java index 589dde3e6b..d63c98c144 100755 --- a/services/src/main/java/org/keycloak/protocol/saml/SamlService.java +++ b/services/src/main/java/org/keycloak/protocol/saml/SamlService.java @@ -58,6 +58,7 @@ import org.keycloak.services.managers.AuthenticationManager; import org.keycloak.services.messages.Messages; import org.keycloak.services.resources.RealmsResource; import org.keycloak.services.util.CacheControlUtil; +import org.keycloak.utils.MediaType; import javax.ws.rs.Consumes; import javax.ws.rs.FormParam; @@ -67,7 +68,6 @@ import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; -import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import javax.ws.rs.core.UriInfo; import java.io.IOException; @@ -594,7 +594,7 @@ public class SamlService extends AuthorizationEndpointBase { @GET @Path("clients/{client}") - @Produces(MediaType.TEXT_HTML) + @Produces(MediaType.TEXT_HTML_UTF_8) public Response idpInitiatedSSO(@PathParam("client") String clientUrlName, @QueryParam("RelayState") String relayState) { event.event(EventType.LOGIN); CacheControlUtil.noBackButtonCacheControlHeader(); diff --git a/services/src/main/java/org/keycloak/services/resources/AccountService.java b/services/src/main/java/org/keycloak/services/resources/AccountService.java index ac9bf807f6..b814abdb9c 100755 --- a/services/src/main/java/org/keycloak/services/resources/AccountService.java +++ b/services/src/main/java/org/keycloak/services/resources/AccountService.java @@ -59,9 +59,9 @@ import org.keycloak.services.validation.Validation; import org.keycloak.sessions.AuthenticationSessionModel; import org.keycloak.storage.ReadOnlyException; import org.keycloak.util.JsonSerialization; +import org.keycloak.utils.MediaType; import javax.ws.rs.*; -import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MultivaluedMap; import javax.ws.rs.core.Response; import javax.ws.rs.core.UriBuilder; @@ -252,33 +252,24 @@ public class AccountService extends AbstractSecuredLocalService { */ @Path("/") @GET - @Produces(MediaType.TEXT_HTML) public Response accountPage() { - return forwardToPage(null, AccountPages.ACCOUNT); - } + if (session.getContext().getRequestHeaders().getAcceptableMediaTypes().contains(MediaType.APPLICATION_JSON_TYPE)) { + requireOneOf(AccountRoles.MANAGE_ACCOUNT, AccountRoles.VIEW_PROFILE); - /** - * Get account information. - * - * @return - */ - @Path("/") - @GET - @Produces(MediaType.APPLICATION_JSON) - public Response accountPageJson() { - requireOneOf(AccountRoles.MANAGE_ACCOUNT, AccountRoles.VIEW_PROFILE); - - UserRepresentation rep = ModelToRepresentation.toRepresentation(session, realm, auth.getUser()); - if (rep.getAttributes() != null) { - Iterator itr = rep.getAttributes().keySet().iterator(); - while (itr.hasNext()) { - if (itr.next().startsWith("keycloak.")) { - itr.remove(); + UserRepresentation rep = ModelToRepresentation.toRepresentation(session, realm, auth.getUser()); + if (rep.getAttributes() != null) { + Iterator itr = rep.getAttributes().keySet().iterator(); + while (itr.hasNext()) { + if (itr.next().startsWith("keycloak.")) { + itr.remove(); + } } } - } - return Cors.add(request, Response.ok(rep)).auth().allowedOrigins(auth.getToken()).build(); + return Cors.add(request, Response.ok(rep).type(MediaType.APPLICATION_JSON_TYPE)).auth().allowedOrigins(auth.getToken()).build(); + } else { + return forwardToPage(null, AccountPages.ACCOUNT); + } } public static UriBuilder totpUrl(UriBuilder base) { diff --git a/services/src/main/java/org/keycloak/utils/MediaType.java b/services/src/main/java/org/keycloak/utils/MediaType.java index acb0b4134e..4842f384f3 100644 --- a/services/src/main/java/org/keycloak/utils/MediaType.java +++ b/services/src/main/java/org/keycloak/utils/MediaType.java @@ -40,4 +40,8 @@ public class MediaType { public static final String APPLICATION_JWT = "application/jwt"; public static final javax.ws.rs.core.MediaType APPLICATION_JWT_TYPE = new javax.ws.rs.core.MediaType("application", "jwt"); + public static final String APPLICATION_XML = javax.ws.rs.core.MediaType.APPLICATION_XML; + + public static final String TEXT_XML = javax.ws.rs.core.MediaType.TEXT_XML; + } diff --git a/testsuite/integration-arquillian/servers/auth-server/services/testsuite-providers/src/main/java/org/keycloak/testsuite/rest/TestApplicationResourceProvider.java b/testsuite/integration-arquillian/servers/auth-server/services/testsuite-providers/src/main/java/org/keycloak/testsuite/rest/TestApplicationResourceProvider.java index f47061a305..988108809d 100644 --- a/testsuite/integration-arquillian/servers/auth-server/services/testsuite-providers/src/main/java/org/keycloak/testsuite/rest/TestApplicationResourceProvider.java +++ b/testsuite/integration-arquillian/servers/auth-server/services/testsuite-providers/src/main/java/org/keycloak/testsuite/rest/TestApplicationResourceProvider.java @@ -17,7 +17,6 @@ package org.keycloak.testsuite.rest; -import org.jboss.resteasy.annotations.Query; import org.jboss.resteasy.annotations.cache.NoCache; import org.jboss.resteasy.spi.HttpRequest; import org.jboss.resteasy.spi.ResteasyProviderFactory; @@ -30,6 +29,7 @@ import org.keycloak.representations.adapters.action.TestAvailabilityAction; import org.keycloak.services.resource.RealmResourceProvider; import org.keycloak.services.resources.RealmsResource; import org.keycloak.testsuite.rest.resource.TestingOIDCEndpointsApplicationResource; +import org.keycloak.utils.MediaType; import javax.ws.rs.Consumes; import javax.ws.rs.GET; @@ -38,7 +38,6 @@ import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; -import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MultivaluedMap; import javax.ws.rs.core.Response; import javax.ws.rs.core.UriBuilder; @@ -69,21 +68,21 @@ public class TestApplicationResourceProvider implements RealmResourceProvider { } @POST - @Consumes(MediaType.TEXT_PLAIN) + @Consumes(MediaType.TEXT_PLAIN_UTF_8) @Path("/admin/k_logout") public void adminLogout(String data) throws JWSInputException { adminLogoutActions.add(new JWSInput(data).readJsonContent(LogoutAction.class)); } @POST - @Consumes(MediaType.TEXT_PLAIN) + @Consumes(MediaType.TEXT_PLAIN_UTF_8) @Path("/admin/k_push_not_before") public void adminPushNotBefore(String data) throws JWSInputException { adminPushNotBeforeActions.add(new JWSInput(data).readJsonContent(PushNotBeforeAction.class)); } @POST - @Consumes(MediaType.TEXT_PLAIN) + @Consumes(MediaType.TEXT_PLAIN_UTF_8) @Path("/admin/k_test_available") public void testAvailable(String data) throws JWSInputException { adminTestAvailabilityAction.add(new JWSInput(data).readJsonContent(TestAvailabilityAction.class)); @@ -119,7 +118,7 @@ public class TestApplicationResourceProvider implements RealmResourceProvider { } @POST - @Produces(MediaType.TEXT_HTML) + @Produces(MediaType.TEXT_HTML_UTF_8) @Path("/{action}") public String post(@PathParam("action") String action) { String title = "APP_REQUEST"; @@ -148,7 +147,7 @@ public class TestApplicationResourceProvider implements RealmResourceProvider { } @GET - @Produces(MediaType.TEXT_HTML) + @Produces(MediaType.TEXT_HTML_UTF_8) @Path("/{action}") public String get(@PathParam("action") String action) { //String requestUri = session.getContext().getUri().getRequestUri().toString(); @@ -171,7 +170,7 @@ public class TestApplicationResourceProvider implements RealmResourceProvider { @GET @NoCache - @Produces(MediaType.TEXT_HTML) + @Produces(MediaType.TEXT_HTML_UTF_8) @Path("/get-account-profile") public String getAccountProfile(@QueryParam("token") String token, @QueryParam("account-uri") String accountUri) { StringBuilder sb = new StringBuilder(); diff --git a/testsuite/integration-arquillian/servers/auth-server/services/testsuite-providers/src/main/java/org/keycloak/testsuite/rest/resource/TestCacheResource.java b/testsuite/integration-arquillian/servers/auth-server/services/testsuite-providers/src/main/java/org/keycloak/testsuite/rest/resource/TestCacheResource.java index 9847b27dcb..964e80da8b 100644 --- a/testsuite/integration-arquillian/servers/auth-server/services/testsuite-providers/src/main/java/org/keycloak/testsuite/rest/resource/TestCacheResource.java +++ b/testsuite/integration-arquillian/servers/auth-server/services/testsuite-providers/src/main/java/org/keycloak/testsuite/rest/resource/TestCacheResource.java @@ -27,18 +27,17 @@ import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; -import javax.ws.rs.core.MediaType; import org.infinispan.Cache; import org.infinispan.client.hotrod.RemoteCache; import org.infinispan.remoting.transport.Transport; -import org.infinispan.remoting.transport.jgroups.JGroupsTransport; import org.jgroups.JChannel; import org.keycloak.connections.infinispan.InfinispanConnectionProvider; import org.keycloak.models.KeycloakSession; import org.keycloak.models.sessions.infinispan.entities.UserSessionEntity; import org.keycloak.models.sessions.infinispan.util.InfinispanUtil; import org.keycloak.testsuite.rest.representation.JGroupsStats; +import org.keycloak.utils.MediaType; /** * @author Marek Posolda @@ -82,7 +81,7 @@ public class TestCacheResource { @GET @Path("/clear") - @Consumes(MediaType.TEXT_PLAIN) + @Consumes(MediaType.TEXT_PLAIN_UTF_8) public void clear() { cache.clear(); } diff --git a/testsuite/integration-arquillian/tests/base/src/main/java/org/keycloak/testsuite/client/resources/TestApplicationResource.java b/testsuite/integration-arquillian/tests/base/src/main/java/org/keycloak/testsuite/client/resources/TestApplicationResource.java index afefde3833..96c3c8df19 100644 --- a/testsuite/integration-arquillian/tests/base/src/main/java/org/keycloak/testsuite/client/resources/TestApplicationResource.java +++ b/testsuite/integration-arquillian/tests/base/src/main/java/org/keycloak/testsuite/client/resources/TestApplicationResource.java @@ -17,18 +17,16 @@ package org.keycloak.testsuite.client.resources; -import org.jboss.resteasy.annotations.cache.NoCache; import org.keycloak.representations.adapters.action.LogoutAction; import org.keycloak.representations.adapters.action.PushNotBeforeAction; import org.keycloak.representations.adapters.action.TestAvailabilityAction; +import org.keycloak.utils.MediaType; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; /** * @author Stian Thorgersen @@ -56,7 +54,7 @@ public interface TestApplicationResource { void clearAdminActions(); @GET - @Produces(MediaType.TEXT_HTML) + @Produces(MediaType.TEXT_HTML_UTF_8) @Path("/get-account-profile") String getAccountProfile(@QueryParam("token") String token, @QueryParam("account-uri") String accountUri); diff --git a/testsuite/integration-arquillian/tests/base/src/main/java/org/keycloak/testsuite/client/resources/TestingCacheResource.java b/testsuite/integration-arquillian/tests/base/src/main/java/org/keycloak/testsuite/client/resources/TestingCacheResource.java index e1aee2a374..1c362ea3a1 100644 --- a/testsuite/integration-arquillian/tests/base/src/main/java/org/keycloak/testsuite/client/resources/TestingCacheResource.java +++ b/testsuite/integration-arquillian/tests/base/src/main/java/org/keycloak/testsuite/client/resources/TestingCacheResource.java @@ -17,18 +17,16 @@ package org.keycloak.testsuite.client.resources; -import java.util.Map; -import java.util.Set; +import org.keycloak.testsuite.rest.representation.JGroupsStats; +import org.keycloak.testsuite.rest.representation.RemoteCacheStats; +import org.keycloak.utils.MediaType; import javax.ws.rs.Consumes; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; -import javax.ws.rs.core.MediaType; - -import org.keycloak.testsuite.rest.representation.JGroupsStats; -import org.keycloak.testsuite.rest.representation.RemoteCacheStats; +import java.util.Set; /** * @author Marek Posolda @@ -55,7 +53,7 @@ public interface TestingCacheResource { @GET @Path("/clear") - @Consumes(MediaType.TEXT_PLAIN) + @Consumes(MediaType.TEXT_PLAIN_UTF_8) void clear(); @GET diff --git a/testsuite/integration-arquillian/tests/base/src/main/java/org/keycloak/testsuite/client/resources/TestingResource.java b/testsuite/integration-arquillian/tests/base/src/main/java/org/keycloak/testsuite/client/resources/TestingResource.java index 2787c0e82f..080afcf769 100644 --- a/testsuite/integration-arquillian/tests/base/src/main/java/org/keycloak/testsuite/client/resources/TestingResource.java +++ b/testsuite/integration-arquillian/tests/base/src/main/java/org/keycloak/testsuite/client/resources/TestingResource.java @@ -18,14 +18,13 @@ package org.keycloak.testsuite.client.resources; import org.jboss.resteasy.annotations.cache.NoCache; -import org.keycloak.common.util.MultivaluedHashMap; import org.keycloak.representations.idm.AdminEventRepresentation; import org.keycloak.representations.idm.AuthenticationFlowRepresentation; import org.keycloak.representations.idm.EventRepresentation; import org.keycloak.representations.idm.UserRepresentation; import org.keycloak.testsuite.components.TestProvider; import org.keycloak.testsuite.rest.representation.AuthenticatorState; -import org.keycloak.testsuite.rest.resource.TestCacheResource; +import org.keycloak.utils.MediaType; import javax.ws.rs.Consumes; import javax.ws.rs.GET; @@ -35,8 +34,6 @@ import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; import java.util.List; import java.util.Map; @@ -259,8 +256,8 @@ public interface TestingResource { @POST @Path("/run-on-server") - @Consumes(MediaType.TEXT_PLAIN) - @Produces(MediaType.TEXT_PLAIN) + @Consumes(MediaType.TEXT_PLAIN_UTF_8) + @Produces(MediaType.TEXT_PLAIN_UTF_8) String runOnServer(String runOnServer); }