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 8c19b8d830e CAMEL-21712: Add rfc8707 support to http component (#17116)
8c19b8d830e is described below
commit 8c19b8d830e6ed858044cace703e4de08624aa1f
Author: Mikael Andersson Wigander
<[email protected]>
AuthorDate: Sun Feb 16 13:47:26 2025 +0100
CAMEL-21712: Add rfc8707 support to http component (#17116)
* First commit for RFC8707
* auto-generated changes
* fix: wrong name on argument for the resource indicator
* small change to trigger a push
* fix: correcting variable name
---
.../apache/camel/http/common/HttpConfiguration.java | 20 ++++++++++++++++++++
.../camel-http/src/main/docs/http-component.adoc | 16 ++++++++++++++++
.../apache/camel/component/http/HttpComponent.java | 2 ++
.../camel/component/http/OAuth2ClientConfigurer.java | 11 +++++++++--
.../endpoint/dsl/HttpEndpointBuilderFactory.java | 15 +++++++++++++++
5 files changed, 62 insertions(+), 2 deletions(-)
diff --git
a/components/camel-http-common/src/main/java/org/apache/camel/http/common/HttpConfiguration.java
b/components/camel-http-common/src/main/java/org/apache/camel/http/common/HttpConfiguration.java
index 4975645a296..5f859b5cc15 100644
---
a/components/camel-http-common/src/main/java/org/apache/camel/http/common/HttpConfiguration.java
+++
b/components/camel-http-common/src/main/java/org/apache/camel/http/common/HttpConfiguration.java
@@ -42,6 +42,8 @@ public class HttpConfiguration implements Serializable {
private String oauth2TokenEndpoint;
@Metadata(label = "producer,security", description = "OAuth2 scope")
private String oauth2Scope;
+ @Metadata(label = "producer,security", description = "OAuth2 Resource
Indicator")
+ private String oauth2ResourceIndicator;
@UriParam(label = "producer,security", defaultValue = "false",
description = "Whether to cache OAuth2 client tokens.")
private boolean oauth2CacheTokens = false;
@@ -324,4 +326,22 @@ public class HttpConfiguration implements Serializable {
public void setOauth2CachedTokensExpirationMarginSeconds(long
oauth2CachedTokensExpirationMarginSeconds) {
this.oauth2CachedTokensExpirationMarginSeconds =
oauth2CachedTokensExpirationMarginSeconds;
}
+
+ /**
+ * Gets oauth 2 resource indicator.
+ *
+ * @return the oauth 2 resource indicator
+ */
+ public String getOauth2ResourceIndicator() {
+ return oauth2ResourceIndicator;
+ }
+
+ /**
+ * Sets oauth 2 resource indicator.
+ *
+ * @param oauth2ResourceIndicator the oauth 2 resource indicator
+ */
+ public void setOauth2ResourceIndicator(final String
oauth2ResourceIndicator) {
+ this.oauth2ResourceIndicator = oauth2ResourceIndicator;
+ }
}
diff --git a/components/camel-http/src/main/docs/http-component.adoc
b/components/camel-http/src/main/docs/http-component.adoc
index b2f8d16fa92..01df39e161a 100644
--- a/components/camel-http/src/main/docs/http-component.adoc
+++ b/components/camel-http/src/main/docs/http-component.adoc
@@ -361,6 +361,22 @@ from("direct:start")
.to("https://localhost:9090/?oauth2ClientId=" + clientId +
"&oauth2ClientSecret=" + clientSecret + "&oauth2TokenEndpoint=" + tokenEndpoint
+ "&oauth2Scope=" + scope);
------------------------------------------------------------------------------------
+Additional support for OAuth2 is for RFC 8707 where a _Resource Indicator_
must be provided in the body:
+
+[source,java]
+------------------------------------------------------------------------------------
+String clientId = "my-client-id";
+String clientSecret = "my-client-secret";
+String tokenEndpoint =
"https://localhost:8080/realms/master/protocol/openid-connect/token";
+String scope = "my-scope"; // optional scope
+String resourceIndicator = "https://localhost:9090"; // optional, for RFC 8707
+
+from("direct:start")
+ .to("https://localhost:9090/?oauth2ClientId=" + clientId +
"&oauth2ClientSecret=" + clientSecret + "&oauth2TokenEndpoint=" + tokenEndpoint
+ "&oauth2Scope=" + scope + "&resource=" + resourceIndicator);
+------------------------------------------------------------------------------------
+
+NOTE: Resource Indicator is the URL to the actual endpoint as defined in the
component URI.
+
[NOTE]
Camel only provides support for OAuth2 client credentials flow
diff --git
a/components/camel-http/src/main/java/org/apache/camel/component/http/HttpComponent.java
b/components/camel-http/src/main/java/org/apache/camel/component/http/HttpComponent.java
index 559453b003f..76862f38d26 100644
---
a/components/camel-http/src/main/java/org/apache/camel/component/http/HttpComponent.java
+++
b/components/camel-http/src/main/java/org/apache/camel/component/http/HttpComponent.java
@@ -244,6 +244,7 @@ public class HttpComponent extends HttpCommonComponent
implements RestProducerFa
String clientSecret = getParameter(parameters, "oauth2ClientSecret",
String.class);
String tokenEndpoint = getParameter(parameters, "oauth2TokenEndpoint",
String.class);
String scope = getParameter(parameters, "oauth2Scope", String.class);
+ String resourceIndicator = getParameter(parameters,
"oauth2ResourceIndicator", String.class);
HttpConfiguration configDefaults = new HttpConfiguration();
boolean cacheTokens = getParameter(
parameters,
@@ -267,6 +268,7 @@ public class HttpComponent extends HttpCommonComponent
implements RestProducerFa
clientId,
clientSecret,
tokenEndpoint,
+ resourceIndicator,
scope,
cacheTokens,
cachedTokensDefaultExpirySeconds,
diff --git
a/components/camel-http/src/main/java/org/apache/camel/component/http/OAuth2ClientConfigurer.java
b/components/camel-http/src/main/java/org/apache/camel/component/http/OAuth2ClientConfigurer.java
index ee29c73b8c6..3e1291f1903 100644
---
a/components/camel-http/src/main/java/org/apache/camel/component/http/OAuth2ClientConfigurer.java
+++
b/components/camel-http/src/main/java/org/apache/camel/component/http/OAuth2ClientConfigurer.java
@@ -49,12 +49,15 @@ public class OAuth2ClientConfigurer implements
HttpClientConfigurer {
private final Long cachedTokensDefaultExpirySeconds;
private final Long cachedTokensExpirationMarginSeconds;
private final static Map<OAuth2URIAndCredentials, TokenCache> tokenCache =
new HashMap<>();
+ private final String resourceIndicator;
- public OAuth2ClientConfigurer(String clientId, String clientSecret, String
tokenEndpoint, String scope, boolean cacheTokens,
+ public OAuth2ClientConfigurer(String clientId, String clientSecret, String
tokenEndpoint, String resourceIndicator,
+ String scope, boolean cacheTokens,
long cachedTokensDefaultExpirySeconds, long
cachedTokensExpirationMarginSeconds) {
this.clientId = clientId;
this.clientSecret = clientSecret;
this.tokenEndpoint = tokenEndpoint;
+ this.resourceIndicator = resourceIndicator;
this.scope = scope;
this.cacheTokens = cacheTokens;
this.cachedTokensDefaultExpirySeconds =
cachedTokensDefaultExpirySeconds;
@@ -91,6 +94,7 @@ public class OAuth2ClientConfigurer implements
HttpClientConfigurer {
}
private JsonObject getAccessTokenResponse(HttpClient httpClient) throws
IOException {
+ String bodyStr = "grant_type=client_credentials";
String url = tokenEndpoint;
if (scope != null) {
String sep = "?";
@@ -104,7 +108,10 @@ public class OAuth2ClientConfigurer implements
HttpClientConfigurer {
httpPost.addHeader(HttpHeaders.AUTHORIZATION,
HttpCredentialsHelper.generateBasicAuthHeader(clientId,
clientSecret));
- httpPost.setEntity(new StringEntity("grant_type=client_credentials",
ContentType.APPLICATION_FORM_URLENCODED));
+ if (null != resourceIndicator) {
+ bodyStr = String.join(bodyStr, "&resource=" + resourceIndicator);
+ }
+ httpPost.setEntity(new StringEntity(bodyStr,
ContentType.APPLICATION_FORM_URLENCODED));
AtomicReference<JsonObject> result = new AtomicReference<>();
httpClient.execute(httpPost, response -> {
diff --git
a/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/HttpEndpointBuilderFactory.java
b/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/HttpEndpointBuilderFactory.java
index 4eab7411484..c191da74009 100644
---
a/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/HttpEndpointBuilderFactory.java
+++
b/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/HttpEndpointBuilderFactory.java
@@ -792,6 +792,21 @@ public interface HttpEndpointBuilderFactory {
doSetProperty("oauth2TokenEndpoint", oauth2TokenEndpoint);
return this;
}
+
+ /**
+ * Oauth2 Resource Indicator.
+ *
+ * The option is a: <code>java.lang.String</code> type.
+ *
+ * Group: security
+ *
+ * @param oauth2ResourceIndicator the oauth 2 resource indicator
+ * @return the dsl builder
+ */
+ default HttpEndpointBuilder oauth2ResourceIndicator(String
oauth2ResourceIndicator) {
+ doSetProperty("oauth2ResourceIndicator", oauth2ResourceIndicator);
+ return this;
+ }
/**
* To configure security using SSLContextParameters. Important: Only
one
* instance of org.apache.camel.util.jsse.SSLContextParameters is