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");
}
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\"");
}
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"));
}
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"));
}
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));
}
Aggregations