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:53:49 UTC

(camel) branch main 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 main
in repository https://gitbox.apache.org/repos/asf/camel.git


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

commit 8886146842cb077ee075eafd4bafbb7a7f4e8887
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 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");
             }