From 7a1480bb070f998187e68bc6cb1d9738166df182 Mon Sep 17 00:00:00 2001 From: Justin Richer Date: Mon, 23 Feb 2015 13:43:08 -0500 Subject: [PATCH] moved and consolidated json utilities --- .../DynamicServerConfigurationService.java | 12 +- .../ClientDetailsEntityJsonProcessor.java | 14 +- .../mitre/{discovery => }/util/JsonUtils.java | 117 ++++++++++++++- .../impl/AbstractMITREidDataService.java | 142 ------------------ .../service/impl/MITREidDataService_1_0.java | 7 +- .../service/impl/MITREidDataService_1_1.java | 7 +- .../service/impl/MITREidDataService_1_2.java | 9 +- .../openid/connect/view/JsonEntityView.java | 40 ++--- 8 files changed, 169 insertions(+), 179 deletions(-) rename openid-connect-common/src/main/java/org/mitre/{discovery => }/util/JsonUtils.java (67%) delete mode 100644 openid-connect-server/src/main/java/org/mitre/openid/connect/service/impl/AbstractMITREidDataService.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 159ef8554..e2d4a9319 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 @@ -19,12 +19,12 @@ */ package org.mitre.openid.connect.client.service.impl; -import static org.mitre.discovery.util.JsonUtils.getAsBoolean; -import static org.mitre.discovery.util.JsonUtils.getAsEncryptionMethodList; -import static org.mitre.discovery.util.JsonUtils.getAsJweAlgorithmList; -import static org.mitre.discovery.util.JsonUtils.getAsJwsAlgorithmList; -import static org.mitre.discovery.util.JsonUtils.getAsString; -import static org.mitre.discovery.util.JsonUtils.getAsStringList; +import static org.mitre.util.JsonUtils.getAsBoolean; +import static org.mitre.util.JsonUtils.getAsEncryptionMethodList; +import static org.mitre.util.JsonUtils.getAsJweAlgorithmList; +import static org.mitre.util.JsonUtils.getAsJwsAlgorithmList; +import static org.mitre.util.JsonUtils.getAsString; +import static org.mitre.util.JsonUtils.getAsStringList; import java.util.HashSet; import java.util.Set; 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 d4bef98a2..d344c9176 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 @@ -20,13 +20,6 @@ package org.mitre.openid.connect; -import static org.mitre.discovery.util.JsonUtils.getAsArray; -import static org.mitre.discovery.util.JsonUtils.getAsDate; -import static org.mitre.discovery.util.JsonUtils.getAsJweAlgorithm; -import static org.mitre.discovery.util.JsonUtils.getAsJweEncryptionMethod; -import static org.mitre.discovery.util.JsonUtils.getAsJwsAlgorithm; -import static org.mitre.discovery.util.JsonUtils.getAsString; -import static org.mitre.discovery.util.JsonUtils.getAsStringSet; import static org.mitre.oauth2.model.RegisteredClientFields.APPLICATION_TYPE; import static org.mitre.oauth2.model.RegisteredClientFields.CLIENT_ID; import static org.mitre.oauth2.model.RegisteredClientFields.CLIENT_ID_ISSUED_AT; @@ -63,6 +56,13 @@ import static org.mitre.oauth2.model.RegisteredClientFields.TOS_URI; import static org.mitre.oauth2.model.RegisteredClientFields.USERINFO_ENCRYPTED_RESPONSE_ALG; import static org.mitre.oauth2.model.RegisteredClientFields.USERINFO_ENCRYPTED_RESPONSE_ENC; import static org.mitre.oauth2.model.RegisteredClientFields.USERINFO_SIGNED_RESPONSE_ALG; +import static org.mitre.util.JsonUtils.getAsArray; +import static org.mitre.util.JsonUtils.getAsDate; +import static org.mitre.util.JsonUtils.getAsJweAlgorithm; +import static org.mitre.util.JsonUtils.getAsJweEncryptionMethod; +import static org.mitre.util.JsonUtils.getAsJwsAlgorithm; +import static org.mitre.util.JsonUtils.getAsString; +import static org.mitre.util.JsonUtils.getAsStringSet; import org.mitre.oauth2.model.ClientDetailsEntity; import org.mitre.oauth2.model.ClientDetailsEntity.AppType; diff --git a/openid-connect-common/src/main/java/org/mitre/discovery/util/JsonUtils.java b/openid-connect-common/src/main/java/org/mitre/util/JsonUtils.java similarity index 67% rename from openid-connect-common/src/main/java/org/mitre/discovery/util/JsonUtils.java rename to openid-connect-common/src/main/java/org/mitre/util/JsonUtils.java index 3956be065..e1638e041 100644 --- a/openid-connect-common/src/main/java/org/mitre/discovery/util/JsonUtils.java +++ b/openid-connect-common/src/main/java/org/mitre/util/JsonUtils.java @@ -17,20 +17,35 @@ /** * */ -package org.mitre.discovery.util; +package org.mitre.util; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.io.Serializable; import java.util.ArrayList; import java.util.Date; +import java.util.HashMap; +import java.util.HashSet; import java.util.List; +import java.util.Map; import java.util.Set; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import com.google.common.collect.Lists; import com.google.common.collect.Sets; +import com.google.common.io.BaseEncoding; 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.google.gson.stream.JsonReader; +import com.google.gson.stream.JsonWriter; import com.nimbusds.jose.EncryptionMethod; import com.nimbusds.jose.JWEAlgorithm; import com.nimbusds.jose.JWSAlgorithm; @@ -41,8 +56,11 @@ import com.nimbusds.jose.JWSAlgorithm; * @author jricher * */ +@SuppressWarnings(value = {"rawtypes", "unchecked"}) public class JsonUtils { + private static Logger logger = LoggerFactory.getLogger(JsonUtils.class); + private static Gson gson = new Gson(); /** @@ -216,4 +234,101 @@ public class JsonUtils { } } + public static T base64UrlDecodeObject(String encoded, Class type) { + if (encoded == null) { + return null; + } else { + T deserialized = null; + try { + byte[] decoded = BaseEncoding.base64Url().decode(encoded); + ByteArrayInputStream bais = new ByteArrayInputStream(decoded); + ObjectInputStream ois = new ObjectInputStream(bais); + deserialized = type.cast(ois.readObject()); + ois.close(); + bais.close(); + } catch (Exception ex) { + logger.error("Unable to decode object", ex); + } + return deserialized; + } + } + + public static String base64UrlEncodeObject(Serializable obj) { + if (obj == null) { + return null; + } else { + String encoded = null; + try { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + ObjectOutputStream oos = new ObjectOutputStream(baos); + oos.writeObject(obj); + encoded = BaseEncoding.base64Url().encode(baos.toByteArray()); + oos.close(); + baos.close(); + } catch (IOException ex) { + logger.error("Unable to encode object", ex); + } + return encoded; + } + } + + public static Map readMap(JsonReader reader) throws IOException { + Map map = new HashMap(); + reader.beginObject(); + while(reader.hasNext()) { + String name = reader.nextName(); + Object value = null; + switch(reader.peek()) { + case STRING: + value = reader.nextString(); + break; + case BOOLEAN: + value = reader.nextBoolean(); + break; + case NUMBER: + value = reader.nextLong(); + break; + } + map.put(name, value); + } + reader.endObject(); + return map; + } + + public static Set readSet(JsonReader reader) throws IOException { + Set arraySet = null; + reader.beginArray(); + switch (reader.peek()) { + case STRING: + arraySet = new HashSet(); + while (reader.hasNext()) { + arraySet.add(reader.nextString()); + } + break; + case NUMBER: + arraySet = new HashSet(); + while (reader.hasNext()) { + arraySet.add(reader.nextLong()); + } + break; + default: + arraySet = new HashSet(); + break; + } + reader.endArray(); + return arraySet; + } + + public static void writeNullSafeArray(JsonWriter writer, Set items) throws IOException { + if (items != null) { + writer.beginArray(); + for (String s : items) { + writer.value(s); + } + writer.endArray(); + } else { + writer.nullValue(); + } + } + } diff --git a/openid-connect-server/src/main/java/org/mitre/openid/connect/service/impl/AbstractMITREidDataService.java b/openid-connect-server/src/main/java/org/mitre/openid/connect/service/impl/AbstractMITREidDataService.java deleted file mode 100644 index ecb3d0bdd..000000000 --- a/openid-connect-server/src/main/java/org/mitre/openid/connect/service/impl/AbstractMITREidDataService.java +++ /dev/null @@ -1,142 +0,0 @@ -/******************************************************************************* - * Copyright 2015 The MITRE Corporation - * and the MIT Kerberos and Internet Trust Consortium - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - *******************************************************************************/ -package org.mitre.openid.connect.service.impl; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.ObjectInputStream; -import java.io.ObjectOutputStream; -import java.io.Serializable; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; - -import org.mitre.openid.connect.service.MITREidDataService; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.google.common.io.BaseEncoding; -import com.google.gson.stream.JsonReader; -import com.google.gson.stream.JsonWriter; - -/** - * - * @author arielak - */ -@SuppressWarnings(value = {"rawtypes", "unchecked"}) -public abstract class AbstractMITREidDataService implements MITREidDataService { - private static Logger logger = LoggerFactory.getLogger(AbstractMITREidDataService.class); - - protected static T base64UrlDecodeObject(String encoded, Class type) { - if (encoded == null) { - return null; - } else { - T deserialized = null; - try { - byte[] decoded = BaseEncoding.base64Url().decode(encoded); - ByteArrayInputStream bais = new ByteArrayInputStream(decoded); - ObjectInputStream ois = new ObjectInputStream(bais); - deserialized = type.cast(ois.readObject()); - ois.close(); - bais.close(); - } catch (Exception ex) { - logger.error("Unable to decode object", ex); - } - return deserialized; - } - } - - protected static String base64UrlEncodeObject(Serializable obj) { - if (obj == null) { - return null; - } else { - String encoded = null; - try { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - ObjectOutputStream oos = new ObjectOutputStream(baos); - oos.writeObject(obj); - encoded = BaseEncoding.base64Url().encode(baos.toByteArray()); - oos.close(); - baos.close(); - } catch (IOException ex) { - logger.error("Unable to encode object", ex); - } - return encoded; - } - } - protected static Set readSet(JsonReader reader) throws IOException { - Set arraySet = null; - reader.beginArray(); - switch (reader.peek()) { - case STRING: - arraySet = new HashSet(); - while (reader.hasNext()) { - arraySet.add(reader.nextString()); - } - break; - case NUMBER: - arraySet = new HashSet(); - while (reader.hasNext()) { - arraySet.add(reader.nextLong()); - } - break; - default: - arraySet = new HashSet(); - break; - } - reader.endArray(); - return arraySet; - } - - protected static Map readMap(JsonReader reader) throws IOException { - Map map = new HashMap(); - reader.beginObject(); - while(reader.hasNext()) { - String name = reader.nextName(); - Object value = null; - switch(reader.peek()) { - case STRING: - value = reader.nextString(); - break; - case BOOLEAN: - value = reader.nextBoolean(); - break; - case NUMBER: - value = reader.nextLong(); - break; - } - map.put(name, value); - } - reader.endObject(); - return map; - } - - protected void writeNullSafeArray(JsonWriter writer, Set items) - throws IOException { - if (items != null) { - writer.beginArray(); - for (String s : items) { - writer.value(s); - } - writer.endArray(); - } else { - writer.nullValue(); - } - } -} diff --git a/openid-connect-server/src/main/java/org/mitre/openid/connect/service/impl/MITREidDataService_1_0.java b/openid-connect-server/src/main/java/org/mitre/openid/connect/service/impl/MITREidDataService_1_0.java index fdfd3460c..46e0e7684 100644 --- a/openid-connect-server/src/main/java/org/mitre/openid/connect/service/impl/MITREidDataService_1_0.java +++ b/openid-connect-server/src/main/java/org/mitre/openid/connect/service/impl/MITREidDataService_1_0.java @@ -16,6 +16,10 @@ *******************************************************************************/ package org.mitre.openid.connect.service.impl; +import static org.mitre.util.JsonUtils.base64UrlDecodeObject; +import static org.mitre.util.JsonUtils.readMap; +import static org.mitre.util.JsonUtils.readSet; + import java.io.IOException; import java.text.ParseException; import java.util.Collection; @@ -47,6 +51,7 @@ import org.mitre.openid.connect.model.WhitelistedSite; import org.mitre.openid.connect.repository.ApprovedSiteRepository; import org.mitre.openid.connect.repository.BlacklistedSiteRepository; import org.mitre.openid.connect.repository.WhitelistedSiteRepository; +import org.mitre.openid.connect.service.MITREidDataService; import org.mitre.openid.connect.util.DateUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -71,7 +76,7 @@ import com.google.gson.stream.JsonWriter; */ @Service @SuppressWarnings(value = {"unchecked"}) -public class MITREidDataService_1_0 extends AbstractMITREidDataService { +public class MITREidDataService_1_0 implements MITREidDataService { private final static Logger logger = LoggerFactory.getLogger(MITREidDataService_1_0.class); @Autowired diff --git a/openid-connect-server/src/main/java/org/mitre/openid/connect/service/impl/MITREidDataService_1_1.java b/openid-connect-server/src/main/java/org/mitre/openid/connect/service/impl/MITREidDataService_1_1.java index 2ea2ff70e..8d20652c3 100644 --- a/openid-connect-server/src/main/java/org/mitre/openid/connect/service/impl/MITREidDataService_1_1.java +++ b/openid-connect-server/src/main/java/org/mitre/openid/connect/service/impl/MITREidDataService_1_1.java @@ -16,6 +16,10 @@ *******************************************************************************/ package org.mitre.openid.connect.service.impl; +import static org.mitre.util.JsonUtils.base64UrlDecodeObject; +import static org.mitre.util.JsonUtils.readMap; +import static org.mitre.util.JsonUtils.readSet; + import java.io.IOException; import java.io.Serializable; import java.text.ParseException; @@ -49,6 +53,7 @@ import org.mitre.openid.connect.model.WhitelistedSite; import org.mitre.openid.connect.repository.ApprovedSiteRepository; import org.mitre.openid.connect.repository.BlacklistedSiteRepository; import org.mitre.openid.connect.repository.WhitelistedSiteRepository; +import org.mitre.openid.connect.service.MITREidDataService; import org.mitre.openid.connect.util.DateUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -74,7 +79,7 @@ import com.google.gson.stream.JsonWriter; */ @Service @SuppressWarnings(value = {"unchecked"}) -public class MITREidDataService_1_1 extends AbstractMITREidDataService { +public class MITREidDataService_1_1 implements MITREidDataService { private final static Logger logger = LoggerFactory.getLogger(MITREidDataService_1_1.class); @Autowired diff --git a/openid-connect-server/src/main/java/org/mitre/openid/connect/service/impl/MITREidDataService_1_2.java b/openid-connect-server/src/main/java/org/mitre/openid/connect/service/impl/MITREidDataService_1_2.java index 9924656ce..168f45581 100644 --- a/openid-connect-server/src/main/java/org/mitre/openid/connect/service/impl/MITREidDataService_1_2.java +++ b/openid-connect-server/src/main/java/org/mitre/openid/connect/service/impl/MITREidDataService_1_2.java @@ -16,6 +16,12 @@ *******************************************************************************/ package org.mitre.openid.connect.service.impl; +import static org.mitre.util.JsonUtils.base64UrlDecodeObject; +import static org.mitre.util.JsonUtils.base64UrlEncodeObject; +import static org.mitre.util.JsonUtils.readMap; +import static org.mitre.util.JsonUtils.readSet; +import static org.mitre.util.JsonUtils.writeNullSafeArray; + import java.io.IOException; import java.io.Serializable; import java.text.ParseException; @@ -49,6 +55,7 @@ import org.mitre.openid.connect.model.WhitelistedSite; import org.mitre.openid.connect.repository.ApprovedSiteRepository; import org.mitre.openid.connect.repository.BlacklistedSiteRepository; import org.mitre.openid.connect.repository.WhitelistedSiteRepository; +import org.mitre.openid.connect.service.MITREidDataService; import org.mitre.openid.connect.util.DateUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -74,7 +81,7 @@ import com.google.gson.stream.JsonWriter; */ @Service @SuppressWarnings(value = {"unchecked"}) -public class MITREidDataService_1_2 extends AbstractMITREidDataService { +public class MITREidDataService_1_2 implements MITREidDataService { private final static Logger logger = LoggerFactory.getLogger(MITREidDataService_1_2.class); @Autowired diff --git a/openid-connect-server/src/main/java/org/mitre/openid/connect/view/JsonEntityView.java b/openid-connect-server/src/main/java/org/mitre/openid/connect/view/JsonEntityView.java index 62adddc33..cd5cb819f 100644 --- a/openid-connect-server/src/main/java/org/mitre/openid/connect/view/JsonEntityView.java +++ b/openid-connect-server/src/main/java/org/mitre/openid/connect/view/JsonEntityView.java @@ -50,27 +50,27 @@ public class JsonEntityView extends AbstractView { public static final String VIEWNAME = "jsonEntityView"; private Gson gson = new GsonBuilder() - .setExclusionStrategies(new ExclusionStrategy() { - - @Override - public boolean shouldSkipField(FieldAttributes f) { - - return false; - } - - @Override - public boolean shouldSkipClass(Class clazz) { - // skip the JPA binding wrapper - if (clazz.equals(BeanPropertyBindingResult.class)) { - return true; + .setExclusionStrategies(new ExclusionStrategy() { + + @Override + public boolean shouldSkipField(FieldAttributes f) { + + return false; } - return false; - } - - }) - .serializeNulls() - .setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ") - .create(); + + @Override + public boolean shouldSkipClass(Class clazz) { + // skip the JPA binding wrapper + if (clazz.equals(BeanPropertyBindingResult.class)) { + return true; + } + return false; + } + + }) + .serializeNulls() + .setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ") + .create(); @Override protected void renderMergedOutputModel(Map model, HttpServletRequest request, HttpServletResponse response) {