Skip to content

Commit 64ddcfc

Browse files
committed
Polish gh-1152
1 parent 5131714 commit 64ddcfc

File tree

2 files changed

+15
-30
lines changed

2 files changed

+15
-30
lines changed

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

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
import org.springframework.security.authentication.AuthenticationProvider;
1919
import org.springframework.security.core.Authentication;
20-
import org.springframework.security.oauth2.core.OAuth2AccessToken;
2120
import org.springframework.security.oauth2.core.OAuth2AuthenticationException;
2221
import org.springframework.security.oauth2.core.OAuth2ErrorCodes;
2322
import org.springframework.security.oauth2.core.OAuth2RefreshToken;
@@ -56,25 +55,6 @@ static <T extends OAuth2Token> OAuth2Authorization invalidate(
5655
(metadata) ->
5756
metadata.put(OAuth2Authorization.Token.INVALIDATED_METADATA_NAME, true));
5857

59-
if (OAuth2AuthorizationCode.class.isAssignableFrom(token.getClass())) {
60-
OAuth2Authorization.Token<OAuth2AccessToken> accessToken = authorization.getAccessToken();
61-
if (accessToken != null && !accessToken.isInvalidated()) {
62-
authorizationBuilder.token(
63-
accessToken.getToken(),
64-
(metadata) ->
65-
metadata.put(OAuth2Authorization.Token.INVALIDATED_METADATA_NAME, true));
66-
}
67-
68-
OAuth2Authorization.Token<OAuth2RefreshToken> refreshToken = authorization.getRefreshToken();
69-
if (refreshToken != null && !refreshToken.isInvalidated()) {
70-
authorizationBuilder.token(
71-
refreshToken.getToken(),
72-
(metadata) ->
73-
metadata.put(OAuth2Authorization.Token.INVALIDATED_METADATA_NAME, true));
74-
}
75-
76-
}
77-
7858
if (OAuth2RefreshToken.class.isAssignableFrom(token.getClass())) {
7959
authorizationBuilder.token(
8060
authorization.getAccessToken().getToken(),

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

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,16 @@ public Authentication authenticate(Authentication authentication) throws Authent
150150

151151
if (!authorizationCode.isActive()) {
152152
if (authorizationCode.isInvalidated()) {
153-
authorization = OAuth2AuthenticationProviderUtils.invalidate(authorization, authorizationCode.getToken());
154-
this.authorizationService.save(authorization);
155-
if (this.logger.isWarnEnabled()) {
156-
this.logger.warn(LogMessage.format("Invalidated authorization tokens previously issued based on the authorization code"));
153+
OAuth2Token token = authorization.getRefreshToken() != null ?
154+
authorization.getRefreshToken().getToken() :
155+
authorization.getAccessToken().getToken();
156+
if (token != null) {
157+
// Invalidate the access (and refresh) token as the client is attempting to use the authorization code more than once
158+
authorization = OAuth2AuthenticationProviderUtils.invalidate(authorization, token);
159+
this.authorizationService.save(authorization);
160+
if (this.logger.isWarnEnabled()) {
161+
this.logger.warn(LogMessage.format("Invalidated authorization token(s) previously issued to registered client '%s'", registeredClient.getId()));
162+
}
157163
}
158164
}
159165
throw new OAuth2AuthenticationException(OAuth2ErrorCodes.INVALID_GRANT);
@@ -176,12 +182,7 @@ public Authentication authenticate(Authentication authentication) throws Authent
176182
.authorizationGrant(authorizationCodeAuthentication);
177183
// @formatter:on
178184

179-
// @formatter:off
180-
OAuth2Authorization.Builder authorizationBuilder = OAuth2Authorization.from(authorization)
181-
// Invalidate the authorization code as it can only be used once
182-
.token(authorizationCode.getToken(), metadata ->
183-
metadata.put(OAuth2Authorization.Token.INVALIDATED_METADATA_NAME, true));
184-
// @formatter:on
185+
OAuth2Authorization.Builder authorizationBuilder = OAuth2Authorization.from(authorization);
185186

186187
// ----- Access token -----
187188
OAuth2TokenContext tokenContext = tokenContextBuilder.tokenType(OAuth2TokenType.ACCESS_TOKEN).build();
@@ -262,6 +263,9 @@ public Authentication authenticate(Authentication authentication) throws Authent
262263

263264
authorization = authorizationBuilder.build();
264265

266+
// Invalidate the authorization code as it can only be used once
267+
authorization = OAuth2AuthenticationProviderUtils.invalidate(authorization, authorizationCode.getToken());
268+
265269
this.authorizationService.save(authorization);
266270

267271
if (this.logger.isTraceEnabled()) {
@@ -314,4 +318,5 @@ private SessionInformation getSessionInformation(Authentication principal) {
314318
}
315319
return sessionInformation;
316320
}
321+
317322
}

0 commit comments

Comments
 (0)