Skip to content

Commit a5ce97f

Browse files
committed
Merge branch '1.4.x'
2 parents 779d87a + ded6faa commit a5ce97f

File tree

2 files changed

+22
-19
lines changed

2 files changed

+22
-19
lines changed

oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/server/authorization/authentication/OAuth2DeviceCodeAuthenticationProvider.java

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,25 @@ public Authentication authenticate(Authentication authentication) throws Authent
138138
// In https://www.rfc-editor.org/rfc/rfc8628.html#section-3.5,
139139
// the following error codes are defined:
140140

141+
// expired_token
142+
// The "device_code" has expired, and the device authorization
143+
// session has concluded. The client MAY commence a new device
144+
// authorization request but SHOULD wait for user interaction before
145+
// restarting to avoid unnecessary polling.
146+
if (deviceCode.isExpired()) {
147+
if (!deviceCode.isInvalidated()) {
148+
// Invalidate the device code
149+
authorization = OAuth2Authorization.from(authorization).invalidate(deviceCode.getToken()).build();
150+
this.authorizationService.save(authorization);
151+
if (this.logger.isWarnEnabled()) {
152+
this.logger.warn(LogMessage.format("Invalidated device code used by registered client '%s'",
153+
authorization.getRegisteredClientId()));
154+
}
155+
}
156+
OAuth2Error error = new OAuth2Error(EXPIRED_TOKEN, null, DEVICE_ERROR_URI);
157+
throw new OAuth2AuthenticationException(error);
158+
}
159+
141160
// authorization_pending
142161
// The authorization request is still pending as the end user hasn't
143162
// yet completed the user-interaction steps (Section 3.3). The
@@ -166,23 +185,6 @@ public Authentication authenticate(Authentication authentication) throws Authent
166185
throw new OAuth2AuthenticationException(error);
167186
}
168187

169-
// expired_token
170-
// The "device_code" has expired, and the device authorization
171-
// session has concluded. The client MAY commence a new device
172-
// authorization request but SHOULD wait for user interaction before
173-
// restarting to avoid unnecessary polling.
174-
if (deviceCode.isExpired()) {
175-
// Invalidate the device code
176-
authorization = OAuth2Authorization.from(authorization).invalidate(deviceCode.getToken()).build();
177-
this.authorizationService.save(authorization);
178-
if (this.logger.isWarnEnabled()) {
179-
this.logger.warn(LogMessage.format("Invalidated device code used by registered client '%s'",
180-
authorization.getRegisteredClientId()));
181-
}
182-
OAuth2Error error = new OAuth2Error(EXPIRED_TOKEN, null, DEVICE_ERROR_URI);
183-
throw new OAuth2AuthenticationException(error);
184-
}
185-
186188
// Verify the DPoP Proof (if available)
187189
Jwt dPoPProof = DPoPProofVerifier.verifyIfAvailable(deviceCodeAuthentication);
188190

oauth2-authorization-server/src/test/java/org/springframework/security/oauth2/server/authorization/authentication/OAuth2DeviceCodeAuthenticationProviderTests.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ public void authenticateWhenUserCodeIsNotInvalidatedThenThrowOAuth2Authenticatio
209209
RegisteredClient registeredClient = TestRegisteredClients.registeredClient().build();
210210
Authentication authentication = createAuthentication(registeredClient);
211211
OAuth2Authorization authorization = TestOAuth2Authorizations.authorization(registeredClient)
212+
.token(createDeviceCode())
212213
.token(createUserCode())
213214
.build();
214215
given(this.authorizationService.findByToken(anyString(), any(OAuth2TokenType.class))).willReturn(authorization);
@@ -227,7 +228,7 @@ public void authenticateWhenUserCodeIsNotInvalidatedThenThrowOAuth2Authenticatio
227228
}
228229

229230
@Test
230-
public void authenticateWhenDeviceCodeIsInvalidatedThenThrowOAuth2AuthenticationException() {
231+
public void authenticateWhenDeviceCodeAndUserCodeAreInvalidatedThenThrowOAuth2AuthenticationException() {
231232
RegisteredClient registeredClient = TestRegisteredClients.registeredClient().build();
232233
Authentication authentication = createAuthentication(registeredClient);
233234
OAuth2Authorization authorization = TestOAuth2Authorizations.authorization(registeredClient)
@@ -255,7 +256,7 @@ public void authenticateWhenDeviceCodeIsExpiredThenThrowOAuth2AuthenticationExce
255256
Authentication authentication = createAuthentication(registeredClient);
256257
OAuth2Authorization authorization = TestOAuth2Authorizations.authorization(registeredClient)
257258
.token(createExpiredDeviceCode())
258-
.token(createUserCode(), withInvalidated())
259+
.token(createUserCode())
259260
.build();
260261
given(this.authorizationService.findByToken(anyString(), any(OAuth2TokenType.class))).willReturn(authorization);
261262
// @formatter:off

0 commit comments

Comments
 (0)