Skip to content

Commit d5c05d7

Browse files
authored
empty expected audience array should throw InvalidClaimException (#679)
1 parent bad6035 commit d5c05d7

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

lib/src/main/java/com/auth0/jwt/JWTVerifier.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -364,12 +364,19 @@ private boolean assertInstantIsLessThanOrEqualToNow(Instant claimVal, long leewa
364364
}
365365

366366
private boolean assertValidAudienceClaim(
367-
List<String> audience,
368-
List<String> values,
367+
List<String> actualAudience,
368+
List<String> expectedAudience,
369369
boolean shouldContainAll
370370
) {
371-
return !(audience == null || (shouldContainAll && !audience.containsAll(values))
372-
|| (!shouldContainAll && Collections.disjoint(audience, values)));
371+
if (actualAudience == null || expectedAudience == null) {
372+
return false;
373+
}
374+
375+
if (shouldContainAll) {
376+
return actualAudience.containsAll(expectedAudience);
377+
} else {
378+
return !Collections.disjoint(actualAudience, expectedAudience);
379+
}
373380
}
374381

375382
private void assertPositive(long leeway) {

lib/src/test/java/com/auth0/jwt/JWTVerifierTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,21 @@ public void shouldThrowWhenAudienceClaimIsNullWithAnAudience() {
310310
assertThat(e.getClaimValue().asArray(String.class), is(new String[] {null}));
311311
}
312312

313+
@Test
314+
public void shouldThrowWhenExpectedEmptyList() {
315+
IncorrectClaimException e = assertThrows(null, IncorrectClaimException.class, () -> {
316+
// Token 'aud': 'wide audience'
317+
String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJ3aWRlIGF1ZGllbmNlIn0.c9anq03XepcuEKWEVsPk9cck0sIIfrT6hHbBsCar49o";
318+
JWTVerifier.init(Algorithm.HMAC256("secret"))
319+
.withAnyOfAudience(new String[0])
320+
.build()
321+
.verify(token);
322+
});
323+
assertThat(e.getMessage(), is("The Claim 'aud' value doesn't contain the required audience."));
324+
assertThat(e.getClaimName(), is(RegisteredClaims.AUDIENCE));
325+
assertThat(e.getClaimValue().asString(), is("wide audience"));
326+
}
327+
313328
@Test
314329
public void shouldNotReplaceWhenMultipleChecksAreAdded() {
315330
JWTVerifier verifier = JWTVerifier.init(Algorithm.HMAC256("secret"))

0 commit comments

Comments
 (0)