From a2d85d7f7a6abf3371ff652f1f9f996216903f82 Mon Sep 17 00:00:00 2001 From: d036670 Date: Sun, 25 Aug 2024 19:20:29 +0200 Subject: [PATCH] sonar refactorings --- .../PasswordGrantAuthenticationManager.java | 16 ++++++---------- .../PasswordGrantAuthenticationManagerTest.java | 5 +---- .../webapp/WEB-INF/spring/oauth-endpoints.xml | 1 - 3 files changed, 7 insertions(+), 15 deletions(-) diff --git a/server/src/main/java/org/cloudfoundry/identity/uaa/authentication/manager/PasswordGrantAuthenticationManager.java b/server/src/main/java/org/cloudfoundry/identity/uaa/authentication/manager/PasswordGrantAuthenticationManager.java index 20ad6d5c3b7..c5d21eff13a 100644 --- a/server/src/main/java/org/cloudfoundry/identity/uaa/authentication/manager/PasswordGrantAuthenticationManager.java +++ b/server/src/main/java/org/cloudfoundry/identity/uaa/authentication/manager/PasswordGrantAuthenticationManager.java @@ -19,7 +19,6 @@ import org.cloudfoundry.identity.uaa.provider.oauth.ExternalOAuthAuthenticationManager; import org.cloudfoundry.identity.uaa.provider.oauth.ExternalOAuthCodeToken; import org.cloudfoundry.identity.uaa.provider.oauth.ExternalOAuthProviderConfigurator; -import org.cloudfoundry.identity.uaa.zone.MultitenantClientServices; import org.cloudfoundry.identity.uaa.zone.IdentityZoneHolder; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.ApplicationEvent; @@ -46,7 +45,6 @@ import java.net.URL; import java.util.*; import java.util.function.Supplier; -import java.util.stream.Collectors; import static org.cloudfoundry.identity.uaa.oauth.token.TokenConstants.GRANT_TYPE_PASSWORD; import static org.springframework.http.MediaType.APPLICATION_JSON; @@ -57,16 +55,14 @@ public class PasswordGrantAuthenticationManager implements AuthenticationManager private IdentityProviderProvisioning identityProviderProvisioning; private RestTemplateConfig restTemplateConfig; private ExternalOAuthAuthenticationManager externalOAuthAuthenticationManager; - private MultitenantClientServices clientDetailsService; private ExternalOAuthProviderConfigurator externalOAuthProviderProvisioning; private ApplicationEventPublisher eventPublisher; - public PasswordGrantAuthenticationManager(DynamicZoneAwareAuthenticationManager zoneAwareAuthzAuthenticationManager, final @Qualifier("identityProviderProvisioning") IdentityProviderProvisioning identityProviderProvisioning, RestTemplateConfig restTemplateConfig, ExternalOAuthAuthenticationManager externalOAuthAuthenticationManager, MultitenantClientServices clientDetailsService, ExternalOAuthProviderConfigurator externalOAuthProviderProvisioning) { + public PasswordGrantAuthenticationManager(DynamicZoneAwareAuthenticationManager zoneAwareAuthzAuthenticationManager, final @Qualifier("identityProviderProvisioning") IdentityProviderProvisioning identityProviderProvisioning, RestTemplateConfig restTemplateConfig, ExternalOAuthAuthenticationManager externalOAuthAuthenticationManager, ExternalOAuthProviderConfigurator externalOAuthProviderProvisioning) { this.zoneAwareAuthzAuthenticationManager = zoneAwareAuthzAuthenticationManager; this.identityProviderProvisioning = identityProviderProvisioning; this.restTemplateConfig = restTemplateConfig; this.externalOAuthAuthenticationManager = externalOAuthAuthenticationManager; - this.clientDetailsService = clientDetailsService; this.externalOAuthProviderProvisioning = externalOAuthProviderProvisioning; } @@ -81,8 +77,8 @@ public Authentication authenticate(Authentication authentication) throws Authent if (identityProvider != null) { possibleProviders = List.of(identityProvider.getOriginKey()); } else { - List identityProviders = identityProviderProvisioning.retrieveActive(IdentityZoneHolder.get().getId()).stream().filter(this::providerSupportsPasswordGrant).map(IdentityProvider::getOriginKey).collect(Collectors.toList()); - possibleProviders = Optional.ofNullable(allowedProviders).orElse(identityProviders).stream().filter(identityProviders::contains).collect(Collectors.toList()); + List identityProviders = identityProviderProvisioning.retrieveActive(IdentityZoneHolder.get().getId()).stream().filter(this::providerSupportsPasswordGrant).map(IdentityProvider::getOriginKey).toList(); + possibleProviders = Optional.ofNullable(allowedProviders).orElse(identityProviders).stream().filter(identityProviders::contains).toList(); } if (uaaLoginHint == null) { if (defaultProvider != null && possibleProviders.contains(defaultProvider)) { @@ -255,11 +251,11 @@ private List getAllowedProviders() { if (clientAuth == null) { throw new BadCredentialsException("No client authentication found."); } + List allowedProviders = null; if (clientAuth.getPrincipal() instanceof UaaClient uaaClient && uaaClient.getAdditionalInformation() != null) { - return (List) uaaClient.getAdditionalInformation().get(ClientConstants.ALLOWED_PROVIDERS); - } else { - return null; + allowedProviders = (List) uaaClient.getAdditionalInformation().get(ClientConstants.ALLOWED_PROVIDERS); } + return allowedProviders; } @Override diff --git a/server/src/test/java/org/cloudfoundry/identity/uaa/authentication/manager/PasswordGrantAuthenticationManagerTest.java b/server/src/test/java/org/cloudfoundry/identity/uaa/authentication/manager/PasswordGrantAuthenticationManagerTest.java index cefb3a6f40d..10465f59a96 100644 --- a/server/src/test/java/org/cloudfoundry/identity/uaa/authentication/manager/PasswordGrantAuthenticationManagerTest.java +++ b/server/src/test/java/org/cloudfoundry/identity/uaa/authentication/manager/PasswordGrantAuthenticationManagerTest.java @@ -29,7 +29,6 @@ import org.cloudfoundry.identity.uaa.provider.oauth.ExternalOAuthCodeToken; import org.cloudfoundry.identity.uaa.provider.oauth.ExternalOAuthProviderConfigurator; import org.cloudfoundry.identity.uaa.zone.IdentityZoneHolder; -import org.cloudfoundry.identity.uaa.zone.MultitenantClientServices; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -79,7 +78,6 @@ class PasswordGrantAuthenticationManagerTest { private IdentityProviderProvisioning identityProviderProvisioning; private RestTemplateConfig restTemplateConfig; private ExternalOAuthAuthenticationManager externalOAuthAuthenticationManager; - private MultitenantClientServices clientDetailsService; private ExternalOAuthProviderConfigurator externalOAuthProviderConfigurator; private ApplicationEventPublisher eventPublisher; @@ -95,7 +93,6 @@ void setUp() throws Exception { identityProviderProvisioning = mock(IdentityProviderProvisioning.class); restTemplateConfig = mock(RestTemplateConfig.class); externalOAuthAuthenticationManager = mock(ExternalOAuthAuthenticationManager.class); - clientDetailsService = mock(MultitenantClientServices.class); externalOAuthProviderConfigurator = mock(ExternalOAuthProviderConfigurator.class); idp = mock(IdentityProvider.class); @@ -126,7 +123,7 @@ void setUp() throws Exception { when(clientAuth.getPrincipal()).thenReturn(uaaClient); when(uaaClient.getAdditionalInformation()).thenReturn(mock(Map.class)); - instance = new PasswordGrantAuthenticationManager(zoneAwareAuthzAuthenticationManager, identityProviderProvisioning, restTemplateConfig, externalOAuthAuthenticationManager, clientDetailsService, externalOAuthProviderConfigurator); + instance = new PasswordGrantAuthenticationManager(zoneAwareAuthzAuthenticationManager, identityProviderProvisioning, restTemplateConfig, externalOAuthAuthenticationManager, externalOAuthProviderConfigurator); IdentityZoneHolder.clear(); eventPublisher = mock(ApplicationEventPublisher.class); instance.setApplicationEventPublisher(eventPublisher); diff --git a/uaa/src/main/webapp/WEB-INF/spring/oauth-endpoints.xml b/uaa/src/main/webapp/WEB-INF/spring/oauth-endpoints.xml index 87664c17e94..c2e6763c55c 100755 --- a/uaa/src/main/webapp/WEB-INF/spring/oauth-endpoints.xml +++ b/uaa/src/main/webapp/WEB-INF/spring/oauth-endpoints.xml @@ -696,7 +696,6 @@ -