-
Notifications
You must be signed in to change notification settings - Fork 580
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Jwt now support multiple issuers and multi issuer validation
Signed-off-by: David Kral <[email protected]>
- Loading branch information
Showing
2 changed files
with
128 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,8 +18,10 @@ | |
|
||
import java.time.Instant; | ||
import java.time.temporal.ChronoUnit; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
import java.util.UUID; | ||
import java.util.logging.Logger; | ||
|
||
|
@@ -29,7 +31,9 @@ | |
import org.junit.jupiter.api.Test; | ||
|
||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.CoreMatchers.startsWith; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
/** | ||
* Unit test for {@link Jwt}. | ||
|
@@ -101,4 +105,53 @@ public void testOidcJwt() { | |
errors.log(LOGGER); | ||
errors.checkValid(); | ||
} | ||
|
||
@Test | ||
public void testMultiIssuers() { | ||
String audience = "id_of_audience"; | ||
String subject = "54564645646465"; | ||
String username = "[email protected]"; | ||
String issuer = "I am issuer"; | ||
String secondIssuer = "I am second issuer"; | ||
String invalidIssuer = "I am invalid issuer"; | ||
Instant now = Instant.now(); | ||
|
||
Jwt jwt = Jwt.builder() | ||
.jwtId(UUID.randomUUID().toString()) | ||
.subject(subject) | ||
.preferredUsername(username) | ||
.algorithm(JwkRSA.ALG_RS256) | ||
.addAudience(audience) | ||
.addIssuer(issuer) | ||
.addIssuer(secondIssuer) | ||
// time info | ||
.issueTime(now) | ||
.build(); | ||
|
||
//and this one should be valid | ||
List<Validator<Jwt>> vals = new ArrayList<>(); | ||
Jwt.addIssuerValidator(vals, issuer, true); | ||
Jwt.addIssuerValidator(vals, secondIssuer, true); | ||
|
||
Errors errors = jwt.validate(vals); | ||
|
||
errors.log(LOGGER); | ||
errors.checkValid(); | ||
|
||
//another try with defaults | ||
errors = jwt.validate(issuer, audience); | ||
errors.log(LOGGER); | ||
errors.checkValid(); | ||
|
||
Errors.ErrorMessagesException exception = assertThrows(Errors.ErrorMessagesException.class, () -> { | ||
List<Validator<Jwt>> validators = new ArrayList<>(); | ||
Jwt.addIssuerValidator(validators, invalidIssuer, true); | ||
Errors errors2 = jwt.validate(validators); | ||
errors2.log(LOGGER); | ||
errors2.checkValid(); | ||
}); | ||
assertThat(exception.getMessage(), | ||
startsWith("FATAL: Issuer must contain issuer \"I am invalid issuer\", " | ||
+ "yet it contains: [I am issuer, I am second issuer]")); | ||
} | ||
} |