Search in sources :

Example 6 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)

Example 7 with OAuth2TokenValidatorResult

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

the class NimbusJwtDecoderTests method decodeWhenReadingErrorPickTheFirstErrorMessage.

@Test
public void decodeWhenReadingErrorPickTheFirstErrorMessage() {
    OAuth2TokenValidator<Jwt> jwtValidator = mock(OAuth2TokenValidator.class);
    this.jwtDecoder.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.jwtDecoder.decode(SIGNED_JWT)).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 8 with OAuth2TokenValidatorResult

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

the class NimbusJwtDecoderTests method decodeWhenJwtValidationHasTwoErrorsThenJwtExceptionMessageShowsFirstError.

@Test
public void decodeWhenJwtValidationHasTwoErrorsThenJwtExceptionMessageShowsFirstError() {
    OAuth2Error firstFailure = new OAuth2Error("mock-error", "mock-description", "mock-uri");
    OAuth2Error secondFailure = new OAuth2Error("another-error", "another-description", "another-uri");
    OAuth2TokenValidatorResult result = OAuth2TokenValidatorResult.failure(firstFailure, secondFailure);
    OAuth2TokenValidator<Jwt> jwtValidator = mock(OAuth2TokenValidator.class);
    given(jwtValidator.validate(any(Jwt.class))).willReturn(result);
    this.jwtDecoder.setJwtValidator(jwtValidator);
    // @formatter:off
    assertThatExceptionOfType(JwtValidationException.class).isThrownBy(() -> this.jwtDecoder.decode(SIGNED_JWT)).withMessageContaining("mock-description").satisfies((ex) -> assertThat(ex).hasFieldOrPropertyWithValue("errors", Arrays.asList(firstFailure, secondFailure)));
// @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 9 with OAuth2TokenValidatorResult

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

the class NimbusReactiveJwtDecoderTests method decodeWhenUsingCustomValidatorThenValidatorIsInvoked.

@Test
public void decodeWhenUsingCustomValidatorThenValidatorIsInvoked() {
    OAuth2TokenValidator jwtValidator = mock(OAuth2TokenValidator.class);
    this.decoder.setJwtValidator(jwtValidator);
    OAuth2Error error = new OAuth2Error("mock-error", "mock-description", "mock-uri");
    OAuth2TokenValidatorResult result = OAuth2TokenValidatorResult.failure(error);
    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 : OAuth2TokenValidator(org.springframework.security.oauth2.core.OAuth2TokenValidator) OAuth2Error(org.springframework.security.oauth2.core.OAuth2Error) OAuth2TokenValidatorResult(org.springframework.security.oauth2.core.OAuth2TokenValidatorResult) Test(org.junit.jupiter.api.Test)

Example 10 with OAuth2TokenValidatorResult

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

the class NimbusJwtDecoderJwkSupportTests method decodeWhenJwtValidationHasTwoErrorsThenJwtExceptionMessageShowsFirstError.

@Test
public void decodeWhenJwtValidationHasTwoErrorsThenJwtExceptionMessageShowsFirstError() throws Exception {
    try (MockWebServer server = new MockWebServer()) {
        server.enqueue(new MockResponse().setBody(JWK_SET));
        String jwkSetUrl = server.url("/.well-known/jwks.json").toString();
        NimbusJwtDecoderJwkSupport decoder = new NimbusJwtDecoderJwkSupport(jwkSetUrl);
        OAuth2Error firstFailure = new OAuth2Error("mock-error", "mock-description", "mock-uri");
        OAuth2Error secondFailure = new OAuth2Error("another-error", "another-description", "another-uri");
        OAuth2TokenValidatorResult result = OAuth2TokenValidatorResult.failure(firstFailure, secondFailure);
        OAuth2TokenValidator<Jwt> jwtValidator = mock(OAuth2TokenValidator.class);
        given(jwtValidator.validate(any(Jwt.class))).willReturn(result);
        decoder.setJwtValidator(jwtValidator);
        // @formatter:off
        assertThatExceptionOfType(JwtValidationException.class).isThrownBy(() -> decoder.decode(SIGNED_JWT)).withMessageContaining("mock-description").satisfies((ex) -> assertThat(ex).hasFieldOrPropertyWithValue("errors", Arrays.asList(firstFailure, secondFailure)));
    // @formatter:on
    }
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) MockWebServer(okhttp3.mockwebserver.MockWebServer) OAuth2Error(org.springframework.security.oauth2.core.OAuth2Error) OAuth2TokenValidatorResult(org.springframework.security.oauth2.core.OAuth2TokenValidatorResult) Test(org.junit.jupiter.api.Test)

Aggregations

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