From 740a2066b01f5a0ad6633bb9b617022918d37d65 Mon Sep 17 00:00:00 2001 From: Felix Schumacher Date: Sat, 9 May 2020 11:55:31 +0200 Subject: [PATCH] When preemptive auth is disabled HTTP Sampler does not automatically respond to Basic Auth challenge Bugzilla Id: 64267 --- .../protocol/http/sampler/HTTPHC4Impl.java | 86 ++++++++++++++++++- xdocs/changes.xml | 1 + 2 files changed, 85 insertions(+), 2 deletions(-) diff --git a/src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/sampler/HTTPHC4Impl.java b/src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/sampler/HTTPHC4Impl.java index 8d4dd85233b..4824ed6a700 100644 --- a/src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/sampler/HTTPHC4Impl.java +++ b/src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/sampler/HTTPHC4Impl.java @@ -24,6 +24,7 @@ import java.io.UnsupportedEncodingException; import java.net.InetAddress; import java.net.InetSocketAddress; +import java.net.MalformedURLException; import java.net.URI; import java.net.URISyntaxException; import java.net.URL; @@ -62,6 +63,7 @@ import org.apache.http.auth.AuthState; import org.apache.http.auth.Credentials; import org.apache.http.auth.NTCredentials; +import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.client.AuthCache; import org.apache.http.client.AuthenticationStrategy; import org.apache.http.client.CredentialsProvider; @@ -205,6 +207,80 @@ public class HTTPHC4Impl extends HTTPHCAbstractImpl { private static final InputStreamFactory BROTLI = BrotliInputStream::new; + private static final class ManagedCredentialsProvider implements CredentialsProvider { + private AuthManager authManager; + private Credentials proxyCredentials; + private AuthScope proxyAuthScope; + + public ManagedCredentialsProvider(AuthManager authManager, AuthScope proxyAuthScope, Credentials proxyCredentials) { + this.authManager = authManager; + this.proxyAuthScope = proxyAuthScope; + this.proxyCredentials = proxyCredentials; + } + + @Override + public void setCredentials(AuthScope authscope, Credentials credentials) { + log.debug("Store creds {} for {}", credentials, authscope); + } + + @Override + public Credentials getCredentials(AuthScope authScope) { + log.info("Get creds for {}", authScope); + if (this.proxyAuthScope != null && authScope.equals(proxyAuthScope)) { + return proxyCredentials; + } + final Authorization authorization = getAuthorizationForAuthScope(authScope); + if (authorization == null) { + return null; + } + return new UsernamePasswordCredentials(authorization.getUser(), authorization.getPass()); + } + + /** + * Find the Authorization for the given AuthScope. We can't ask the AuthManager + * by the URL, as we didn't get the scheme or path of the URL. Therefore we do a + * best guess on the information we have + * + * @param authScope information which destination we want to get credentials for + * @return matching authorization information entry from the AuthManager + */ + private Authorization getAuthorizationForAuthScope(AuthScope authScope) { + if (authScope == null) { + return null; + } + for (JMeterProperty authProp : authManager.getAuthObjects()) { + Object authObject = authProp.getObjectValue(); + if (authObject instanceof Authorization) { + Authorization auth = (Authorization) authObject; + if (!authScope.getRealm().equals(auth.getRealm())) { + continue; + } + try { + URL authUrl = new URL(auth.getURL()); + if (authUrl.getHost().equals(authScope.getHost()) && getPort(authUrl) == authScope.getPort()) { + return auth; + } + } catch (MalformedURLException e) { + log.debug("Invalid URL {} in authManager", auth.getURL()); + } + } + } + return null; + } + + private int getPort(URL url) { + if (url.getPort() == -1) { + return url.getProtocol().equals("https") ? 443 : 80; + } + return url.getPort(); + } + + @Override + public void clear() { + log.debug("clear creds"); + } + } + private static final class PreemptiveAuthRequestInterceptor implements HttpRequestInterceptor { @Override public void process(HttpRequest request, HttpContext context) throws HttpException, IOException { @@ -1055,21 +1131,27 @@ private MutableTripleHTTP Samplers and Test Script Recorder

Other Samplers