Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor and fix duplicate #3112

Merged
merged 1 commit into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -118,7 +116,7 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse res = (HttpServletResponse) response;

Map<String, String> loginInfo = getCredentials(req);
Map<String, String> loginInfo = UaaHttpRequestUtils.getCredentials(req, parameterNames);

boolean buggyVmcAcceptHeader = false;

Expand Down Expand Up @@ -184,29 +182,6 @@ public String getHeader(String name) {
chain.doFilter(request, response);
}

private Map<String, String> getCredentials(HttpServletRequest request) {
Map<String, String> credentials = new HashMap<>();

for (String paramName : parameterNames) {
String value = request.getParameter(paramName);
if (value != null) {
if (value.startsWith("{")) {
try {
Map<String, String> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String, String> credentials = getCredentials(request);
Map<String, String> credentials = UaaHttpRequestUtils.getCredentials(request, parameterNames);
String passcode = credentials.get("passcode");
if (passcode!=null) {
return new ExpiringCodeAuthentication(request, passcode);
Expand All @@ -251,30 +251,6 @@ protected Authentication extractCredentials(HttpServletRequest request) {
}
return null;
}
private Map<String, String> getCredentials(HttpServletRequest request) {
Map<String, String> credentials = new HashMap<String, String>();

for (String paramName : parameterNames) {
String value = request.getParameter(paramName);
if (value != null) {
if (value.startsWith("{")) {
try {
Map<String, String> jsonCredentials = JsonUtils.readValue(value,
new TypeReference<Map<String, String>>() {
});
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<String, String> getCredentials(HttpServletRequest request, List<String> parameterNames) {
Map<String, String> credentials = new HashMap<>();

for (String paramName : parameterNames) {
String value = request.getParameter(paramName);
if (value != null) {
if (value.startsWith("{")) {
try {
Map<String, String> 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;
}
}
Loading