|
1 | 1 | /* |
2 | | - * Copyright 2020-2023 the original author or authors. |
| 2 | + * Copyright 2020-2025 the original author or authors. |
3 | 3 | * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | * you may not use this file except in compliance with the License. |
@@ -134,9 +134,30 @@ public Authentication authenticate(Authentication authentication) throws Authent |
134 | 134 | throw new OAuth2AuthenticationException(OAuth2ErrorCodes.INVALID_GRANT); |
135 | 135 | } |
136 | 136 |
|
| 137 | + if (deviceCode.isInvalidated() && !userCode.isInvalidated()) { |
| 138 | + throw new OAuth2AuthenticationException(OAuth2ErrorCodes.INVALID_GRANT); |
| 139 | + } |
| 140 | + |
137 | 141 | // In https://www.rfc-editor.org/rfc/rfc8628.html#section-3.5, |
138 | 142 | // the following error codes are defined: |
139 | 143 |
|
| 144 | + // expired_token |
| 145 | + // The "device_code" has expired, and the device authorization |
| 146 | + // session has concluded. The client MAY commence a new device |
| 147 | + // authorization request but SHOULD wait for user interaction before |
| 148 | + // restarting to avoid unnecessary polling. |
| 149 | + 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())); |
| 156 | + } |
| 157 | + OAuth2Error error = new OAuth2Error(EXPIRED_TOKEN, null, DEVICE_ERROR_URI); |
| 158 | + throw new OAuth2AuthenticationException(error); |
| 159 | + } |
| 160 | + |
140 | 161 | // authorization_pending |
141 | 162 | // The authorization request is still pending as the end user hasn't |
142 | 163 | // yet completed the user-interaction steps (Section 3.3). The |
@@ -165,23 +186,6 @@ public Authentication authenticate(Authentication authentication) throws Authent |
165 | 186 | throw new OAuth2AuthenticationException(error); |
166 | 187 | } |
167 | 188 |
|
168 | | - // expired_token |
169 | | - // The "device_code" has expired, and the device authorization |
170 | | - // session has concluded. The client MAY commence a new device |
171 | | - // authorization request but SHOULD wait for user interaction before |
172 | | - // restarting to avoid unnecessary polling. |
173 | | - if (deviceCode.isExpired()) { |
174 | | - // Invalidate the device code |
175 | | - authorization = OAuth2AuthenticationProviderUtils.invalidate(authorization, deviceCode.getToken()); |
176 | | - this.authorizationService.save(authorization); |
177 | | - if (this.logger.isWarnEnabled()) { |
178 | | - this.logger.warn(LogMessage.format("Invalidated device code used by registered client '%s'", |
179 | | - authorization.getRegisteredClientId())); |
180 | | - } |
181 | | - OAuth2Error error = new OAuth2Error(EXPIRED_TOKEN, null, DEVICE_ERROR_URI); |
182 | | - throw new OAuth2AuthenticationException(error); |
183 | | - } |
184 | | - |
185 | 189 | if (this.logger.isTraceEnabled()) { |
186 | 190 | this.logger.trace("Validated device token request parameters"); |
187 | 191 | } |
|
0 commit comments