|
15 | 15 | */
|
16 | 16 | package org.springframework.security.oauth2.server.authorization.authentication;
|
17 | 17 |
|
| 18 | +import java.time.Instant; |
| 19 | +import java.time.temporal.ChronoUnit; |
18 | 20 | import java.util.HashMap;
|
19 | 21 | import java.util.Map;
|
20 | 22 |
|
@@ -182,6 +184,26 @@ public void authenticateWhenInvalidClientSecretThenThrowOAuth2AuthenticationExce
|
182 | 184 | verify(this.passwordEncoder).matches(any(), any());
|
183 | 185 | }
|
184 | 186 |
|
| 187 | + @Test |
| 188 | + public void authenticateWhenExpiredClientSecretThenThrowOAuth2AuthenticationException() { |
| 189 | + RegisteredClient registeredClient = TestRegisteredClients.registeredClient() |
| 190 | + .clientSecretExpiresAt(Instant.now().minus(1, ChronoUnit.HOURS).truncatedTo(ChronoUnit.SECONDS)) |
| 191 | + .build(); |
| 192 | + when(this.registeredClientRepository.findByClientId(eq(registeredClient.getClientId()))) |
| 193 | + .thenReturn(registeredClient); |
| 194 | + |
| 195 | + OAuth2ClientAuthenticationToken authentication = new OAuth2ClientAuthenticationToken( |
| 196 | + registeredClient.getClientId(), ClientAuthenticationMethod.CLIENT_SECRET_BASIC, registeredClient.getClientSecret(), null); |
| 197 | + assertThatThrownBy(() -> this.authenticationProvider.authenticate(authentication)) |
| 198 | + .isInstanceOf(OAuth2AuthenticationException.class) |
| 199 | + .extracting(ex -> ((OAuth2AuthenticationException) ex).getError()) |
| 200 | + .satisfies(error -> { |
| 201 | + assertThat(error.getErrorCode()).isEqualTo(OAuth2ErrorCodes.INVALID_CLIENT); |
| 202 | + assertThat(error.getDescription()).contains("client_secret_expires_at"); |
| 203 | + }); |
| 204 | + verify(this.passwordEncoder).matches(any(), any()); |
| 205 | + } |
| 206 | + |
185 | 207 | @Test
|
186 | 208 | public void authenticateWhenValidCredentialsThenAuthenticated() {
|
187 | 209 | RegisteredClient registeredClient = TestRegisteredClients.registeredClient().build();
|
|
0 commit comments