|
45 | 45 | import org.springframework.security.oauth2.jwt.Jwt;
|
46 | 46 | import org.springframework.security.oauth2.jwt.JwtClaimsSet;
|
47 | 47 | import org.springframework.security.oauth2.jwt.JwtEncoder;
|
| 48 | +import org.springframework.security.oauth2.server.authorization.JwtEncodingContext; |
48 | 49 | import org.springframework.security.oauth2.server.authorization.OAuth2Authorization;
|
| 50 | +import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationCode; |
49 | 51 | import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationService;
|
| 52 | +import org.springframework.security.oauth2.server.authorization.OAuth2TokenCustomizer; |
50 | 53 | import org.springframework.security.oauth2.server.authorization.TestOAuth2Authorizations;
|
51 | 54 | import org.springframework.security.oauth2.server.authorization.client.RegisteredClient;
|
52 | 55 | import org.springframework.security.oauth2.server.authorization.client.TestRegisteredClients;
|
53 |
| -import org.springframework.security.oauth2.server.authorization.JwtEncodingContext; |
54 |
| -import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationCode; |
55 |
| -import org.springframework.security.oauth2.server.authorization.OAuth2TokenCustomizer; |
56 | 56 |
|
57 | 57 | import static org.assertj.core.api.Assertions.assertThat;
|
58 | 58 | import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
@@ -222,6 +222,31 @@ public void authenticateWhenInvalidatedCodeThenThrowOAuth2AuthenticationExceptio
|
222 | 222 | .isEqualTo(OAuth2ErrorCodes.INVALID_GRANT);
|
223 | 223 | }
|
224 | 224 |
|
| 225 | + // gh-290 |
| 226 | + @Test |
| 227 | + public void authenticateWhenExpiredCodeThenThrowOAuth2AuthenticationException() { |
| 228 | + RegisteredClient registeredClient = TestRegisteredClients.registeredClient().build(); |
| 229 | + OAuth2AuthorizationCode authorizationCode = new OAuth2AuthorizationCode( |
| 230 | + AUTHORIZATION_CODE, Instant.now().minusSeconds(300), Instant.now().minusSeconds(60)); |
| 231 | + OAuth2Authorization authorization = TestOAuth2Authorizations.authorization(registeredClient) |
| 232 | + .token(authorizationCode) |
| 233 | + .build(); |
| 234 | + when(this.authorizationService.findByToken(eq(AUTHORIZATION_CODE), eq(AUTHORIZATION_CODE_TOKEN_TYPE))) |
| 235 | + .thenReturn(authorization); |
| 236 | + |
| 237 | + OAuth2ClientAuthenticationToken clientPrincipal = new OAuth2ClientAuthenticationToken(registeredClient); |
| 238 | + OAuth2AuthorizationRequest authorizationRequest = authorization.getAttribute( |
| 239 | + OAuth2AuthorizationRequest.class.getName()); |
| 240 | + OAuth2AuthorizationCodeAuthenticationToken authentication = |
| 241 | + new OAuth2AuthorizationCodeAuthenticationToken(AUTHORIZATION_CODE, clientPrincipal, authorizationRequest.getRedirectUri(), null); |
| 242 | + |
| 243 | + assertThatThrownBy(() -> this.authenticationProvider.authenticate(authentication)) |
| 244 | + .isInstanceOf(OAuth2AuthenticationException.class) |
| 245 | + .extracting(ex -> ((OAuth2AuthenticationException) ex).getError()) |
| 246 | + .extracting("errorCode") |
| 247 | + .isEqualTo(OAuth2ErrorCodes.INVALID_GRANT); |
| 248 | + } |
| 249 | + |
225 | 250 | @Test
|
226 | 251 | public void authenticateWhenValidCodeThenReturnAccessToken() {
|
227 | 252 | RegisteredClient registeredClient = TestRegisteredClients.registeredClient().build();
|
|
0 commit comments