|
29 | 29 | import org.mockito.ArgumentCaptor;
|
30 | 30 |
|
31 | 31 | import org.springframework.security.authentication.TestingAuthenticationToken;
|
| 32 | +import org.springframework.security.config.Customizer; |
32 | 33 | import org.springframework.security.core.Authentication;
|
33 | 34 | import org.springframework.security.oauth2.core.AuthorizationGrantType;
|
| 35 | +import org.springframework.security.oauth2.core.OAuth2AuthorizationCode; |
34 | 36 | import org.springframework.security.oauth2.core.OAuth2Error;
|
35 | 37 | import org.springframework.security.oauth2.core.OAuth2ErrorCodes;
|
36 | 38 | import org.springframework.security.oauth2.core.OAuth2TokenType;
|
| 39 | +import org.springframework.security.oauth2.core.authentication.OAuth2AuthenticationContext; |
37 | 40 | import org.springframework.security.oauth2.core.authentication.OAuth2AuthenticationValidator;
|
38 | 41 | import org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest;
|
39 | 42 | import org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationResponseType;
|
40 | 43 | import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames;
|
41 | 44 | import org.springframework.security.oauth2.core.endpoint.PkceParameterNames;
|
42 | 45 | import org.springframework.security.oauth2.core.oidc.OidcScopes;
|
43 | 46 | import org.springframework.security.oauth2.server.authorization.OAuth2Authorization;
|
44 |
| -import org.springframework.security.oauth2.core.OAuth2AuthorizationCode; |
45 | 47 | import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationConsent;
|
46 | 48 | import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationConsentService;
|
47 | 49 | import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationService;
|
@@ -129,6 +131,13 @@ public void setAuthenticationValidatorResolverWhenNullThenThrowIllegalArgumentEx
|
129 | 131 | .hasMessage("authenticationValidatorResolver cannot be null");
|
130 | 132 | }
|
131 | 133 |
|
| 134 | + @Test |
| 135 | + public void setAuthorizationConsentCustomizerWhenNullThenThrowIllegalArgumentException() { |
| 136 | + assertThatThrownBy(() -> this.authenticationProvider.setAuthorizationConsentCustomizer(null)) |
| 137 | + .isInstanceOf(IllegalArgumentException.class) |
| 138 | + .hasMessage("authorizationConsentCustomizer cannot be null"); |
| 139 | + } |
| 140 | + |
132 | 141 | @Test
|
133 | 142 | public void authenticateWhenInvalidClientIdThenThrowOAuth2AuthorizationCodeRequestAuthenticationException() {
|
134 | 143 | RegisteredClient registeredClient = TestRegisteredClients.registeredClient().build();
|
@@ -773,6 +782,53 @@ public void authenticateWhenConsentRequestApproveAllThenReturnAuthorizationCode(
|
773 | 782 | OAuth2AuthorizationCodeRequestAuthenticationToken authenticationResult =
|
774 | 783 | (OAuth2AuthorizationCodeRequestAuthenticationToken) this.authenticationProvider.authenticate(authentication);
|
775 | 784 |
|
| 785 | + assertAuthorizationConsentRequestWithAuthorizationCodeResult(registeredClient, authorization, authenticationResult); |
| 786 | + } |
| 787 | + |
| 788 | + @Test |
| 789 | + public void authenticateWhenCustomAuthorizationConsentCustomizerThenUsed() { |
| 790 | + RegisteredClient registeredClient = TestRegisteredClients.registeredClient() |
| 791 | + .build(); |
| 792 | + when(this.registeredClientRepository.findByClientId(eq(registeredClient.getClientId()))) |
| 793 | + .thenReturn(registeredClient); |
| 794 | + OAuth2Authorization authorization = TestOAuth2Authorizations.authorization(registeredClient) |
| 795 | + .principalName(this.principal.getName()) |
| 796 | + .build(); |
| 797 | + OAuth2AuthorizationRequest authorizationRequest = authorization.getAttribute(OAuth2AuthorizationRequest.class.getName()); |
| 798 | + Set<String> authorizedScopes = authorizationRequest.getScopes(); |
| 799 | + OAuth2AuthorizationCodeRequestAuthenticationToken authentication = |
| 800 | + authorizationConsentRequestAuthentication(registeredClient, this.principal) |
| 801 | + .scopes(authorizedScopes) // Approve all scopes |
| 802 | + .build(); |
| 803 | + when(this.authorizationService.findByToken(eq(authentication.getState()), eq(STATE_TOKEN_TYPE))) |
| 804 | + .thenReturn(authorization); |
| 805 | + |
| 806 | + @SuppressWarnings("unchecked") |
| 807 | + Customizer<OAuth2AuthenticationContext> authorizationConsentCustomizer = mock(Customizer.class); |
| 808 | + this.authenticationProvider.setAuthorizationConsentCustomizer(authorizationConsentCustomizer); |
| 809 | + |
| 810 | + OAuth2AuthorizationCodeRequestAuthenticationToken authenticationResult = |
| 811 | + (OAuth2AuthorizationCodeRequestAuthenticationToken) this.authenticationProvider.authenticate(authentication); |
| 812 | + |
| 813 | + assertAuthorizationConsentRequestWithAuthorizationCodeResult(registeredClient, authorization, authenticationResult); |
| 814 | + |
| 815 | + ArgumentCaptor<OAuth2AuthenticationContext> contextCaptor = ArgumentCaptor.forClass(OAuth2AuthenticationContext.class); |
| 816 | + verify(authorizationConsentCustomizer).customize(contextCaptor.capture()); |
| 817 | + |
| 818 | + OAuth2AuthenticationContext context = contextCaptor.getValue(); |
| 819 | + assertThat((Authentication) context.getAuthentication()).isEqualTo(authentication); |
| 820 | + assertThat(context.get(OAuth2AuthorizationConsent.Builder.class)).isInstanceOf(OAuth2AuthorizationConsent.Builder.class); |
| 821 | + assertThat(context.get(OAuth2Authorization.class)).isInstanceOf(OAuth2Authorization.class); |
| 822 | + assertThat(context.get(OAuth2AuthorizationRequest.class)).isInstanceOf(OAuth2AuthorizationRequest.class); |
| 823 | + } |
| 824 | + |
| 825 | + private void assertAuthorizationConsentRequestWithAuthorizationCodeResult( |
| 826 | + RegisteredClient registeredClient, |
| 827 | + OAuth2Authorization authorization, |
| 828 | + OAuth2AuthorizationCodeRequestAuthenticationToken authenticationResult) { |
| 829 | + OAuth2AuthorizationRequest authorizationRequest = authorization.getAttribute(OAuth2AuthorizationRequest.class.getName()); |
| 830 | + Set<String> authorizedScopes = authorizationRequest.getScopes(); |
| 831 | + |
776 | 832 | ArgumentCaptor<OAuth2AuthorizationConsent> authorizationConsentCaptor = ArgumentCaptor.forClass(OAuth2AuthorizationConsent.class);
|
777 | 833 | verify(this.authorizationConsentService).save(authorizationConsentCaptor.capture());
|
778 | 834 | OAuth2AuthorizationConsent authorizationConsent = authorizationConsentCaptor.getValue();
|
|
0 commit comments