made binary encode/decode null safe

pull/695/head
Justin Richer 2014-10-06 23:25:48 -04:00
parent a8377513a6
commit 1fbdd240f1
3 changed files with 44 additions and 28 deletions

View File

@ -328,8 +328,12 @@ public class MITREidDataService_1_0 extends MITREidDataService_1_X {
if (subName.equals("clientAuthorization")) {
clientAuthorization = readAuthorizationRequest(reader);
} else if (subName.equals("userAuthentication")) {
String authString = reader.nextString();
userAuthentication = base64UrlDecodeObject(authString, Authentication.class);
if (reader.peek() == JsonToken.NULL) {
reader.skipValue();
} else {
String authString = reader.nextString();
userAuthentication = base64UrlDecodeObject(authString, Authentication.class);
}
} else {
logger.debug("Found unexpected entry");
reader.skipValue();

View File

@ -667,8 +667,12 @@ public class MITREidDataService_1_1 extends MITREidDataService_1_X {
if (subName.equals("clientAuthorization")) {
clientAuthorization = readAuthorizationRequest(reader);
} else if (subName.equals("userAuthentication")) {
String authString = reader.nextString();
userAuthentication = base64UrlDecodeObject(authString, Authentication.class);
if (reader.peek() == JsonToken.NULL) {
reader.skipValue();
} else {
String authString = reader.nextString();
userAuthentication = base64UrlDecodeObject(authString, Authentication.class);
}
} else {
logger.debug("Found unexpected entry");
reader.skipValue();

View File

@ -71,33 +71,41 @@ public abstract class MITREidDataService_1_X implements MITREidDataService {
}
protected static <T> T base64UrlDecodeObject(String encoded, Class<T> type) {
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;
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) {
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;
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;