Remove deprecated converters in OAuth2AccessTokenResponseHttpMessageConverter

Closes gh-11513
This commit is contained in:
Joe Grandja 2022-07-14 16:48:47 -04:00
parent 746d27eab1
commit 8c12c3dad0
4 changed files with 11 additions and 163 deletions

View File

@ -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<Map<String, String>, OAuth2AccessTokenResponse> {
private final Converter<Map<String, Object>, OAuth2AccessTokenResponse> delegate = new DefaultMapOAuth2AccessTokenResponseConverter();
@Override
public OAuth2AccessTokenResponse convert(Map<String, String> tokenResponseParameters) {
return this.delegate.convert(new HashMap<>(tokenResponseParameters));
}
}

View File

@ -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<OAuth2AccessTokenResponse, Map<String, String>> {
private final Converter<OAuth2AccessTokenResponse, Map<String, Object>> delegate = new DefaultOAuth2AccessTokenResponseMapConverter();
@Override
public Map<String, String> convert(OAuth2AccessTokenResponse tokenResponse) {
Map<String, String> stringTokenResponseParameters = new HashMap<>();
this.delegate.convert(tokenResponse)
.forEach((key, value) -> stringTokenResponseParameters.put(key, String.valueOf(value)));
return stringTokenResponseParameters;
}
}

View File

@ -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<Object> jsonMessageConverter = HttpMessageConverters.getJsonMessageConverter();
/**
* @deprecated This field should no longer be used
*/
@Deprecated
protected Converter<Map<String, String>, OAuth2AccessTokenResponse> tokenResponseConverter = new MapOAuth2AccessTokenResponseConverter();
private Converter<Map<String, Object>, OAuth2AccessTokenResponse> accessTokenResponseConverter = new DefaultMapOAuth2AccessTokenResponseConverter();
/**
* @deprecated This field should no longer be used
*/
@Deprecated
protected Converter<OAuth2AccessTokenResponse, Map<String, String>> tokenResponseParametersConverter = new OAuth2AccessTokenResponseMapConverter();
private Converter<OAuth2AccessTokenResponse, Map<String, Object>> accessTokenResponseParametersConverter = new DefaultOAuth2AccessTokenResponseMapConverter();
public OAuth2AccessTokenResponseHttpMessageConverter() {
@ -90,15 +74,6 @@ public class OAuth2AccessTokenResponseHttpMessageConverter
try {
Map<String, Object> tokenResponseParameters = (Map<String, Object>) 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<String, String> 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<String, Object> 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<String, Object> 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<Map<String, String>, 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<OAuth2AccessTokenResponse, Map<String, String>> 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

View File

@ -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")