Skip to content

Commit 55224b5

Browse files
committed
Polish gh-12853
1 parent aaa0dd8 commit 55224b5

File tree

6 files changed

+20
-42
lines changed

6 files changed

+20
-42
lines changed

oauth2/oauth2-core/src/main/java/org/springframework/security/oauth2/core/OAuth2DeviceCode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
* @see <a target="_blank" href= "https://tools.ietf.org/html/rfc8628#section-3.2">Section
2929
* 3.2 Device Authorization Response</a>
3030
*/
31-
public final class OAuth2DeviceCode extends AbstractOAuth2Token {
31+
public class OAuth2DeviceCode extends AbstractOAuth2Token {
3232

3333
/**
3434
* Constructs an {@code OAuth2DeviceCode} using the provided parameters.

oauth2/oauth2-core/src/main/java/org/springframework/security/oauth2/core/OAuth2UserCode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
* @see <a target="_blank" href= "https://tools.ietf.org/html/rfc8628#section-3.2">Section
2929
* 3.2 Device Authorization Response</a>
3030
*/
31-
public final class OAuth2UserCode extends AbstractOAuth2Token {
31+
public class OAuth2UserCode extends AbstractOAuth2Token {
3232

3333
/**
3434
* Constructs an {@code OAuth2UserCode} using the provided parameters.

oauth2/oauth2-core/src/main/java/org/springframework/security/oauth2/core/endpoint/OAuth2DeviceAuthorizationResponse.java

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -129,16 +129,6 @@ public static Builder with(OAuth2DeviceCode deviceCode, OAuth2UserCode userCode)
129129
return new Builder(deviceCode, userCode);
130130
}
131131

132-
/**
133-
* Returns a new {@link Builder}, initialized with the provided response.
134-
* @param deviceAuthorizationResponse the response to initialize the builder with
135-
* @return the {@link Builder}
136-
*/
137-
public static Builder withResponse(OAuth2DeviceAuthorizationResponse deviceAuthorizationResponse) {
138-
Assert.notNull(deviceAuthorizationResponse, "deviceAuthorizationResponse cannot be null");
139-
return new Builder(deviceAuthorizationResponse);
140-
}
141-
142132
/**
143133
* A builder for {@link OAuth2DeviceAuthorizationResponse}.
144134
*/
@@ -158,17 +148,6 @@ public static final class Builder {
158148

159149
private Map<String, Object> additionalParameters;
160150

161-
private Builder(OAuth2DeviceAuthorizationResponse response) {
162-
OAuth2DeviceCode deviceCode = response.getDeviceCode();
163-
OAuth2UserCode userCode = response.getUserCode();
164-
this.deviceCode = deviceCode.getTokenValue();
165-
this.userCode = userCode.getTokenValue();
166-
this.verificationUri = response.getVerificationUri();
167-
this.verificationUriComplete = response.getVerificationUriComplete();
168-
this.expiresIn = ChronoUnit.SECONDS.between(deviceCode.getIssuedAt(), deviceCode.getExpiresAt());
169-
this.interval = response.getInterval();
170-
}
171-
172151
private Builder(OAuth2DeviceCode deviceCode, OAuth2UserCode userCode) {
173152
this.deviceCode = deviceCode.getTokenValue();
174153
this.userCode = userCode.getTokenValue();

oauth2/oauth2-core/src/main/java/org/springframework/security/oauth2/core/endpoint/OAuth2ParameterNames.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -152,33 +152,32 @@ public final class OAuth2ParameterNames {
152152
public static final String TOKEN_TYPE_HINT = "token_type_hint";
153153

154154
/**
155-
* {@code device_code} - used in Device Authorization Request and Device Authorization
156-
* Response.
155+
* {@code device_code} - used in Device Authorization Response and Device Access Token
156+
* Request.
157157
* @since 6.1
158158
*/
159159
public static final String DEVICE_CODE = "device_code";
160160

161161
/**
162-
* {@code user_code} - used in Device Authorization Request and Device Authorization
163-
* Response.
162+
* {@code user_code} - used in Device Authorization Response.
164163
* @since 6.1
165164
*/
166165
public static final String USER_CODE = "user_code";
167166

168167
/**
169-
* {@code verification_uri} - Used in Device Authorization Response.
168+
* {@code verification_uri} - used in Device Authorization Response.
170169
* @since 6.1
171170
*/
172171
public static final String VERIFICATION_URI = "verification_uri";
173172

174173
/**
175-
* {@code verification_uri_complete} - Used in Device Authorization Response.
174+
* {@code verification_uri_complete} - used in Device Authorization Response.
176175
* @since 6.1
177176
*/
178177
public static final String VERIFICATION_URI_COMPLETE = "verification_uri_complete";
179178

180179
/**
181-
* {@code interval} - Used in Device Authorization Response.
180+
* {@code interval} - used in Device Authorization Response.
182181
* @since 6.1
183182
*/
184183
public static final String INTERVAL = "interval";

oauth2/oauth2-core/src/main/java/org/springframework/security/oauth2/core/http/converter/OAuth2DeviceAuthorizationResponseHttpMessageConverter.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public class OAuth2DeviceAuthorizationResponseHttpMessageConverter
5656
private static final ParameterizedTypeReference<Map<String, Object>> STRING_OBJECT_MAP = new ParameterizedTypeReference<>() {
5757
};
5858

59-
private final GenericHttpMessageConverter<Object> jsonMessageConvereter = HttpMessageConverters
59+
private final GenericHttpMessageConverter<Object> jsonMessageConverter = HttpMessageConverters
6060
.getJsonMessageConverter();
6161

6262
private Converter<Map<String, Object>, OAuth2DeviceAuthorizationResponse> deviceAuthorizationResponseConverter = new DefaultMapOAuth2DeviceAuthorizationResponseConverter();
@@ -74,7 +74,7 @@ protected OAuth2DeviceAuthorizationResponse readInternal(Class<? extends OAuth2D
7474
HttpInputMessage inputMessage) throws HttpMessageNotReadableException {
7575

7676
try {
77-
Map<String, Object> deviceAuthorizationResponseParameters = (Map<String, Object>) this.jsonMessageConvereter
77+
Map<String, Object> deviceAuthorizationResponseParameters = (Map<String, Object>) this.jsonMessageConverter
7878
.read(STRING_OBJECT_MAP.getType(), null, inputMessage);
7979
return this.deviceAuthorizationResponseConverter.convert(deviceAuthorizationResponseParameters);
8080
}
@@ -90,9 +90,9 @@ protected void writeInternal(OAuth2DeviceAuthorizationResponse deviceAuthorizati
9090
HttpOutputMessage outputMessage) throws HttpMessageNotWritableException {
9191

9292
try {
93-
Map<String, Object> deviceauthorizationResponseParameters = this.deviceAuthorizationResponseParametersConverter
93+
Map<String, Object> deviceAuthorizationResponseParameters = this.deviceAuthorizationResponseParametersConverter
9494
.convert(deviceAuthorizationResponse);
95-
this.jsonMessageConvereter.write(deviceauthorizationResponseParameters, STRING_OBJECT_MAP.getType(),
95+
this.jsonMessageConverter.write(deviceAuthorizationResponseParameters, STRING_OBJECT_MAP.getType(),
9696
MediaType.APPLICATION_JSON, outputMessage);
9797
}
9898
catch (Exception ex) {
@@ -107,7 +107,7 @@ protected void writeInternal(OAuth2DeviceAuthorizationResponse deviceAuthorizati
107107
* @param deviceAuthorizationResponseConverter the {@link Converter} used for
108108
* converting to an {@link OAuth2DeviceAuthorizationResponse}
109109
*/
110-
public void setDeviceAuthorizationResponseConverter(
110+
public final void setDeviceAuthorizationResponseConverter(
111111
Converter<Map<String, Object>, OAuth2DeviceAuthorizationResponse> deviceAuthorizationResponseConverter) {
112112
Assert.notNull(deviceAuthorizationResponseConverter, "deviceAuthorizationResponseConverter cannot be null");
113113
this.deviceAuthorizationResponseConverter = deviceAuthorizationResponseConverter;
@@ -121,7 +121,7 @@ public void setDeviceAuthorizationResponseConverter(
121121
* for converting to a {@code Map} representation of the Device Authorization Response
122122
* parameters
123123
*/
124-
public void setDeviceAuthorizationResponseParametersConverter(
124+
public final void setDeviceAuthorizationResponseParametersConverter(
125125
Converter<OAuth2DeviceAuthorizationResponse, Map<String, Object>> deviceAuthorizationResponseParametersConverter) {
126126
Assert.notNull(deviceAuthorizationResponseParametersConverter,
127127
"deviceAuthorizationResponseParametersConverter cannot be null");
@@ -167,11 +167,10 @@ private static String getParameterValue(Map<String, Object> parameters, String p
167167
return (obj != null) ? obj.toString() : null;
168168
}
169169

170-
private static long getParameterValue(Map<String, Object> tokenResponseParameters, String parameterName,
171-
long defaultValue) {
170+
private static long getParameterValue(Map<String, Object> parameters, String parameterName, long defaultValue) {
172171
long parameterValue = defaultValue;
173172

174-
Object obj = tokenResponseParameters.get(parameterName);
173+
Object obj = parameters.get(parameterName);
175174
if (obj != null) {
176175
// Final classes Long and Integer do not need to be coerced
177176
if (obj.getClass() == Long.class) {
@@ -221,8 +220,9 @@ public Map<String, Object> convert(OAuth2DeviceAuthorizationResponse deviceAutho
221220

222221
private static long getExpiresIn(OAuth2DeviceAuthorizationResponse deviceAuthorizationResponse) {
223222
if (deviceAuthorizationResponse.getDeviceCode().getExpiresAt() != null) {
224-
return ChronoUnit.SECONDS.between(Instant.now(),
225-
deviceAuthorizationResponse.getDeviceCode().getExpiresAt());
223+
Instant issuedAt = (deviceAuthorizationResponse.getDeviceCode().getIssuedAt() != null)
224+
? deviceAuthorizationResponse.getDeviceCode().getIssuedAt() : Instant.now();
225+
return ChronoUnit.SECONDS.between(issuedAt, deviceAuthorizationResponse.getDeviceCode().getExpiresAt());
226226
}
227227
return -1;
228228
}

oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/http/converter/OAuth2DeviceAuthorizationResponseHttpMessageConverterTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ public void writeInternalWhenOAuth2DeviceAuthorizationResponseThenWriteResponse(
176176
assertThat(authorizationResponse).contains("\"verification_uri\":\"https://example.com/device\"");
177177
assertThat(authorizationResponse)
178178
.contains("\"verification_uri_complete\":\"https://example.com/device?user_code=WDJB-MJHT\"");
179-
assertThat(authorizationResponse).contains("\"expires_in\":");
179+
assertThat(authorizationResponse).contains("\"expires_in\":1800");
180180
assertThat(authorizationResponse).contains("\"interval\":5");
181181
assertThat(authorizationResponse).contains("\"custom_parameter_1\":\"custom-value-1\"");
182182
assertThat(authorizationResponse).contains("\"custom_parameter_2\":\"custom-value-2\"");

0 commit comments

Comments
 (0)