You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by "gilvansfilho (via GitHub)" <gi...@apache.org> on 2023/10/02 16:16:20 UTC

[PR] CAMEL-18637: OAuth2 authentication with client credentials flow [camel]

gilvansfilho opened a new pull request, #11628:
URL: https://github.com/apache/camel/pull/11628

   # Description
   
   Adding oauth2 authentication to camel-http component as requested in that [Jira issue](https://issues.apache.org/jira/projects/CAMEL/issues/CAMEL-18637).
   
   <!--
   - Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
   -->
   
   # Target
   
   - [ ] I checked that the commit is targeting the correct branch (note that Camel 3 uses `camel-3.x`, whereas Camel 4 uses the `main` branch)
   
   # Tracking
   - [ ] If this is a large change, bug fix, or code improvement, I checked there is a [JIRA issue](https://issues.apache.org/jira/browse/CAMEL) filed for the change (usually before you start working on it).
   
   <!--
   # *Note*: trivial changes like, typos, minor documentation fixes and other small items do not require a JIRA issue. In this case your pull request should address just this issue, without pulling in other changes.
   -->
   
   # Apache Camel coding standards and style
   
   - [ ] I checked that each commit in the pull request has a meaningful subject line and body.
   
   <!--
   If you're unsure, you can format the pull request title like `[CAMEL-XXX] Fixes bug in camel-file component`, where you replace `CAMEL-XXX` with the appropriate JIRA issue.
   -->
   
   - [ ] I have run `mvn clean install -DskipTests` locally and I have committed all auto-generated changes
   
   <!--
   You can run the aforementioned command in your module so that the build auto-formats your code. This will also be verified as part of the checks and your PR may be rejected if if there are uncommited changes after running `mvn clean install -DskipTests`.
   
   You can learn more about the contribution guidelines at https://github.com/apache/camel/blob/main/CONTRIBUTING.md
   -->
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@camel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] CAMEL-18637: camel-http support for OAuth2 client credentials flow [camel]

Posted by "oscerd (via GitHub)" <gi...@apache.org>.
oscerd commented on code in PR #11628:
URL: https://github.com/apache/camel/pull/11628#discussion_r1345387813


##########
components/camel-http/src/main/java/org/apache/camel/component/http/OAuth2ClientConfigurer.java:
##########
@@ -0,0 +1,83 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.http;
+
+import org.apache.camel.util.json.DeserializationException;
+import org.apache.camel.util.json.JsonObject;
+import org.apache.camel.util.json.Jsoner;
+import org.apache.hc.client5.http.classic.HttpClient;
+import org.apache.hc.client5.http.classic.methods.HttpPost;
+import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
+import org.apache.hc.core5.http.ContentType;
+import org.apache.hc.core5.http.EntityDetails;
+import org.apache.hc.core5.http.HttpHeaders;
+import org.apache.hc.core5.http.HttpRequest;
+import org.apache.hc.core5.http.io.entity.EntityUtils;
+import org.apache.hc.core5.http.io.entity.StringEntity;
+import org.apache.hc.core5.http.protocol.HttpContext;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class OAuth2ClientConfigurer implements HttpClientConfigurer {
+
+    private static final Logger LOG = LoggerFactory.getLogger(OAuth2ClientConfigurer.class);
+
+    private final String clientId;
+    private final String clientSecret;
+    private final String tokenEndpoint;
+
+    public OAuth2ClientConfigurer(String clientId, String clientSecret, String tokenEndpoint) {
+        this.clientId = clientId;
+        this.clientSecret = clientSecret;
+        this.tokenEndpoint = tokenEndpoint;
+    }
+
+    @Override
+    public void configureHttpClient(HttpClientBuilder clientBuilder) {
+        HttpClient httpClient = clientBuilder.build();
+        clientBuilder.addRequestInterceptorFirst((HttpRequest request, EntityDetails entity, HttpContext context) -> {
+
+            final HttpPost httpPost = new HttpPost(tokenEndpoint);
+
+            httpPost.addHeader(HttpHeaders.AUTHORIZATION,
+                    HttpCredentialsHelper.generateBasicAuthHeader(clientId, clientSecret));
+            httpPost.setEntity(new StringEntity("grant_type=client_credentials", ContentType.APPLICATION_FORM_URLENCODED));
+
+            httpClient.execute(httpPost, response -> {
+
+                try {
+                    String responseString = EntityUtils.toString(response.getEntity());
+
+                    if (response.getCode() == 200) {
+                        String accessToken = ((JsonObject) Jsoner.deserialize(responseString)).getString("access_token");
+                        request.addHeader(HttpHeaders.AUTHORIZATION, accessToken);
+                    } else {
+                        // throw exception?? For that, needs to change HttpClientConfigurer interface to allow it
+                    }
+
+                } catch (DeserializationException e) {
+                    // TODO Auto-generated catch block
+                    e.printStackTrace();
+                }
+
+                return null;
+            });

Review Comment:
   Changing the interface seems reasonable in this case.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@camel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] CAMEL-18637: OAuth2 authentication with client credentials flow [camel]

Posted by "orpiske (via GitHub)" <gi...@apache.org>.
orpiske commented on code in PR #11628:
URL: https://github.com/apache/camel/pull/11628#discussion_r1343069732


##########
components/camel-http/src/main/java/org/apache/camel/component/http/OAuth2ClientConfigurer.java:
##########
@@ -0,0 +1,83 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.http;
+
+import org.apache.camel.util.json.DeserializationException;
+import org.apache.camel.util.json.JsonObject;
+import org.apache.camel.util.json.Jsoner;
+import org.apache.hc.client5.http.classic.HttpClient;
+import org.apache.hc.client5.http.classic.methods.HttpPost;
+import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
+import org.apache.hc.core5.http.ContentType;
+import org.apache.hc.core5.http.EntityDetails;
+import org.apache.hc.core5.http.HttpHeaders;
+import org.apache.hc.core5.http.HttpRequest;
+import org.apache.hc.core5.http.io.entity.EntityUtils;
+import org.apache.hc.core5.http.io.entity.StringEntity;
+import org.apache.hc.core5.http.protocol.HttpContext;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class OAuth2ClientConfigurer implements HttpClientConfigurer {
+
+    private static final Logger LOG = LoggerFactory.getLogger(OAuth2ClientConfigurer.class);
+
+    private final String clientId;
+    private final String clientSecret;
+    private final String tokenEndpoint;
+
+    public OAuth2ClientConfigurer(String clientId, String clientSecret, String tokenEndpoint) {
+        this.clientId = clientId;
+        this.clientSecret = clientSecret;
+        this.tokenEndpoint = tokenEndpoint;
+    }
+
+    @Override
+    public void configureHttpClient(HttpClientBuilder clientBuilder) {
+        HttpClient httpClient = clientBuilder.build();
+        clientBuilder.addRequestInterceptorFirst((HttpRequest request, EntityDetails entity, HttpContext context) -> {
+
+            final HttpPost httpPost = new HttpPost(tokenEndpoint);
+
+            httpPost.addHeader(HttpHeaders.AUTHORIZATION,
+                    HttpCredentialsHelper.generateBasicAuthHeader(clientId, clientSecret));
+            httpPost.setEntity(new StringEntity("grant_type=client_credentials", ContentType.APPLICATION_FORM_URLENCODED));
+
+            httpClient.execute(httpPost, response -> {
+
+                try {
+                    String responseString = EntityUtils.toString(response.getEntity());
+
+                    if (response.getCode() == 200) {
+                        String accessToken = ((JsonObject) Jsoner.deserialize(responseString)).getString("access_token");
+                        request.addHeader(HttpHeaders.AUTHORIZATION, accessToken);
+                    } else {
+                        // throw exception?? For that, needs to change HttpClientConfigurer interface to allow it
+                    }
+
+                } catch (DeserializationException e) {
+                    // TODO Auto-generated catch block
+                    e.printStackTrace();
+                }
+
+                return null;
+            });

Review Comment:
   As we discussed via chat, this part seems to be one of interest. We might want to have some flexibility about how to handle the exceptions here but, also, be able to avoid unnecessary overhead when it's not necessary.  
   
   I think it would be interesting to get some insights from @davsclaus, @oscerd and others about what they think we should do here. 
   



##########
components/camel-http/src/main/java/org/apache/camel/component/http/OAuth2ClientConfigurer.java:
##########
@@ -0,0 +1,83 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.http;
+
+import org.apache.camel.util.json.DeserializationException;
+import org.apache.camel.util.json.JsonObject;
+import org.apache.camel.util.json.Jsoner;
+import org.apache.hc.client5.http.classic.HttpClient;
+import org.apache.hc.client5.http.classic.methods.HttpPost;
+import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
+import org.apache.hc.core5.http.ContentType;
+import org.apache.hc.core5.http.EntityDetails;
+import org.apache.hc.core5.http.HttpHeaders;
+import org.apache.hc.core5.http.HttpRequest;
+import org.apache.hc.core5.http.io.entity.EntityUtils;
+import org.apache.hc.core5.http.io.entity.StringEntity;
+import org.apache.hc.core5.http.protocol.HttpContext;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class OAuth2ClientConfigurer implements HttpClientConfigurer {
+
+    private static final Logger LOG = LoggerFactory.getLogger(OAuth2ClientConfigurer.class);
+
+    private final String clientId;
+    private final String clientSecret;
+    private final String tokenEndpoint;
+
+    public OAuth2ClientConfigurer(String clientId, String clientSecret, String tokenEndpoint) {
+        this.clientId = clientId;
+        this.clientSecret = clientSecret;
+        this.tokenEndpoint = tokenEndpoint;
+    }
+
+    @Override
+    public void configureHttpClient(HttpClientBuilder clientBuilder) {
+        HttpClient httpClient = clientBuilder.build();
+        clientBuilder.addRequestInterceptorFirst((HttpRequest request, EntityDetails entity, HttpContext context) -> {
+
+            final HttpPost httpPost = new HttpPost(tokenEndpoint);
+
+            httpPost.addHeader(HttpHeaders.AUTHORIZATION,
+                    HttpCredentialsHelper.generateBasicAuthHeader(clientId, clientSecret));
+            httpPost.setEntity(new StringEntity("grant_type=client_credentials", ContentType.APPLICATION_FORM_URLENCODED));
+
+            httpClient.execute(httpPost, response -> {
+
+                try {
+                    String responseString = EntityUtils.toString(response.getEntity());
+
+                    if (response.getCode() == 200) {
+                        String accessToken = ((JsonObject) Jsoner.deserialize(responseString)).getString("access_token");
+                        request.addHeader(HttpHeaders.AUTHORIZATION, accessToken);
+                    } else {
+                        // throw exception?? For that, needs to change HttpClientConfigurer interface to allow it
+                    }
+
+                } catch (DeserializationException e) {
+                    // TODO Auto-generated catch block
+                    e.printStackTrace();
+                }
+
+                return null;
+            });

Review Comment:
   As we discussed via chat, this part seems to be one of interest. We might want to have some flexibility about how to handle the exceptions here but, also, be able to avoid unnecessary overhead when it's not necessary.  
   
   I think it would be interesting to get some insights from @davsclaus, @oscerd and others about what they think you should do here. 
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@camel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] CAMEL-18637: camel-http support for OAuth2 client credentials flow [camel]

Posted by "davsclaus (via GitHub)" <gi...@apache.org>.
davsclaus commented on PR #11628:
URL: https://github.com/apache/camel/pull/11628#issuecomment-1758522672

   Do you have test failure locally with HttpNoConnectionRedeliveryTest ?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@camel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] CAMEL-18637: OAuth2 authentication with client credentials flow [camel]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #11628:
URL: https://github.com/apache/camel/pull/11628#issuecomment-1743316535

   :star2: Thank you for your contribution to the Apache Camel project! :star2: 
   
   :robot: CI automation will test this PR automatically.
   
   :camel: Apache Camel Committers, please review the following items:
   
   * First-time contributors **require MANUAL approval** for the GitHub Actions to run
   
   * You can use the command `/component-test (camel-)component-name1 (camel-)component-name2..` to request a test from the test bot.
   
   * You can label PRs using `build-all`, `build-dependents`, `skip-tests` and `test-dependents` to fine-tune the checks executed by this PR.
   
   * Build and test logs are available in the Summary page. **Only** [Apache Camel committers](https://camel.apache.org/community/team/#committers) have access to the summary. 
   
   * :warning: Be careful when sharing logs. Review their contents before sharing them publicly.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@camel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] CAMEL-18637: OAuth2 authentication with client credentials flow [camel]

Posted by "gilvansfilho (via GitHub)" <gi...@apache.org>.
gilvansfilho commented on PR #11628:
URL: https://github.com/apache/camel/pull/11628#issuecomment-1743333109

   When done with code I will provide documentation. 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@camel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] CAMEL-18637: OAuth2 authentication with client credentials flow [camel]

Posted by "orpiske (via GitHub)" <gi...@apache.org>.
orpiske commented on code in PR #11628:
URL: https://github.com/apache/camel/pull/11628#discussion_r1343070006


##########
components/camel-http/src/test/java/org/apache/camel/component/http/BaseHttpTest.java:
##########
@@ -32,6 +32,10 @@ public abstract class BaseHttpTest extends HttpServerTestSupport {
 
     protected void assertExchange(Exchange exchange) {
         assertNotNull(exchange);
+
+        if (exchange.getException() != null)
+            exchange.getException().printStackTrace();

Review Comment:
   Missing `{` and `}`



##########
components/camel-http/src/main/java/org/apache/camel/component/http/OAuth2ClientConfigurer.java:
##########
@@ -0,0 +1,83 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.http;
+
+import org.apache.camel.util.json.DeserializationException;
+import org.apache.camel.util.json.JsonObject;
+import org.apache.camel.util.json.Jsoner;
+import org.apache.hc.client5.http.classic.HttpClient;
+import org.apache.hc.client5.http.classic.methods.HttpPost;
+import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
+import org.apache.hc.core5.http.ContentType;
+import org.apache.hc.core5.http.EntityDetails;
+import org.apache.hc.core5.http.HttpHeaders;
+import org.apache.hc.core5.http.HttpRequest;
+import org.apache.hc.core5.http.io.entity.EntityUtils;
+import org.apache.hc.core5.http.io.entity.StringEntity;
+import org.apache.hc.core5.http.protocol.HttpContext;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class OAuth2ClientConfigurer implements HttpClientConfigurer {
+
+    private static final Logger LOG = LoggerFactory.getLogger(OAuth2ClientConfigurer.class);
+
+    private final String clientId;
+    private final String clientSecret;
+    private final String tokenEndpoint;
+
+    public OAuth2ClientConfigurer(String clientId, String clientSecret, String tokenEndpoint) {
+        this.clientId = clientId;
+        this.clientSecret = clientSecret;
+        this.tokenEndpoint = tokenEndpoint;
+    }
+
+    @Override
+    public void configureHttpClient(HttpClientBuilder clientBuilder) {
+        HttpClient httpClient = clientBuilder.build();
+        clientBuilder.addRequestInterceptorFirst((HttpRequest request, EntityDetails entity, HttpContext context) -> {
+
+            final HttpPost httpPost = new HttpPost(tokenEndpoint);
+
+            httpPost.addHeader(HttpHeaders.AUTHORIZATION,
+                    HttpCredentialsHelper.generateBasicAuthHeader(clientId, clientSecret));
+            httpPost.setEntity(new StringEntity("grant_type=client_credentials", ContentType.APPLICATION_FORM_URLENCODED));
+
+            httpClient.execute(httpPost, response -> {
+
+                try {
+                    String responseString = EntityUtils.toString(response.getEntity());
+
+                    if (response.getCode() == 200) {
+                        String accessToken = ((JsonObject) Jsoner.deserialize(responseString)).getString("access_token");
+                        request.addHeader(HttpHeaders.AUTHORIZATION, accessToken);
+                    } else {
+                        // throw exception?? For that, needs to change HttpClientConfigurer interface to allow it
+                    }
+
+                } catch (DeserializationException e) {
+                    // TODO Auto-generated catch block
+                    e.printStackTrace();
+                }
+
+                return null;
+            });

Review Comment:
   As we discussed via chat, this part seems to be one of interest. We might want to have some flexibility about how to handle the exceptions here but, also, be able to avoid unnecessary overhead when it's not necessary.  
   
   I think it would be interesting to get an insight from @davsclaus, @oscerd and others about what they think we should do here. 
   



##########
components/camel-http/src/test/java/org/apache/camel/component/http/handler/OAuth2TokenRequestHandler.java:
##########
@@ -0,0 +1,62 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.http.handler;
+
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+
+import org.apache.camel.component.http.HttpCredentialsHelper;
+import org.apache.hc.core5.http.ClassicHttpRequest;
+import org.apache.hc.core5.http.ClassicHttpResponse;
+import org.apache.hc.core5.http.ContentType;
+import org.apache.hc.core5.http.HttpException;
+import org.apache.hc.core5.http.HttpHeaders;
+import org.apache.hc.core5.http.io.HttpRequestHandler;
+import org.apache.hc.core5.http.io.entity.EntityUtils;
+import org.apache.hc.core5.http.io.entity.StringEntity;
+import org.apache.hc.core5.http.protocol.HttpContext;
+import org.apache.hc.core5.net.WWWFormCodec;
+
+public class OAuth2TokenRequestHandler implements HttpRequestHandler {
+
+    private String clientId;
+    private String clientSecret;
+    private String expectedToken;
+
+    public OAuth2TokenRequestHandler(String expectedToken, String clientId, String clientSecret) {
+        this.expectedToken = expectedToken;
+        this.clientId = clientId;
+        this.clientSecret = clientSecret;
+    }
+
+    @Override
+    public void handle(ClassicHttpRequest request, ClassicHttpResponse response, HttpContext context)
+            throws HttpException, IOException {
+
+        String requestBody = EntityUtils.toString(request.getEntity());
+        WWWFormCodec.parse(requestBody, StandardCharsets.UTF_8).stream()
+                .filter(pair -> pair.getName().equals("grant_type") && pair.getValue().equals("client_credentials"))
+                .findAny().orElseThrow(() -> new HttpException("Invalid or missing grant_type"));
+
+        if (request.getHeader(HttpHeaders.AUTHORIZATION) == null || !request.getHeader(HttpHeaders.AUTHORIZATION).getValue()
+                .equals(HttpCredentialsHelper.generateBasicAuthHeader(clientId, clientSecret)))
+            throw new HttpException("Invalid credentials");
+
+        response.setEntity(new StringEntity("{ \"access_token\": \"" + expectedToken + "\" }", ContentType.APPLICATION_JSON));

Review Comment:
   Maybe I'm nitpicking too much, but I kind dislike havingt this quoted escapes as I think they make the code harder to read. I am **just thinking out loud on this one and you don't need to change**, but if you could do it more cleanly (here and anywhere else this is needed), I believe it would improve readability.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@camel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] CAMEL-18637: camel-http support for OAuth2 client credentials flow [camel]

Posted by "davsclaus (via GitHub)" <gi...@apache.org>.
davsclaus commented on code in PR #11628:
URL: https://github.com/apache/camel/pull/11628#discussion_r1353250496


##########
components/camel-http/src/main/java/org/apache/camel/component/http/OAuth2ClientConfigurer.java:
##########
@@ -0,0 +1,84 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.http;
+
+import org.apache.camel.util.json.DeserializationException;
+import org.apache.camel.util.json.JsonObject;
+import org.apache.camel.util.json.Jsoner;
+import org.apache.hc.client5.http.classic.HttpClient;
+import org.apache.hc.client5.http.classic.methods.HttpPost;
+import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
+import org.apache.hc.core5.http.ContentType;
+import org.apache.hc.core5.http.EntityDetails;
+import org.apache.hc.core5.http.HttpException;
+import org.apache.hc.core5.http.HttpHeaders;
+import org.apache.hc.core5.http.HttpRequest;
+import org.apache.hc.core5.http.io.entity.EntityUtils;
+import org.apache.hc.core5.http.io.entity.StringEntity;
+import org.apache.hc.core5.http.protocol.HttpContext;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class OAuth2ClientConfigurer implements HttpClientConfigurer {
+
+    private static final Logger LOG = LoggerFactory.getLogger(OAuth2ClientConfigurer.class);
+
+    private final String clientId;
+    private final String clientSecret;
+    private final String tokenEndpoint;
+
+    public OAuth2ClientConfigurer(String clientId, String clientSecret, String tokenEndpoint) {
+        this.clientId = clientId;
+        this.clientSecret = clientSecret;
+        this.tokenEndpoint = tokenEndpoint;
+    }
+
+    @Override
+    public void configureHttpClient(HttpClientBuilder clientBuilder) {
+        HttpClient httpClient = clientBuilder.build();
+        clientBuilder.addRequestInterceptorFirst((HttpRequest request, EntityDetails entity, HttpContext context) -> {
+
+            final HttpPost httpPost = new HttpPost(tokenEndpoint);
+
+            httpPost.addHeader(HttpHeaders.AUTHORIZATION,
+                    HttpCredentialsHelper.generateBasicAuthHeader(clientId, clientSecret));
+            httpPost.setEntity(new StringEntity("grant_type=client_credentials", ContentType.APPLICATION_FORM_URLENCODED));
+
+            httpClient.execute(httpPost, response -> {
+
+                try {
+                    String responseString = EntityUtils.toString(response.getEntity());
+
+                    if (response.getCode() == 200) {
+                        String accessToken = ((JsonObject) Jsoner.deserialize(responseString)).getString("access_token");
+                        request.addHeader(HttpHeaders.AUTHORIZATION, accessToken);
+                    } else {
+                        LOG.error("Received error response from token request with Status Code: {} Http Body: {}", response.getCode(), responseString);

Review Comment:
   I think its harsh to do an ERROR log for a obtaining a token that failed, eg it could be a server temporary out of service etc. I would suggest to not log, but to have the http status code in the thrown exception.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@camel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] CAMEL-18637: camel-http support for OAuth2 client credentials flow [camel]

Posted by "davsclaus (via GitHub)" <gi...@apache.org>.
davsclaus commented on code in PR #11628:
URL: https://github.com/apache/camel/pull/11628#discussion_r1354305726


##########
components/camel-http/src/main/java/org/apache/camel/component/http/OAuth2ClientConfigurer.java:
##########
@@ -0,0 +1,79 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.http;
+
+import org.apache.camel.util.json.DeserializationException;
+import org.apache.camel.util.json.JsonObject;
+import org.apache.camel.util.json.Jsoner;
+import org.apache.hc.client5.http.classic.HttpClient;
+import org.apache.hc.client5.http.classic.methods.HttpPost;
+import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
+import org.apache.hc.core5.http.ContentType;
+import org.apache.hc.core5.http.EntityDetails;
+import org.apache.hc.core5.http.HttpException;
+import org.apache.hc.core5.http.HttpHeaders;
+import org.apache.hc.core5.http.HttpRequest;
+import org.apache.hc.core5.http.io.entity.EntityUtils;
+import org.apache.hc.core5.http.io.entity.StringEntity;
+import org.apache.hc.core5.http.protocol.HttpContext;
+
+public class OAuth2ClientConfigurer implements HttpClientConfigurer {
+
+    private final String clientId;
+    private final String clientSecret;
+    private final String tokenEndpoint;
+
+    public OAuth2ClientConfigurer(String clientId, String clientSecret, String tokenEndpoint) {
+        this.clientId = clientId;
+        this.clientSecret = clientSecret;
+        this.tokenEndpoint = tokenEndpoint;
+    }
+
+    @Override
+    public void configureHttpClient(HttpClientBuilder clientBuilder) {
+        HttpClient httpClient = clientBuilder.build();
+        clientBuilder.addRequestInterceptorFirst((HttpRequest request, EntityDetails entity, HttpContext context) -> {
+
+            final HttpPost httpPost = new HttpPost(tokenEndpoint);
+
+            httpPost.addHeader(HttpHeaders.AUTHORIZATION,
+                    HttpCredentialsHelper.generateBasicAuthHeader(clientId, clientSecret));
+            httpPost.setEntity(new StringEntity("grant_type=client_credentials", ContentType.APPLICATION_FORM_URLENCODED));
+
+            httpClient.execute(httpPost, response -> {
+
+                try {
+                    String responseString = EntityUtils.toString(response.getEntity());
+
+                    if (response.getCode() == 200) {
+                        String accessToken = ((JsonObject) Jsoner.deserialize(responseString)).getString("access_token");
+                        request.addHeader(HttpHeaders.AUTHORIZATION, accessToken);
+                    } else {
+                        throw new HttpException("Received error response from token request with Status Code: {}", response.getCode());

Review Comment:
   This exception text is a bit wrong - its not use log {} placeholders. 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@camel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] CAMEL-18637: OAuth2 authentication with client credentials flow [camel]

Posted by "gilvansfilho (via GitHub)" <gi...@apache.org>.
gilvansfilho commented on PR #11628:
URL: https://github.com/apache/camel/pull/11628#issuecomment-1744832811

   > You need to fully build the project. There are uncommitted changes. I'll take a look later
   
   Pushed missing files.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@camel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] CAMEL-18637: camel-http support for OAuth2 client credentials flow [camel]

Posted by "davsclaus (via GitHub)" <gi...@apache.org>.
davsclaus merged PR #11628:
URL: https://github.com/apache/camel/pull/11628


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@camel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] CAMEL-18637: camel-http support for OAuth2 client credentials flow [camel]

Posted by "gilvansfilho (via GitHub)" <gi...@apache.org>.
gilvansfilho commented on PR #11628:
URL: https://github.com/apache/camel/pull/11628#issuecomment-1756105551

    I think I'm done. Could someone take a look?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@camel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] CAMEL-18637: camel-http support for OAuth2 client credentials flow [camel]

Posted by "davsclaus (via GitHub)" <gi...@apache.org>.
davsclaus commented on PR #11628:
URL: https://github.com/apache/camel/pull/11628#issuecomment-1758596843

   Ok PR is now passing


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@camel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org