|
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;
|
@@ -145,10 +146,72 @@ public void authenticateWhenAuthorizationNotFoundThenThrowOAuth2AuthenticationEx
|
145 | 146 | verifyNoInteractions(this.registeredClientRepository, this.authorizationConsentService);
|
146 | 147 | }
|
147 | 148 |
|
| 149 | + @Test |
| 150 | + public void authenticateWhenUserCodeIsInvalidedThenThrowOAuth2AuthenticationException() { |
| 151 | + RegisteredClient registeredClient = TestRegisteredClients.registeredClient().build(); |
| 152 | + // @formatter:off |
| 153 | + OAuth2Authorization authorization = TestOAuth2Authorizations |
| 154 | + .authorization(registeredClient) |
| 155 | + .token(createDeviceCode()) |
| 156 | + .token(createUserCode(), withInvalidated()) |
| 157 | + .attribute(OAuth2ParameterNames.SCOPE, registeredClient.getScopes()) |
| 158 | + .build(); |
| 159 | + // @formatter:on |
| 160 | + given(this.authorizationService.findByToken(anyString(), any(OAuth2TokenType.class))).willReturn(authorization); |
| 161 | + Authentication authentication = createAuthentication(); |
| 162 | + // @formatter:off |
| 163 | + assertThatExceptionOfType(OAuth2AuthenticationException.class) |
| 164 | + .isThrownBy(() -> this.authenticationProvider.authenticate(authentication)) |
| 165 | + .extracting(OAuth2AuthenticationException::getError) |
| 166 | + .extracting(OAuth2Error::getErrorCode) |
| 167 | + .isEqualTo(OAuth2ErrorCodes.INVALID_GRANT); |
| 168 | + // @formatter:on |
| 169 | + |
| 170 | + verify(this.authorizationService).findByToken(USER_CODE, |
| 171 | + OAuth2DeviceVerificationAuthenticationProvider.USER_CODE_TOKEN_TYPE); |
| 172 | + verifyNoMoreInteractions(this.authorizationService); |
| 173 | + verifyNoInteractions(this.registeredClientRepository, this.authorizationConsentService); |
| 174 | + } |
| 175 | + |
| 176 | + @Test |
| 177 | + public void authenticateWhenUserCodeIsExpiredThenThrowOAuth2AuthenticationException() { |
| 178 | + RegisteredClient registeredClient = TestRegisteredClients.registeredClient().build(); |
| 179 | + // @formatter:off |
| 180 | + OAuth2Authorization authorization = TestOAuth2Authorizations |
| 181 | + .authorization(registeredClient) |
| 182 | + // Device code would also be expired but not relevant for this test |
| 183 | + .token(createDeviceCode()) |
| 184 | + .token(createExpiredUserCode()) |
| 185 | + .attribute(OAuth2ParameterNames.SCOPE, registeredClient.getScopes()) |
| 186 | + .build(); |
| 187 | + // @formatter:on |
| 188 | + given(this.authorizationService.findByToken(anyString(), any(OAuth2TokenType.class))).willReturn(authorization); |
| 189 | + Authentication authentication = createAuthentication(); |
| 190 | + // @formatter:off |
| 191 | + assertThatExceptionOfType(OAuth2AuthenticationException.class) |
| 192 | + .isThrownBy(() -> this.authenticationProvider.authenticate(authentication)) |
| 193 | + .extracting(OAuth2AuthenticationException::getError) |
| 194 | + .extracting(OAuth2Error::getErrorCode) |
| 195 | + .isEqualTo(OAuth2ErrorCodes.INVALID_GRANT); |
| 196 | + // @formatter:on |
| 197 | + |
| 198 | + verify(this.authorizationService).findByToken(USER_CODE, |
| 199 | + OAuth2DeviceVerificationAuthenticationProvider.USER_CODE_TOKEN_TYPE); |
| 200 | + verifyNoMoreInteractions(this.authorizationService); |
| 201 | + verifyNoInteractions(this.registeredClientRepository, this.authorizationConsentService); |
| 202 | + } |
| 203 | + |
148 | 204 | @Test
|
149 | 205 | public void authenticateWhenPrincipalNotAuthenticatedThenReturnUnauthenticated() {
|
150 | 206 | RegisteredClient registeredClient = TestRegisteredClients.registeredClient().build();
|
151 |
| - OAuth2Authorization authorization = TestOAuth2Authorizations.authorization(registeredClient).build(); |
| 207 | + // @formatter:off |
| 208 | + OAuth2Authorization authorization = TestOAuth2Authorizations |
| 209 | + .authorization(registeredClient) |
| 210 | + .token(createDeviceCode()) |
| 211 | + .token(createUserCode()) |
| 212 | + .attribute(OAuth2ParameterNames.SCOPE, registeredClient.getScopes()) |
| 213 | + .build(); |
| 214 | + // @formatter:on |
152 | 215 | TestingAuthenticationToken principal = new TestingAuthenticationToken("user", null);
|
153 | 216 | Authentication authentication = new OAuth2DeviceVerificationAuthenticationToken(principal, USER_CODE,
|
154 | 217 | Collections.emptyMap());
|
@@ -331,6 +394,15 @@ private static OAuth2UserCode createUserCode() {
|
331 | 394 | return new OAuth2UserCode(USER_CODE, issuedAt, issuedAt.plus(30, ChronoUnit.MINUTES));
|
332 | 395 | }
|
333 | 396 |
|
| 397 | + private static OAuth2UserCode createExpiredUserCode() { |
| 398 | + Instant issuedAt = Instant.now().minus(45, ChronoUnit.MINUTES); |
| 399 | + return new OAuth2UserCode(USER_CODE, issuedAt, issuedAt.plus(30, ChronoUnit.MINUTES)); |
| 400 | + } |
| 401 | + |
| 402 | + private static Consumer<Map<String, Object>> withInvalidated() { |
| 403 | + return (metadata) -> metadata.put(OAuth2Authorization.Token.INVALIDATED_METADATA_NAME, true); |
| 404 | + } |
| 405 | + |
334 | 406 | private static Function<OAuth2Authorization.Token<? extends OAuth2Token>, Boolean> isInvalidated() {
|
335 | 407 | return (token) -> token.getMetadata(OAuth2Authorization.Token.INVALIDATED_METADATA_NAME);
|
336 | 408 | }
|
|
0 commit comments