You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2023/12/20 11:57:12 UTC

(camel) branch camel-4.0.x updated: CAMEL-20254: camel-http - pre-emptive authentication breaks basic auth (#12498)

This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch camel-4.0.x
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/camel-4.0.x by this push:
     new dafea022841 CAMEL-20254: camel-http - pre-emptive authentication breaks basic auth (#12498)
dafea022841 is described below

commit dafea022841fc379cf1ecba26fd13ef593997c3e
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Wed Dec 20 12:53:43 2023 +0100

    CAMEL-20254: camel-http - pre-emptive authentication breaks basic auth (#12498)
---
 .../apache/camel/component/http/HttpCredentialsHelper.java  | 13 +++++++++++++
 .../java/org/apache/camel/component/http/HttpEndpoint.java  |  2 +-
 .../component/http/PreemptiveAuthExecChainHandler.java      | 10 ++++++++++
 3 files changed, 24 insertions(+), 1 deletion(-)

diff --git a/components/camel-http/src/main/java/org/apache/camel/component/http/HttpCredentialsHelper.java b/components/camel-http/src/main/java/org/apache/camel/component/http/HttpCredentialsHelper.java
index f51163f93b5..c4d7da2eca5 100644
--- a/components/camel-http/src/main/java/org/apache/camel/component/http/HttpCredentialsHelper.java
+++ b/components/camel-http/src/main/java/org/apache/camel/component/http/HttpCredentialsHelper.java
@@ -22,6 +22,8 @@ import org.apache.hc.client5.http.auth.AuthScope;
 import org.apache.hc.client5.http.auth.Credentials;
 import org.apache.hc.client5.http.auth.CredentialsProvider;
 import org.apache.hc.client5.http.auth.CredentialsStore;
+import org.apache.hc.client5.http.auth.NTCredentials;
+import org.apache.hc.client5.http.auth.UsernamePasswordCredentials;
 import org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider;
 
 final class HttpCredentialsHelper {
@@ -40,4 +42,15 @@ final class HttpCredentialsHelper {
         return credentialsProvider;
     }
 
+    public static Credentials getCredentials(String method, String username, String password, String host, String domain) {
+        if (username != null && password != null) {
+            if (domain != null && host != null) {
+                return new NTCredentials(username, password.toCharArray(), host, domain);
+            } else {
+                return new UsernamePasswordCredentials(username, password.toCharArray());
+            }
+        }
+        return null;
+    }
+
 }
diff --git a/components/camel-http/src/main/java/org/apache/camel/component/http/HttpEndpoint.java b/components/camel-http/src/main/java/org/apache/camel/component/http/HttpEndpoint.java
index 309ef7ff39c..7f0cbd0d4b2 100644
--- a/components/camel-http/src/main/java/org/apache/camel/component/http/HttpEndpoint.java
+++ b/components/camel-http/src/main/java/org/apache/camel/component/http/HttpEndpoint.java
@@ -262,7 +262,7 @@ public class HttpEndpoint extends HttpCommonEndpoint {
 
         if (isAuthenticationPreemptive()) {
             // setup the preemptive authentication here
-            clientBuilder.addExecInterceptorFirst("preemptive-auth", new PreemptiveAuthExecChainHandler());
+            clientBuilder.addExecInterceptorFirst("preemptive-auth", new PreemptiveAuthExecChainHandler(this));
         }
         String userAgent = getUserAgent();
         if (userAgent != null) {
diff --git a/components/camel-http/src/main/java/org/apache/camel/component/http/PreemptiveAuthExecChainHandler.java b/components/camel-http/src/main/java/org/apache/camel/component/http/PreemptiveAuthExecChainHandler.java
index cbfe6126c94..40c96676619 100644
--- a/components/camel-http/src/main/java/org/apache/camel/component/http/PreemptiveAuthExecChainHandler.java
+++ b/components/camel-http/src/main/java/org/apache/camel/component/http/PreemptiveAuthExecChainHandler.java
@@ -34,6 +34,12 @@ import org.apache.hc.core5.http.HttpHost;
 
 public class PreemptiveAuthExecChainHandler implements ExecChainHandler {
 
+    private final HttpEndpoint endpoint;
+
+    public PreemptiveAuthExecChainHandler(HttpEndpoint endpoint) {
+        this.endpoint = endpoint;
+    }
+
     @Override
     public ClassicHttpResponse execute(
             ClassicHttpRequest request,
@@ -48,6 +54,10 @@ public class PreemptiveAuthExecChainHandler implements ExecChainHandler {
             CredentialsProvider credentialsProvider = context.getCredentialsProvider();
             HttpHost httpHost = scope.route.getTargetHost();
             Credentials credentials = credentialsProvider.getCredentials(new AuthScope(httpHost), context);
+            if (credentials == null) {
+                credentials = HttpCredentialsHelper.getCredentials(endpoint.getAuthMethod(), endpoint.getAuthUsername(),
+                        endpoint.getAuthPassword(), endpoint.getAuthHost(), endpoint.getAuthHost());
+            }
             if (credentials == null) {
                 throw new HttpException("No credentials for preemptive authentication");
             }