From c3faab615de8257c140c940adc9e1010a1713b10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcos=20Villaca=C3=B1as?= Date: Thu, 20 Apr 2023 13:22:33 +0200 Subject: [PATCH] Proxy-Authorization header solves Proxy 407 HTTP Proxy authentication was failing with a 407 error code. After an in-depth review, we discovered that the header sending was incorrect since the Proxy-Authorization key was missing. This pull request aims to solve this issue by configuring the appropriate header for HttpProxyRequest whenever credentials are provided. Note that the code supports both NTLM and Basic authentication methods. This code fixes our problem due to the correct authentication process from the headers set. --- .../java/quickfix/mina/ProtocolFactory.java | 24 +++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/quickfixj-core/src/main/java/quickfix/mina/ProtocolFactory.java b/quickfixj-core/src/main/java/quickfix/mina/ProtocolFactory.java index 13b88f0eec..cb3f69be58 100644 --- a/quickfixj-core/src/main/java/quickfix/mina/ProtocolFactory.java +++ b/quickfixj-core/src/main/java/quickfix/mina/ProtocolFactory.java @@ -21,7 +21,11 @@ import java.net.InetSocketAddress; import java.net.SocketAddress; +import java.util.Base64; +import java.util.Collections; import java.util.HashMap; +import java.util.List; +import java.util.Map; import org.apache.mina.core.service.IoAcceptor; @@ -150,17 +154,29 @@ private static ProxyRequest createHttpProxyRequest(InetSocketAddress address, String proxyPassword, String proxyDomain, String proxyWorkstation) { + HttpProxyRequest req = new HttpProxyRequest(address); HashMap props = new HashMap<>(); - props.put(HttpProxyConstants.USER_PROPERTY, proxyUser); - props.put(HttpProxyConstants.PWD_PROPERTY, proxyPassword); + Map> headers = new HashMap<>(); + boolean authenticationNTLM = false; + if (proxyDomain != null && proxyWorkstation != null) { props.put(HttpProxyConstants.DOMAIN_PROPERTY, proxyDomain); props.put(HttpProxyConstants.WORKSTATION_PROPERTY, proxyWorkstation); + authenticationNTLM = true; + } + if (proxyUser != null && proxyPassword != null) { + props.put(HttpProxyConstants.USER_PROPERTY, proxyUser); + props.put(HttpProxyConstants.PWD_PROPERTY, proxyPassword); + String proxyCredentials = proxyUser + ":" + proxyPassword; + String proxyCredentialsEncoded = Base64.getEncoder().encodeToString(proxyCredentials.getBytes()); + String proxyAuthorization = (authenticationNTLM ? "NTLM " : "Basic ") + proxyCredentialsEncoded; + headers.put("Proxy-Authorization", Collections.singletonList(proxyAuthorization)); } - HttpProxyRequest req = new HttpProxyRequest(address); req.setProperties(props); - if (proxyVersion != null && proxyVersion.equalsIgnoreCase("1.1")) { + req.setHeaders(headers); + + if (proxyVersion != null && "1.1".equalsIgnoreCase(proxyVersion)) { req.setHttpVersion(HttpProxyConstants.HTTP_1_1); } else { req.setHttpVersion(HttpProxyConstants.HTTP_1_0);