From 8c12c3dad02171e1fe50a48d3e8751db3f97bc21 Mon Sep 17 00:00:00 2001 From: Joe Grandja Date: Thu, 14 Jul 2022 16:48:47 -0400 Subject: [PATCH] Remove deprecated converters in OAuth2AccessTokenResponseHttpMessageConverter Closes gh-11513 --- ...MapOAuth2AccessTokenResponseConverter.java | 44 ------------ ...OAuth2AccessTokenResponseMapConverter.java | 47 ------------- ...cessTokenResponseHttpMessageConverter.java | 68 +------------------ ...okenResponseHttpMessageConverterTests.java | 15 ++-- 4 files changed, 11 insertions(+), 163 deletions(-) delete mode 100644 oauth2/oauth2-core/src/main/java/org/springframework/security/oauth2/core/endpoint/MapOAuth2AccessTokenResponseConverter.java delete mode 100644 oauth2/oauth2-core/src/main/java/org/springframework/security/oauth2/core/endpoint/OAuth2AccessTokenResponseMapConverter.java diff --git a/oauth2/oauth2-core/src/main/java/org/springframework/security/oauth2/core/endpoint/MapOAuth2AccessTokenResponseConverter.java b/oauth2/oauth2-core/src/main/java/org/springframework/security/oauth2/core/endpoint/MapOAuth2AccessTokenResponseConverter.java deleted file mode 100644 index c271b3721d..0000000000 --- a/oauth2/oauth2-core/src/main/java/org/springframework/security/oauth2/core/endpoint/MapOAuth2AccessTokenResponseConverter.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright 2002-2021 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.springframework.security.oauth2.core.endpoint; - -import java.util.HashMap; -import java.util.Map; - -import org.springframework.core.convert.converter.Converter; - -/** - * A {@link Converter} that converts the provided OAuth 2.0 Access Token Response - * parameters to an {@link OAuth2AccessTokenResponse}. - * - * @author Joe Grandja - * @author Nikita Konev - * @since 5.3 - * @deprecated Use {@link DefaultMapOAuth2AccessTokenResponseConverter} instead - */ -@Deprecated -public final class MapOAuth2AccessTokenResponseConverter - implements Converter, OAuth2AccessTokenResponse> { - - private final Converter, OAuth2AccessTokenResponse> delegate = new DefaultMapOAuth2AccessTokenResponseConverter(); - - @Override - public OAuth2AccessTokenResponse convert(Map tokenResponseParameters) { - return this.delegate.convert(new HashMap<>(tokenResponseParameters)); - } - -} diff --git a/oauth2/oauth2-core/src/main/java/org/springframework/security/oauth2/core/endpoint/OAuth2AccessTokenResponseMapConverter.java b/oauth2/oauth2-core/src/main/java/org/springframework/security/oauth2/core/endpoint/OAuth2AccessTokenResponseMapConverter.java deleted file mode 100644 index e5af12f3e2..0000000000 --- a/oauth2/oauth2-core/src/main/java/org/springframework/security/oauth2/core/endpoint/OAuth2AccessTokenResponseMapConverter.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright 2002-2021 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.springframework.security.oauth2.core.endpoint; - -import java.util.HashMap; -import java.util.Map; - -import org.springframework.core.convert.converter.Converter; - -/** - * A {@link Converter} that converts the provided {@link OAuth2AccessTokenResponse} to a - * {@code Map} representation of the OAuth 2.0 Access Token Response parameters. - * - * @author Joe Grandja - * @author Nikita Konev - * @since 5.3 - * @deprecated Use {@link DefaultOAuth2AccessTokenResponseMapConverter} instead - */ -@Deprecated -public final class OAuth2AccessTokenResponseMapConverter - implements Converter> { - - private final Converter> delegate = new DefaultOAuth2AccessTokenResponseMapConverter(); - - @Override - public Map convert(OAuth2AccessTokenResponse tokenResponse) { - Map stringTokenResponseParameters = new HashMap<>(); - this.delegate.convert(tokenResponse) - .forEach((key, value) -> stringTokenResponseParameters.put(key, String.valueOf(value))); - return stringTokenResponseParameters; - } - -} diff --git a/oauth2/oauth2-core/src/main/java/org/springframework/security/oauth2/core/http/converter/OAuth2AccessTokenResponseHttpMessageConverter.java b/oauth2/oauth2-core/src/main/java/org/springframework/security/oauth2/core/http/converter/OAuth2AccessTokenResponseHttpMessageConverter.java index 075ae6837e..061a45d296 100644 --- a/oauth2/oauth2-core/src/main/java/org/springframework/security/oauth2/core/http/converter/OAuth2AccessTokenResponseHttpMessageConverter.java +++ b/oauth2/oauth2-core/src/main/java/org/springframework/security/oauth2/core/http/converter/OAuth2AccessTokenResponseHttpMessageConverter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2021 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,8 +18,6 @@ package org.springframework.security.oauth2.core.http.converter; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; -import java.util.HashMap; -import java.util.LinkedHashMap; import java.util.Map; import org.springframework.core.ParameterizedTypeReference; @@ -34,9 +32,7 @@ import org.springframework.http.converter.HttpMessageNotReadableException; import org.springframework.http.converter.HttpMessageNotWritableException; import org.springframework.security.oauth2.core.endpoint.DefaultMapOAuth2AccessTokenResponseConverter; import org.springframework.security.oauth2.core.endpoint.DefaultOAuth2AccessTokenResponseMapConverter; -import org.springframework.security.oauth2.core.endpoint.MapOAuth2AccessTokenResponseConverter; import org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse; -import org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponseMapConverter; import org.springframework.util.Assert; /** @@ -58,20 +54,8 @@ public class OAuth2AccessTokenResponseHttpMessageConverter private GenericHttpMessageConverter jsonMessageConverter = HttpMessageConverters.getJsonMessageConverter(); - /** - * @deprecated This field should no longer be used - */ - @Deprecated - protected Converter, OAuth2AccessTokenResponse> tokenResponseConverter = new MapOAuth2AccessTokenResponseConverter(); - private Converter, OAuth2AccessTokenResponse> accessTokenResponseConverter = new DefaultMapOAuth2AccessTokenResponseConverter(); - /** - * @deprecated This field should no longer be used - */ - @Deprecated - protected Converter> tokenResponseParametersConverter = new OAuth2AccessTokenResponseMapConverter(); - private Converter> accessTokenResponseParametersConverter = new DefaultOAuth2AccessTokenResponseMapConverter(); public OAuth2AccessTokenResponseHttpMessageConverter() { @@ -90,15 +74,6 @@ public class OAuth2AccessTokenResponseHttpMessageConverter try { Map tokenResponseParameters = (Map) this.jsonMessageConverter .read(STRING_OBJECT_MAP.getType(), null, inputMessage); - // Only use deprecated converter if it has been set directly - if (this.tokenResponseConverter.getClass() != MapOAuth2AccessTokenResponseConverter.class) { - // gh-6463: Parse parameter values as Object in order to handle potential - // JSON Object and then convert values to String - Map stringTokenResponseParameters = new HashMap<>(); - tokenResponseParameters - .forEach((key, value) -> stringTokenResponseParameters.put(key, String.valueOf(value))); - return this.tokenResponseConverter.convert(stringTokenResponseParameters); - } return this.accessTokenResponseConverter.convert(tokenResponseParameters); } catch (Exception ex) { @@ -112,15 +87,8 @@ public class OAuth2AccessTokenResponseHttpMessageConverter protected void writeInternal(OAuth2AccessTokenResponse tokenResponse, HttpOutputMessage outputMessage) throws HttpMessageNotWritableException { try { - Map tokenResponseParameters; - // Only use deprecated converter if it has been set directly - if (this.tokenResponseParametersConverter.getClass() != OAuth2AccessTokenResponseMapConverter.class) { - tokenResponseParameters = new LinkedHashMap<>( - this.tokenResponseParametersConverter.convert(tokenResponse)); - } - else { - tokenResponseParameters = this.accessTokenResponseParametersConverter.convert(tokenResponse); - } + Map tokenResponseParameters = this.accessTokenResponseParametersConverter + .convert(tokenResponse); this.jsonMessageConverter.write(tokenResponseParameters, STRING_OBJECT_MAP.getType(), MediaType.APPLICATION_JSON, outputMessage); } @@ -130,20 +98,6 @@ public class OAuth2AccessTokenResponseHttpMessageConverter } } - /** - * Sets the {@link Converter} used for converting the OAuth 2.0 Access Token Response - * parameters to an {@link OAuth2AccessTokenResponse}. - * @param tokenResponseConverter the {@link Converter} used for converting to an - * {@link OAuth2AccessTokenResponse} - * @deprecated Use {@link #setAccessTokenResponseConverter(Converter)} instead - */ - @Deprecated - public final void setTokenResponseConverter( - Converter, OAuth2AccessTokenResponse> tokenResponseConverter) { - Assert.notNull(tokenResponseConverter, "tokenResponseConverter cannot be null"); - this.tokenResponseConverter = tokenResponseConverter; - } - /** * Sets the {@link Converter} used for converting the OAuth 2.0 Access Token Response * parameters to an {@link OAuth2AccessTokenResponse}. @@ -157,22 +111,6 @@ public class OAuth2AccessTokenResponseHttpMessageConverter this.accessTokenResponseConverter = accessTokenResponseConverter; } - /** - * Sets the {@link Converter} used for converting the - * {@link OAuth2AccessTokenResponse} to a {@code Map} representation of the OAuth 2.0 - * Access Token Response parameters. - * @param tokenResponseParametersConverter the {@link Converter} used for converting - * to a {@code Map} representation of the Access Token Response parameters - * @deprecated Use {@link #setAccessTokenResponseParametersConverter(Converter)} - * instead - */ - @Deprecated - public final void setTokenResponseParametersConverter( - Converter> tokenResponseParametersConverter) { - Assert.notNull(tokenResponseParametersConverter, "tokenResponseParametersConverter cannot be null"); - this.tokenResponseParametersConverter = tokenResponseParametersConverter; - } - /** * Sets the {@link Converter} used for converting the * {@link OAuth2AccessTokenResponse} to a {@code Map} representation of the OAuth 2.0 diff --git a/oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/http/converter/OAuth2AccessTokenResponseHttpMessageConverterTests.java b/oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/http/converter/OAuth2AccessTokenResponseHttpMessageConverterTests.java index 842ea51181..79e149db4d 100644 --- a/oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/http/converter/OAuth2AccessTokenResponseHttpMessageConverterTests.java +++ b/oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/http/converter/OAuth2AccessTokenResponseHttpMessageConverterTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2021 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -64,14 +64,15 @@ public class OAuth2AccessTokenResponseHttpMessageConverterTests { } @Test - public void setTokenResponseConverterWhenConverterIsNullThenThrowIllegalArgumentException() { - assertThatIllegalArgumentException().isThrownBy(() -> this.messageConverter.setTokenResponseConverter(null)); + public void setAccessTokenResponseConverterWhenConverterIsNullThenThrowIllegalArgumentException() { + assertThatIllegalArgumentException() + .isThrownBy(() -> this.messageConverter.setAccessTokenResponseConverter(null)); } @Test - public void setTokenResponseParametersConverterWhenConverterIsNullThenThrowIllegalArgumentException() { + public void setAccessTokenResponseParametersConverterWhenConverterIsNullThenThrowIllegalArgumentException() { assertThatIllegalArgumentException() - .isThrownBy(() -> this.messageConverter.setTokenResponseParametersConverter(null)); + .isThrownBy(() -> this.messageConverter.setAccessTokenResponseParametersConverter(null)); } @Test @@ -159,7 +160,7 @@ public class OAuth2AccessTokenResponseHttpMessageConverterTests { public void readInternalWhenConversionFailsThenThrowHttpMessageNotReadableException() { Converter tokenResponseConverter = mock(Converter.class); given(tokenResponseConverter.convert(any())).willThrow(RuntimeException.class); - this.messageConverter.setTokenResponseConverter(tokenResponseConverter); + this.messageConverter.setAccessTokenResponseConverter(tokenResponseConverter); String tokenResponse = "{}"; MockClientHttpResponse response = new MockClientHttpResponse(tokenResponse.getBytes(), HttpStatus.OK); assertThatExceptionOfType(HttpMessageNotReadableException.class) @@ -199,7 +200,7 @@ public class OAuth2AccessTokenResponseHttpMessageConverterTests { public void writeInternalWhenConversionFailsThenThrowHttpMessageNotWritableException() { Converter tokenResponseParametersConverter = mock(Converter.class); given(tokenResponseParametersConverter.convert(any())).willThrow(RuntimeException.class); - this.messageConverter.setTokenResponseParametersConverter(tokenResponseParametersConverter); + this.messageConverter.setAccessTokenResponseParametersConverter(tokenResponseParametersConverter); // @formatter:off OAuth2AccessTokenResponse accessTokenResponse = OAuth2AccessTokenResponse .withToken("access-token-1234")