Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions sdk/spring/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
## 4.5.0-beta.2 (Unreleased)
Upgrade Spring Boot dependencies version to 2.7.4 and Spring Cloud dependencies version to 2021.0.4

### Bugs Fixed
- Fix bug: The parameter 'scope' is duplicated. [31191](https://github.com/Azure/azure-sdk-for-java/issues/31191).

## 4.4.0 (2022-09-26)
Upgrade Spring Boot dependencies version to 2.7.3 and Spring Cloud dependencies version to 2021.0.3
Upgrade Spring Boot dependencies version to 2.7.2 and Spring Cloud dependencies version to 2021.0.3.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,11 @@

import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.converter.FormHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.security.oauth2.client.http.OAuth2ErrorResponseErrorHandler;
import org.springframework.security.oauth2.core.http.converter.OAuth2AccessTokenResponseHttpMessageConverter;
import org.springframework.util.Assert;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
* Util class used to create {@link RestTemplate}s for all Azure AD related http requests.
*/
Expand All @@ -27,25 +24,12 @@ public static RestTemplate createRestTemplate(RestTemplateBuilder builder) {
}

public static RestTemplate createOAuth2ErrorResponseHandledRestTemplate(RestTemplateBuilder builder) {
RestTemplate restTemplate = createRestTemplate(builder);
restTemplate.setErrorHandler(new OAuth2ErrorResponseErrorHandler());
return restTemplate;
builder = builder.errorHandler(new OAuth2ErrorResponseErrorHandler());
return createRestTemplate(builder);
}

public static RestTemplate createOAuth2AccessTokenResponseClientRestTemplate(RestTemplateBuilder builder) {
RestTemplate restTemplate = createOAuth2ErrorResponseHandledRestTemplate(builder);
List<HttpMessageConverter<?>> converters = restTemplate.getMessageConverters();
if (notContainsElementOfType(converters, FormHttpMessageConverter.class)) {
converters.add(new FormHttpMessageConverter());
}
if (notContainsElementOfType(converters, OAuth2AccessTokenResponseHttpMessageConverter.class)) {
converters.add(new OAuth2AccessTokenResponseHttpMessageConverter());
}
return restTemplate;
}

private static boolean notContainsElementOfType(List<?> list, Class<?> clazz) {
return list.stream().noneMatch(item -> item.getClass().equals(clazz));
builder = builder.messageConverters(new FormHttpMessageConverter(), new OAuth2AccessTokenResponseHttpMessageConverter());
return createOAuth2ErrorResponseHandledRestTemplate(builder);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@
import org.springframework.security.oauth2.client.OAuth2AuthorizedClient;
import org.springframework.security.oauth2.client.jackson2.OAuth2ClientJackson2Module;

import java.util.HashMap;
import java.util.Collections;
import java.util.Map;

public final class SerializerUtils {
private static final ObjectMapper OBJECT_MAPPER;
private static final TypeReference<Map<String, OAuth2AuthorizedClient>> TYPE_REFERENCE =
new TypeReference<Map<String, OAuth2AuthorizedClient>>() {
};
new TypeReference<>() {};

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


static {
OBJECT_MAPPER = new ObjectMapper();
Expand Down Expand Up @@ -45,7 +44,7 @@ public static String serializeOAuth2AuthorizedClientMap(Map<String, OAuth2Author

public static Map<String, OAuth2AuthorizedClient> deserializeOAuth2AuthorizedClientMap(String authorizedClientsString) {
if (authorizedClientsString == null) {
return new HashMap<>();
return Collections.emptyMap();
}
Map<String, OAuth2AuthorizedClient> authorizedClients;
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@
package com.azure.spring.cloud.autoconfigure.aad.implementation.oauth2;

import com.azure.spring.cloud.core.implementation.util.AzureSpringIdentifier;
import org.springframework.core.convert.converter.Converter;
import org.springframework.http.HttpHeaders;
import org.springframework.http.RequestEntity;
import org.springframework.security.oauth2.client.endpoint.OAuth2AuthorizationCodeGrantRequest;
import org.springframework.security.oauth2.client.endpoint.OAuth2AuthorizationCodeGrantRequestEntityConverter;
import org.springframework.util.MultiValueMap;
import org.springframework.util.MultiValueMapAdapter;

import java.util.Collections;
import java.util.UUID;
Expand All @@ -20,30 +19,23 @@
public abstract class AbstractOAuth2AuthorizationCodeGrantRequestEntityConverter
extends OAuth2AuthorizationCodeGrantRequestEntityConverter {

protected AbstractOAuth2AuthorizationCodeGrantRequestEntityConverter() {
addHeadersConverter(this::getHttpHeaders);
addParametersConverter(this::getHttpBody);
}

/**
* Gets the application ID.
*
* @return the application ID
*/
protected abstract String getApplicationId();

@Override
@SuppressWarnings("unchecked")
public RequestEntity<?> convert(OAuth2AuthorizationCodeGrantRequest request) {
addHeadersConverter(headersConverter);
addParametersConverter(parametersConverter);
return super.convert(request);
}

private final Converter<OAuth2AuthorizationCodeGrantRequest, HttpHeaders> headersConverter = (request) -> getHttpHeaders();

private final Converter<OAuth2AuthorizationCodeGrantRequest, MultiValueMap<String, String>> parametersConverter = this::getHttpBody;

/**
* Additional default headers information.
* @return HttpHeaders
*/
public HttpHeaders getHttpHeaders() {
public HttpHeaders getHttpHeaders(OAuth2AuthorizationCodeGrantRequest request) {
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.put("x-client-SKU", Collections.singletonList(getApplicationId()));
httpHeaders.put("x-client-VER", Collections.singletonList(AzureSpringIdentifier.VERSION));
Expand All @@ -57,6 +49,6 @@ public HttpHeaders getHttpHeaders() {
* @return MultiValueMap
*/
public MultiValueMap<String, String> getHttpBody(OAuth2AuthorizationCodeGrantRequest request) {
return null;
return new MultiValueMapAdapter<>(Collections.emptyMap());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

Expand Down Expand Up @@ -48,10 +49,10 @@ public void saveAuthorizedClient(OAuth2AuthorizedClient authorizedClient, Authen
Assert.notNull(authorizedClient, "authorizedClient cannot be null");
Assert.notNull(request, MSG_REQUEST_CANNOT_BE_NULL);
Assert.notNull(response, "response cannot be null");
Map<String, OAuth2AuthorizedClient> authorizedClients = this.getAuthorizedClients(request);
authorizedClients.put(authorizedClient.getClientRegistration().getRegistrationId(), authorizedClient);
Map<String, OAuth2AuthorizedClient> authorizedClientsMap = new HashMap<>(this.getAuthorizedClients(request));
authorizedClientsMap.put(authorizedClient.getClientRegistration().getRegistrationId(), authorizedClient);
request.getSession().setAttribute(AUTHORIZED_CLIENTS_ATTR_NAME,
serializeOAuth2AuthorizedClientMap(authorizedClients));
serializeOAuth2AuthorizedClientMap(Collections.unmodifiableMap(authorizedClientsMap)));

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. This will make the code more robust.

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,23 @@ void testAadRestOperationConfiguration() {

RestTemplate restTemplate = createRestTemplate(builder);
ResponseErrorHandler handler = restTemplate.getErrorHandler();
assertNotEquals(handler.getClass(), OAuth2ErrorResponseErrorHandler.class);
assertNotEquals(OAuth2ErrorResponseErrorHandler.class, handler.getClass());
List<HttpMessageConverter<?>> converters = restTemplate.getMessageConverters();
assertFalse(hasItemOfClass(converters, FormHttpMessageConverter.class));
assertFalse(hasItemOfClass(converters, OAuth2AccessTokenResponseHttpMessageConverter.class));

restTemplate = createOAuth2ErrorResponseHandledRestTemplate(builder);
handler = restTemplate.getErrorHandler();
assertEquals(handler.getClass(), OAuth2ErrorResponseErrorHandler.class);
assertEquals(OAuth2ErrorResponseErrorHandler.class, handler.getClass());
converters = restTemplate.getMessageConverters();
assertFalse(hasItemOfClass(converters, FormHttpMessageConverter.class));
assertFalse(hasItemOfClass(converters, OAuth2AccessTokenResponseHttpMessageConverter.class));

restTemplate = createOAuth2AccessTokenResponseClientRestTemplate(builder);
handler = restTemplate.getErrorHandler();
assertEquals(handler.getClass(), OAuth2ErrorResponseErrorHandler.class);
assertEquals(OAuth2ErrorResponseErrorHandler.class, handler.getClass());
converters = restTemplate.getMessageConverters();
assertEquals(2, converters.size());
assertTrue(hasItemOfClass(converters, FormHttpMessageConverter.class));
assertTrue(hasItemOfClass(converters, OAuth2AccessTokenResponseHttpMessageConverter.class));
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ private HttpHeaders convertedHeaderOf(AadClientRegistrationRepository repository

private Object[] expectedHeaders(AadClientRegistrationRepository repository) {
return new AadOAuth2AuthorizationCodeGrantRequestEntityConverter(repository.getAzureClientAccessTokenScopes())
.getHttpHeaders()
.getHttpHeaders(null)
.entrySet()
.stream()
.filter(entry -> !entry.getKey().equals("client-request-id"))
Expand Down