Polish OAuth2AccessTokenResponse converters

Since these converters no longer have a direct reference to the HTTP
stack, it would be better to move them into another package. Also, now
that the converters are public, we should follow the prevailing
converter naming convention, which is to call it STConverter for an
implementation of Converter<S, T>.
This commit is contained in:
Josh Cummings 2020-01-29 12:03:00 -07:00
parent 704f98688d
commit 7550907e03
No known key found for this signature in database
GPG Key ID: 49EF60DD7FF83443
5 changed files with 45 additions and 44 deletions

View File

@ -13,16 +13,19 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.security.oauth2.core.http.converter;
package org.springframework.security.oauth2.core.endpoint;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import org.springframework.core.convert.converter.Converter;
import org.springframework.security.oauth2.core.OAuth2AccessToken;
import org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse;
import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames;
import org.springframework.util.StringUtils;
import java.util.*;
/**
* A {@link Converter} that converts the provided
* OAuth 2.0 Access Token Response parameters to an {@link OAuth2AccessTokenResponse}.
@ -31,7 +34,7 @@ import java.util.*;
* @author Nikita Konev
* @since 5.3
*/
public final class OAuth2AccessTokenResponseConverter implements Converter<Map<String, String>, OAuth2AccessTokenResponse> {
public final class MapOAuth2AccessTokenResponseConverter implements Converter<Map<String, String>, OAuth2AccessTokenResponse> {
private static final Set<String> TOKEN_RESPONSE_PARAMETER_NAMES = new HashSet<>(Arrays.asList(
OAuth2ParameterNames.ACCESS_TOKEN,
OAuth2ParameterNames.EXPIRES_IN,

View File

@ -13,19 +13,17 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.security.oauth2.core.http.converter;
import org.springframework.core.convert.converter.Converter;
import org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse;
import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
package org.springframework.security.oauth2.core.endpoint;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.HashMap;
import java.util.Map;
import org.springframework.core.convert.converter.Converter;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
/**
* A {@link Converter} that converts the provided {@link OAuth2AccessTokenResponse}
* to a {@code Map} representation of the OAuth 2.0 Access Token Response parameters.
@ -34,7 +32,7 @@ import java.util.Map;
* @author Nikita Konev
* @since 5.3
*/
public final class OAuth2AccessTokenResponseParametersConverter implements Converter<OAuth2AccessTokenResponse, Map<String, String>> {
public final class OAuth2AccessTokenResponseMapConverter implements Converter<OAuth2AccessTokenResponse, Map<String, String>> {
@Override
public Map<String, String> convert(OAuth2AccessTokenResponse tokenResponse) {

View File

@ -15,6 +15,10 @@
*/
package org.springframework.security.oauth2.core.http.converter;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.core.convert.converter.Converter;
import org.springframework.http.HttpInputMessage;
@ -25,13 +29,11 @@ import org.springframework.http.converter.GenericHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;
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;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Map;
/**
* A {@link HttpMessageConverter} for an {@link OAuth2AccessTokenResponse OAuth 2.0 Access Token Response}.
*
@ -49,10 +51,10 @@ public class OAuth2AccessTokenResponseHttpMessageConverter extends AbstractHttpM
private GenericHttpMessageConverter<Object> jsonMessageConverter = HttpMessageConverters.getJsonMessageConverter();
protected Converter<Map<String, String>, OAuth2AccessTokenResponse> tokenResponseConverter =
new OAuth2AccessTokenResponseConverter();
new MapOAuth2AccessTokenResponseConverter();
protected Converter<OAuth2AccessTokenResponse, Map<String, String>> tokenResponseParametersConverter =
new OAuth2AccessTokenResponseParametersConverter();
new OAuth2AccessTokenResponseMapConverter();
public OAuth2AccessTokenResponseHttpMessageConverter() {
super(DEFAULT_CHARSET, MediaType.APPLICATION_JSON, new MediaType("application", "*+json"));

View File

@ -13,32 +13,32 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.security.oauth2.core.http.converter;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.springframework.security.oauth2.core.OAuth2AccessToken;
import org.springframework.security.oauth2.core.OAuth2RefreshToken;
import org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse;
package org.springframework.security.oauth2.core.endpoint;
import java.time.Duration;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.springframework.security.oauth2.core.OAuth2AccessToken;
import org.springframework.security.oauth2.core.OAuth2RefreshToken;
/**
* Tests for {@link OAuth2AccessTokenResponseConverter}.
* Tests for {@link MapOAuth2AccessTokenResponseConverter}.
*
* @author Nikita Konev
*/
public class OAuth2AccessTokenResponseConverterTest {
public class MapOAuth2AccessTokenResponseConverterTest {
private OAuth2AccessTokenResponseConverter messageConverter;
private MapOAuth2AccessTokenResponseConverter messageConverter;
@Before
public void setup() {
this.messageConverter = new OAuth2AccessTokenResponseConverter();
this.messageConverter = new MapOAuth2AccessTokenResponseConverter();
}

View File

@ -13,33 +13,31 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.security.oauth2.core.http.converter;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.springframework.security.oauth2.core.OAuth2AccessToken;
import org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse;
package org.springframework.security.oauth2.core.endpoint;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import static org.junit.Assert.*;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.springframework.security.oauth2.core.OAuth2AccessToken;
/**
* Tests for {@link OAuth2AccessTokenResponseParametersConverter}.
* Tests for {@link OAuth2AccessTokenResponseMapConverter}.
*
* @author Nikita Konev
*/
public class OAuth2AccessTokenResponseParametersConverterTest {
public class OAuth2AccessTokenResponseMapConverterTest {
private OAuth2AccessTokenResponseParametersConverter messageConverter;
private OAuth2AccessTokenResponseMapConverter messageConverter;
@Before
public void setup() {
this.messageConverter = new OAuth2AccessTokenResponseParametersConverter();
this.messageConverter = new OAuth2AccessTokenResponseMapConverter();
}