X Tutup
Skip to content

Commit 6ffa703

Browse files
itdevelopmentappslbalmaceda
authored andcommitted
Support multiple issuers auth0#246 (auth0#288)
* Support multiple issuers auth0#246 * Implemented comments after review * Implemented comments after review, rolled back remove claim unit test * Implemented comments after review * Added tests to increase coverage
1 parent 7fcd1bf commit 6ffa703

File tree

3 files changed

+49
-8
lines changed

3 files changed

+49
-8
lines changed

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,12 @@ public static class BaseVerification implements Verification {
6161
/**
6262
* Require a specific Issuer ("iss") claim.
6363
*
64-
* @param issuer the required Issuer value
64+
* @param issuer the required Issuer value. If multiple values are given, the claim must at least match one of them
6565
* @return this same Verification instance.
6666
*/
6767
@Override
68-
public Verification withIssuer(String issuer) {
69-
requireClaim(PublicClaims.ISSUER, issuer);
68+
public Verification withIssuer(String... issuer) {
69+
requireClaim(PublicClaims.ISSUER, issuer == null ? null : Arrays.asList(issuer));
7070
return this;
7171
}
7272

@@ -90,7 +90,7 @@ public Verification withSubject(String subject) {
9090
*/
9191
@Override
9292
public Verification withAudience(String... audience) {
93-
requireClaim(PublicClaims.AUDIENCE, Arrays.asList(audience));
93+
requireClaim(PublicClaims.AUDIENCE, audience == null ? null : Arrays.asList(audience));
9494
return this;
9595
}
9696

@@ -398,7 +398,6 @@ private void verifyClaims(DecodedJWT jwt, Map<String, Object> claims) throws Tok
398398
for (Map.Entry<String, Object> entry : claims.entrySet()) {
399399
switch (entry.getKey()) {
400400
case PublicClaims.AUDIENCE:
401-
//noinspection unchecked
402401
assertValidAudienceClaim(jwt.getAudience(), (List<String>) entry.getValue());
403402
break;
404403
case PublicClaims.EXPIRES_AT:
@@ -411,7 +410,7 @@ private void verifyClaims(DecodedJWT jwt, Map<String, Object> claims) throws Tok
411410
assertValidDateClaim(jwt.getNotBefore(), (Long) entry.getValue(), false);
412411
break;
413412
case PublicClaims.ISSUER:
414-
assertValidStringClaim(entry.getKey(), jwt.getIssuer(), (String) entry.getValue());
413+
assertValidIssuerClaim(jwt.getIssuer(), (List<String>) entry.getValue());
415414
break;
416415
case PublicClaims.JWT_ID:
417416
assertValidStringClaim(entry.getKey(), jwt.getId(), (String) entry.getValue());
@@ -486,4 +485,10 @@ private void assertValidAudienceClaim(List<String> audience, List<String> value)
486485
throw new InvalidClaimException("The Claim 'aud' value doesn't contain the required audience.");
487486
}
488487
}
488+
489+
private void assertValidIssuerClaim(String issuer, List<String> value) {
490+
if (issuer == null || !value.contains(issuer)) {
491+
throw new InvalidClaimException("The Claim 'iss' value doesn't match the required issuer.");
492+
}
493+
}
489494
}

lib/src/main/java/com/auth0/jwt/interfaces/Verification.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import java.util.Date;
66

77
public interface Verification {
8-
Verification withIssuer(String issuer);
8+
Verification withIssuer(String... issuer);
99

1010
Verification withSubject(String subject);
1111

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

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,41 @@ public void shouldValidateIssuer() throws Exception {
5151
assertThat(jwt, is(notNullValue()));
5252
}
5353

54+
@Test
55+
public void shouldValidateMultipleIssuers() {
56+
String auth0Token = "eyJhbGciOiJIUzI1NiIsImN0eSI6IkpXVCJ9.eyJpc3MiOiJhdXRoMCJ9.mZ0m_N1J4PgeqWmi903JuUoDRZDBPB7HwkS4nVyWH1M";
57+
String otherIssuertoken = "eyJhbGciOiJIUzI1NiIsImN0eSI6IkpXVCJ9.eyJpc3MiOiJvdGhlcklzc3VlciJ9.k4BCOJJl-c0_Y-49VD_mtt-u0QABKSV5i3W-RKc74co";
58+
JWTVerifier verifier = JWTVerifier.init(Algorithm.HMAC256("secret"))
59+
.withIssuer("otherIssuer", "auth0")
60+
.build();
61+
62+
assertThat(verifier.verify(auth0Token), is(notNullValue()));
63+
assertThat(verifier.verify(otherIssuertoken), is(notNullValue()));
64+
}
65+
5466
@Test
5567
public void shouldThrowOnInvalidIssuer() throws Exception {
5668
exception.expect(InvalidClaimException.class);
57-
exception.expectMessage("The Claim 'iss' value doesn't match the required one.");
69+
exception.expectMessage("The Claim 'iss' value doesn't match the required issuer.");
5870
String token = "eyJhbGciOiJIUzI1NiIsImN0eSI6IkpXVCJ9.eyJpc3MiOiJhdXRoMCJ9.mZ0m_N1J4PgeqWmi903JuUoDRZDBPB7HwkS4nVyWH1M";
5971
JWTVerifier.init(Algorithm.HMAC256("secret"))
6072
.withIssuer("invalid")
6173
.build()
6274
.verify(token);
6375
}
6476

77+
@Test
78+
public void shouldThrowOnNullIssuer() throws Exception {
79+
exception.expect(InvalidClaimException.class);
80+
exception.expectMessage("The Claim 'iss' value doesn't match the required issuer.");
81+
82+
String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.e30.t-IDcSemACt8x4iTMCda8Yhe3iZaWbvV5XKSTbuAn0M";
83+
JWTVerifier.init(Algorithm.HMAC256("secret"))
84+
.withIssuer("auth0")
85+
.build()
86+
.verify(token);
87+
}
88+
6589
@Test
6690
public void shouldValidateSubject() throws Exception {
6791
String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.Rq8IxqeX7eA6GgYxlcHdPFVRNFFZc5rEI3MQTZZbK3I";
@@ -126,6 +150,18 @@ public void shouldThrowOnInvalidAudience() throws Exception {
126150
.verify(token);
127151
}
128152

153+
@Test
154+
public void shouldRemoveAudienceWhenPassingNull() throws Exception {
155+
Algorithm algorithm = mock(Algorithm.class);
156+
JWTVerifier verifier = JWTVerifier.init(algorithm)
157+
.withAudience("John")
158+
.withAudience(null)
159+
.build();
160+
161+
assertThat(verifier.claims, is(notNullValue()));
162+
assertThat(verifier.claims, not(hasKey("aud")));
163+
}
164+
129165
@Test
130166
public void shouldThrowOnNullCustomClaimName() throws Exception {
131167
exception.expect(IllegalArgumentException.class);

0 commit comments

Comments
 (0)
X Tutup