Search in sources :

Example 1 with OAuth2TokenValidator

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

the class OAuth2ResourceServerBeanDefinitionParserTests method requestWhenCustomJwtValidatorFailsThenCorrespondingErrorMessage.

@Test
public void requestWhenCustomJwtValidatorFailsThenCorrespondingErrorMessage() throws Exception {
    this.spring.configLocations(xml("MockJwtValidator"), xml("Jwt")).autowire();
    mockRestOperations(jwks("Default"));
    String token = this.token("ValidNoScopes");
    OAuth2TokenValidator<Jwt> jwtValidator = this.spring.getContext().getBean(OAuth2TokenValidator.class);
    OAuth2Error error = new OAuth2Error("custom-error", "custom-description", "custom-uri");
    given(jwtValidator.validate(any(Jwt.class))).willReturn(OAuth2TokenValidatorResult.failure(error));
    // @formatter:off
    this.mvc.perform(get("/").header("Authorization", "Bearer " + token)).andExpect(status().isUnauthorized()).andExpect(header().string(HttpHeaders.WWW_AUTHENTICATE, containsString("custom-description")));
// @formatter:on
}
Also used : Jwt(org.springframework.security.oauth2.jwt.Jwt) OAuth2Error(org.springframework.security.oauth2.core.OAuth2Error) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.jupiter.api.Test)

Example 2 with OAuth2TokenValidator

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

the class OidcIdTokenDecoderFactoryTests method createDecoderWhenCustomJwtValidatorFactorySetThenApplied.

@Test
public void createDecoderWhenCustomJwtValidatorFactorySetThenApplied() {
    Function<ClientRegistration, OAuth2TokenValidator<Jwt>> customJwtValidatorFactory = mock(Function.class);
    this.idTokenDecoderFactory.setJwtValidatorFactory(customJwtValidatorFactory);
    ClientRegistration clientRegistration = this.registration.build();
    given(customJwtValidatorFactory.apply(same(clientRegistration))).willReturn(new OidcIdTokenValidator(clientRegistration));
    this.idTokenDecoderFactory.createDecoder(clientRegistration);
    verify(customJwtValidatorFactory).apply(same(clientRegistration));
}
Also used : ClientRegistration(org.springframework.security.oauth2.client.registration.ClientRegistration) OAuth2TokenValidator(org.springframework.security.oauth2.core.OAuth2TokenValidator) Test(org.junit.jupiter.api.Test)

Example 3 with OAuth2TokenValidator

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

the class ReactiveOidcIdTokenDecoderFactoryTests method createDecoderWhenCustomJwtValidatorFactorySetThenApplied.

@Test
public void createDecoderWhenCustomJwtValidatorFactorySetThenApplied() {
    Function<ClientRegistration, OAuth2TokenValidator<Jwt>> customJwtValidatorFactory = mock(Function.class);
    this.idTokenDecoderFactory.setJwtValidatorFactory(customJwtValidatorFactory);
    ClientRegistration clientRegistration = this.registration.build();
    given(customJwtValidatorFactory.apply(same(clientRegistration))).willReturn(new OidcIdTokenValidator(clientRegistration));
    this.idTokenDecoderFactory.createDecoder(clientRegistration);
    verify(customJwtValidatorFactory).apply(same(clientRegistration));
}
Also used : ClientRegistration(org.springframework.security.oauth2.client.registration.ClientRegistration) OAuth2TokenValidator(org.springframework.security.oauth2.core.OAuth2TokenValidator) Test(org.junit.jupiter.api.Test)

Example 4 with OAuth2TokenValidator

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

the class NimbusJwtDecoderTests method decodeWhenJwtFailsValidationThenReturnsCorrespondingErrorMessage.

@Test
public void decodeWhenJwtFailsValidationThenReturnsCorrespondingErrorMessage() {
    OAuth2Error failure = new OAuth2Error("mock-error", "mock-description", "mock-uri");
    OAuth2TokenValidator<Jwt> jwtValidator = mock(OAuth2TokenValidator.class);
    given(jwtValidator.validate(any(Jwt.class))).willReturn(OAuth2TokenValidatorResult.failure(failure));
    this.jwtDecoder.setJwtValidator(jwtValidator);
    // @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) Test(org.junit.jupiter.api.Test)

Example 5 with OAuth2TokenValidator

use of org.springframework.security.oauth2.core.OAuth2TokenValidator 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)

Aggregations

Test (org.junit.jupiter.api.Test)13 OAuth2Error (org.springframework.security.oauth2.core.OAuth2Error)9 OAuth2TokenValidator (org.springframework.security.oauth2.core.OAuth2TokenValidator)5 OAuth2TokenValidatorResult (org.springframework.security.oauth2.core.OAuth2TokenValidatorResult)5 MockWebServer (okhttp3.mockwebserver.MockWebServer)4 Jwt (org.springframework.security.oauth2.jwt.Jwt)4 Collection (java.util.Collection)2 MockResponse (okhttp3.mockwebserver.MockResponse)2 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)2 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)2 ClientRegistration (org.springframework.security.oauth2.client.registration.ClientRegistration)2 DelegatingOAuth2TokenValidator (org.springframework.security.oauth2.core.DelegatingOAuth2TokenValidator)2 JwtDecoder (org.springframework.security.oauth2.jwt.JwtDecoder)1 NimbusReactiveJwtDecoder (org.springframework.security.oauth2.jwt.NimbusReactiveJwtDecoder)1 ReactiveJwtDecoder (org.springframework.security.oauth2.jwt.ReactiveJwtDecoder)1 SupplierJwtDecoder (org.springframework.security.oauth2.jwt.SupplierJwtDecoder)1 SupplierReactiveJwtDecoder (org.springframework.security.oauth2.jwt.SupplierReactiveJwtDecoder)1