Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package org.springframework.security.oauth2.jwt;

import java.util.function.Predicate;

import org.springframework.security.oauth2.core.OAuth2TokenValidator;
import org.springframework.security.oauth2.core.OAuth2TokenValidatorResult;
import org.springframework.util.Assert;
Expand All @@ -28,15 +30,17 @@
*/
public final class JwtIssuerValidator implements OAuth2TokenValidator<Jwt> {

private final JwtClaimValidator<String> validator;
private final JwtClaimValidator<Object> validator;

/**
* Constructs a {@link JwtIssuerValidator} using the provided parameters
* @param issuer - The issuer that each {@link Jwt} should have.
*/
public JwtIssuerValidator(String issuer) {
Assert.notNull(issuer, "issuer cannot be null");
this.validator = new JwtClaimValidator(JwtClaimNames.ISS, issuer::equals);

Predicate<Object> testClaimValue = (claimValue) -> (claimValue != null) && issuer.equals(claimValue.toString());
this.validator = new JwtClaimValidator<>(JwtClaimNames.ISS, testClaimValue);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

package org.springframework.security.oauth2.jwt;

import java.net.MalformedURLException;
import java.net.URL;

import org.junit.Test;

import org.springframework.security.oauth2.core.OAuth2TokenValidatorResult;
Expand All @@ -42,13 +45,29 @@ public void validateWhenIssuerMatchesThenReturnsSuccess() {
// @formatter:on
}

@Test
public void validateWhenIssuerUrlMatchesThenReturnsSuccess() throws MalformedURLException {
Jwt jwt = TestJwts.jwt().claim("iss", new URL(ISSUER)).build();

assertThat(this.validator.validate(jwt)).isEqualTo(OAuth2TokenValidatorResult.success());
}

@Test
public void validateWhenIssuerMismatchesThenReturnsError() {
Jwt jwt = TestJwts.jwt().claim(JwtClaimNames.ISS, "https://other").build();
OAuth2TokenValidatorResult result = this.validator.validate(jwt);
assertThat(result.getErrors()).isNotEmpty();
}

@Test
public void validateWhenIssuerUrlMismatchesThenReturnsError() throws MalformedURLException {
Jwt jwt = TestJwts.jwt().claim(JwtClaimNames.ISS, new URL("https://other")).build();

OAuth2TokenValidatorResult result = this.validator.validate(jwt);

assertThat(result.getErrors()).isNotEmpty();
}

@Test
public void validateWhenJwtHasNoIssuerThenReturnsError() {
Jwt jwt = TestJwts.jwt().claim(JwtClaimNames.AUD, "https://aud").build();
Expand Down