Polish gh-12853

This commit is contained in:
Joe Grandja 2023-03-20 15:33:53 -04:00
parent aaa0dd8836
commit 55224b58e0
6 changed files with 20 additions and 42 deletions

View File

@ -28,7 +28,7 @@ import java.time.Instant;
* @see <a target="_blank" href= "https://tools.ietf.org/html/rfc8628#section-3.2">Section * @see <a target="_blank" href= "https://tools.ietf.org/html/rfc8628#section-3.2">Section
* 3.2 Device Authorization Response</a> * 3.2 Device Authorization Response</a>
*/ */
public final class OAuth2DeviceCode extends AbstractOAuth2Token { public class OAuth2DeviceCode extends AbstractOAuth2Token {
/** /**
* Constructs an {@code OAuth2DeviceCode} using the provided parameters. * Constructs an {@code OAuth2DeviceCode} using the provided parameters.

View File

@ -28,7 +28,7 @@ import java.time.Instant;
* @see <a target="_blank" href= "https://tools.ietf.org/html/rfc8628#section-3.2">Section * @see <a target="_blank" href= "https://tools.ietf.org/html/rfc8628#section-3.2">Section
* 3.2 Device Authorization Response</a> * 3.2 Device Authorization Response</a>
*/ */
public final class OAuth2UserCode extends AbstractOAuth2Token { public class OAuth2UserCode extends AbstractOAuth2Token {
/** /**
* Constructs an {@code OAuth2UserCode} using the provided parameters. * Constructs an {@code OAuth2UserCode} using the provided parameters.

View File

@ -129,16 +129,6 @@ public final class OAuth2DeviceAuthorizationResponse {
return new Builder(deviceCode, userCode); return new Builder(deviceCode, userCode);
} }
/**
* Returns a new {@link Builder}, initialized with the provided response.
* @param deviceAuthorizationResponse the response to initialize the builder with
* @return the {@link Builder}
*/
public static Builder withResponse(OAuth2DeviceAuthorizationResponse deviceAuthorizationResponse) {
Assert.notNull(deviceAuthorizationResponse, "deviceAuthorizationResponse cannot be null");
return new Builder(deviceAuthorizationResponse);
}
/** /**
* A builder for {@link OAuth2DeviceAuthorizationResponse}. * A builder for {@link OAuth2DeviceAuthorizationResponse}.
*/ */
@ -158,17 +148,6 @@ public final class OAuth2DeviceAuthorizationResponse {
private Map<String, Object> additionalParameters; private Map<String, Object> additionalParameters;
private Builder(OAuth2DeviceAuthorizationResponse response) {
OAuth2DeviceCode deviceCode = response.getDeviceCode();
OAuth2UserCode userCode = response.getUserCode();
this.deviceCode = deviceCode.getTokenValue();
this.userCode = userCode.getTokenValue();
this.verificationUri = response.getVerificationUri();
this.verificationUriComplete = response.getVerificationUriComplete();
this.expiresIn = ChronoUnit.SECONDS.between(deviceCode.getIssuedAt(), deviceCode.getExpiresAt());
this.interval = response.getInterval();
}
private Builder(OAuth2DeviceCode deviceCode, OAuth2UserCode userCode) { private Builder(OAuth2DeviceCode deviceCode, OAuth2UserCode userCode) {
this.deviceCode = deviceCode.getTokenValue(); this.deviceCode = deviceCode.getTokenValue();
this.userCode = userCode.getTokenValue(); this.userCode = userCode.getTokenValue();

View File

@ -152,33 +152,32 @@ public final class OAuth2ParameterNames {
public static final String TOKEN_TYPE_HINT = "token_type_hint"; public static final String TOKEN_TYPE_HINT = "token_type_hint";
/** /**
* {@code device_code} - used in Device Authorization Request and Device Authorization * {@code device_code} - used in Device Authorization Response and Device Access Token
* Response. * Request.
* @since 6.1 * @since 6.1
*/ */
public static final String DEVICE_CODE = "device_code"; public static final String DEVICE_CODE = "device_code";
/** /**
* {@code user_code} - used in Device Authorization Request and Device Authorization * {@code user_code} - used in Device Authorization Response.
* Response.
* @since 6.1 * @since 6.1
*/ */
public static final String USER_CODE = "user_code"; public static final String USER_CODE = "user_code";
/** /**
* {@code verification_uri} - Used in Device Authorization Response. * {@code verification_uri} - used in Device Authorization Response.
* @since 6.1 * @since 6.1
*/ */
public static final String VERIFICATION_URI = "verification_uri"; public static final String VERIFICATION_URI = "verification_uri";
/** /**
* {@code verification_uri_complete} - Used in Device Authorization Response. * {@code verification_uri_complete} - used in Device Authorization Response.
* @since 6.1 * @since 6.1
*/ */
public static final String VERIFICATION_URI_COMPLETE = "verification_uri_complete"; public static final String VERIFICATION_URI_COMPLETE = "verification_uri_complete";
/** /**
* {@code interval} - Used in Device Authorization Response. * {@code interval} - used in Device Authorization Response.
* @since 6.1 * @since 6.1
*/ */
public static final String INTERVAL = "interval"; public static final String INTERVAL = "interval";

View File

@ -56,7 +56,7 @@ public class OAuth2DeviceAuthorizationResponseHttpMessageConverter
private static final ParameterizedTypeReference<Map<String, Object>> STRING_OBJECT_MAP = new ParameterizedTypeReference<>() { private static final ParameterizedTypeReference<Map<String, Object>> STRING_OBJECT_MAP = new ParameterizedTypeReference<>() {
}; };
private final GenericHttpMessageConverter<Object> jsonMessageConvereter = HttpMessageConverters private final GenericHttpMessageConverter<Object> jsonMessageConverter = HttpMessageConverters
.getJsonMessageConverter(); .getJsonMessageConverter();
private Converter<Map<String, Object>, OAuth2DeviceAuthorizationResponse> deviceAuthorizationResponseConverter = new DefaultMapOAuth2DeviceAuthorizationResponseConverter(); private Converter<Map<String, Object>, OAuth2DeviceAuthorizationResponse> deviceAuthorizationResponseConverter = new DefaultMapOAuth2DeviceAuthorizationResponseConverter();
@ -74,7 +74,7 @@ public class OAuth2DeviceAuthorizationResponseHttpMessageConverter
HttpInputMessage inputMessage) throws HttpMessageNotReadableException { HttpInputMessage inputMessage) throws HttpMessageNotReadableException {
try { try {
Map<String, Object> deviceAuthorizationResponseParameters = (Map<String, Object>) this.jsonMessageConvereter Map<String, Object> deviceAuthorizationResponseParameters = (Map<String, Object>) this.jsonMessageConverter
.read(STRING_OBJECT_MAP.getType(), null, inputMessage); .read(STRING_OBJECT_MAP.getType(), null, inputMessage);
return this.deviceAuthorizationResponseConverter.convert(deviceAuthorizationResponseParameters); return this.deviceAuthorizationResponseConverter.convert(deviceAuthorizationResponseParameters);
} }
@ -90,9 +90,9 @@ public class OAuth2DeviceAuthorizationResponseHttpMessageConverter
HttpOutputMessage outputMessage) throws HttpMessageNotWritableException { HttpOutputMessage outputMessage) throws HttpMessageNotWritableException {
try { try {
Map<String, Object> deviceauthorizationResponseParameters = this.deviceAuthorizationResponseParametersConverter Map<String, Object> deviceAuthorizationResponseParameters = this.deviceAuthorizationResponseParametersConverter
.convert(deviceAuthorizationResponse); .convert(deviceAuthorizationResponse);
this.jsonMessageConvereter.write(deviceauthorizationResponseParameters, STRING_OBJECT_MAP.getType(), this.jsonMessageConverter.write(deviceAuthorizationResponseParameters, STRING_OBJECT_MAP.getType(),
MediaType.APPLICATION_JSON, outputMessage); MediaType.APPLICATION_JSON, outputMessage);
} }
catch (Exception ex) { catch (Exception ex) {
@ -107,7 +107,7 @@ public class OAuth2DeviceAuthorizationResponseHttpMessageConverter
* @param deviceAuthorizationResponseConverter the {@link Converter} used for * @param deviceAuthorizationResponseConverter the {@link Converter} used for
* converting to an {@link OAuth2DeviceAuthorizationResponse} * converting to an {@link OAuth2DeviceAuthorizationResponse}
*/ */
public void setDeviceAuthorizationResponseConverter( public final void setDeviceAuthorizationResponseConverter(
Converter<Map<String, Object>, OAuth2DeviceAuthorizationResponse> deviceAuthorizationResponseConverter) { Converter<Map<String, Object>, OAuth2DeviceAuthorizationResponse> deviceAuthorizationResponseConverter) {
Assert.notNull(deviceAuthorizationResponseConverter, "deviceAuthorizationResponseConverter cannot be null"); Assert.notNull(deviceAuthorizationResponseConverter, "deviceAuthorizationResponseConverter cannot be null");
this.deviceAuthorizationResponseConverter = deviceAuthorizationResponseConverter; this.deviceAuthorizationResponseConverter = deviceAuthorizationResponseConverter;
@ -121,7 +121,7 @@ public class OAuth2DeviceAuthorizationResponseHttpMessageConverter
* for converting to a {@code Map} representation of the Device Authorization Response * for converting to a {@code Map} representation of the Device Authorization Response
* parameters * parameters
*/ */
public void setDeviceAuthorizationResponseParametersConverter( public final void setDeviceAuthorizationResponseParametersConverter(
Converter<OAuth2DeviceAuthorizationResponse, Map<String, Object>> deviceAuthorizationResponseParametersConverter) { Converter<OAuth2DeviceAuthorizationResponse, Map<String, Object>> deviceAuthorizationResponseParametersConverter) {
Assert.notNull(deviceAuthorizationResponseParametersConverter, Assert.notNull(deviceAuthorizationResponseParametersConverter,
"deviceAuthorizationResponseParametersConverter cannot be null"); "deviceAuthorizationResponseParametersConverter cannot be null");
@ -167,11 +167,10 @@ public class OAuth2DeviceAuthorizationResponseHttpMessageConverter
return (obj != null) ? obj.toString() : null; return (obj != null) ? obj.toString() : null;
} }
private static long getParameterValue(Map<String, Object> tokenResponseParameters, String parameterName, private static long getParameterValue(Map<String, Object> parameters, String parameterName, long defaultValue) {
long defaultValue) {
long parameterValue = defaultValue; long parameterValue = defaultValue;
Object obj = tokenResponseParameters.get(parameterName); Object obj = parameters.get(parameterName);
if (obj != null) { if (obj != null) {
// Final classes Long and Integer do not need to be coerced // Final classes Long and Integer do not need to be coerced
if (obj.getClass() == Long.class) { if (obj.getClass() == Long.class) {
@ -221,8 +220,9 @@ public class OAuth2DeviceAuthorizationResponseHttpMessageConverter
private static long getExpiresIn(OAuth2DeviceAuthorizationResponse deviceAuthorizationResponse) { private static long getExpiresIn(OAuth2DeviceAuthorizationResponse deviceAuthorizationResponse) {
if (deviceAuthorizationResponse.getDeviceCode().getExpiresAt() != null) { if (deviceAuthorizationResponse.getDeviceCode().getExpiresAt() != null) {
return ChronoUnit.SECONDS.between(Instant.now(), Instant issuedAt = (deviceAuthorizationResponse.getDeviceCode().getIssuedAt() != null)
deviceAuthorizationResponse.getDeviceCode().getExpiresAt()); ? deviceAuthorizationResponse.getDeviceCode().getIssuedAt() : Instant.now();
return ChronoUnit.SECONDS.between(issuedAt, deviceAuthorizationResponse.getDeviceCode().getExpiresAt());
} }
return -1; return -1;
} }

View File

@ -176,7 +176,7 @@ public class OAuth2DeviceAuthorizationResponseHttpMessageConverterTests {
assertThat(authorizationResponse).contains("\"verification_uri\":\"https://example.com/device\""); assertThat(authorizationResponse).contains("\"verification_uri\":\"https://example.com/device\"");
assertThat(authorizationResponse) assertThat(authorizationResponse)
.contains("\"verification_uri_complete\":\"https://example.com/device?user_code=WDJB-MJHT\""); .contains("\"verification_uri_complete\":\"https://example.com/device?user_code=WDJB-MJHT\"");
assertThat(authorizationResponse).contains("\"expires_in\":"); assertThat(authorizationResponse).contains("\"expires_in\":1800");
assertThat(authorizationResponse).contains("\"interval\":5"); assertThat(authorizationResponse).contains("\"interval\":5");
assertThat(authorizationResponse).contains("\"custom_parameter_1\":\"custom-value-1\""); assertThat(authorizationResponse).contains("\"custom_parameter_1\":\"custom-value-1\"");
assertThat(authorizationResponse).contains("\"custom_parameter_2\":\"custom-value-2\""); assertThat(authorizationResponse).contains("\"custom_parameter_2\":\"custom-value-2\"");