From 8286d1ce3418b78e5e7f215e11b5a073e5ec4ea8 Mon Sep 17 00:00:00 2001 From: Stian Thorgersen Date: Wed, 30 Apr 2014 16:36:39 +0100 Subject: [PATCH 1/2] =?UTF-8?q?Fix=20breadcrumb=20for=20audit=20pages?= =?UTF-8?q?=C2=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../admin/base/resources/partials/realm-audit-config.html | 4 ++-- .../theme/admin/base/resources/partials/realm-audit.html | 3 +-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/forms/common-themes/src/main/resources/theme/admin/base/resources/partials/realm-audit-config.html b/forms/common-themes/src/main/resources/theme/admin/base/resources/partials/realm-audit-config.html index 3697e7e27a..4d6bfea586 100755 --- a/forms/common-themes/src/main/resources/theme/admin/base/resources/partials/realm-audit-config.html +++ b/forms/common-themes/src/main/resources/theme/admin/base/resources/partials/realm-audit-config.html @@ -9,8 +9,8 @@

{{realm.realm}} Audit Config

diff --git a/forms/common-themes/src/main/resources/theme/admin/base/resources/partials/realm-audit.html b/forms/common-themes/src/main/resources/theme/admin/base/resources/partials/realm-audit.html index 01543200fb..7b58c9c4b6 100755 --- a/forms/common-themes/src/main/resources/theme/admin/base/resources/partials/realm-audit.html +++ b/forms/common-themes/src/main/resources/theme/admin/base/resources/partials/realm-audit.html @@ -9,8 +9,7 @@

{{realm.realm}} Audit Log

From 1d760388dee1b207362b9207b4b9e4736ecd2fe8 Mon Sep 17 00:00:00 2001 From: Stian Thorgersen Date: Tue, 6 May 2014 12:22:46 +0100 Subject: [PATCH 2/2] KEYCLOAK-441 Remove org.json --- audit/jpa/pom.xml | 10 ++-- .../keycloak/audit/jpa/JpaAuditProvider.java | 29 +++++++---- audit/mongo/pom.xml | 5 -- bundled-war-example/pom.xml | 4 -- pom.xml | 7 --- .../aerogear-ups/auth-server/pom.xml | 4 -- server/pom.xml | 4 -- .../services/resources/AccountService.java | 11 ++-- social/core/pom.xml | 4 +- .../social/AbstractOAuth2Provider.java | 6 ++- .../org/keycloak/social/utils/SimpleHttp.java | 9 ++-- .../keycloak/social/utils/SimpleHttpTest.java | 50 +++++++++---------- .../keycloak/social/utils/ToJsonServlet.java | 12 +++-- social/facebook/pom.xml | 10 ++-- .../social/facebook/FacebookProvider.java | 11 ++-- social/github/pom.xml | 10 ++-- .../social/github/GitHubProvider.java | 12 ++--- social/google/pom.xml | 10 ++-- .../social/google/GoogleProvider.java | 13 ++--- social/twitter/pom.xml | 6 --- 20 files changed, 108 insertions(+), 119 deletions(-) diff --git a/audit/jpa/pom.xml b/audit/jpa/pom.xml index 504136a68f..067774c665 100755 --- a/audit/jpa/pom.xml +++ b/audit/jpa/pom.xml @@ -42,6 +42,11 @@ ${hibernate.entitymanager.version} provided + + org.codehaus.jackson + jackson-mapper-asl + provided + junit junit @@ -52,11 +57,6 @@ h2 test - - org.json - json - provided - diff --git a/audit/jpa/src/main/java/org/keycloak/audit/jpa/JpaAuditProvider.java b/audit/jpa/src/main/java/org/keycloak/audit/jpa/JpaAuditProvider.java index 97479513c2..5401902d90 100644 --- a/audit/jpa/src/main/java/org/keycloak/audit/jpa/JpaAuditProvider.java +++ b/audit/jpa/src/main/java/org/keycloak/audit/jpa/JpaAuditProvider.java @@ -1,13 +1,15 @@ package org.keycloak.audit.jpa; -import org.json.JSONObject; +import org.codehaus.jackson.map.ObjectMapper; +import org.codehaus.jackson.type.TypeReference; +import org.jboss.logging.Logger; import org.keycloak.audit.AuditProvider; import org.keycloak.audit.Event; import org.keycloak.audit.EventQuery; import javax.persistence.EntityManager; import javax.persistence.EntityTransaction; -import java.util.HashMap; +import java.io.IOException; import java.util.Map; import java.util.UUID; @@ -16,6 +18,11 @@ import java.util.UUID; */ public class JpaAuditProvider implements AuditProvider { + private static final ObjectMapper mapper = new ObjectMapper(); + private static final TypeReference> mapType = new TypeReference>() { + }; + private static final Logger logger = Logger.getLogger(JpaAuditProvider.class); + private EntityManager em; private EntityTransaction tx; @@ -78,7 +85,11 @@ public class JpaAuditProvider implements AuditProvider { e.setUserId(o.getUserId()); e.setIpAddress(o.getIpAddress()); e.setError(o.getError()); - e.setDetailsJson(new JSONObject(o.getDetails()).toString()); + try { + e.setDetailsJson(mapper.writeValueAsString(o.getDetails())); + } catch (IOException ex) { + logger.error("Failed to write log details", ex); + } return e; } @@ -91,14 +102,12 @@ public class JpaAuditProvider implements AuditProvider { e.setUserId(o.getUserId()); e.setIpAddress(o.getIpAddress()); e.setError(o.getError()); - - JSONObject object = new JSONObject(o.getDetailsJson()); - Map details = new HashMap(); - for (Object k : object.keySet()) { - details.put((String) k, object.getString((String) k)); + try { + Map details = mapper.readValue(o.getDetailsJson(), mapType); + e.setDetails(details); + } catch (IOException ex) { + logger.error("Failed to read log details", ex); } - - e.setDetails(details); return e; } diff --git a/audit/mongo/pom.xml b/audit/mongo/pom.xml index 4f5098ee44..7bb0f6bc5a 100755 --- a/audit/mongo/pom.xml +++ b/audit/mongo/pom.xml @@ -41,11 +41,6 @@ junit test - - org.json - json - provided - diff --git a/bundled-war-example/pom.xml b/bundled-war-example/pom.xml index 0ca04a7609..3797f58e4f 100755 --- a/bundled-war-example/pom.xml +++ b/bundled-war-example/pom.xml @@ -87,10 +87,6 @@ keycloak-social-core ${project.version} - - org.json - json - org.keycloak keycloak-social-github diff --git a/pom.xml b/pom.xml index 34ed8179b8..640300d717 100755 --- a/pom.xml +++ b/pom.xml @@ -33,7 +33,6 @@ 1.5.10 7.1.1.Final 8.0.0.Final - 20131018 1.6 @@ -286,12 +285,6 @@ 2.3.19 - - org.json - json - ${json.version} - - com.google.http-client diff --git a/project-integrations/aerogear-ups/auth-server/pom.xml b/project-integrations/aerogear-ups/auth-server/pom.xml index e30625f80a..7c738b825f 100755 --- a/project-integrations/aerogear-ups/auth-server/pom.xml +++ b/project-integrations/aerogear-ups/auth-server/pom.xml @@ -73,10 +73,6 @@ keycloak-social-core ${project.version} - - org.json - json - org.keycloak keycloak-social-github diff --git a/server/pom.xml b/server/pom.xml index fc93352039..7fb498b502 100755 --- a/server/pom.xml +++ b/server/pom.xml @@ -68,10 +68,6 @@ keycloak-social-core ${project.version} - - org.json - json - org.keycloak keycloak-social-github 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 327f20ad1f..2f998e721b 100755 --- a/services/src/main/java/org/keycloak/services/resources/AccountService.java +++ b/services/src/main/java/org/keycloak/services/resources/AccountService.java @@ -234,13 +234,14 @@ public class AccountService { e.setEvent(e.getEvent().replace('_', ' ')); Map details = new HashMap(); - Iterator itr = e.getDetails().keySet().iterator(); - for (Map.Entry d : e.getDetails().entrySet()) { - if (AUDIT_DETAILS.contains(d.getKey())) { - details.put(d.getKey().replace('_', ' '), d.getValue()); + if (e.getDetails() != null) { + Iterator itr = e.getDetails().keySet().iterator(); + for (Map.Entry d : e.getDetails().entrySet()) { + if (AUDIT_DETAILS.contains(d.getKey())) { + details.put(d.getKey().replace('_', ' '), d.getValue()); + } } } - e.setDetails(details); } account.setEvents(events); diff --git a/social/core/pom.xml b/social/core/pom.xml index 457854326a..d19c6caa50 100755 --- a/social/core/pom.xml +++ b/social/core/pom.xml @@ -21,8 +21,8 @@ provided - org.json - json + org.codehaus.jackson + jackson-mapper-asl provided diff --git a/social/core/src/main/java/org/keycloak/social/AbstractOAuth2Provider.java b/social/core/src/main/java/org/keycloak/social/AbstractOAuth2Provider.java index 92d64857af..f3fdea728b 100644 --- a/social/core/src/main/java/org/keycloak/social/AbstractOAuth2Provider.java +++ b/social/core/src/main/java/org/keycloak/social/AbstractOAuth2Provider.java @@ -1,6 +1,6 @@ package org.keycloak.social; -import org.json.JSONObject; +import org.codehaus.jackson.map.ObjectMapper; import org.keycloak.OAuth2Constants; import org.keycloak.social.utils.SimpleHttp; @@ -14,6 +14,8 @@ import java.util.regex.Pattern; */ public abstract class AbstractOAuth2Provider implements SocialProvider { + private static ObjectMapper mapper = new ObjectMapper(); + private static final String AUTHORIZATION_CODE = "authorization_code"; private static final String ACCESS_TOKEN = "access_token"; private static final String CLIENT_ID = "client_id"; @@ -76,7 +78,7 @@ public abstract class AbstractOAuth2Provider implements SocialProvider { String accessToken; if (response.startsWith("{")) { - accessToken = new JSONObject(response).getString(ACCESS_TOKEN); + accessToken = mapper.readTree(response).get(ACCESS_TOKEN).getTextValue(); } else { Matcher matcher = Pattern.compile(TOKEN_REGEX).matcher(response); if (matcher.find()) { diff --git a/social/core/src/main/java/org/keycloak/social/utils/SimpleHttp.java b/social/core/src/main/java/org/keycloak/social/utils/SimpleHttp.java index d5ef204f07..757d58f3ae 100644 --- a/social/core/src/main/java/org/keycloak/social/utils/SimpleHttp.java +++ b/social/core/src/main/java/org/keycloak/social/utils/SimpleHttp.java @@ -1,6 +1,7 @@ package org.keycloak.social.utils; -import org.json.JSONObject; +import org.codehaus.jackson.JsonNode; +import org.codehaus.jackson.map.ObjectMapper; import java.io.IOException; import java.io.InputStream; @@ -18,6 +19,8 @@ import java.util.Map; */ public class SimpleHttp { + private static ObjectMapper mapper = new ObjectMapper(); + private String url; private String method; private Map headers; @@ -52,8 +55,8 @@ public class SimpleHttp { return this; } - public JSONObject asJson() throws IOException { - return new JSONObject(asString()); + public JsonNode asJson() throws IOException { + return mapper.readTree(asString()); } public String asString() throws IOException { diff --git a/social/core/src/main/test/java/org/keycloak/social/utils/SimpleHttpTest.java b/social/core/src/main/test/java/org/keycloak/social/utils/SimpleHttpTest.java index dea58c7f1d..6f3103ded2 100644 --- a/social/core/src/main/test/java/org/keycloak/social/utils/SimpleHttpTest.java +++ b/social/core/src/main/test/java/org/keycloak/social/utils/SimpleHttpTest.java @@ -2,7 +2,7 @@ package org.keycloak.social.utils; import io.undertow.servlet.api.DeploymentInfo; import io.undertow.servlet.api.ServletInfo; -import org.json.JSONObject; +import org.codehaus.jackson.JsonNode; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -45,56 +45,56 @@ public class SimpleHttpTest { @Test public void testPostNoParams() throws IOException { - JSONObject o = SimpleHttp.doPost("http://localhost:8081/tojson").asJson(); - JSONObject p = o.getJSONObject("params"); + JsonNode o = SimpleHttp.doPost("http://localhost:8081/tojson").asJson(); + JsonNode p = o.get("params"); - assertEquals(0, p.length()); + assertEquals(0, p.size()); } @Test public void testPost() throws IOException { - JSONObject o = SimpleHttp.doPost("http://localhost:8081/tojson").param("key-one", "value one ;)").param("key-two", "value two!&").asJson(); - JSONObject p = o.getJSONObject("params"); + JsonNode o = SimpleHttp.doPost("http://localhost:8081/tojson").param("key-one", "value one ;)").param("key-two", "value two!&").asJson(); + JsonNode p = o.get("params"); - assertEquals(2, p.length()); - assertEquals("value one ;)", p.getString("key-one")); - assertEquals("value two!&", p.getString("key-two")); + assertEquals(2, p.size()); + assertEquals("value one ;)", p.get("key-one").getTextValue()); + assertEquals("value two!&", p.get("key-two").getTextValue()); } @Test public void testPostCustomHeader() throws IOException { - JSONObject o = SimpleHttp.doPost("http://localhost:8081/tojson").header("Accept", "application/json").header("Authorization", "bearer dsfsadfsdf").asJson(); - JSONObject h = o.getJSONObject("headers"); + JsonNode o = SimpleHttp.doPost("http://localhost:8081/tojson").header("Accept", "application/json").header("Authorization", "bearer dsfsadfsdf").asJson(); + JsonNode h = o.get("headers"); - assertEquals("application/json", h.getString("Accept")); - assertEquals("bearer dsfsadfsdf", h.getString("Authorization")); + assertEquals("application/json", h.get("Accept").getTextValue()); + assertEquals("bearer dsfsadfsdf", h.get("Authorization").getTextValue()); } @Test public void testGetNoParams() throws IOException { - JSONObject o = SimpleHttp.doGet("http://localhost:8081/tojson").asJson(); - JSONObject p = o.getJSONObject("params"); + JsonNode o = SimpleHttp.doGet("http://localhost:8081/tojson").asJson(); + JsonNode p = o.get("params"); - assertEquals(0, p.length()); + assertEquals(0, p.size()); } @Test public void testGet() throws IOException { - JSONObject o = SimpleHttp.doGet("http://localhost:8081/tojson").param("key-one", "value one ;)").param("key-two", "value two!&").asJson(); - JSONObject p = o.getJSONObject("params"); + JsonNode o = SimpleHttp.doGet("http://localhost:8081/tojson").param("key-one", "value one ;)").param("key-two", "value two!&").asJson(); + JsonNode p = o.get("params"); - assertEquals(2, p.length()); - assertEquals("value one ;)", p.getString("key-one")); - assertEquals("value two!&", p.getString("key-two")); + assertEquals(2, p.size()); + assertEquals("value one ;)", p.get("key-one").getTextValue()); + assertEquals("value two!&", p.get("key-two").getTextValue()); } @Test public void testGetCustomHeader() throws IOException { - JSONObject o = SimpleHttp.doGet("http://localhost:8081/tojson").header("Accept", "application/json").header("Authorization", "bearer dsfsadfsdf").asJson(); - JSONObject h = o.getJSONObject("headers"); + JsonNode o = SimpleHttp.doGet("http://localhost:8081/tojson").header("Accept", "application/json").header("Authorization", "bearer dsfsadfsdf").asJson(); + JsonNode h = o.get("headers"); - assertEquals("application/json", h.getString("Accept")); - assertEquals("bearer dsfsadfsdf", h.getString("Authorization")); + assertEquals("application/json", h.get("Accept")); + assertEquals("bearer dsfsadfsdf", h.get("Authorization").getTextValue()); } } diff --git a/social/core/src/main/test/java/org/keycloak/social/utils/ToJsonServlet.java b/social/core/src/main/test/java/org/keycloak/social/utils/ToJsonServlet.java index 1d738e7da1..e63b4b1bd5 100644 --- a/social/core/src/main/test/java/org/keycloak/social/utils/ToJsonServlet.java +++ b/social/core/src/main/test/java/org/keycloak/social/utils/ToJsonServlet.java @@ -1,6 +1,8 @@ package org.keycloak.social.utils; -import org.json.JSONObject; +import org.codehaus.jackson.JsonNode; +import org.codehaus.jackson.map.ObjectMapper; +import org.codehaus.jackson.node.ObjectNode; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; @@ -14,6 +16,8 @@ import java.util.Enumeration; */ public class ToJsonServlet extends HttpServlet { + private static final ObjectMapper mapper = new ObjectMapper(); + @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { toJson(req, resp); @@ -25,9 +29,9 @@ public class ToJsonServlet extends HttpServlet { } private void toJson(HttpServletRequest req, HttpServletResponse resp) throws IOException { - JSONObject o = new JSONObject(); + ObjectNode o = mapper.createObjectNode(); - JSONObject headers = new JSONObject(); + ObjectNode headers = mapper.createObjectNode(); Enumeration headerNames = req.getHeaderNames(); while (headerNames.hasMoreElements()) { String n = headerNames.nextElement(); @@ -35,7 +39,7 @@ public class ToJsonServlet extends HttpServlet { } o.put("headers", headers); - JSONObject params = new JSONObject(); + ObjectNode params = mapper.createObjectNode(); Enumeration parameterNames = req.getParameterNames(); while (parameterNames.hasMoreElements()) { String n = parameterNames.nextElement(); diff --git a/social/facebook/pom.xml b/social/facebook/pom.xml index be3679c958..bcacfb8299 100755 --- a/social/facebook/pom.xml +++ b/social/facebook/pom.xml @@ -14,16 +14,16 @@ - - org.json - json - provided - org.keycloak keycloak-social-core ${project.version} provided + + org.codehaus.jackson + jackson-mapper-asl + provided + diff --git a/social/facebook/src/main/java/org/keycloak/social/facebook/FacebookProvider.java b/social/facebook/src/main/java/org/keycloak/social/facebook/FacebookProvider.java index 9f6403608a..830abff642 100755 --- a/social/facebook/src/main/java/org/keycloak/social/facebook/FacebookProvider.java +++ b/social/facebook/src/main/java/org/keycloak/social/facebook/FacebookProvider.java @@ -1,6 +1,6 @@ package org.keycloak.social.facebook; -import org.json.JSONObject; +import org.codehaus.jackson.JsonNode; import org.keycloak.social.AbstractOAuth2Provider; import org.keycloak.social.AuthRequest; import org.keycloak.social.SocialProviderConfig; @@ -50,11 +50,12 @@ public class FacebookProvider extends AbstractOAuth2Provider { @Override protected SocialUser getProfile(String accessToken) throws SocialProviderException { try { - JSONObject profile = SimpleHttp.doGet(PROFILE_URL).header("Authorization", "Bearer " + accessToken).asJson(); + JsonNode profile = SimpleHttp.doGet(PROFILE_URL).header("Authorization", "Bearer " + accessToken).asJson(); - SocialUser user = new SocialUser(profile.getString("id"), profile.getString("username")); - user.setName(profile.optString("first_name"), profile.optString("last_name")); - user.setEmail(profile.optString("email")); + SocialUser user = new SocialUser(profile.get("id").getTextValue(), profile.get("username").getTextValue()); + user.setName(profile.has("first_name") ? profile.get("first_name").getTextValue() : null, + profile.has("last_name") ? profile.get("last_name").getTextValue() : null); + user.setEmail(profile.has("email") ? profile.get("email").getTextValue() : null); return user; } catch (Exception e) { diff --git a/social/github/pom.xml b/social/github/pom.xml index e8dc767d31..0c55002c6e 100755 --- a/social/github/pom.xml +++ b/social/github/pom.xml @@ -14,16 +14,16 @@ - - org.json - json - provided - org.keycloak keycloak-social-core ${project.version} provided + + org.codehaus.jackson + jackson-mapper-asl + provided + diff --git a/social/github/src/main/java/org/keycloak/social/github/GitHubProvider.java b/social/github/src/main/java/org/keycloak/social/github/GitHubProvider.java index 95c1067e91..8e25bf4932 100755 --- a/social/github/src/main/java/org/keycloak/social/github/GitHubProvider.java +++ b/social/github/src/main/java/org/keycloak/social/github/GitHubProvider.java @@ -1,9 +1,7 @@ package org.keycloak.social.github; -import org.json.JSONObject; +import org.codehaus.jackson.JsonNode; import org.keycloak.social.AbstractOAuth2Provider; -import org.keycloak.social.AuthRequest; -import org.keycloak.social.SocialProviderConfig; import org.keycloak.social.SocialProviderException; import org.keycloak.social.SocialUser; import org.keycloak.social.utils.SimpleHttp; @@ -50,11 +48,11 @@ public class GitHubProvider extends AbstractOAuth2Provider { @Override protected SocialUser getProfile(String accessToken) throws SocialProviderException { try { - JSONObject profile = SimpleHttp.doGet(PROFILE_URL).header("Authorization", "Bearer " + accessToken).asJson(); + JsonNode profile = SimpleHttp.doGet(PROFILE_URL).header("Authorization", "Bearer " + accessToken).asJson(); - SocialUser user = new SocialUser(profile.get("id").toString(), profile.getString("login")); - user.setName(profile.optString("name")); - user.setEmail(profile.optString("email")); + SocialUser user = new SocialUser(profile.get("id").toString(), profile.get("login").getTextValue()); + user.setName(profile.has("name") ? profile.get("name").getTextValue() : null); + user.setEmail(profile.has("email") ? profile.get("email").getTextValue() : null); return user; } catch (Exception e) { diff --git a/social/google/pom.xml b/social/google/pom.xml index c575a86985..ae6922a6b8 100755 --- a/social/google/pom.xml +++ b/social/google/pom.xml @@ -14,16 +14,16 @@ - - org.json - json - provided - org.keycloak keycloak-social-core ${project.version} provided + + org.codehaus.jackson + jackson-mapper-asl + provided + diff --git a/social/google/src/main/java/org/keycloak/social/google/GoogleProvider.java b/social/google/src/main/java/org/keycloak/social/google/GoogleProvider.java index ab3d8f47ea..cd87d5722c 100755 --- a/social/google/src/main/java/org/keycloak/social/google/GoogleProvider.java +++ b/social/google/src/main/java/org/keycloak/social/google/GoogleProvider.java @@ -21,11 +21,11 @@ */ package org.keycloak.social.google; -import org.json.JSONObject; +import org.codehaus.jackson.JsonNode; import org.keycloak.social.AbstractOAuth2Provider; -import org.keycloak.social.utils.SimpleHttp; import org.keycloak.social.SocialProviderException; import org.keycloak.social.SocialUser; +import org.keycloak.social.utils.SimpleHttp; /** * @author Stian Thorgersen @@ -69,11 +69,12 @@ public class GoogleProvider extends AbstractOAuth2Provider { @Override protected SocialUser getProfile(String accessToken) throws SocialProviderException { try { - JSONObject profile = SimpleHttp.doGet(PROFILE_URL).header("Authorization", "Bearer " + accessToken).asJson(); + JsonNode profile = SimpleHttp.doGet(PROFILE_URL).header("Authorization", "Bearer " + accessToken).asJson(); - SocialUser user = new SocialUser(profile.getString("sub"), profile.getString("email")); - user.setName(profile.optString("given_name"), profile.optString("family_name")); - user.setEmail(profile.optString("email")); + SocialUser user = new SocialUser(profile.get("sub").getTextValue(), profile.get("email").getTextValue()); + user.setName(profile.has("given_name") ? profile.get("given_name").getTextValue() : null, + profile.has("family_name") ? profile.get("family_name").getTextValue() : null); + user.setEmail(profile.has("email") ? profile.get("email").getTextValue() : null); return user; } catch (Exception e) { diff --git a/social/twitter/pom.xml b/social/twitter/pom.xml index 0511193976..65b976cc69 100755 --- a/social/twitter/pom.xml +++ b/social/twitter/pom.xml @@ -20,12 +20,6 @@ ${project.version} provided - - - org.json - json - provided - org.twitter4j twitter4j-core