From 6b96fbb4e9568a8f9ef8dc07deb77a37aa86d615 Mon Sep 17 00:00:00 2001 From: Justin Richer Date: Mon, 16 Sep 2013 16:39:35 -0400 Subject: [PATCH] refactored some json utils to their own static class --- .../DynamicServerConfigurationService.java | 55 +++-- .../org/mitre/discovery/util/JsonUtils.java | 193 ++++++++++++++++++ .../ClientDetailsEntityJsonProcessor.java | 100 +-------- 3 files changed, 232 insertions(+), 116 deletions(-) create mode 100644 openid-connect-common/src/main/java/org/mitre/discovery/util/JsonUtils.java diff --git a/openid-connect-client/src/main/java/org/mitre/openid/connect/client/service/impl/DynamicServerConfigurationService.java b/openid-connect-client/src/main/java/org/mitre/openid/connect/client/service/impl/DynamicServerConfigurationService.java index fc508ca91..559401c10 100644 --- a/openid-connect-client/src/main/java/org/mitre/openid/connect/client/service/impl/DynamicServerConfigurationService.java +++ b/openid-connect-client/src/main/java/org/mitre/openid/connect/client/service/impl/DynamicServerConfigurationService.java @@ -40,6 +40,8 @@ import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; +import static org.mitre.discovery.util.JsonUtils.*; + /** * * Dynamically fetches OpenID Connect server configurations based on the issuer. Caches the server configurations. @@ -148,24 +150,41 @@ public class DynamicServerConfigurationService implements ServerConfigurationSer conf.setIssuer(o.get("issuer").getAsString()); - if (o.has("authorization_endpoint")) { - conf.setAuthorizationEndpointUri(o.get("authorization_endpoint").getAsString()); - } - if (o.has("token_endpoint")) { - conf.setTokenEndpointUri(o.get("token_endpoint").getAsString()); - } - if (o.has("jwks_uri")) { - conf.setJwksUri(o.get("jwks_uri").getAsString()); - } - if (o.has("userinfo_endpoint")) { - conf.setUserInfoUri(o.get("userinfo_endpoint").getAsString()); - } - if (o.has("registration_endpoint")) { - conf.setRegistrationEndpointUri(o.get("registration_endpoint").getAsString()); - } - if (o.has("introspection_endpoint")) { - conf.setIntrospectionEndpointUri(o.get("introspection_endpoint").getAsString()); - } + + conf.setAuthorizationEndpointUri(getAsString(o, "authorization_endpoint")); + conf.setTokenEndpointUri(getAsString(o, "token_endpoint")); + conf.setJwksUri(getAsString(o, "jwks_uri")); + conf.setUserInfoUri(getAsString(o, "userinfo_endpoint")); + conf.setRegistrationEndpointUri(getAsString(o, "registration_endpoint")); + conf.setIntrospectionEndpointUri(getAsString(o, "introspection_endpoint")); + conf.setAcrValuesSupported(getAsStringList(o, "acr_values_supported")); + conf.setCheckSessionIframe(getAsString(o, "check_session_iframe")); + conf.setClaimsLocalesSupported(getAsStringList(o, "claims_locales_supported")); + conf.setClaimsParameterSupported(getAsBoolean(o, "claims_parameter_supported")); + conf.setClaimsSupported(getAsStringList(o, "claims_supported")); + conf.setDisplayValuesSupported(getAsStringList(o, "display_values_supported")); + conf.setEndSessionEndpoint(getAsString(o, "end_session_endpoint")); + conf.setGrantTypesSupported(getAsStringList(o, "grant_types_supported")); + conf.setIdTokenSigningAlgValuesSupported(getAsJwsAlgorithmList(o, "id_token_signing_alg_values_supported")); + conf.setIdTokenEncryptionAlgValuesSupported(getAsJweAlgorithmList(o, "id_token_encryption_alg_values_supported")); + conf.setIdTokenEncryptionEncValuesSupported(getAsEncryptionMethodList(o, "id_token_encryption_enc_values_supported")); + conf.setOpPolicyUri(getAsString(o, "op_policy_uri")); + conf.setOpTosUri(getAsString(o, "op_tos_uri")); + conf.setRequestObjectEncryptionAlgValuesSupported(getAsJweAlgorithmList(o, "request_object_encryption_alg_values_supported")); + conf.setRequestObjectEncryptionEncValuesSupported(getAsEncryptionMethodList(o, "request_object_encryption_enc_values_supported")); + conf.setRequestObjectSigningAlgValuesSupported(getAsJwsAlgorithmList(o, "request_object_signing_alg_values_supported")); + conf.setRequestParameterSupported(getAsBoolean(o, "request_parameter_supported")); + conf.setRequestUriParameterSupported(getAsBoolean(o, "request_uri_parameter_supported")); + conf.setResponseTypesSupported(getAsStringList(o, "response_types_supported")); + conf.setScopesSupported(getAsStringList(o, "scopes_supported")); + conf.setSubjectTypesSupported(getAsStringList(o, "subject_types_supported")); + conf.setServiceDocumentation(getAsString(o, "service_documentation")); + conf.setTokenEndpointAuthMethodsSupported(getAsStringList(o, "token_endpoint_auth_methods")); + conf.setTokenEndpointAuthSigningAlgValuesSupported(getAsJwsAlgorithmList(o, "token_endpoint_auth_signing_alg_values_supported")); + conf.setUiLocalesSupported(getAsStringList(o, "ui_locales_supported")); + conf.setUserinfoEncryptionAlgValuesSupported(getAsJweAlgorithmList(o, "userinfo_encryption_alg_values_supported")); + conf.setUserinfoEncryptionEncValuesSupported(getAsEncryptionMethodList(o, "userinfo_encryption_enc_values_supported")); + conf.setUserinfoSigningAlgValuesSupported(getAsJwsAlgorithmList(o, "userinfo_signing_alg_values_supported")); return conf; } else { diff --git a/openid-connect-common/src/main/java/org/mitre/discovery/util/JsonUtils.java b/openid-connect-common/src/main/java/org/mitre/discovery/util/JsonUtils.java new file mode 100644 index 000000000..a1ccb8535 --- /dev/null +++ b/openid-connect-common/src/main/java/org/mitre/discovery/util/JsonUtils.java @@ -0,0 +1,193 @@ +/** + * + */ +package org.mitre.discovery.util; + +import java.util.ArrayList; +import java.util.Date; +import java.util.List; +import java.util.Set; + +import com.google.gson.Gson; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonSyntaxException; +import com.google.gson.reflect.TypeToken; +import com.nimbusds.jose.EncryptionMethod; +import com.nimbusds.jose.JWEAlgorithm; +import com.nimbusds.jose.JWSAlgorithm; + +/** + * A collection of null-safe converters from common classes and JSON elements, using GSON. + * + * @author jricher + * + */ +public class JsonUtils { + + private static Gson gson = new Gson(); + + /** + * Translate a set of strings to a JSON array + * @param value + * @return + */ + public static JsonElement getAsArray(Set value) { + return gson.toJsonTree(value, new TypeToken>(){}.getType()); + } + + /** + * Gets the value of the given member (expressed as integer seconds since epoch) as a Date + */ + public static Date getAsDate(JsonObject o, String member) { + if (o.has(member)) { + JsonElement e = o.get(member); + if (e != null && e.isJsonPrimitive()) { + return new Date(e.getAsInt() * 1000L); + } else { + return null; + } + } else { + return null; + } + } + + /** + * Gets the value of the given member as a JWE Algorithm, null if it doesn't exist + */ + public static JWEAlgorithm getAsJweAlgorithm(JsonObject o, String member) { + String s = getAsString(o, member); + if (s != null) { + return JWEAlgorithm.parse(s); + } else { + return null; + } + } + + /** + * Gets the value of the given member as a JWE Encryption Method, null if it doesn't exist + */ + public static EncryptionMethod getAsJweEncryptionMethod(JsonObject o, String member) { + String s = getAsString(o, member); + if (s != null) { + return EncryptionMethod.parse(s); + } else { + return null; + } + } + + /** + * Gets the value of the given member as a JWS Algorithm, null if it doesn't exist + */ + public static JWSAlgorithm getAsJwsAlgorithm(JsonObject o, String member) { + String s = getAsString(o, member); + if (s != null) { + return JWSAlgorithm.parse(s); + } else { + return null; + } + } + + /** + * Gets the value of the given member as a string, null if it doesn't exist + */ + public static String getAsString(JsonObject o, String member) { + if (o.has(member)) { + JsonElement e = o.get(member); + if (e != null && e.isJsonPrimitive()) { + return e.getAsString(); + } else { + return null; + } + } else { + return null; + } + } + + /** + * Gets the value of the given member as a boolean, null if it doesn't exist + */ + public static Boolean getAsBoolean(JsonObject o, String member) { + if (o.has(member)) { + JsonElement e = o.get(member); + if (e != null && e.isJsonPrimitive()) { + return e.getAsBoolean(); + } else { + return null; + } + } else { + return null; + } + } + + /** + * Gets the value of the given given member as a set of strings, null if it doesn't exist + */ + public static Set getAsStringSet(JsonObject o, String member) throws JsonSyntaxException { + if (o.has(member)) { + return gson.fromJson(o.get(member), new TypeToken>(){}.getType()); + } else { + return null; + } + } + + /** + * Gets the value of the given given member as a set of strings, null if it doesn't exist + */ + public static List getAsStringList(JsonObject o, String member) throws JsonSyntaxException { + if (o.has(member)) { + return gson.fromJson(o.get(member), new TypeToken>(){}.getType()); + } else { + return null; + } + } + + /** + * Gets the value of the given member as a list of JWS Algorithms, null if it doesn't exist + */ + public static List getAsJwsAlgorithmList(JsonObject o, String member) { + List strings = getAsStringList(o, member); + if (strings != null) { + List algs = new ArrayList(); + for (String alg : strings) { + algs.add(JWSAlgorithm.parse(alg)); + } + return algs; + } else { + return null; + } + } + + /** + * Gets the value of the given member as a list of JWS Algorithms, null if it doesn't exist + */ + public static List getAsJweAlgorithmList(JsonObject o, String member) { + List strings = getAsStringList(o, member); + if (strings != null) { + List algs = new ArrayList(); + for (String alg : strings) { + algs.add(JWEAlgorithm.parse(alg)); + } + return algs; + } else { + return null; + } + } + + /** + * Gets the value of the given member as a list of JWS Algorithms, null if it doesn't exist + */ + public static List getAsEncryptionMethodList(JsonObject o, String member) { + List strings = getAsStringList(o, member); + if (strings != null) { + List algs = new ArrayList(); + for (String alg : strings) { + algs.add(EncryptionMethod.parse(alg)); + } + return algs; + } else { + return null; + } + } + +} 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 1407afb61..4c14794e3 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 @@ -19,8 +19,6 @@ */ package org.mitre.openid.connect; -import java.util.Date; -import java.util.Set; import org.mitre.oauth2.model.ClientDetailsEntity; import org.mitre.oauth2.model.ClientDetailsEntity.AppType; @@ -31,15 +29,11 @@ import org.mitre.oauth2.model.RegisteredClient; import com.google.common.base.Joiner; import com.google.common.base.Splitter; import com.google.common.collect.Sets; -import com.google.gson.Gson; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; -import com.google.gson.JsonSyntaxException; -import com.google.gson.reflect.TypeToken; -import com.nimbusds.jose.EncryptionMethod; -import com.nimbusds.jose.JWEAlgorithm; -import com.nimbusds.jose.JWSAlgorithm; + +import static org.mitre.discovery.util.JsonUtils.*; /** * @author jricher @@ -47,7 +41,6 @@ import com.nimbusds.jose.JWSAlgorithm; */ public class ClientDetailsEntityJsonProcessor { - private static Gson gson = new Gson(); private static JsonParser parser = new JsonParser(); /** @@ -236,94 +229,5 @@ public class ClientDetailsEntityJsonProcessor { return o; } - /** - * Gets the value of the given member as a JWE Algorithm, null if it doesn't exist - */ - private static JWEAlgorithm getAsJweAlgorithm(JsonObject o, String member) { - String s = getAsString(o, member); - if (s != null) { - return JWEAlgorithm.parse(s); - } else { - return null; - } - } - - /** - * Gets the value of the given member as a JWE Encryption Method, null if it doesn't exist - */ - private static EncryptionMethod getAsJweEncryptionMethod(JsonObject o, String member) { - String s = getAsString(o, member); - if (s != null) { - return EncryptionMethod.parse(s); - } else { - return null; - } - } - - /** - * Gets the value of the given member as a JWS Algorithm, null if it doesn't exist - */ - private static JWSAlgorithm getAsJwsAlgorithm(JsonObject o, String member) { - String s = getAsString(o, member); - if (s != null) { - return JWSAlgorithm.parse(s); - } else { - return null; - } - } - - /** - * Gets the value of the given member as a string, null if it doesn't exist - */ - private static String getAsString(JsonObject o, String member) { - if (o.has(member)) { - JsonElement e = o.get(member); - if (e != null && e.isJsonPrimitive()) { - return e.getAsString(); - } else { - return null; - } - } else { - return null; - } - } - - /** - * Gets the value of the given member (expressed as integer seconds since epoch) as a Date - */ - private static Date getAsDate(JsonObject o, String member) { - if (o.has(member)) { - JsonElement e = o.get(member); - if (e != null && e.isJsonPrimitive()) { - return new Date(e.getAsInt() * 1000L); - } else { - return null; - } - } else { - return null; - } - } - - /** - * Gets the value of the given given member as a set of strings, null if it doesn't exist - */ - private static Set getAsStringSet(JsonObject o, String member) throws JsonSyntaxException { - if (o.has(member)) { - return gson.fromJson(o.get(member), new TypeToken>(){}.getType()); - } else { - return null; - } - } - - - /** - * Translate a set of strings to a JSON array - * @param value - * @return - */ - private static JsonElement getAsArray(Set value) { - return gson.toJsonTree(value, new TypeToken>(){}.getType()); - } - }