Search in sources :

Example 26 with OAuth2AccessTokenResponse

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

the class OAuth2BodyExtractorsTests method oauth2AccessTokenResponseWhenEmptyThenException.

@Test
public void oauth2AccessTokenResponseWhenEmptyThenException() {
    BodyExtractor<Mono<OAuth2AccessTokenResponse>, ReactiveHttpInputMessage> extractor = OAuth2BodyExtractors.oauth2AccessTokenResponse();
    MockClientHttpResponse response = new MockClientHttpResponse(HttpStatus.OK);
    Mono<OAuth2AccessTokenResponse> result = extractor.extract(response, this.context);
    // @formatter:off
    assertThatExceptionOfType(OAuth2AuthorizationException.class).isThrownBy(result::block).withMessageContaining("Empty OAuth 2.0 Access Token Response");
// @formatter:on
}
Also used : OAuth2AccessTokenResponse(org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse) Mono(reactor.core.publisher.Mono) ReactiveHttpInputMessage(org.springframework.http.ReactiveHttpInputMessage) MockClientHttpResponse(org.springframework.mock.http.client.reactive.MockClientHttpResponse) Test(org.junit.jupiter.api.Test)

Example 27 with OAuth2AccessTokenResponse

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

the class OAuth2BodyExtractorsTests method oauth2AccessTokenResponseWhenInvalidJsonThenException.

@Test
public void oauth2AccessTokenResponseWhenInvalidJsonThenException() {
    BodyExtractor<Mono<OAuth2AccessTokenResponse>, ReactiveHttpInputMessage> extractor = OAuth2BodyExtractors.oauth2AccessTokenResponse();
    MockClientHttpResponse response = new MockClientHttpResponse(HttpStatus.OK);
    response.getHeaders().setContentType(MediaType.APPLICATION_JSON);
    response.setBody("{");
    Mono<OAuth2AccessTokenResponse> result = extractor.extract(response, this.context);
    // @formatter:off
    assertThatExceptionOfType(OAuth2AuthorizationException.class).isThrownBy(result::block).withMessageContaining("An error occurred parsing the Access Token response");
// @formatter:on
}
Also used : OAuth2AccessTokenResponse(org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse) Mono(reactor.core.publisher.Mono) ReactiveHttpInputMessage(org.springframework.http.ReactiveHttpInputMessage) MockClientHttpResponse(org.springframework.mock.http.client.reactive.MockClientHttpResponse) Test(org.junit.jupiter.api.Test)

Example 28 with OAuth2AccessTokenResponse

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

the class OAuth2BodyExtractorsTests method oauth2AccessTokenResponseWhenMultipleAttributeTypesThenCreated.

@Test
public // gh-6087
void oauth2AccessTokenResponseWhenMultipleAttributeTypesThenCreated() {
    BodyExtractor<Mono<OAuth2AccessTokenResponse>, ReactiveHttpInputMessage> extractor = OAuth2BodyExtractors.oauth2AccessTokenResponse();
    MockClientHttpResponse response = new MockClientHttpResponse(HttpStatus.OK);
    response.getHeaders().setContentType(MediaType.APPLICATION_JSON);
    // @formatter:off
    response.setBody("{\n" + "       \"access_token\":\"2YotnFZFEjr1zCsicMWpAA\",\n" + "       \"token_type\":\"Bearer\",\n" + "       \"expires_in\":3600,\n" + "       \"refresh_token\":\"tGzv3JOkF0XG5Qx2TlKWIA\",\n" + "       \"subjson\":{}, \n" + "		  \"list\":[]  \n" + "     }");
    // @formatter:on
    Instant now = Instant.now();
    OAuth2AccessTokenResponse result = extractor.extract(response, this.context).block();
    assertThat(result.getAccessToken().getTokenValue()).isEqualTo("2YotnFZFEjr1zCsicMWpAA");
    assertThat(result.getAccessToken().getTokenType()).isEqualTo(OAuth2AccessToken.TokenType.BEARER);
    assertThat(result.getAccessToken().getExpiresAt()).isBetween(now.plusSeconds(3600), now.plusSeconds(3600 + 2));
    assertThat(result.getRefreshToken().getTokenValue()).isEqualTo("tGzv3JOkF0XG5Qx2TlKWIA");
    assertThat(result.getAdditionalParameters().get("subjson")).isInstanceOfAny(Map.class);
    assertThat(result.getAdditionalParameters().get("list")).isInstanceOfAny(List.class);
}
Also used : OAuth2AccessTokenResponse(org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse) Mono(reactor.core.publisher.Mono) Instant(java.time.Instant) ReactiveHttpInputMessage(org.springframework.http.ReactiveHttpInputMessage) MockClientHttpResponse(org.springframework.mock.http.client.reactive.MockClientHttpResponse) Test(org.junit.jupiter.api.Test)

Example 29 with OAuth2AccessTokenResponse

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

the class OAuth2AccessTokenResponseHttpMessageConverterTests method readInternalWhenSuccessfulTokenResponseWithObjectThenReadOAuth2AccessTokenResponse.

// gh-6463
@Test
public void readInternalWhenSuccessfulTokenResponseWithObjectThenReadOAuth2AccessTokenResponse() {
    // @formatter:off
    String tokenResponse = "{\n" + "   \"access_token\": \"access-token-1234\",\n" + "   \"token_type\": \"bearer\",\n" + "   \"expires_in\": 3600,\n" + "   \"scope\": \"read write\",\n" + "   \"refresh_token\": \"refresh-token-1234\",\n" + "   \"custom_object_1\": {\"name1\": \"value1\"},\n" + "   \"custom_object_2\": [\"value1\", \"value2\"],\n" + "   \"custom_parameter_1\": \"custom-value-1\",\n" + "   \"custom_parameter_2\": \"custom-value-2\"\n" + "}\n";
    // @formatter:on
    MockClientHttpResponse response = new MockClientHttpResponse(tokenResponse.getBytes(), HttpStatus.OK);
    OAuth2AccessTokenResponse accessTokenResponse = this.messageConverter.readInternal(OAuth2AccessTokenResponse.class, response);
    assertThat(accessTokenResponse.getAccessToken().getTokenValue()).isEqualTo("access-token-1234");
    assertThat(accessTokenResponse.getAccessToken().getTokenType()).isEqualTo(OAuth2AccessToken.TokenType.BEARER);
    assertThat(accessTokenResponse.getAccessToken().getExpiresAt()).isBeforeOrEqualTo(Instant.now().plusSeconds(3600));
    assertThat(accessTokenResponse.getAccessToken().getScopes()).containsExactly("read", "write");
    assertThat(accessTokenResponse.getRefreshToken().getTokenValue()).isEqualTo("refresh-token-1234");
    Map<String, String> additionalParameters = accessTokenResponse.getAdditionalParameters().entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, (entry) -> String.valueOf(entry.getValue())));
    assertThat(additionalParameters).containsExactly(entry("custom_object_1", "{name1=value1}"), entry("custom_object_2", "[value1, value2]"), entry("custom_parameter_1", "custom-value-1"), entry("custom_parameter_2", "custom-value-2"));
}
Also used : OAuth2AccessTokenResponse(org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse) Converter(org.springframework.core.convert.converter.Converter) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Set(java.util.Set) HashMap(java.util.HashMap) Instant(java.time.Instant) HttpMessageNotWritableException(org.springframework.http.converter.HttpMessageNotWritableException) MockClientHttpResponse(org.springframework.mock.http.client.MockClientHttpResponse) OAuth2AccessTokenResponse(org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse) Collectors(java.util.stream.Collectors) Assertions.entry(org.assertj.core.api.Assertions.entry) HttpMessageNotReadableException(org.springframework.http.converter.HttpMessageNotReadableException) Test(org.junit.jupiter.api.Test) HttpStatus(org.springframework.http.HttpStatus) BDDMockito.given(org.mockito.BDDMockito.given) Map(java.util.Map) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) OAuth2AccessToken(org.springframework.security.oauth2.core.OAuth2AccessToken) MockHttpOutputMessage(org.springframework.mock.http.MockHttpOutputMessage) LinkedHashSet(java.util.LinkedHashSet) Mockito.mock(org.mockito.Mockito.mock) HashMap(java.util.HashMap) Map(java.util.Map) MockClientHttpResponse(org.springframework.mock.http.client.MockClientHttpResponse) Test(org.junit.jupiter.api.Test)

Example 30 with OAuth2AccessTokenResponse

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

the class OAuth2AccessTokenResponseHttpMessageConverterTests method readInternalWhenSuccessfulTokenResponseWithNullValueThenReadOAuth2AccessTokenResponse.

// gh-8108
@Test
public void readInternalWhenSuccessfulTokenResponseWithNullValueThenReadOAuth2AccessTokenResponse() {
    // @formatter:off
    String tokenResponse = "{\n" + "   \"access_token\": \"access-token-1234\",\n" + "   \"token_type\": \"bearer\",\n" + "   \"expires_in\": 3600,\n" + "   \"scope\": null,\n" + "   \"refresh_token\": \"refresh-token-1234\"\n" + "}\n";
    // @formatter:on
    MockClientHttpResponse response = new MockClientHttpResponse(tokenResponse.getBytes(), HttpStatus.OK);
    OAuth2AccessTokenResponse accessTokenResponse = this.messageConverter.readInternal(OAuth2AccessTokenResponse.class, response);
    assertThat(accessTokenResponse.getAccessToken().getTokenValue()).isEqualTo("access-token-1234");
    assertThat(accessTokenResponse.getAccessToken().getTokenType()).isEqualTo(OAuth2AccessToken.TokenType.BEARER);
    assertThat(accessTokenResponse.getAccessToken().getExpiresAt()).isBeforeOrEqualTo(Instant.now().plusSeconds(3600));
    assertThat(accessTokenResponse.getAccessToken().getScopes()).isEmpty();
    assertThat(accessTokenResponse.getRefreshToken().getTokenValue()).isEqualTo("refresh-token-1234");
}
Also used : OAuth2AccessTokenResponse(org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse) MockClientHttpResponse(org.springframework.mock.http.client.MockClientHttpResponse) Test(org.junit.jupiter.api.Test)

Aggregations

OAuth2AccessTokenResponse (org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse)134 Test (org.junit.jupiter.api.Test)122 OAuth2AccessToken (org.springframework.security.oauth2.core.OAuth2AccessToken)43 ClientRegistration (org.springframework.security.oauth2.client.registration.ClientRegistration)40 Instant (java.time.Instant)37 HashMap (java.util.HashMap)32 OAuth2AuthorizationRequest (org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest)27 Mono (reactor.core.publisher.Mono)18 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)16 TestingAuthenticationToken (org.springframework.security.authentication.TestingAuthenticationToken)16 OAuth2AuthorizationExchange (org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationExchange)16 OAuth2AuthorizationCodeGrantRequest (org.springframework.security.oauth2.client.endpoint.OAuth2AuthorizationCodeGrantRequest)15 OAuth2AuthorizationResponse (org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationResponse)15 OAuth2AuthenticationException (org.springframework.security.oauth2.core.OAuth2AuthenticationException)14 BeforeEach (org.junit.jupiter.api.BeforeEach)13 Map (java.util.Map)12 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)12 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)11 Assertions.assertThatExceptionOfType (org.assertj.core.api.Assertions.assertThatExceptionOfType)11 Assertions.assertThatIllegalArgumentException (org.assertj.core.api.Assertions.assertThatIllegalArgumentException)11