Search in sources :

Example 31 with OAuth2AccessTokenResponse

use of org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse 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 32 with OAuth2AccessTokenResponse

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

the class OAuth2AccessTokenResponseHttpMessageConverterTests method writeInternalWhenOAuth2AccessTokenResponseThenWriteTokenResponse.

@Test
public void writeInternalWhenOAuth2AccessTokenResponseThenWriteTokenResponse() throws Exception {
    Instant expiresAt = Instant.now().plusSeconds(3600);
    Set<String> scopes = new LinkedHashSet<>(Arrays.asList("read", "write"));
    Map<String, Object> additionalParameters = new HashMap<>();
    additionalParameters.put("custom_parameter_1", "custom-value-1");
    additionalParameters.put("custom_parameter_2", "custom-value-2");
    // @formatter:off
    OAuth2AccessTokenResponse accessTokenResponse = OAuth2AccessTokenResponse.withToken("access-token-1234").tokenType(OAuth2AccessToken.TokenType.BEARER).expiresIn(expiresAt.toEpochMilli()).scopes(scopes).refreshToken("refresh-token-1234").additionalParameters(additionalParameters).build();
    // @formatter:on
    MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
    this.messageConverter.writeInternal(accessTokenResponse, outputMessage);
    String tokenResponse = outputMessage.getBodyAsString();
    assertThat(tokenResponse).contains("\"access_token\":\"access-token-1234\"");
    assertThat(tokenResponse).contains("\"token_type\":\"Bearer\"");
    assertThat(tokenResponse).contains("\"expires_in\"");
    assertThat(tokenResponse).contains("\"scope\":\"read write\"");
    assertThat(tokenResponse).contains("\"refresh_token\":\"refresh-token-1234\"");
    assertThat(tokenResponse).contains("\"custom_parameter_1\":\"custom-value-1\"");
    assertThat(tokenResponse).contains("\"custom_parameter_2\":\"custom-value-2\"");
}
Also used : LinkedHashSet(java.util.LinkedHashSet) OAuth2AccessTokenResponse(org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse) HashMap(java.util.HashMap) Instant(java.time.Instant) MockHttpOutputMessage(org.springframework.mock.http.MockHttpOutputMessage) Test(org.junit.jupiter.api.Test)

Example 33 with OAuth2AccessTokenResponse

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

the class DefaultMapOAuth2AccessTokenResponseConverterTests method shouldConvertFull.

@Test
public void shouldConvertFull() {
    Map<String, Object> map = new HashMap<>();
    map.put("access_token", "access-token-1234");
    map.put("token_type", "bearer");
    map.put("expires_in", "3600");
    map.put("scope", "read write");
    map.put("refresh_token", "refresh-token-1234");
    map.put("custom_parameter_1", "custom-value-1");
    map.put("custom_parameter_2", "custom-value-2");
    OAuth2AccessTokenResponse converted = this.messageConverter.convert(map);
    OAuth2AccessToken accessToken = converted.getAccessToken();
    Assertions.assertNotNull(accessToken);
    Assertions.assertEquals("access-token-1234", accessToken.getTokenValue());
    Assertions.assertEquals(OAuth2AccessToken.TokenType.BEARER, accessToken.getTokenType());
    Set<String> scopes = accessToken.getScopes();
    Assertions.assertNotNull(scopes);
    Assertions.assertEquals(2, scopes.size());
    Assertions.assertTrue(scopes.contains("read"));
    Assertions.assertTrue(scopes.contains("write"));
    Assertions.assertEquals(3600, Duration.between(accessToken.getIssuedAt(), accessToken.getExpiresAt()).getSeconds());
    OAuth2RefreshToken refreshToken = converted.getRefreshToken();
    Assertions.assertNotNull(refreshToken);
    Assertions.assertEquals("refresh-token-1234", refreshToken.getTokenValue());
    Map<String, Object> additionalParameters = converted.getAdditionalParameters();
    Assertions.assertNotNull(additionalParameters);
    Assertions.assertEquals(2, additionalParameters.size());
    Assertions.assertEquals("custom-value-1", additionalParameters.get("custom_parameter_1"));
    Assertions.assertEquals("custom-value-2", additionalParameters.get("custom_parameter_2"));
}
Also used : OAuth2RefreshToken(org.springframework.security.oauth2.core.OAuth2RefreshToken) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) OAuth2AccessToken(org.springframework.security.oauth2.core.OAuth2AccessToken) Test(org.junit.jupiter.api.Test)

Example 34 with OAuth2AccessTokenResponse

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

the class DefaultMapOAuth2AccessTokenResponseConverterTests method shouldConvertWithObjectAdditionalParameter.

// gh-9685
@Test
public void shouldConvertWithObjectAdditionalParameter() {
    Map<String, Object> map = new HashMap<>();
    map.put("access_token", "access-token-1234");
    map.put("token_type", "bearer");
    map.put("expires_in", "3600");
    map.put("scope", "read write");
    map.put("refresh_token", "refresh-token-1234");
    Map<String, Object> nestedObject = new LinkedHashMap<>();
    nestedObject.put("a", "first value");
    nestedObject.put("b", "second value");
    map.put("custom_parameter_1", nestedObject);
    map.put("custom_parameter_2", "custom-value-2");
    OAuth2AccessTokenResponse converted = this.messageConverter.convert(map);
    OAuth2AccessToken accessToken = converted.getAccessToken();
    Assertions.assertNotNull(accessToken);
    Assertions.assertEquals("access-token-1234", accessToken.getTokenValue());
    Assertions.assertEquals(OAuth2AccessToken.TokenType.BEARER, accessToken.getTokenType());
    Set<String> scopes = accessToken.getScopes();
    Assertions.assertNotNull(scopes);
    Assertions.assertEquals(2, scopes.size());
    Assertions.assertTrue(scopes.contains("read"));
    Assertions.assertTrue(scopes.contains("write"));
    Assertions.assertEquals(3600, Duration.between(accessToken.getIssuedAt(), accessToken.getExpiresAt()).getSeconds());
    OAuth2RefreshToken refreshToken = converted.getRefreshToken();
    Assertions.assertNotNull(refreshToken);
    Assertions.assertEquals("refresh-token-1234", refreshToken.getTokenValue());
    Map<String, Object> additionalParameters = converted.getAdditionalParameters();
    Assertions.assertNotNull(additionalParameters);
    Assertions.assertEquals(2, additionalParameters.size());
    Assertions.assertEquals(nestedObject, additionalParameters.get("custom_parameter_1"));
    Assertions.assertEquals("custom-value-2", additionalParameters.get("custom_parameter_2"));
}
Also used : OAuth2RefreshToken(org.springframework.security.oauth2.core.OAuth2RefreshToken) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) OAuth2AccessToken(org.springframework.security.oauth2.core.OAuth2AccessToken) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.jupiter.api.Test)

Example 35 with OAuth2AccessTokenResponse

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

the class OAuth2AccessTokenResponseBodyExtractor method oauth2AccessTokenResponse.

private static Mono<AccessTokenResponse> oauth2AccessTokenResponse(TokenResponse tokenResponse) {
    if (tokenResponse.indicatesSuccess()) {
        return Mono.just(tokenResponse).cast(AccessTokenResponse.class);
    }
    TokenErrorResponse tokenErrorResponse = (TokenErrorResponse) tokenResponse;
    ErrorObject errorObject = tokenErrorResponse.getErrorObject();
    OAuth2Error oauth2Error = getOAuth2Error(errorObject);
    return Mono.error(new OAuth2AuthorizationException(oauth2Error));
}
Also used : OAuth2AuthorizationException(org.springframework.security.oauth2.core.OAuth2AuthorizationException) TokenErrorResponse(com.nimbusds.oauth2.sdk.TokenErrorResponse) ErrorObject(com.nimbusds.oauth2.sdk.ErrorObject) OAuth2Error(org.springframework.security.oauth2.core.OAuth2Error)

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