|
1 | 1 | /*
|
2 |
| - * Copyright 2020-2023 the original author or authors. |
| 2 | + * Copyright 2020-2025 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
20 | 20 | import java.time.temporal.ChronoUnit;
|
21 | 21 | import java.util.Collections;
|
22 | 22 | import java.util.Map;
|
| 23 | +import java.util.function.Consumer; |
23 | 24 | import java.util.function.Function;
|
24 | 25 |
|
25 | 26 | import org.junit.jupiter.api.BeforeEach;
|
|
55 | 56 | import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
56 | 57 | import static org.mockito.ArgumentMatchers.any;
|
57 | 58 | import static org.mockito.ArgumentMatchers.anyString;
|
| 59 | +import static org.mockito.ArgumentMatchers.eq; |
58 | 60 | import static org.mockito.BDDMockito.given;
|
59 | 61 | import static org.mockito.Mockito.mock;
|
60 | 62 | import static org.mockito.Mockito.verify;
|
@@ -145,10 +147,81 @@ public void authenticateWhenAuthorizationNotFoundThenThrowOAuth2AuthenticationEx
|
145 | 147 | verifyNoInteractions(this.registeredClientRepository, this.authorizationConsentService);
|
146 | 148 | }
|
147 | 149 |
|
| 150 | + @Test |
| 151 | + public void authenticateWhenUserCodeIsInvalidatedThenThrowOAuth2AuthenticationException() { |
| 152 | + RegisteredClient registeredClient = TestRegisteredClients.registeredClient().build(); |
| 153 | + // @formatter:off |
| 154 | + OAuth2Authorization authorization = TestOAuth2Authorizations |
| 155 | + .authorization(registeredClient) |
| 156 | + .token(createDeviceCode()) |
| 157 | + .token(createUserCode(), withInvalidated()) |
| 158 | + .attribute(OAuth2ParameterNames.SCOPE, registeredClient.getScopes()) |
| 159 | + .build(); |
| 160 | + // @formatter:on |
| 161 | + given(this.authorizationService.findByToken(eq(USER_CODE), |
| 162 | + eq(OAuth2DeviceVerificationAuthenticationProvider.USER_CODE_TOKEN_TYPE))) |
| 163 | + .willReturn(authorization); |
| 164 | + Authentication authentication = createAuthentication(); |
| 165 | + // @formatter:off |
| 166 | + assertThatExceptionOfType(OAuth2AuthenticationException.class) |
| 167 | + .isThrownBy(() -> this.authenticationProvider.authenticate(authentication)) |
| 168 | + .extracting(OAuth2AuthenticationException::getError) |
| 169 | + .extracting(OAuth2Error::getErrorCode) |
| 170 | + .isEqualTo(OAuth2ErrorCodes.INVALID_GRANT); |
| 171 | + // @formatter:on |
| 172 | + |
| 173 | + verify(this.authorizationService).findByToken(USER_CODE, |
| 174 | + OAuth2DeviceVerificationAuthenticationProvider.USER_CODE_TOKEN_TYPE); |
| 175 | + verifyNoMoreInteractions(this.authorizationService); |
| 176 | + verifyNoInteractions(this.registeredClientRepository, this.authorizationConsentService); |
| 177 | + } |
| 178 | + |
| 179 | + @Test |
| 180 | + public void authenticateWhenUserCodeIsExpiredAndNotInvalidatedThenThrowOAuth2AuthenticationException() { |
| 181 | + RegisteredClient registeredClient = TestRegisteredClients.registeredClient().build(); |
| 182 | + // @formatter:off |
| 183 | + OAuth2Authorization authorization = TestOAuth2Authorizations |
| 184 | + .authorization(registeredClient) |
| 185 | + // Device code would also be expired but not relevant for this test |
| 186 | + .token(createDeviceCode()) |
| 187 | + .token(createExpiredUserCode()) |
| 188 | + .attribute(OAuth2ParameterNames.SCOPE, registeredClient.getScopes()) |
| 189 | + .build(); |
| 190 | + // @formatter:on |
| 191 | + given(this.authorizationService.findByToken(eq(USER_CODE), |
| 192 | + eq(OAuth2DeviceVerificationAuthenticationProvider.USER_CODE_TOKEN_TYPE))) |
| 193 | + .willReturn(authorization); |
| 194 | + Authentication authentication = createAuthentication(); |
| 195 | + // @formatter:off |
| 196 | + assertThatExceptionOfType(OAuth2AuthenticationException.class) |
| 197 | + .isThrownBy(() -> this.authenticationProvider.authenticate(authentication)) |
| 198 | + .extracting(OAuth2AuthenticationException::getError) |
| 199 | + .extracting(OAuth2Error::getErrorCode) |
| 200 | + .isEqualTo(OAuth2ErrorCodes.INVALID_GRANT); |
| 201 | + // @formatter:on |
| 202 | + |
| 203 | + ArgumentCaptor<OAuth2Authorization> authorizationCaptor = ArgumentCaptor.forClass(OAuth2Authorization.class); |
| 204 | + verify(this.authorizationService).findByToken(USER_CODE, |
| 205 | + OAuth2DeviceVerificationAuthenticationProvider.USER_CODE_TOKEN_TYPE); |
| 206 | + verify(this.authorizationService).save(authorizationCaptor.capture()); |
| 207 | + verifyNoMoreInteractions(this.authorizationService); |
| 208 | + verifyNoInteractions(this.registeredClientRepository, this.authorizationConsentService); |
| 209 | + |
| 210 | + OAuth2Authorization updatedAuthorization = authorizationCaptor.getValue(); |
| 211 | + assertThat(updatedAuthorization.getToken(OAuth2UserCode.class)).extracting(isInvalidated()).isEqualTo(true); |
| 212 | + } |
| 213 | + |
148 | 214 | @Test
|
149 | 215 | public void authenticateWhenPrincipalNotAuthenticatedThenReturnUnauthenticated() {
|
150 | 216 | RegisteredClient registeredClient = TestRegisteredClients.registeredClient().build();
|
151 |
| - OAuth2Authorization authorization = TestOAuth2Authorizations.authorization(registeredClient).build(); |
| 217 | + // @formatter:off |
| 218 | + OAuth2Authorization authorization = TestOAuth2Authorizations |
| 219 | + .authorization(registeredClient) |
| 220 | + .token(createDeviceCode()) |
| 221 | + .token(createUserCode()) |
| 222 | + .attribute(OAuth2ParameterNames.SCOPE, registeredClient.getScopes()) |
| 223 | + .build(); |
| 224 | + // @formatter:on |
152 | 225 | TestingAuthenticationToken principal = new TestingAuthenticationToken("user", null);
|
153 | 226 | Authentication authentication = new OAuth2DeviceVerificationAuthenticationToken(principal, USER_CODE,
|
154 | 227 | Collections.emptyMap());
|
@@ -331,6 +404,15 @@ private static OAuth2UserCode createUserCode() {
|
331 | 404 | return new OAuth2UserCode(USER_CODE, issuedAt, issuedAt.plus(30, ChronoUnit.MINUTES));
|
332 | 405 | }
|
333 | 406 |
|
| 407 | + private static OAuth2UserCode createExpiredUserCode() { |
| 408 | + Instant issuedAt = Instant.now().minus(45, ChronoUnit.MINUTES); |
| 409 | + return new OAuth2UserCode(USER_CODE, issuedAt, issuedAt.plus(30, ChronoUnit.MINUTES)); |
| 410 | + } |
| 411 | + |
| 412 | + private static Consumer<Map<String, Object>> withInvalidated() { |
| 413 | + return (metadata) -> metadata.put(OAuth2Authorization.Token.INVALIDATED_METADATA_NAME, true); |
| 414 | + } |
| 415 | + |
334 | 416 | private static Function<OAuth2Authorization.Token<? extends OAuth2Token>, Boolean> isInvalidated() {
|
335 | 417 | return (token) -> token.getMetadata(OAuth2Authorization.Token.INVALIDATED_METADATA_NAME);
|
336 | 418 | }
|
|
0 commit comments