Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SDK-3158] Null claim handling #564

Merged
merged 17 commits into from
Mar 25, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fixed formatting errors
  • Loading branch information
poovamraj committed Mar 25, 2022
commit 2f1c5df7c13f571d624acd3c1b6dae9d6336ea11
14 changes: 11 additions & 3 deletions lib/src/main/java/com/auth0/jwt/JWTCreator.java
Original file line number Diff line number Diff line change
@@ -372,6 +372,13 @@ public Builder withClaim(String name, List<?> list) throws IllegalArgumentExcept
return this;
}

/**
* Add a custom claim with null value.
*
* @param name the Claim's name.
* @return this same Builder instance.
* @throws IllegalArgumentException if the name is null
*/
public Builder withNullClaim(String name) throws IllegalArgumentException {
assertNonNull(name);
addClaim(name, null);
@@ -426,7 +433,8 @@ public Builder withArrayClaim(String name, Long[] items) throws IllegalArgumentE
* <p>
* Accepted 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 and {@linkplain List}s can contain null elements.
* {@linkplain String} and {@linkplain Date}.
* {@linkplain Map}s and {@linkplain List}s can contain null elements.
* </p>
*
* <p>
@@ -445,7 +453,7 @@ public Builder withPayload(Map<String, ?> payloadClaims) throws IllegalArgumentE

if (!validatePayload(payloadClaims)) {
throw new IllegalArgumentException("Claim values must only be of types Map, List, Boolean, Integer, "
+ "Long, Double, String and Date");
+ "Long, Double, String, Date and Null");
}

// add claims only after validating all claims so as not to corrupt the claims map of this builder
@@ -509,7 +517,7 @@ private static boolean isSupportedType(Object value) {
}

private static boolean isBasicType(Object value) {
if(value == null){
if (value == null) {
return true;
} else {
Class<?> c = value.getClass();
4 changes: 2 additions & 2 deletions lib/src/main/java/com/auth0/jwt/JWTVerifier.java
Original file line number Diff line number Diff line change
@@ -298,8 +298,8 @@ private boolean assertValidCollectionClaim(Claim claim, Object[] expectedClaimVa
}
}
} else {
claimArr = claim.isNull() || claim.isMissing() ?
Collections.emptyList() : Arrays.asList(claim.as(Object[].class));
claimArr = claim.isNull() || claim.isMissing()
? Collections.emptyList() : Arrays.asList(claim.as(Object[].class));
}
List<Object> valueArr = Arrays.asList(expectedClaimValue);
return claimArr.containsAll(valueArr);
4 changes: 2 additions & 2 deletions lib/src/main/java/com/auth0/jwt/impl/JsonNodeClaim.java
Original file line number Diff line number Diff line change
@@ -126,7 +126,7 @@ public Map<String, Object> asMap() throws JWTDecodeException {
@Override
public <T> T as(Class<T> clazz) throws JWTDecodeException {
try {
if(isMissing() || isNull()) {
if (isMissing() || isNull()) {
return null;
}
return objectReader.treeAsTokens(data).readValueAs(clazz);
@@ -147,7 +147,7 @@ public boolean isMissing() {

@Override
public String toString() {
if(isMissing()) {
if (isMissing()) {
return "Missing claim";
} else if (isNull()) {
return "Null claim";
Original file line number Diff line number Diff line change
@@ -134,6 +134,7 @@ default Verification withAnyOfAudience(String... audience) {

/**
* Require a specific Claim value to be null.
*
* @param name the Claim's name.
* @return this same Verification instance.
* @throws IllegalArgumentException if the name is null.