Search in sources :

Example 6 with Converter

use of org.springframework.core.convert.converter.Converter in project spring-security by spring-projects.

the class OAuth2AccessTokenResponseHttpMessageConverterTests method writeInternalWhenConversionFailsThenThrowHttpMessageNotWritableException.

@Test
public void writeInternalWhenConversionFailsThenThrowHttpMessageNotWritableException() {
    Converter tokenResponseParametersConverter = mock(Converter.class);
    given(tokenResponseParametersConverter.convert(any())).willThrow(RuntimeException.class);
    this.messageConverter.setTokenResponseParametersConverter(tokenResponseParametersConverter);
    // @formatter:off
    OAuth2AccessTokenResponse accessTokenResponse = OAuth2AccessTokenResponse.withToken("access-token-1234").tokenType(OAuth2AccessToken.TokenType.BEARER).expiresIn(Instant.now().plusSeconds(3600).toEpochMilli()).build();
    // @formatter:on
    MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
    assertThatExceptionOfType(HttpMessageNotWritableException.class).isThrownBy(() -> this.messageConverter.writeInternal(accessTokenResponse, outputMessage)).withMessageContaining("An error occurred writing the OAuth 2.0 Access Token Response");
}
Also used : OAuth2AccessTokenResponse(org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse) MockHttpOutputMessage(org.springframework.mock.http.MockHttpOutputMessage) Converter(org.springframework.core.convert.converter.Converter) Test(org.junit.jupiter.api.Test)

Example 7 with Converter

use of org.springframework.core.convert.converter.Converter in project spring-security by spring-projects.

the class OAuth2ErrorHttpMessageConverterTests method readInternalWhenConversionFailsThenThrowHttpMessageNotReadableException.

@Test
public void readInternalWhenConversionFailsThenThrowHttpMessageNotReadableException() {
    Converter errorConverter = mock(Converter.class);
    given(errorConverter.convert(any())).willThrow(RuntimeException.class);
    this.messageConverter.setErrorConverter(errorConverter);
    String errorResponse = "{}";
    MockClientHttpResponse response = new MockClientHttpResponse(errorResponse.getBytes(), HttpStatus.BAD_REQUEST);
    assertThatExceptionOfType(HttpMessageNotReadableException.class).isThrownBy(() -> this.messageConverter.readInternal(OAuth2Error.class, response)).withMessageContaining("An error occurred reading the OAuth 2.0 Error");
}
Also used : Converter(org.springframework.core.convert.converter.Converter) MockClientHttpResponse(org.springframework.mock.http.client.MockClientHttpResponse) Test(org.junit.jupiter.api.Test)

Example 8 with Converter

use of org.springframework.core.convert.converter.Converter in project spring-security by spring-projects.

the class OAuth2ErrorHttpMessageConverterTests method writeInternalWhenConversionFailsThenThrowHttpMessageNotWritableException.

@Test
public void writeInternalWhenConversionFailsThenThrowHttpMessageNotWritableException() {
    Converter errorParametersConverter = mock(Converter.class);
    given(errorParametersConverter.convert(any())).willThrow(RuntimeException.class);
    this.messageConverter.setErrorParametersConverter(errorParametersConverter);
    OAuth2Error oauth2Error = new OAuth2Error("unauthorized_client", "The client is not authorized", "https://tools.ietf.org/html/rfc6749#section-5.2");
    MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
    assertThatExceptionOfType(HttpMessageNotWritableException.class).isThrownBy(() -> this.messageConverter.writeInternal(oauth2Error, outputMessage)).withMessageContaining("An error occurred writing the OAuth 2.0 Error");
}
Also used : MockHttpOutputMessage(org.springframework.mock.http.MockHttpOutputMessage) Converter(org.springframework.core.convert.converter.Converter) OAuth2Error(org.springframework.security.oauth2.core.OAuth2Error) Test(org.junit.jupiter.api.Test)

Example 9 with Converter

use of org.springframework.core.convert.converter.Converter in project spring-security by spring-projects.

the class DelegatingJwtGrantedAuthoritiesConverterTests method convertWhenMultipleConvertersThenDuplicatesRemoved.

@Test
public void convertWhenMultipleConvertersThenDuplicatesRemoved() {
    Converter<Jwt, Collection<GrantedAuthority>> one = (jwt) -> AuthorityUtils.createAuthorityList("one", "two");
    Converter<Jwt, Collection<GrantedAuthority>> two = (jwt) -> AuthorityUtils.createAuthorityList("one", "three");
    DelegatingJwtGrantedAuthoritiesConverter composite = new DelegatingJwtGrantedAuthoritiesConverter(one, two);
    Jwt jwt = TestJwts.jwt().build();
    Collection<GrantedAuthority> authorities = composite.convert(jwt);
    assertThat(authorityListToOrderedSet(authorities)).containsExactly("one", "two", "three");
}
Also used : Test(org.junit.jupiter.api.Test) Converter(org.springframework.core.convert.converter.Converter) Collection(java.util.Collection) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) TestJwts(org.springframework.security.oauth2.jwt.TestJwts) Jwt(org.springframework.security.oauth2.jwt.Jwt) AuthorityUtils(org.springframework.security.core.authority.AuthorityUtils) LinkedHashSet(java.util.LinkedHashSet) GrantedAuthority(org.springframework.security.core.GrantedAuthority) Jwt(org.springframework.security.oauth2.jwt.Jwt) GrantedAuthority(org.springframework.security.core.GrantedAuthority) Collection(java.util.Collection) Test(org.junit.jupiter.api.Test)

Example 10 with Converter

use of org.springframework.core.convert.converter.Converter in project spring-security by spring-projects.

the class ReactiveJwtAuthenticationConverterTests method convertWithOverriddenGrantedAuthoritiesConverter.

@Test
public void convertWithOverriddenGrantedAuthoritiesConverter() {
    Jwt jwt = TestJwts.jwt().claim("scope", "message:read message:write").build();
    Converter<Jwt, Flux<GrantedAuthority>> grantedAuthoritiesConverter = (token) -> Flux.just(new SimpleGrantedAuthority("blah"));
    this.jwtAuthenticationConverter.setJwtGrantedAuthoritiesConverter(grantedAuthoritiesConverter);
    AbstractAuthenticationToken authentication = this.jwtAuthenticationConverter.convert(jwt).block();
    Collection<GrantedAuthority> authorities = authentication.getAuthorities();
    assertThat(authorities).containsExactly(new SimpleGrantedAuthority("blah"));
}
Also used : Test(org.junit.jupiter.api.Test) Converter(org.springframework.core.convert.converter.Converter) Flux(reactor.core.publisher.Flux) AbstractAuthenticationToken(org.springframework.security.authentication.AbstractAuthenticationToken) Collection(java.util.Collection) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) TestJwts(org.springframework.security.oauth2.jwt.TestJwts) Jwt(org.springframework.security.oauth2.jwt.Jwt) GrantedAuthority(org.springframework.security.core.GrantedAuthority) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) AbstractAuthenticationToken(org.springframework.security.authentication.AbstractAuthenticationToken) Jwt(org.springframework.security.oauth2.jwt.Jwt) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) GrantedAuthority(org.springframework.security.core.GrantedAuthority) Flux(reactor.core.publisher.Flux) Test(org.junit.jupiter.api.Test)

Aggregations

Converter (org.springframework.core.convert.converter.Converter)33 Test (org.junit.jupiter.api.Test)25 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)15 Assertions.assertThatIllegalArgumentException (org.assertj.core.api.Assertions.assertThatIllegalArgumentException)13 Assertions.assertThatExceptionOfType (org.assertj.core.api.Assertions.assertThatExceptionOfType)12 ClientRegistration (org.springframework.security.oauth2.client.registration.ClientRegistration)12 StandardCharsets (java.nio.charset.StandardCharsets)10 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)10 BDDMockito.given (org.mockito.BDDMockito.given)10 Mockito.mock (org.mockito.Mockito.mock)10 OAuth2AccessTokenResponse (org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse)9 JWK (com.nimbusds.jose.jwk.JWK)8 Collections (java.util.Collections)8 Function (java.util.function.Function)8 SecretKeySpec (javax.crypto.spec.SecretKeySpec)8 MockResponse (okhttp3.mockwebserver.MockResponse)8 MockWebServer (okhttp3.mockwebserver.MockWebServer)8 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)8 AfterEach (org.junit.jupiter.api.AfterEach)8 BeforeEach (org.junit.jupiter.api.BeforeEach)8