|
15 | 15 | */
|
16 | 16 | package org.springframework.security.oauth2.server.authorization.authentication;
|
17 | 17 |
|
18 |
| -import java.security.Principal; |
19 |
| -import java.time.Instant; |
20 |
| -import java.time.temporal.ChronoUnit; |
21 |
| -import java.util.Collections; |
22 |
| -import java.util.HashSet; |
23 |
| -import java.util.Set; |
24 |
| - |
25 | 18 | import org.junit.Before;
|
26 | 19 | import org.junit.Test;
|
27 | 20 | import org.mockito.ArgumentCaptor;
|
28 |
| - |
29 | 21 | import org.springframework.security.authentication.TestingAuthenticationToken;
|
30 | 22 | import org.springframework.security.core.Authentication;
|
31 |
| -import org.springframework.security.oauth2.core.AuthorizationGrantType; |
32 |
| -import org.springframework.security.oauth2.core.ClientAuthenticationMethod; |
33 |
| -import org.springframework.security.oauth2.core.OAuth2AuthenticationException; |
34 |
| -import org.springframework.security.oauth2.core.OAuth2ErrorCodes; |
35 |
| -import org.springframework.security.oauth2.core.OAuth2RefreshToken; |
36 |
| -import org.springframework.security.oauth2.core.OAuth2RefreshToken2; |
37 |
| -import org.springframework.security.oauth2.core.OAuth2TokenType; |
| 23 | +import org.springframework.security.oauth2.core.*; |
38 | 24 | import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames;
|
39 | 25 | import org.springframework.security.oauth2.jose.jws.SignatureAlgorithm;
|
40 | 26 | import org.springframework.security.oauth2.jwt.JoseHeaderNames;
|
41 | 27 | import org.springframework.security.oauth2.jwt.Jwt;
|
42 | 28 | import org.springframework.security.oauth2.jwt.JwtEncoder;
|
43 |
| -import org.springframework.security.oauth2.server.authorization.OAuth2Authorization; |
44 |
| -import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationService; |
45 |
| -import org.springframework.security.oauth2.server.authorization.TestOAuth2Authorizations; |
| 29 | +import org.springframework.security.oauth2.server.authorization.*; |
46 | 30 | import org.springframework.security.oauth2.server.authorization.client.RegisteredClient;
|
47 | 31 | import org.springframework.security.oauth2.server.authorization.client.TestRegisteredClients;
|
48 |
| -import org.springframework.security.oauth2.server.authorization.JwtEncodingContext; |
49 |
| -import org.springframework.security.oauth2.server.authorization.OAuth2TokenCustomizer; |
| 32 | + |
| 33 | +import java.security.Principal; |
| 34 | +import java.time.Duration; |
| 35 | +import java.time.Instant; |
| 36 | +import java.time.temporal.ChronoUnit; |
| 37 | +import java.util.Collections; |
| 38 | +import java.util.HashSet; |
| 39 | +import java.util.Set; |
50 | 40 |
|
51 | 41 | import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;
|
52 | 42 | import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
|
53 | 43 | import static org.mockito.ArgumentMatchers.any;
|
54 | 44 | import static org.mockito.ArgumentMatchers.eq;
|
55 |
| -import static org.mockito.Mockito.mock; |
56 |
| -import static org.mockito.Mockito.verify; |
57 |
| -import static org.mockito.Mockito.when; |
| 45 | +import static org.mockito.Mockito.*; |
58 | 46 |
|
59 | 47 | /**
|
60 | 48 | * Tests for {@link OAuth2RefreshTokenAuthenticationProvider}.
|
@@ -364,6 +352,73 @@ public void authenticateWhenRevokedRefreshTokenThenThrowOAuth2AuthenticationExce
|
364 | 352 | .isEqualTo(OAuth2ErrorCodes.INVALID_GRANT);
|
365 | 353 | }
|
366 | 354 |
|
| 355 | + @Test |
| 356 | + public void authenticateWhenClientIsPublicThenIssueReducedRefreshToken() { |
| 357 | + Duration refreshTokenTimeToLive = Duration.ofHours(24); |
| 358 | + Duration currentTokenDisuseDuration = Duration.ofHours(1); |
| 359 | + RegisteredClient registeredClient = TestRegisteredClients.registeredPublicClient() |
| 360 | + .authorizationGrantType(AuthorizationGrantType.REFRESH_TOKEN) |
| 361 | + .tokenSettings(tokenSettings -> { |
| 362 | + tokenSettings.reuseRefreshTokens(false); |
| 363 | + tokenSettings.refreshTokenTimeToLive(refreshTokenTimeToLive); |
| 364 | + }) |
| 365 | + .build(); |
| 366 | + OAuth2RefreshToken refreshToken = new OAuth2RefreshToken2("refresh-token", |
| 367 | + Instant.now().minus(currentTokenDisuseDuration), Instant.now().plus(refreshTokenTimeToLive)); |
| 368 | + OAuth2Authorization authorization = TestOAuth2Authorizations.authorization(registeredClient) |
| 369 | + .token(refreshToken) |
| 370 | + .build(); |
| 371 | + when(this.authorizationService.findByToken( |
| 372 | + eq(authorization.getRefreshToken().getToken().getTokenValue()), |
| 373 | + eq(OAuth2TokenType.REFRESH_TOKEN))) |
| 374 | + .thenReturn(authorization); |
| 375 | + |
| 376 | + OAuth2ClientAuthenticationToken clientPrincipal = new OAuth2ClientAuthenticationToken(registeredClient); |
| 377 | + OAuth2RefreshTokenAuthenticationToken authentication = new OAuth2RefreshTokenAuthenticationToken( |
| 378 | + authorization.getRefreshToken().getToken().getTokenValue(), clientPrincipal, null, null); |
| 379 | + |
| 380 | + OAuth2AccessTokenAuthenticationToken authenticationToken = (OAuth2AccessTokenAuthenticationToken) |
| 381 | + this.authenticationProvider.authenticate(authentication); |
| 382 | + |
| 383 | + assertThat(authenticationToken.getRefreshToken()).isNotNull(); |
| 384 | + assertThat(authenticationToken.getRefreshToken().getExpiresAt()) |
| 385 | + .isNotNull() |
| 386 | + .isBeforeOrEqualTo(Instant.now().plus(refreshTokenTimeToLive.minus(currentTokenDisuseDuration))); |
| 387 | + } |
| 388 | + |
| 389 | + @Test |
| 390 | + public void authenticateWhenClientIsPublicAndCurrentTokenHasNotIssuedAtThenGenerateRefreshToken() { |
| 391 | + Duration refreshTokenTimeToLive = Duration.ofHours(24); |
| 392 | + RegisteredClient registeredClient = TestRegisteredClients.registeredPublicClient() |
| 393 | + .authorizationGrantType(AuthorizationGrantType.REFRESH_TOKEN) |
| 394 | + .tokenSettings(tokenSettings -> { |
| 395 | + tokenSettings.reuseRefreshTokens(false); |
| 396 | + tokenSettings.refreshTokenTimeToLive(refreshTokenTimeToLive); |
| 397 | + }) |
| 398 | + .build(); |
| 399 | + OAuth2RefreshToken refreshToken = new OAuth2RefreshToken2("refresh-token", |
| 400 | + null, Instant.now().plus(refreshTokenTimeToLive)); |
| 401 | + OAuth2Authorization authorization = TestOAuth2Authorizations.authorization(registeredClient) |
| 402 | + .token(refreshToken) |
| 403 | + .build(); |
| 404 | + when(this.authorizationService.findByToken( |
| 405 | + eq(authorization.getRefreshToken().getToken().getTokenValue()), |
| 406 | + eq(OAuth2TokenType.REFRESH_TOKEN))) |
| 407 | + .thenReturn(authorization); |
| 408 | + |
| 409 | + OAuth2ClientAuthenticationToken clientPrincipal = new OAuth2ClientAuthenticationToken(registeredClient); |
| 410 | + OAuth2RefreshTokenAuthenticationToken authentication = new OAuth2RefreshTokenAuthenticationToken( |
| 411 | + authorization.getRefreshToken().getToken().getTokenValue(), clientPrincipal, null, null); |
| 412 | + |
| 413 | + OAuth2AccessTokenAuthenticationToken authenticationToken = (OAuth2AccessTokenAuthenticationToken) |
| 414 | + this.authenticationProvider.authenticate(authentication); |
| 415 | + |
| 416 | + assertThat(authenticationToken.getRefreshToken()).isNotNull(); |
| 417 | + assertThat(authenticationToken.getRefreshToken().getExpiresAt()) |
| 418 | + .isNotNull() |
| 419 | + .isBeforeOrEqualTo(Instant.now().plus(refreshTokenTimeToLive)); |
| 420 | + } |
| 421 | + |
367 | 422 | private static Jwt createJwt(Set<String> scope) {
|
368 | 423 | Instant issuedAt = Instant.now();
|
369 | 424 | Instant expiresAt = issuedAt.plus(1, ChronoUnit.HOURS);
|
|
0 commit comments