audience field is now plural

pull/263/head
Justin Richer 2013-01-18 17:59:46 -05:00
parent 27a26e0a35
commit da43ba4d55
2 changed files with 70 additions and 7 deletions

View File

@ -19,15 +19,19 @@ import java.io.ByteArrayInputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.util.Date; import java.util.Date;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import org.apache.commons.codec.binary.Base64; import org.apache.commons.codec.binary.Base64;
import com.google.common.collect.Lists;
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.JsonPrimitive; import com.google.gson.JsonPrimitive;
import com.google.gson.reflect.TypeToken;
/** /**
* Generic container for JSON-based claims. Backed with a {@link Map} that preserves * Generic container for JSON-based claims. Backed with a {@link Map} that preserves
@ -96,6 +100,21 @@ public class ClaimSet {
} }
} }
// TODO: not convinced I like this construct
public List getClaimAsList(String key) {
Object v = claims.get(key);
if (v != null) {
if (v instanceof List<?>) {
return (List) v;
} else {
// return a list of the singular element
return Lists.newArrayList(v);
}
} else {
return null;
}
}
/** /**
* Set an extension claim * Set an extension claim
*/ */
@ -150,6 +169,9 @@ public class ClaimSet {
* @return a copy of the data in this header in a JsonObject * @return a copy of the data in this header in a JsonObject
*/ */
public JsonObject getAsJsonObject() { public JsonObject getAsJsonObject() {
Gson g = new Gson();
JsonObject o = new JsonObject(); JsonObject o = new JsonObject();
@ -174,9 +196,11 @@ public class ClaimSet {
} else if (claim.getValue() instanceof Date) { } else if (claim.getValue() instanceof Date) {
// dates get serialized out as integers // dates get serialized out as integers
o.addProperty(claim.getKey(), ((Date)claim.getValue()).getTime() / 1000L); o.addProperty(claim.getKey(), ((Date)claim.getValue()).getTime() / 1000L);
} else if (claim.getValue() instanceof List) {
o.add(claim.getKey(), g.toJsonTree(claim.getValue(), new TypeToken<List<String>>(){}.getType()));
} else if (claim.getValue() != null) { } else if (claim.getValue() != null) {
// try to put it in as a string // try to put it in as a string
o.addProperty(claim.getKey(), claim.getValue().toString()); o.addProperty(claim.getKey(), g.toJson(claim.getValue()));
} else { } else {
// otherwise add in as a null // otherwise add in as a null
o.add(claim.getKey(), null); o.add(claim.getKey(), null);

View File

@ -15,11 +15,17 @@
******************************************************************************/ ******************************************************************************/
package org.mitre.jwt.model; package org.mitre.jwt.model;
import java.lang.reflect.Type;
import java.util.Date; import java.util.Date;
import java.util.List;
import java.util.Map.Entry; import java.util.Map.Entry;
import com.google.common.collect.Lists;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
public class JwtClaims extends ClaimSet { public class JwtClaims extends ClaimSet {
@ -70,7 +76,16 @@ public class JwtClaims extends ClaimSet {
} else if (element.getKey().equals(ISSUER)) { } else if (element.getKey().equals(ISSUER)) {
setIssuer(element.getValue().getAsString()); setIssuer(element.getValue().getAsString());
} else if (element.getKey().equals(AUDIENCE)) { } else if (element.getKey().equals(AUDIENCE)) {
setAudience(element.getValue().getAsString()); if (element.getValue().isJsonArray()) {
// it's an array of strings, set it as such
//setAudience(element.getValue().getAsJsonArray());
Type collectionType = new TypeToken<List<String>>(){}.getType();
List<String> values = new Gson().fromJson(element.getValue(), collectionType);
setAudience(values);
} else {
// it's a single value
setAudience(element.getValue().getAsString());
}
} else if (element.getKey().equals(SUBJECT)) { } else if (element.getKey().equals(SUBJECT)) {
setSubject(element.getValue().getAsString()); setSubject(element.getValue().getAsString());
} else if (element.getKey().equals(JWT_ID)) { } else if (element.getKey().equals(JWT_ID)) {
@ -144,17 +159,22 @@ public class JwtClaims extends ClaimSet {
setClaim(ISSUER, issuer); setClaim(ISSUER, issuer);
} }
/** /**
* @return the audience * @return the audience
*/ */
public String getAudience() { public List<String> getAudience() {
return getClaimAsString(AUDIENCE); return (List<String>) getClaimAsList(AUDIENCE);
} }
/** /**
* @param audience the audience to set * @param audience the audience to set
*/ */
public void setAudience(String audience) { public void setAudience(String audience) {
setClaim(AUDIENCE, Lists.newArrayList(audience));
}
public void setAudience(List<String> audience) {
setClaim(AUDIENCE, audience); setClaim(AUDIENCE, audience);
} }
@ -214,4 +234,23 @@ public class JwtClaims extends ClaimSet {
setClaim(NONCE, nonce); setClaim(NONCE, nonce);
} }
/* (non-Javadoc)
* @see org.mitre.jwt.model.ClaimSet#getAsJsonObject()
*/
@Override
public JsonObject getAsJsonObject() {
JsonObject o = super.getAsJsonObject();
// special handling for audience claim
if (o.has(AUDIENCE) && o.get(AUDIENCE).isJsonArray()) {
JsonArray aud = o.get(AUDIENCE).getAsJsonArray();
// overwrite single-sized arrays as a string
if (aud.size() == 1) {
o.addProperty(AUDIENCE, aud.get(0).getAsString());
}
}
return o;
}
} }