Search in sources :

Example 1 with OAuth2TokenValidatorResult

use of org.springframework.security.oauth2.core.OAuth2TokenValidatorResult in project spring-security by spring-projects.

the class NimbusJwtDecoder method validateJwt.

private Jwt validateJwt(Jwt jwt) {
    OAuth2TokenValidatorResult result = this.jwtValidator.validate(jwt);
    if (result.hasErrors()) {
        Collection<OAuth2Error> errors = result.getErrors();
        String validationErrorString = getJwtValidationExceptionMessage(errors);
        throw new JwtValidationException(validationErrorString, errors);
    }
    return jwt;
}
Also used : OAuth2TokenValidatorResult(org.springframework.security.oauth2.core.OAuth2TokenValidatorResult) OAuth2Error(org.springframework.security.oauth2.core.OAuth2Error)

Example 2 with OAuth2TokenValidatorResult

use of org.springframework.security.oauth2.core.OAuth2TokenValidatorResult in project spring-security by spring-projects.

the class NimbusReactiveJwtDecoderTests method decodeWhenReadingErrorPickTheFirstErrorMessage.

@Test
public void decodeWhenReadingErrorPickTheFirstErrorMessage() {
    OAuth2TokenValidator<Jwt> jwtValidator = mock(OAuth2TokenValidator.class);
    this.decoder.setJwtValidator(jwtValidator);
    OAuth2Error errorEmpty = new OAuth2Error("mock-error", "", "mock-uri");
    OAuth2Error error = new OAuth2Error("mock-error", "mock-description", "mock-uri");
    OAuth2Error error2 = new OAuth2Error("mock-error-second", "mock-description-second", "mock-uri-second");
    OAuth2TokenValidatorResult result = OAuth2TokenValidatorResult.failure(errorEmpty, error, error2);
    given(jwtValidator.validate(any(Jwt.class))).willReturn(result);
    // @formatter:off
    assertThatExceptionOfType(JwtValidationException.class).isThrownBy(() -> this.decoder.decode(this.messageReadToken).block()).withMessageContaining("mock-description");
// @formatter:on
}
Also used : OAuth2Error(org.springframework.security.oauth2.core.OAuth2Error) OAuth2TokenValidatorResult(org.springframework.security.oauth2.core.OAuth2TokenValidatorResult) Test(org.junit.jupiter.api.Test)

Example 3 with OAuth2TokenValidatorResult

use of org.springframework.security.oauth2.core.OAuth2TokenValidatorResult in project spring-security by spring-projects.

the class JwtIssuerValidatorTests method validateWhenIssuerUrlMismatchesThenReturnsError.

@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();
}
Also used : OAuth2TokenValidatorResult(org.springframework.security.oauth2.core.OAuth2TokenValidatorResult) URL(java.net.URL) Test(org.junit.jupiter.api.Test)

Example 4 with OAuth2TokenValidatorResult

use of org.springframework.security.oauth2.core.OAuth2TokenValidatorResult in project spring-security by spring-projects.

the class JwtIssuerValidatorTests method validateWhenJwtHasNoIssuerThenReturnsError.

@Test
public void validateWhenJwtHasNoIssuerThenReturnsError() {
    Jwt jwt = TestJwts.jwt().claim(JwtClaimNames.AUD, "https://aud").build();
    OAuth2TokenValidatorResult result = this.validator.validate(jwt);
    assertThat(result.getErrors()).isNotEmpty();
}
Also used : OAuth2TokenValidatorResult(org.springframework.security.oauth2.core.OAuth2TokenValidatorResult) Test(org.junit.jupiter.api.Test)

Example 5 with OAuth2TokenValidatorResult

use of org.springframework.security.oauth2.core.OAuth2TokenValidatorResult in project spring-security by spring-projects.

the class JwtIssuerValidatorTests method validateWhenIssuerMismatchesThenReturnsError.

@Test
public void validateWhenIssuerMismatchesThenReturnsError() {
    Jwt jwt = TestJwts.jwt().claim(JwtClaimNames.ISS, "https://other").build();
    OAuth2TokenValidatorResult result = this.validator.validate(jwt);
    assertThat(result.getErrors()).isNotEmpty();
}
Also used : OAuth2TokenValidatorResult(org.springframework.security.oauth2.core.OAuth2TokenValidatorResult) Test(org.junit.jupiter.api.Test)

Aggregations

OAuth2TokenValidatorResult (org.springframework.security.oauth2.core.OAuth2TokenValidatorResult)11 Test (org.junit.jupiter.api.Test)9 OAuth2Error (org.springframework.security.oauth2.core.OAuth2Error)9 Instant (java.time.Instant)2 URL (java.net.URL)1 Duration (java.time.Duration)1 MockResponse (okhttp3.mockwebserver.MockResponse)1 MockWebServer (okhttp3.mockwebserver.MockWebServer)1 OAuth2TokenValidator (org.springframework.security.oauth2.core.OAuth2TokenValidator)1