Skip to content

Commit

Permalink
Improve Javadoc, use anon types where possible, add unit test for Map…
Browse files Browse the repository at this point in the history
… in List, formatting
  • Loading branch information
skjolber authored and lbalmaceda committed Feb 13, 2020
1 parent 0e5e12f commit 0130a26
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 42 deletions.
32 changes: 18 additions & 14 deletions lib/src/main/java/com/auth0/jwt/JWTCreator.java
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ public Builder withArrayClaim(String name, Integer[] items) throws IllegalArgume
* @param name the Claim's name.
* @param items the Claim's value.
* @return this same Builder instance.
* @throws IllegalArgumentException if the name is null, or if the value is null or of an unsupported type
* @throws IllegalArgumentException if the name is null
*/
public Builder withArrayClaim(String name, Long[] items) throws IllegalArgumentException {
assertNonNull(name);
Expand All @@ -307,15 +307,18 @@ public Builder withArrayClaim(String name, Long[] items) throws IllegalArgumentE

/**
* Add a custom Map Claim with the given items.
* Accepted nested types are {@linkplain Map} and {@linkplain List} with basic types
* Boolean, Integer, Long, Double, String and Date.
*
* Accepted nested types are {@linkplain Map} and {@linkplain List} with basic types
* {@linkplain Boolean}, {@linkplain Integer}, {@linkplain Long}, {@linkplain Double},
* {@linkplain String} and {@linkplain Date}. {@linkplain Map}s cannot contain null keys or values.
* {@linkplain List}s can contain null elements.
*
* @param name the Claim's name.
* @param map the Claim's key-values.
* @return this same Builder instance.
* @throws IllegalArgumentException if the name is null, or if the value is null or of an unsupported type
* @throws IllegalArgumentException if the name is null, or if the map contents does not validate.
*/
public Builder withClaim(String name, Map<String, Object> map) throws IllegalArgumentException {
public Builder withClaim(String name, Map<String, ?> map) throws IllegalArgumentException {
assertNonNull(name);
// validate map contents
if(!validateClaim(map)) {
Expand All @@ -328,16 +331,18 @@ public Builder withClaim(String name, Map<String, Object> map) throws IllegalArg
/**
* Add a custom List Claim with the given items.
*
* Accepted nested types are {@linkplain Map} and {@linkplain List} with basic types
* Boolean, Integer, Long, Double, String and Date.
*
* Accepted nested types are {@linkplain Map} and {@linkplain List} with basic types
* {@linkplain Boolean}, {@linkplain Integer}, {@linkplain Long}, {@linkplain Double},
* {@linkplain String} and {@linkplain Date}. {@linkplain Map}s cannot contain null keys or values.
* {@linkplain List}s can contain null elements.
*
* @param name the Claim's name.
* @param list the Claim's list of values.
* @return this same Builder instance.
* @throws IllegalArgumentException if the name is null, or if the value is null or of an unsupported type
* @throws IllegalArgumentException if the name is null, or if the list contents does not validate.
*/

public Builder withClaim(String name, List<Object> list) throws IllegalArgumentException {
public Builder withClaim(String name, List<?> list) throws IllegalArgumentException {
assertNonNull(name);
// validate list contents
if(!validateClaim(list)) {
Expand All @@ -347,9 +352,9 @@ public Builder withClaim(String name, List<Object> list) throws IllegalArgumentE
return this;
}

private static boolean validateClaim(Map<?, Object> map) {
private static boolean validateClaim(Map<?, ?> map) {
// do not accept null values in maps
for (Entry<?, Object> entry : map.entrySet()) {
for (Entry<?, ?> entry : map.entrySet()) {
Object value = entry.getValue();
if(value == null || !isSupportedType(value)) {
return false;
Expand All @@ -372,12 +377,11 @@ private static boolean validateClaim(List<?> list) {
return true;
}

@SuppressWarnings("unchecked")
private static boolean isSupportedType(Object value) {
if(value instanceof List) {
return validateClaim((List<?>)value);
} else if(value instanceof Map) {
return validateClaim((Map<?, Object>)value);
return validateClaim((Map<?, ?>)value);
} else {
return isBasicType(value);
}
Expand Down
70 changes: 42 additions & 28 deletions lib/src/test/java/com/auth0/jwt/JWTCreatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -436,9 +436,9 @@ public void shouldAcceptCustomArrayClaimOfTypeLong() throws Exception {
String[] parts = jwt.split("\\.");
assertThat(parts[1], is("eyJuYW1lIjpbMSwyLDNdfQ"));
}

@Test
public void shouldAcceptCustomClaimOfTypeObject() throws Exception {
public void shouldAcceptCustomClaimOfTypeMap() throws Exception {
Map<String, Object> data = new HashMap<>();
data.put("test1", "abc");
data.put("test2", "def");
Expand All @@ -450,33 +450,32 @@ public void shouldAcceptCustomClaimOfTypeObject() throws Exception {
String[] parts = jwt.split("\\.");
assertThat(parts[1], is("eyJkYXRhIjp7InRlc3QyIjoiZGVmIiwidGVzdDEiOiJhYmMifX0"));
}

@Test
public void shouldRefuseCustomClaimOfTypeUserPojo() throws Exception{
Map<String, Object> data = new HashMap<>();
Map<String, Object> data = new HashMap<>();
data.put("test1", new UserPojo("Michael", 255));

exception.expect(IllegalArgumentException.class);

JWTCreator.init()
JWTCreator.init()
.withClaim("pojo", data)
.sign(Algorithm.HMAC256("secret"));
}



@SuppressWarnings("unchecked")
@Test
@Test
public void shouldAcceptCustomMapClaimOfBasicObjectTypes() throws Exception {
Map<String, Object> data = new HashMap<>();

// simple types
data.put("string", "abc");
data.put("integer", 1);
data.put("long", Long.MAX_VALUE);
data.put("double", 123.456d);
data.put("date", new Date(123L));
data.put("boolean", true);

// array types
data.put("intArray", new Integer[]{3, 5});
data.put("longArray", new Long[]{Long.MAX_VALUE, Long.MIN_VALUE});
Expand All @@ -486,7 +485,7 @@ public void shouldAcceptCustomMapClaimOfBasicObjectTypes() throws Exception {

Map<String, Object> sub = new HashMap<>();
sub.put("subKey", "subValue");

data.put("map", sub);

String jwt = JWTCreator.init()
Expand All @@ -506,7 +505,7 @@ public void shouldAcceptCustomMapClaimOfBasicObjectTypes() throws Exception {
assertThat(map.get("double"), is(123.456d));
assertThat(map.get("date"), is(123));
assertThat(map.get("boolean"), is(true));

// array types
assertThat(map.get("intArray"), is(Arrays.asList(new Integer[]{3, 5})));
assertThat(map.get("longArray"), is(Arrays.asList(new Long[]{Long.MAX_VALUE, Long.MIN_VALUE})));
Expand All @@ -517,12 +516,12 @@ public void shouldAcceptCustomMapClaimOfBasicObjectTypes() throws Exception {
assertThat(map.get("map"), is(sub));

}

@SuppressWarnings("unchecked")
@Test
@Test
public void shouldAcceptCustomListClaimOfBasicObjectTypes() throws Exception {
List<Object> data = new ArrayList<>();

// simple types
data.add("abc");
data.add(1);
Expand All @@ -540,7 +539,7 @@ public void shouldAcceptCustomListClaimOfBasicObjectTypes() throws Exception {

Map<String, Object> sub = new HashMap<>();
sub.put("subKey", "subValue");

data.add(sub);

String jwt = JWTCreator.init()
Expand Down Expand Up @@ -571,7 +570,7 @@ public void shouldAcceptCustomListClaimOfBasicObjectTypes() throws Exception {
assertThat(list.get(10), is(sub));

}

@Test
public void shouldAcceptCustomClaimForNullListItem() throws Exception{
Map<String, Object> data = new HashMap<>();
Expand All @@ -581,7 +580,7 @@ public void shouldAcceptCustomClaimForNullListItem() throws Exception{
.withClaim("pojo", data)
.sign(Algorithm.HMAC256("secret"));
}

@Test
public void shouldRefuseCustomClaimForNullMapValue() throws Exception{
Map<String, Object> data = new HashMap<>();
Expand All @@ -593,7 +592,7 @@ public void shouldRefuseCustomClaimForNullMapValue() throws Exception{
.withClaim("pojo", data)
.sign(Algorithm.HMAC256("secret"));
}

@Test
public void shouldRefuseCustomClaimForNullMapKey() throws Exception{
Map<String, Object> data = new HashMap<>();
Expand All @@ -604,8 +603,9 @@ public void shouldRefuseCustomClaimForNullMapKey() throws Exception{
JWTCreator.init()
.withClaim("pojo", data)
.sign(Algorithm.HMAC256("secret"));
}

}

@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void shouldRefuseCustomMapClaimForNonStringKey() throws Exception{
Map data = new HashMap<>();
Expand All @@ -617,28 +617,42 @@ public void shouldRefuseCustomMapClaimForNonStringKey() throws Exception{
.withClaim("pojo", (Map<String, Object>)data)
.sign(Algorithm.HMAC256("secret"));
}

@Test
public void shouldRefuseCustomListClaimForUnknownListElement() throws Exception{
List<Object> list = Arrays.asList(new UserPojo("Michael", 255));
List<Object> list = Arrays.asList(new UserPojo("Michael", 255));

exception.expect(IllegalArgumentException.class);

JWTCreator.init()
.withClaim("list", list)
.sign(Algorithm.HMAC256("secret"));
}

@Test
public void shouldRefuseCustomListClaimForUnknownListElementWrappedInAMap() throws Exception{
List<Object> list = Arrays.asList(new UserPojo("Michael", 255));

Map<String, Object> data = new HashMap<>();
data.put("someList", list);

exception.expect(IllegalArgumentException.class);

JWTCreator.init()
.withClaim("list", list)
.sign(Algorithm.HMAC256("secret"));
}

@Test
public void shouldRefuseCustomListClaimForUnknownArrayType() throws Exception{
List<Object> list = new ArrayList<>();
List<Object> list = new ArrayList<>();
list.add(new Object[] {"test"});

exception.expect(IllegalArgumentException.class);

JWTCreator.init()
.withClaim("list", list)
.sign(Algorithm.HMAC256("secret"));
}

}

0 comments on commit 0130a26

Please sign in to comment.