Merge pull request #363 from stianst/master

Removed org.json
This commit is contained in:
Stian Thorgersen 2014-05-06 13:05:30 +01:00
commit e1f2d296bf
22 changed files with 111 additions and 123 deletions

View file

@ -42,6 +42,11 @@
<version>${hibernate.entitymanager.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
@ -52,11 +57,6 @@
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
</project>

View file

@ -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<Map<String, String>> mapType = new TypeReference<Map<String, String>>() {
};
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<String, String> details = new HashMap<String, String>();
for (Object k : object.keySet()) {
details.put((String) k, object.getString((String) k));
}
try {
Map<String, String> details = mapper.readValue(o.getDetailsJson(), mapType);
e.setDetails(details);
} catch (IOException ex) {
logger.error("Failed to read log details", ex);
}
return e;
}

View file

@ -41,11 +41,6 @@
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<properties>

View file

@ -87,10 +87,6 @@
<artifactId>keycloak-social-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
</dependency>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-social-github</artifactId>

View file

@ -9,8 +9,8 @@
<div id="content">
<ol class="breadcrumb">
<li><a href="#/realms/{{realm.realm}}">{{realm.realm}}</a></li>
<li><a href="#/realms/{{realm.realm}}">Audit</a></li>
<li class="active">Social</li>
<li><a href="#/realms/{{realm.realm}}/audit">Audit</a></li>
<li class="active">Config</li>
</ol>
<h2><span>{{realm.realm}}</span> Audit Config</h2>

View file

@ -9,8 +9,7 @@
<div id="content">
<ol class="breadcrumb">
<li><a href="#/realms/{{realm.realm}}">{{realm.realm}}</a></li>
<li><a href="#/realms/{{realm.realm}}">Audit</a></li>
<li class="active">Social</li>
<li class="active">Audit</li>
</ol>
<h2><span>{{realm.realm}}</span> Audit Log</h2>

View file

@ -33,7 +33,6 @@
<slf4j.version>1.5.10</slf4j.version>
<jboss.version>7.1.1.Final</jboss.version>
<wildfly.version>8.0.0.Final</wildfly.version>
<json.version>20131018</json.version>
<!-- maven-compiler-plugin -->
<maven.compiler.target>1.6</maven.compiler.target>
@ -286,12 +285,6 @@
<version>2.3.19</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>${json.version}</version>
</dependency>
<!-- Google+ -->
<dependency>
<groupId>com.google.http-client</groupId>

View file

@ -73,10 +73,6 @@
<artifactId>keycloak-social-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
</dependency>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-social-github</artifactId>

View file

@ -68,10 +68,6 @@
<artifactId>keycloak-social-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
</dependency>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-social-github</artifactId>

View file

@ -234,13 +234,14 @@ public class AccountService {
e.setEvent(e.getEvent().replace('_', ' '));
Map<String, String> details = new HashMap<String, String>();
if (e.getDetails() != null) {
Iterator<String> itr = e.getDetails().keySet().iterator();
for (Map.Entry<String, String> d : e.getDetails().entrySet()) {
if (AUDIT_DETAILS.contains(d.getKey())) {
details.put(d.getKey().replace('_', ' '), d.getValue());
}
}
}
e.setDetails(details);
}
account.setEvents(events);

View file

@ -21,8 +21,8 @@
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<scope>provided</scope>
</dependency>
<dependency>

View file

@ -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()) {

View file

@ -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<String, String> 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 {

View file

@ -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());
}
}

View file

@ -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<String> 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<String> parameterNames = req.getParameterNames();
while (parameterNames.hasMoreElements()) {
String n = parameterNames.nextElement();

View file

@ -14,16 +14,16 @@
<description/>
<dependencies>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-social-core</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
</project>

View file

@ -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) {

View file

@ -14,16 +14,16 @@
<description/>
<dependencies>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-social-core</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
</project>

View file

@ -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) {

View file

@ -14,16 +14,16 @@
<description/>
<dependencies>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-social-core</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
</project>

View file

@ -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 <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a>
@ -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) {

View file

@ -20,12 +20,6 @@
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.twitter4j</groupId>
<artifactId>twitter4j-core</artifactId>