From b0cce924a2b34ea5c4a9c7bb952d9721238392b2 Mon Sep 17 00:00:00 2001 From: Justin Richer Date: Fri, 1 Aug 2014 00:01:56 -0400 Subject: [PATCH] JsonFileRegisteredClientService now writes out entire client registration to disk, closes #651 --- ...egistrationClientConfigurationService.java | 32 +++++++++++-------- .../impl/JsonFileRegisteredClientService.java | 31 +++--------------- .../ClientDetailsEntityJsonProcessor.java | 10 +++++- 3 files changed, 32 insertions(+), 41 deletions(-) diff --git a/openid-connect-client/src/main/java/org/mitre/openid/connect/client/service/impl/DynamicRegistrationClientConfigurationService.java b/openid-connect-client/src/main/java/org/mitre/openid/connect/client/service/impl/DynamicRegistrationClientConfigurationService.java index e950c88bc..320d32cd8 100644 --- a/openid-connect-client/src/main/java/org/mitre/openid/connect/client/service/impl/DynamicRegistrationClientConfigurationService.java +++ b/openid-connect-client/src/main/java/org/mitre/openid/connect/client/service/impl/DynamicRegistrationClientConfigurationService.java @@ -199,19 +199,25 @@ public class DynamicRegistrationClientConfigurationService implements ClientConf return client; } else { - // load this client's information from the server - HttpHeaders headers = new HttpHeaders(); - headers.set("Authorization", String.format("%s %s", OAuth2AccessToken.BEARER_TYPE, knownClient.getRegistrationAccessToken())); - headers.setAccept(Lists.newArrayList(MediaType.APPLICATION_JSON)); - - HttpEntity entity = new HttpEntity(headers); - - String registered = restTemplate.exchange(knownClient.getRegistrationClientUri(), HttpMethod.GET, entity, String.class).getBody(); - // TODO: handle HTTP errors - - RegisteredClient client = ClientDetailsEntityJsonProcessor.parseRegistered(registered); - - return client; + if (knownClient.getClientId() == null) { + + // load this client's information from the server + HttpHeaders headers = new HttpHeaders(); + headers.set("Authorization", String.format("%s %s", OAuth2AccessToken.BEARER_TYPE, knownClient.getRegistrationAccessToken())); + headers.setAccept(Lists.newArrayList(MediaType.APPLICATION_JSON)); + + HttpEntity entity = new HttpEntity(headers); + + String registered = restTemplate.exchange(knownClient.getRegistrationClientUri(), HttpMethod.GET, entity, String.class).getBody(); + // TODO: handle HTTP errors + + RegisteredClient client = ClientDetailsEntityJsonProcessor.parseRegistered(registered); + + return client; + } else { + // it's got a client ID from the store, don't bother trying to load it + return knownClient; + } } } diff --git a/openid-connect-client/src/main/java/org/mitre/openid/connect/client/service/impl/JsonFileRegisteredClientService.java b/openid-connect-client/src/main/java/org/mitre/openid/connect/client/service/impl/JsonFileRegisteredClientService.java index 0d7d3c036..68cfd1008 100644 --- a/openid-connect-client/src/main/java/org/mitre/openid/connect/client/service/impl/JsonFileRegisteredClientService.java +++ b/openid-connect-client/src/main/java/org/mitre/openid/connect/client/service/impl/JsonFileRegisteredClientService.java @@ -25,11 +25,11 @@ import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.lang.reflect.Type; -import java.util.Date; import java.util.HashMap; import java.util.Map; import org.mitre.oauth2.model.RegisteredClient; +import org.mitre.openid.connect.ClientDetailsEntityJsonProcessor; import org.mitre.openid.connect.client.service.RegisteredClientService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -40,7 +40,6 @@ import com.google.gson.GsonBuilder; import com.google.gson.JsonDeserializationContext; import com.google.gson.JsonDeserializer; import com.google.gson.JsonElement; -import com.google.gson.JsonObject; import com.google.gson.JsonParseException; import com.google.gson.JsonSerializationContext; import com.google.gson.JsonSerializer; @@ -57,38 +56,16 @@ public class JsonFileRegisteredClientService implements RegisteredClientService .registerTypeAdapter(RegisteredClient.class, new JsonSerializer() { @Override public JsonElement serialize(RegisteredClient src, Type typeOfSrc, JsonSerializationContext context) { - JsonObject obj = new JsonObject(); - obj.addProperty("token", src.getRegistrationAccessToken()); - obj.addProperty("uri", src.getRegistrationClientUri()); - if (src.getClientIdIssuedAt() != null) { - obj.addProperty("issued", src.getClientIdIssuedAt().getTime()); - } - if (src.getClientSecretExpiresAt() != null) { - obj.addProperty("expires", src.getClientSecretExpiresAt().getTime()); - } - return obj; + return ClientDetailsEntityJsonProcessor.serialize(src); } }) .registerTypeAdapter(RegisteredClient.class, new JsonDeserializer() { @Override public RegisteredClient deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { - if (json.isJsonObject()) { - JsonObject src = json.getAsJsonObject(); - RegisteredClient rc = new RegisteredClient(); - rc.setRegistrationAccessToken(src.get("token").getAsString()); - rc.setRegistrationClientUri(src.get("uri").getAsString()); - if (src.has("issued") && !src.get("issued").isJsonNull()) { - rc.setClientIdIssuedAt(new Date(src.get("issued").getAsLong())); - } - if (src.has("expires") && !src.get("expires").isJsonNull()) { - rc.setClientSecretExpiresAt(new Date(src.get("expires").getAsLong())); - } - return rc; - } else { - return null; - } + return ClientDetailsEntityJsonProcessor.parseRegistered(json); } }) + .setPrettyPrinting() .create(); private File file; diff --git a/openid-connect-common/src/main/java/org/mitre/openid/connect/ClientDetailsEntityJsonProcessor.java b/openid-connect-common/src/main/java/org/mitre/openid/connect/ClientDetailsEntityJsonProcessor.java index 8ad5f691f..fe3f09368 100644 --- a/openid-connect-common/src/main/java/org/mitre/openid/connect/ClientDetailsEntityJsonProcessor.java +++ b/openid-connect-common/src/main/java/org/mitre/openid/connect/ClientDetailsEntityJsonProcessor.java @@ -58,6 +58,10 @@ public class ClientDetailsEntityJsonProcessor { */ public static ClientDetailsEntity parse(String jsonString) { JsonElement jsonEl = parser.parse(jsonString); + return parse(jsonEl); + } + + public static ClientDetailsEntity parse(JsonElement jsonEl) { if (jsonEl.isJsonObject()) { JsonObject o = jsonEl.getAsJsonObject(); @@ -149,10 +153,14 @@ public class ClientDetailsEntityJsonProcessor { JsonElement jsonEl = parser.parse(jsonString); + return parseRegistered(jsonEl); + } + + public static RegisteredClient parseRegistered(JsonElement jsonEl) { if (jsonEl.isJsonObject()) { JsonObject o = jsonEl.getAsJsonObject(); - ClientDetailsEntity c = parse(jsonString); + ClientDetailsEntity c = parse(jsonEl); RegisteredClient rc = new RegisteredClient(c); // get any fields from the registration