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 10:35:13 UTC

(camel) branch auth created (now d6086396298)

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

davsclaus pushed a change to branch auth
in repository https://gitbox.apache.org/repos/asf/camel.git


      at d6086396298 CAMEL-20254: camel-http - pre-emptive authentication breaks basic auth

This branch includes the following new commits:

     new d6086396298 CAMEL-20254: camel-http - pre-emptive authentication breaks basic auth

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



(camel) 01/01: CAMEL-20254: camel-http - pre-emptive authentication breaks basic auth

Posted by da...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit d608639629818108fbd725f36df247a60107cbf7
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Wed Dec 20 11:35:00 2023 +0100

    CAMEL-20254: camel-http - pre-emptive authentication breaks basic auth
---
 .../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 4c605149e0e..0e23bf2e690 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
@@ -23,6 +23,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;
 import org.apache.hc.client5.http.utils.Base64;
 
@@ -48,4 +50,15 @@ public final class HttpCredentialsHelper {
         return "Basic " + new String(encodedAuth);
     }
 
+    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 cec0dea6e4d..1c1060650ce 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
@@ -261,7 +261,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");
             }