refactored some json utils to their own static class

pull/516/head
Justin Richer 2013-09-16 16:39:35 -04:00
parent e1ed53a229
commit 1d0560edbc
3 changed files with 232 additions and 116 deletions

View File

@ -40,6 +40,8 @@ import com.google.gson.JsonElement;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.google.gson.JsonParser; 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. * 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()); conf.setIssuer(o.get("issuer").getAsString());
if (o.has("authorization_endpoint")) {
conf.setAuthorizationEndpointUri(o.get("authorization_endpoint").getAsString()); conf.setAuthorizationEndpointUri(getAsString(o, "authorization_endpoint"));
} conf.setTokenEndpointUri(getAsString(o, "token_endpoint"));
if (o.has("token_endpoint")) { conf.setJwksUri(getAsString(o, "jwks_uri"));
conf.setTokenEndpointUri(o.get("token_endpoint").getAsString()); conf.setUserInfoUri(getAsString(o, "userinfo_endpoint"));
} conf.setRegistrationEndpointUri(getAsString(o, "registration_endpoint"));
if (o.has("jwks_uri")) { conf.setIntrospectionEndpointUri(getAsString(o, "introspection_endpoint"));
conf.setJwksUri(o.get("jwks_uri").getAsString()); conf.setAcrValuesSupported(getAsStringList(o, "acr_values_supported"));
} conf.setCheckSessionIframe(getAsString(o, "check_session_iframe"));
if (o.has("userinfo_endpoint")) { conf.setClaimsLocalesSupported(getAsStringList(o, "claims_locales_supported"));
conf.setUserInfoUri(o.get("userinfo_endpoint").getAsString()); conf.setClaimsParameterSupported(getAsBoolean(o, "claims_parameter_supported"));
} conf.setClaimsSupported(getAsStringList(o, "claims_supported"));
if (o.has("registration_endpoint")) { conf.setDisplayValuesSupported(getAsStringList(o, "display_values_supported"));
conf.setRegistrationEndpointUri(o.get("registration_endpoint").getAsString()); conf.setEndSessionEndpoint(getAsString(o, "end_session_endpoint"));
} conf.setGrantTypesSupported(getAsStringList(o, "grant_types_supported"));
if (o.has("introspection_endpoint")) { conf.setIdTokenSigningAlgValuesSupported(getAsJwsAlgorithmList(o, "id_token_signing_alg_values_supported"));
conf.setIntrospectionEndpointUri(o.get("introspection_endpoint").getAsString()); 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; return conf;
} else { } else {

View File

@ -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<String> value) {
return gson.toJsonTree(value, new TypeToken<Set<String>>(){}.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<String> getAsStringSet(JsonObject o, String member) throws JsonSyntaxException {
if (o.has(member)) {
return gson.fromJson(o.get(member), new TypeToken<Set<String>>(){}.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<String> getAsStringList(JsonObject o, String member) throws JsonSyntaxException {
if (o.has(member)) {
return gson.fromJson(o.get(member), new TypeToken<List<String>>(){}.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<JWSAlgorithm> getAsJwsAlgorithmList(JsonObject o, String member) {
List<String> strings = getAsStringList(o, member);
if (strings != null) {
List<JWSAlgorithm> algs = new ArrayList<JWSAlgorithm>();
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<JWEAlgorithm> getAsJweAlgorithmList(JsonObject o, String member) {
List<String> strings = getAsStringList(o, member);
if (strings != null) {
List<JWEAlgorithm> algs = new ArrayList<JWEAlgorithm>();
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<EncryptionMethod> getAsEncryptionMethodList(JsonObject o, String member) {
List<String> strings = getAsStringList(o, member);
if (strings != null) {
List<EncryptionMethod> algs = new ArrayList<EncryptionMethod>();
for (String alg : strings) {
algs.add(EncryptionMethod.parse(alg));
}
return algs;
} else {
return null;
}
}
}

View File

@ -19,8 +19,6 @@
*/ */
package org.mitre.openid.connect; 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;
import org.mitre.oauth2.model.ClientDetailsEntity.AppType; 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.Joiner;
import com.google.common.base.Splitter; import com.google.common.base.Splitter;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
import com.google.gson.Gson;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.google.gson.JsonParser; import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken; import static org.mitre.discovery.util.JsonUtils.*;
import com.nimbusds.jose.EncryptionMethod;
import com.nimbusds.jose.JWEAlgorithm;
import com.nimbusds.jose.JWSAlgorithm;
/** /**
* @author jricher * @author jricher
@ -47,7 +41,6 @@ import com.nimbusds.jose.JWSAlgorithm;
*/ */
public class ClientDetailsEntityJsonProcessor { public class ClientDetailsEntityJsonProcessor {
private static Gson gson = new Gson();
private static JsonParser parser = new JsonParser(); private static JsonParser parser = new JsonParser();
/** /**
@ -236,94 +229,5 @@ public class ClientDetailsEntityJsonProcessor {
return o; 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<String> getAsStringSet(JsonObject o, String member) throws JsonSyntaxException {
if (o.has(member)) {
return gson.fromJson(o.get(member), new TypeToken<Set<String>>(){}.getType());
} else {
return null;
}
}
/**
* Translate a set of strings to a JSON array
* @param value
* @return
*/
private static JsonElement getAsArray(Set<String> value) {
return gson.toJsonTree(value, new TypeToken<Set<String>>(){}.getType());
}
} }