From 678060bfbb29b3178240ebfa8c1aba2a670c831d Mon Sep 17 00:00:00 2001 From: strehle Date: Thu, 7 Nov 2024 15:35:22 +0100 Subject: [PATCH] Refactor and fix duplicate found by sonar in https://sonarcloud.io/component_measures?metric=new_duplicated_lines_density&selected=cloudfoundry-identity-parent%3Aserver%2Fsrc%2Fmain%2Fjava%2Forg%2Fcloudfoundry%2Fidentity%2Fuaa%2Fauthentication%2FPasscodeAuthenticationFilter.java&view=list&pullRequest=2908&id=cloudfoundry-identity-parent --- .../AuthzAuthenticationFilter.java | 29 ++----------------- .../PasscodeAuthenticationFilter.java | 28 ++---------------- .../uaa/util/UaaHttpRequestUtils.java | 27 +++++++++++++++++ 3 files changed, 31 insertions(+), 53 deletions(-) diff --git a/server/src/main/java/org/cloudfoundry/identity/uaa/authentication/AuthzAuthenticationFilter.java b/server/src/main/java/org/cloudfoundry/identity/uaa/authentication/AuthzAuthenticationFilter.java index bc23e9da7cd..78116a19191 100644 --- a/server/src/main/java/org/cloudfoundry/identity/uaa/authentication/AuthzAuthenticationFilter.java +++ b/server/src/main/java/org/cloudfoundry/identity/uaa/authentication/AuthzAuthenticationFilter.java @@ -1,10 +1,9 @@ package org.cloudfoundry.identity.uaa.authentication; -import com.fasterxml.jackson.core.type.TypeReference; import org.cloudfoundry.identity.uaa.login.AccountSavingAuthenticationSuccessHandler; import org.cloudfoundry.identity.uaa.oauth.provider.error.OAuth2AuthenticationEntryPoint; -import org.cloudfoundry.identity.uaa.util.JsonUtils; import org.cloudfoundry.identity.uaa.util.SessionUtils; +import org.cloudfoundry.identity.uaa.util.UaaHttpRequestUtils; import org.cloudfoundry.identity.uaa.util.UaaStringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -29,7 +28,6 @@ import java.io.IOException; import java.util.Collections; import java.util.Enumeration; -import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; @@ -118,7 +116,7 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha HttpServletRequest req = (HttpServletRequest) request; HttpServletResponse res = (HttpServletResponse) response; - Map loginInfo = getCredentials(req); + Map loginInfo = UaaHttpRequestUtils.getCredentials(req, parameterNames); boolean buggyVmcAcceptHeader = false; @@ -184,29 +182,6 @@ public String getHeader(String name) { chain.doFilter(request, response); } - private Map getCredentials(HttpServletRequest request) { - Map credentials = new HashMap<>(); - - for (String paramName : parameterNames) { - String value = request.getParameter(paramName); - if (value != null) { - if (value.startsWith("{")) { - try { - Map jsonCredentials = JsonUtils.readValue(value, - new TypeReference<>() { - }); - credentials.putAll(jsonCredentials); - } catch (JsonUtils.JsonUtilException e) { - logger.warn("Unknown format of value for request param: " + paramName + ". Ignoring."); - } - } else { - credentials.put(paramName, value); - } - } - } - - return credentials; - } @Override public void init(FilterConfig filterConfig) { diff --git a/server/src/main/java/org/cloudfoundry/identity/uaa/authentication/PasscodeAuthenticationFilter.java b/server/src/main/java/org/cloudfoundry/identity/uaa/authentication/PasscodeAuthenticationFilter.java index 19882e73bcc..da5fac1813a 100644 --- a/server/src/main/java/org/cloudfoundry/identity/uaa/authentication/PasscodeAuthenticationFilter.java +++ b/server/src/main/java/org/cloudfoundry/identity/uaa/authentication/PasscodeAuthenticationFilter.java @@ -14,8 +14,8 @@ package org.cloudfoundry.identity.uaa.authentication; -import com.fasterxml.jackson.core.type.TypeReference; import org.cloudfoundry.identity.uaa.oauth.provider.OAuth2RequestFactory; +import org.cloudfoundry.identity.uaa.util.UaaHttpRequestUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.cloudfoundry.identity.uaa.codestore.ExpiringCode; @@ -241,7 +241,7 @@ public Authentication authenticate(Authentication authentication) throws Authent protected Authentication extractCredentials(HttpServletRequest request) { String grantType = request.getParameter("grant_type"); if (grantType != null && grantType.equals(GRANT_TYPE_PASSWORD)) { - Map credentials = getCredentials(request); + Map credentials = UaaHttpRequestUtils.getCredentials(request, parameterNames); String passcode = credentials.get("passcode"); if (passcode!=null) { return new ExpiringCodeAuthentication(request, passcode); @@ -251,30 +251,6 @@ protected Authentication extractCredentials(HttpServletRequest request) { } return null; } - private Map getCredentials(HttpServletRequest request) { - Map credentials = new HashMap(); - - for (String paramName : parameterNames) { - String value = request.getParameter(paramName); - if (value != null) { - if (value.startsWith("{")) { - try { - Map jsonCredentials = JsonUtils.readValue(value, - new TypeReference>() { - }); - credentials.putAll(jsonCredentials); - } catch (JsonUtils.JsonUtilException e) { - logger.warn("Unknown format of value for request param: " + paramName + ". Ignoring."); - } - } - else { - credentials.put(paramName, value); - } - } - } - - return credentials; - } @Override public void init(FilterConfig filterConfig) { diff --git a/server/src/main/java/org/cloudfoundry/identity/uaa/util/UaaHttpRequestUtils.java b/server/src/main/java/org/cloudfoundry/identity/uaa/util/UaaHttpRequestUtils.java index 550dd3c3514..9bea6e92948 100644 --- a/server/src/main/java/org/cloudfoundry/identity/uaa/util/UaaHttpRequestUtils.java +++ b/server/src/main/java/org/cloudfoundry/identity/uaa/util/UaaHttpRequestUtils.java @@ -13,6 +13,7 @@ *******************************************************************************/ package org.cloudfoundry.identity.uaa.util; +import com.fasterxml.jackson.core.type.TypeReference; import org.apache.http.HeaderElement; import org.apache.http.HeaderElementIterator; import org.apache.http.HttpResponse; @@ -43,11 +44,14 @@ import javax.net.ssl.HostnameVerifier; import javax.net.ssl.SSLContext; +import javax.servlet.http.HttpServletRequest; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import java.security.KeyManagementException; import java.security.KeyStoreException; import java.security.NoSuchAlgorithmException; +import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; @@ -180,4 +184,27 @@ private static String[] split(final String s) { } return stream(s.split(",")).map(String::trim).toList().toArray(String[]::new); } + + public static Map getCredentials(HttpServletRequest request, List parameterNames) { + Map credentials = new HashMap<>(); + + for (String paramName : parameterNames) { + String value = request.getParameter(paramName); + if (value != null) { + if (value.startsWith("{")) { + try { + Map jsonCredentials = JsonUtils.readValue(value, + new TypeReference<>() { + }); + credentials.putAll(jsonCredentials); + } catch (JsonUtils.JsonUtilException e) { + logger.warn("Unknown format of value for request param: {}. Ignoring.", paramName); + } + } else { + credentials.put(paramName, value); + } + } + } + return credentials; + } }