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

empty expected audience array should throw InvalidClaimException #679

Merged
merged 4 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
15 changes: 11 additions & 4 deletions lib/src/main/java/com/auth0/jwt/JWTVerifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -364,12 +364,19 @@ private boolean assertInstantIsLessThanOrEqualToNow(Instant claimVal, long leewa
}

private boolean assertValidAudienceClaim(
List<String> audience,
List<String> values,
List<String> actualAudience,
List<String> expectedAudience,
boolean shouldContainAll
) {
return !(audience == null || (shouldContainAll && !audience.containsAll(values))
|| (!shouldContainAll && Collections.disjoint(audience, values)));
if (actualAudience == null || expectedAudience == null) {
return false;
}

if (shouldContainAll) {
return actualAudience.containsAll(expectedAudience);
} else {
return !Collections.disjoint(actualAudience, expectedAudience);
}
}

private void assertPositive(long leeway) {
Expand Down
15 changes: 15 additions & 0 deletions lib/src/test/java/com/auth0/jwt/JWTVerifierTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,21 @@ public void shouldThrowWhenAudienceClaimIsNullWithAnAudience() {
assertThat(e.getClaimValue().asArray(String.class), is(new String[] {null}));
}

@Test
public void shouldThrowWhenExpectedEmptyList() {
IncorrectClaimException e = assertThrows(null, IncorrectClaimException.class, () -> {
// Token 'aud': 'wide audience'
String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJ3aWRlIGF1ZGllbmNlIn0.c9anq03XepcuEKWEVsPk9cck0sIIfrT6hHbBsCar49o";
JWTVerifier.init(Algorithm.HMAC256("secret"))
.withAnyOfAudience(new String[0])
.build()
.verify(token);
});
assertThat(e.getMessage(), is("The Claim 'aud' value doesn't contain the required audience."));
assertThat(e.getClaimName(), is(RegisteredClaims.AUDIENCE));
assertThat(e.getClaimValue().asString(), is("wide audience"));
}

@Test
public void shouldNotReplaceWhenMultipleChecksAreAdded() {
JWTVerifier verifier = JWTVerifier.init(Algorithm.HMAC256("secret"))
Expand Down