KEYCLOAK-1723 Allow aud to be single field or array
This commit is contained in:
1 parent
1fb3d7cd3b
commit
5ca3a48094
13 files changed
+128
-918
No files matched your search
@@ -0,0 +1,30 @@
|
||||
package org.keycloak.json;
|
||||
|
||||
import org.codehaus.jackson.JsonNode;
|
||||
import org.codehaus.jackson.JsonParser;
|
||||
import org.codehaus.jackson.JsonProcessingException;
|
||||
import org.codehaus.jackson.map.DeserializationContext;
|
||||
import org.codehaus.jackson.map.JsonDeserializer;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
|
||||
public class StringOrArrayDeserializer extends JsonDeserializer<Object> {
|
||||
|
||||
@Override
|
||||
public Object deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
|
||||
JsonNode jsonNode = jsonParser.readValueAsTree();
|
||||
if (jsonNode.isArray()) {
|
||||
ArrayList<String> a = new ArrayList<>(1);
|
||||
Iterator<JsonNode> itr = jsonNode.iterator();
|
||||
while (itr.hasNext()) {
|
||||
a.add(itr.next().getTextValue());
|
||||
}
|
||||
return a.toArray(new String[a.size()]);
|
||||
} else {
|
||||
return new String[] { jsonNode.getTextValue() };
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package org.keycloak.json;
|
||||
|
||||
import org.codehaus.jackson.JsonGenerator;
|
||||
import org.codehaus.jackson.map.JsonSerializer;
|
||||
import org.codehaus.jackson.map.SerializerProvider;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class StringOrArraySerializer extends JsonSerializer<Object> {
|
||||
@Override
|
||||
public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
|
||||
String[] array = (String[]) o;
|
||||
if (array == null) {
|
||||
jsonGenerator.writeNull();
|
||||
} else if (array.length == 1) {
|
||||
jsonGenerator.writeString(array[0]);
|
||||
} else {
|
||||
jsonGenerator.writeStartArray();
|
||||
for (String s : array) {
|
||||
jsonGenerator.writeString(s);
|
||||
}
|
||||
jsonGenerator.writeEndArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -163,11 +163,6 @@ public class AccessToken extends IDToken {
|
||||
return (AccessToken) super.issuer(issuer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public AccessToken audience(String audience) {
|
||||
return (AccessToken) super.audience(audience);
|
||||
}
|
||||
|
||||
@Override
|
||||
public AccessToken subject(String subject) {
|
||||
return (AccessToken) super.subject(subject);
|
||||
|
||||
@@ -1,12 +1,6 @@
|
||||
package org.keycloak.representations;
|
||||
|
||||
import org.codehaus.jackson.annotate.JsonAnyGetter;
|
||||
import org.codehaus.jackson.annotate.JsonAnySetter;
|
||||
import org.codehaus.jackson.annotate.JsonProperty;
|
||||
import org.codehaus.jackson.annotate.JsonUnwrapped;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
|
||||
|
||||
@@ -4,6 +4,10 @@ import org.codehaus.jackson.annotate.JsonAnyGetter;
|
||||
import org.codehaus.jackson.annotate.JsonAnySetter;
|
||||
import org.codehaus.jackson.annotate.JsonIgnore;
|
||||
import org.codehaus.jackson.annotate.JsonProperty;
|
||||
import org.codehaus.jackson.map.annotate.JsonDeserialize;
|
||||
import org.codehaus.jackson.map.annotate.JsonSerialize;
|
||||
import org.keycloak.json.StringOrArrayDeserializer;
|
||||
import org.keycloak.json.StringOrArraySerializer;
|
||||
import org.keycloak.util.Time;
|
||||
|
||||
import java.io.Serializable;
|
||||
@@ -26,14 +30,16 @@ public class JsonWebToken implements Serializable {
|
||||
@JsonProperty("iss")
|
||||
protected String issuer;
|
||||
@JsonProperty("aud")
|
||||
protected String audience;
|
||||
@JsonSerialize(using = StringOrArraySerializer.class)
|
||||
@JsonDeserialize(using = StringOrArrayDeserializer.class)
|
||||
protected String[] audience;
|
||||
@JsonProperty("sub")
|
||||
protected String subject;
|
||||
@JsonProperty("typ")
|
||||
protected String type;
|
||||
@JsonProperty("azp")
|
||||
public String issuedFor;
|
||||
protected Map<String, Object> otherClaims = new HashMap<String, Object>();
|
||||
protected Map<String, Object> otherClaims = new HashMap<>();
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
@@ -72,7 +78,6 @@ public class JsonWebToken implements Serializable {
|
||||
@JsonIgnore
|
||||
public boolean isNotBefore() {
|
||||
return Time.currentTime() >= notBefore;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -113,12 +118,21 @@ public class JsonWebToken implements Serializable {
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
public String getAudience() {
|
||||
@JsonIgnore
|
||||
public String[] getAudience() {
|
||||
return audience;
|
||||
}
|
||||
|
||||
public JsonWebToken audience(String audience) {
|
||||
public boolean hasAudience(String audience) {
|
||||
for (String a : this.audience) {
|
||||
if (a.equals(audience)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public JsonWebToken audience(String... audience) {
|
||||
this.audience = audience;
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@ package org.keycloak.util;
|
||||
import org.codehaus.jackson.map.ObjectMapper;
|
||||
import org.codehaus.jackson.map.SerializationConfig;
|
||||
import org.codehaus.jackson.map.annotate.JsonSerialize;
|
||||
import org.codehaus.jackson.type.TypeReference;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
package org.keycloak.jose;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.keycloak.representations.JsonWebToken;
|
||||
import org.keycloak.util.JsonSerialization;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* Created by st on 20.08.15.
|
||||
*/
|
||||
public class JsonWebTokenTest {
|
||||
|
||||
@Test
|
||||
public void testAudSingle() throws IOException {
|
||||
String single = "{ \"aud\": \"test\" }";
|
||||
JsonWebToken s = JsonSerialization.readValue(single, JsonWebToken.class);
|
||||
assertArrayEquals(new String[] { "test" }, s.getAudience());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAudArray() throws IOException {
|
||||
String single = "{ \"aud\": [\"test\"] }";
|
||||
JsonWebToken s = JsonSerialization.readValue(single, JsonWebToken.class);
|
||||
assertArrayEquals(new String[]{"test"}, s.getAudience());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() throws IOException {
|
||||
JsonWebToken jsonWebToken = new JsonWebToken();
|
||||
jsonWebToken.audience("test");
|
||||
assertTrue(JsonSerialization.writeValueAsPrettyString(jsonWebToken).contains("\"aud\" : \"test\""));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testArray() throws IOException {
|
||||
JsonWebToken jsonWebToken = new JsonWebToken();
|
||||
jsonWebToken.audience("test", "test2");
|
||||
assertTrue(JsonSerialization.writeValueAsPrettyString(jsonWebToken).contains("\"aud\" : [ \"test\", \"test2\" ]"));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user