Skip to content

Commit 6e1ed9f

Browse files
pavelefrosjgrandja
authored andcommitted
Fix NPE on access token in OAuth2AuthorizationCodeAuthenticationProvider
Closes gh-1233
1 parent 9c1ec34 commit 6e1ed9f

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import org.springframework.security.core.AuthenticationException;
3737
import org.springframework.security.core.session.SessionInformation;
3838
import org.springframework.security.core.session.SessionRegistry;
39+
import org.springframework.security.oauth2.core.AbstractOAuth2Token;
3940
import org.springframework.security.oauth2.core.AuthorizationGrantType;
4041
import org.springframework.security.oauth2.core.ClaimAccessor;
4142
import org.springframework.security.oauth2.core.ClientAuthenticationMethod;
@@ -154,12 +155,12 @@ public Authentication authenticate(Authentication authentication) throws Authent
154155

155156
if (!authorizationCode.isActive()) {
156157
if (authorizationCode.isInvalidated()) {
157-
OAuth2Token token = authorization.getRefreshToken() != null ?
158-
authorization.getRefreshToken().getToken() :
159-
authorization.getAccessToken().getToken();
158+
OAuth2Authorization.Token<? extends AbstractOAuth2Token> token = authorization.getRefreshToken() != null ?
159+
authorization.getRefreshToken() :
160+
authorization.getAccessToken();
160161
if (token != null) {
161162
// Invalidate the access (and refresh) token as the client is attempting to use the authorization code more than once
162-
authorization = OAuth2AuthenticationProviderUtils.invalidate(authorization, token);
163+
authorization = OAuth2AuthenticationProviderUtils.invalidate(authorization, token.getToken());
163164
this.authorizationService.save(authorization);
164165
if (this.logger.isWarnEnabled()) {
165166
this.logger.warn(LogMessage.format("Invalidated authorization token(s) previously issued to registered client '%s'", registeredClient.getId()));

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,33 @@ public void authenticateWhenInvalidatedCodeThenThrowOAuth2AuthenticationExceptio
283283
assertThat(updatedAuthorization.getRefreshToken().isInvalidated()).isTrue();
284284
}
285285

286+
// gh PR 1233
287+
@Test
288+
public void authenticateWhenInvalidatedCodeAndNullRefreshAndAccessTokensThenThrowOAuth2AuthenticationException() {
289+
RegisteredClient registeredClient = TestRegisteredClients.registeredClient().build();
290+
OAuth2AuthorizationCode authorizationCode = new OAuth2AuthorizationCode(
291+
AUTHORIZATION_CODE, Instant.now(), Instant.now().plusSeconds(120));
292+
OAuth2Authorization authorization = TestOAuth2Authorizations.authorization(registeredClient, authorizationCode)
293+
.token(authorizationCode, (metadata) -> metadata.put(OAuth2Authorization.Token.INVALIDATED_METADATA_NAME, true))
294+
.build();
295+
296+
when(this.authorizationService.findByToken(eq(AUTHORIZATION_CODE), eq(AUTHORIZATION_CODE_TOKEN_TYPE)))
297+
.thenReturn(authorization);
298+
299+
OAuth2ClientAuthenticationToken clientPrincipal = new OAuth2ClientAuthenticationToken(
300+
registeredClient, ClientAuthenticationMethod.CLIENT_SECRET_BASIC, registeredClient.getClientSecret());
301+
OAuth2AuthorizationRequest authorizationRequest = authorization.getAttribute(
302+
OAuth2AuthorizationRequest.class.getName());
303+
OAuth2AuthorizationCodeAuthenticationToken authentication =
304+
new OAuth2AuthorizationCodeAuthenticationToken(AUTHORIZATION_CODE, clientPrincipal, authorizationRequest.getRedirectUri(), null);
305+
306+
assertThatThrownBy(() -> this.authenticationProvider.authenticate(authentication))
307+
.isInstanceOf(OAuth2AuthenticationException.class)
308+
.extracting(ex -> ((OAuth2AuthenticationException) ex).getError())
309+
.extracting("errorCode")
310+
.isEqualTo(OAuth2ErrorCodes.INVALID_GRANT);
311+
}
312+
286313
// gh-290
287314
@Test
288315
public void authenticateWhenExpiredCodeThenThrowOAuth2AuthenticationException() {

0 commit comments

Comments
 (0)