audience field is now plural
							parent
							
								
									27a26e0a35
								
							
						
					
					
						commit
						da43ba4d55
					
				| 
						 | 
				
			
			@ -19,15 +19,19 @@ import java.io.ByteArrayInputStream;
 | 
			
		|||
import java.io.InputStreamReader;
 | 
			
		||||
import java.util.Date;
 | 
			
		||||
import java.util.LinkedHashMap;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import java.util.Map;
 | 
			
		||||
import java.util.Map.Entry;
 | 
			
		||||
 | 
			
		||||
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.JsonObject;
 | 
			
		||||
import com.google.gson.JsonParser;
 | 
			
		||||
import com.google.gson.JsonPrimitive;
 | 
			
		||||
import com.google.gson.reflect.TypeToken;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Generic container for JSON-based claims. Backed with a {@link Map} that preserves
 | 
			
		||||
| 
						 | 
				
			
			@ -95,6 +99,21 @@ public class ClaimSet {
 | 
			
		|||
    		return null;
 | 
			
		||||
    	}
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // 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
 | 
			
		||||
| 
						 | 
				
			
			@ -150,6 +169,9 @@ public class ClaimSet {
 | 
			
		|||
	 * @return a copy of the data in this header in a JsonObject
 | 
			
		||||
	 */
 | 
			
		||||
	public JsonObject getAsJsonObject() {
 | 
			
		||||
		
 | 
			
		||||
		Gson g = new Gson();
 | 
			
		||||
		
 | 
			
		||||
		JsonObject o = new JsonObject();
 | 
			
		||||
		
 | 
			
		||||
		
 | 
			
		||||
| 
						 | 
				
			
			@ -174,9 +196,11 @@ public class ClaimSet {
 | 
			
		|||
				} else if (claim.getValue() instanceof Date) {
 | 
			
		||||
					// dates get serialized out as integers
 | 
			
		||||
					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) {
 | 
			
		||||
					// try to put it in as a string
 | 
			
		||||
					o.addProperty(claim.getKey(), claim.getValue().toString());
 | 
			
		||||
					o.addProperty(claim.getKey(), g.toJson(claim.getValue()));
 | 
			
		||||
				} else {
 | 
			
		||||
					// otherwise add in as a null
 | 
			
		||||
					o.add(claim.getKey(), null);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -15,11 +15,17 @@
 | 
			
		|||
 ******************************************************************************/
 | 
			
		||||
package org.mitre.jwt.model;
 | 
			
		||||
 | 
			
		||||
import java.lang.reflect.Type;
 | 
			
		||||
import java.util.Date;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
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.JsonObject;
 | 
			
		||||
import com.google.gson.reflect.TypeToken;
 | 
			
		||||
 | 
			
		||||
public class JwtClaims extends ClaimSet {
 | 
			
		||||
	
 | 
			
		||||
| 
						 | 
				
			
			@ -69,8 +75,17 @@ public class JwtClaims extends ClaimSet {
 | 
			
		|||
                setIssuedAt(new Date(element.getValue().getAsLong() * 1000L));
 | 
			
		||||
	        } else if (element.getKey().equals(ISSUER)) {	        	
 | 
			
		||||
	        	setIssuer(element.getValue().getAsString());
 | 
			
		||||
	        } else if (element.getKey().equals(AUDIENCE)) {	        	
 | 
			
		||||
	        	setAudience(element.getValue().getAsString());
 | 
			
		||||
	        } else if (element.getKey().equals(AUDIENCE)) {
 | 
			
		||||
	        	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)) {	        	
 | 
			
		||||
	        	setSubject(element.getValue().getAsString());
 | 
			
		||||
	        } else if (element.getKey().equals(JWT_ID)) {	        	
 | 
			
		||||
| 
						 | 
				
			
			@ -144,20 +159,25 @@ public class JwtClaims extends ClaimSet {
 | 
			
		|||
    	setClaim(ISSUER, issuer);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
    /**
 | 
			
		||||
     * @return the audience
 | 
			
		||||
     */
 | 
			
		||||
    public String getAudience() {
 | 
			
		||||
    	return getClaimAsString(AUDIENCE);
 | 
			
		||||
    public List<String> getAudience() {
 | 
			
		||||
    	return (List<String>) getClaimAsList(AUDIENCE);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
     * @param audience the audience to set
 | 
			
		||||
     */
 | 
			
		||||
    public void setAudience(String audience) {
 | 
			
		||||
    	setClaim(AUDIENCE, audience);
 | 
			
		||||
    	setClaim(AUDIENCE, Lists.newArrayList(audience));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    
 | 
			
		||||
    public void setAudience(List<String> audience) {
 | 
			
		||||
    	setClaim(AUDIENCE, audience);
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
	/**
 | 
			
		||||
     * @return the principal
 | 
			
		||||
     */
 | 
			
		||||
| 
						 | 
				
			
			@ -214,4 +234,23 @@ public class JwtClaims extends ClaimSet {
 | 
			
		|||
    	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;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue