Skip to content

Commit 29472a1

Browse files
committed
Polish gh-1889
1 parent 8d4da24 commit 29472a1

File tree

2 files changed

+9
-34
lines changed

2 files changed

+9
-34
lines changed

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

+8-10
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,6 @@ public Authentication authenticate(Authentication authentication) throws Authent
134134
throw new OAuth2AuthenticationException(OAuth2ErrorCodes.INVALID_GRANT);
135135
}
136136

137-
if (deviceCode.isInvalidated() && !userCode.isInvalidated()) {
138-
throw new OAuth2AuthenticationException(OAuth2ErrorCodes.INVALID_GRANT);
139-
}
140-
141137
// In https://www.rfc-editor.org/rfc/rfc8628.html#section-3.5,
142138
// the following error codes are defined:
143139

@@ -147,12 +143,14 @@ public Authentication authenticate(Authentication authentication) throws Authent
147143
// authorization request but SHOULD wait for user interaction before
148144
// restarting to avoid unnecessary polling.
149145
if (deviceCode.isExpired()) {
150-
// Invalidate the device code
151-
authorization = OAuth2AuthenticationProviderUtils.invalidate(authorization, deviceCode.getToken());
152-
this.authorizationService.save(authorization);
153-
if (this.logger.isWarnEnabled()) {
154-
this.logger.warn(LogMessage.format("Invalidated device code used by registered client '%s'",
155-
authorization.getRegisteredClientId()));
146+
if (!deviceCode.isInvalidated()) {
147+
// Invalidate the device code
148+
authorization = OAuth2AuthenticationProviderUtils.invalidate(authorization, deviceCode.getToken());
149+
this.authorizationService.save(authorization);
150+
if (this.logger.isWarnEnabled()) {
151+
this.logger.warn(LogMessage.format("Invalidated device code used by registered client '%s'",
152+
authorization.getRegisteredClientId()));
153+
}
156154
}
157155
OAuth2Error error = new OAuth2Error(EXPIRED_TOKEN, null, DEVICE_ERROR_URI);
158156
throw new OAuth2AuthenticationException(error);

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

+1-24
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2023 the original author or authors.
2+
* Copyright 2020-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -232,29 +232,6 @@ public void authenticateWhenDeviceCodeAndUserCodeAreInvalidatedThenThrowOAuth2Au
232232
verifyNoInteractions(this.tokenGenerator);
233233
}
234234

235-
@Test
236-
public void authenticateWhenDeviceCodeIsInvalidatedThenThrowOAuth2AuthenticationException() {
237-
RegisteredClient registeredClient = TestRegisteredClients.registeredClient().build();
238-
Authentication authentication = createAuthentication(registeredClient);
239-
OAuth2Authorization authorization = TestOAuth2Authorizations.authorization(registeredClient)
240-
.token(createDeviceCode(), withInvalidated())
241-
.token(createUserCode())
242-
.build();
243-
given(this.authorizationService.findByToken(anyString(), any(OAuth2TokenType.class))).willReturn(authorization);
244-
// @formatter:off
245-
assertThatExceptionOfType(OAuth2AuthenticationException.class)
246-
.isThrownBy(() -> this.authenticationProvider.authenticate(authentication))
247-
.extracting(OAuth2AuthenticationException::getError)
248-
.extracting(OAuth2Error::getErrorCode)
249-
.isEqualTo(OAuth2ErrorCodes.INVALID_GRANT);
250-
// @formatter:on
251-
252-
verify(this.authorizationService).findByToken(DEVICE_CODE,
253-
OAuth2DeviceCodeAuthenticationProvider.DEVICE_CODE_TOKEN_TYPE);
254-
verifyNoMoreInteractions(this.authorizationService);
255-
verifyNoInteractions(this.tokenGenerator);
256-
}
257-
258235
@Test
259236
public void authenticateWhenDeviceCodeIsExpiredThenThrowOAuth2AuthenticationException() {
260237
RegisteredClient registeredClient = TestRegisteredClients.registeredClient().build();

0 commit comments

Comments
 (0)