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 af81e090b0e4 CAMEL-27226: camel-google-drive, camel-google-mail - the
OAuth access token is sent only in the Authorization header (#30369)
af81e090b0e4 is described below
commit af81e090b0e404839ee59a3301e77c62de80b18f
Author: Claus Ibsen <[email protected]>
AuthorDate: Thu Nov 19 13:56:54 2026 +0100
CAMEL-27226: camel-google-drive, camel-google-mail - the OAuth access token
is sent only in the Authorization header (#30369)
The producers and consumers of google-drive, google-mail and
google-calendar set the options and headers on the request of the Google
API client. They no longer set the credential options (clientId,
clientSecret, accessToken, refreshToken, serviceAccountKey, delegate), so
the accessToken option is no longer sent as the access_token query
parameter; the OAuth credential of the client sends it in the
Authorization header. The list of credential options and the loop are
shared in GoogleApiRequestHelper of camel-google-common.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
.../google/calendar/GoogleCalendarConsumer.java | 10 +-
.../google/calendar/GoogleCalendarProducer.java | 10 +-
.../GoogleCalendarRequestCredentialTest.java | 145 +++++++++++++++++++
.../google/common/GoogleApiRequestHelper.java | 70 +++++++++
.../google/common/GoogleApiRequestHelperTest.java | 75 ++++++++++
.../google/drive/GoogleDriveConsumer.java | 10 +-
.../google/drive/GoogleDriveProducer.java | 10 +-
.../drive/GoogleDriveRequestCredentialTest.java | 159 +++++++++++++++++++++
.../component/google/mail/GoogleMailConsumer.java | 10 +-
.../component/google/mail/GoogleMailProducer.java | 10 +-
.../mail/GoogleMailRequestCredentialTest.java | 145 +++++++++++++++++++
.../ROOT/pages/camel-4x-upgrade-guide-4_24.adoc | 12 +-
12 files changed, 617 insertions(+), 49 deletions(-)
diff --git
a/components/camel-google/camel-google-calendar/src/main/java/org/apache/camel/component/google/calendar/GoogleCalendarConsumer.java
b/components/camel-google/camel-google-calendar/src/main/java/org/apache/camel/component/google/calendar/GoogleCalendarConsumer.java
index 231b076c0b07..5823c28a0cf0 100644
---
a/components/camel-google/camel-google-calendar/src/main/java/org/apache/camel/component/google/calendar/GoogleCalendarConsumer.java
+++
b/components/camel-google/camel-google-calendar/src/main/java/org/apache/camel/component/google/calendar/GoogleCalendarConsumer.java
@@ -17,14 +17,12 @@
package org.apache.camel.component.google.calendar;
import java.util.Map;
-import java.util.Map.Entry;
import com.google.api.client.googleapis.services.AbstractGoogleClientRequest;
import org.apache.camel.Processor;
import org.apache.camel.RuntimeCamelException;
import
org.apache.camel.component.google.calendar.internal.GoogleCalendarApiName;
-import org.apache.camel.spi.BeanIntrospection;
-import org.apache.camel.support.PluginHelper;
+import org.apache.camel.component.google.common.GoogleApiRequestHelper;
import org.apache.camel.support.component.AbstractApiConsumer;
/**
@@ -40,11 +38,7 @@ public class GoogleCalendarConsumer extends
AbstractApiConsumer<GoogleCalendarAp
protected Object doInvokeMethod(Map<String, Object> properties) throws
RuntimeCamelException {
AbstractGoogleClientRequest request = (AbstractGoogleClientRequest)
super.doInvokeMethod(properties);
try {
- BeanIntrospection beanIntrospection
- =
PluginHelper.getBeanIntrospection(getEndpoint().getCamelContext());
- for (Entry<String, Object> p : properties.entrySet()) {
- beanIntrospection.setProperty(getEndpoint().getCamelContext(),
request, p.getKey(), p.getValue());
- }
+
GoogleApiRequestHelper.configureRequest(getEndpoint().getCamelContext(),
request, properties);
return request.execute();
} catch (Exception e) {
throw new RuntimeCamelException(e);
diff --git
a/components/camel-google/camel-google-calendar/src/main/java/org/apache/camel/component/google/calendar/GoogleCalendarProducer.java
b/components/camel-google/camel-google-calendar/src/main/java/org/apache/camel/component/google/calendar/GoogleCalendarProducer.java
index b415066b3402..c0b94a47cc18 100644
---
a/components/camel-google/camel-google-calendar/src/main/java/org/apache/camel/component/google/calendar/GoogleCalendarProducer.java
+++
b/components/camel-google/camel-google-calendar/src/main/java/org/apache/camel/component/google/calendar/GoogleCalendarProducer.java
@@ -17,14 +17,12 @@
package org.apache.camel.component.google.calendar;
import java.util.Map;
-import java.util.Map.Entry;
import com.google.api.client.googleapis.services.AbstractGoogleClientRequest;
import org.apache.camel.RuntimeCamelException;
import
org.apache.camel.component.google.calendar.internal.GoogleCalendarApiName;
import
org.apache.camel.component.google.calendar.internal.GoogleCalendarPropertiesHelper;
-import org.apache.camel.spi.BeanIntrospection;
-import org.apache.camel.support.PluginHelper;
+import org.apache.camel.component.google.common.GoogleApiRequestHelper;
import org.apache.camel.support.component.AbstractApiProducer;
import org.apache.camel.support.component.ApiMethod;
@@ -42,11 +40,7 @@ public class GoogleCalendarProducer extends
AbstractApiProducer<GoogleCalendarAp
throws RuntimeCamelException {
AbstractGoogleClientRequest request = (AbstractGoogleClientRequest)
super.doInvokeMethod(method, properties);
try {
- BeanIntrospection beanIntrospection
- =
PluginHelper.getBeanIntrospection(getEndpoint().getCamelContext());
- for (Entry<String, Object> p : properties.entrySet()) {
- beanIntrospection.setProperty(getEndpoint().getCamelContext(),
request, p.getKey(), p.getValue());
- }
+
GoogleApiRequestHelper.configureRequest(getEndpoint().getCamelContext(),
request, properties);
return request.execute();
} catch (Exception e) {
throw new RuntimeCamelException(e);
diff --git
a/components/camel-google/camel-google-calendar/src/test/java/org/apache/camel/component/google/calendar/GoogleCalendarRequestCredentialTest.java
b/components/camel-google/camel-google-calendar/src/test/java/org/apache/camel/component/google/calendar/GoogleCalendarRequestCredentialTest.java
new file mode 100644
index 000000000000..77eda740a3f8
--- /dev/null
+++
b/components/camel-google/camel-google-calendar/src/test/java/org/apache/camel/component/google/calendar/GoogleCalendarRequestCredentialTest.java
@@ -0,0 +1,145 @@
+/*
+ * 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.google.calendar;
+
+import java.util.Collection;
+import java.util.List;
+import java.util.concurrent.CopyOnWriteArrayList;
+
+import com.google.api.client.auth.oauth2.Credential;
+import com.google.api.client.json.Json;
+import com.google.api.client.json.JsonFactory;
+import com.google.api.client.json.gson.GsonFactory;
+import com.google.api.client.testing.http.MockHttpTransport;
+import com.google.api.client.testing.http.MockLowLevelHttpRequest;
+import com.google.api.client.testing.http.MockLowLevelHttpResponse;
+import com.google.api.services.calendar.Calendar;
+import org.apache.camel.CamelContext;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.google.common.GoogleCredentialsHelper;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.impl.DefaultCamelContext;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+
+/**
+ * The client sends the OAuth access token in the Authorization header; the
request URL does not carry it.
+ */
+class GoogleCalendarRequestCredentialTest {
+
+ private static final String OAUTH =
"clientId=id&clientSecret=secret&accessToken=atoken";
+
+ private final RecordingTransport transport = new RecordingTransport();
+
+ @Test
+ void shouldSendTheAccessTokenOfTheProducerOnlyInTheAuthorizationHeader()
throws Exception {
+ try (CamelContext context = createContext()) {
+ Object body = context.createProducerTemplate()
+
.requestBody("google-calendar://calendars/get?calendarId=primary&" + OAUTH,
(Object) null);
+
+ assertNotNull(body);
+ assertSentWithTheTokenInTheHeaderOnly(transport.requests.get(0));
+ }
+ }
+
+ @Test
+ void shouldSendTheAccessTokenOfTheConsumerOnlyInTheAuthorizationHeader()
throws Exception {
+ try (CamelContext context = createContext()) {
+ MockEndpoint mock = context.getEndpoint("mock:result",
MockEndpoint.class);
+ mock.expectedMinimumMessageCount(1);
+ context.addRoutes(new RouteBuilder() {
+ @Override
+ public void configure() {
+
from("google-calendar://calendars/get?calendarId=primary&delay=100&" +
OAUTH).to("mock:result");
+ }
+ });
+
+ mock.assertIsSatisfied();
+ assertSentWithTheTokenInTheHeaderOnly(transport.requests.get(0));
+ }
+ }
+
+ private static void
assertSentWithTheTokenInTheHeaderOnly(MockLowLevelHttpRequest request) {
+ assertFalse(request.getUrl().contains("access_token"),
request.getUrl());
+ assertFalse(request.getUrl().contains("atoken"), request.getUrl());
+ assertEquals("Bearer atoken",
request.getFirstHeaderValue("Authorization"));
+ }
+
+ private CamelContext createContext() {
+ CamelContext context = new DefaultCamelContext();
+ context.getComponent("google-calendar", GoogleCalendarComponent.class)
+ .setClientFactory(new OAuthClientFactory(transport));
+ context.start();
+ return context;
+ }
+
+ /**
+ * Creates the client with the OAuth credential as the client factory of
the component does, on a transport that
+ * records the requests.
+ */
+ private static final class OAuthClientFactory implements
GoogleCalendarClientFactory {
+
+ private final JsonFactory jsonFactory =
GsonFactory.getDefaultInstance();
+ private final MockHttpTransport transport;
+
+ private OAuthClientFactory(MockHttpTransport transport) {
+ this.transport = transport;
+ }
+
+ @Override
+ public Calendar makeClient(
+ String clientId, String clientSecret, Collection<String>
scopes, String applicationName, String refreshToken,
+ String accessToken, String emailAddress, String p12FileName,
String user) {
+ GoogleCalendarConfiguration configuration = new
GoogleCalendarConfiguration();
+ configuration.setClientId(clientId);
+ configuration.setClientSecret(clientSecret);
+ configuration.setRefreshToken(refreshToken);
+ configuration.setAccessToken(accessToken);
+ try {
+ Credential credential
+ = GoogleCredentialsHelper.getOAuthCredential(null,
configuration, scopes, transport, jsonFactory);
+ return new Calendar.Builder(transport, jsonFactory,
credential).setApplicationName("test").build();
+ } catch (Exception e) {
+ throw new IllegalStateException(e);
+ }
+ }
+
+ @Override
+ public Calendar makeClient(
+ CamelContext camelContext, String serviceAccountKey,
Collection<String> scopes, String applicationName,
+ String delegate) {
+ throw new UnsupportedOperationException("OAuth only");
+ }
+ }
+
+ private static final class RecordingTransport extends MockHttpTransport {
+
+ private final List<MockLowLevelHttpRequest> requests = new
CopyOnWriteArrayList<>();
+
+ @Override
+ public MockLowLevelHttpRequest buildRequest(String method, String url)
{
+ MockLowLevelHttpRequest request = new MockLowLevelHttpRequest(url);
+ request.setResponse(new
MockLowLevelHttpResponse().setContentType(Json.MEDIA_TYPE)
+ .setContent("{\"id\":\"primary\"}"));
+ requests.add(request);
+ return request;
+ }
+ }
+}
diff --git
a/components/camel-google/camel-google-common/src/main/java/org/apache/camel/component/google/common/GoogleApiRequestHelper.java
b/components/camel-google/camel-google-common/src/main/java/org/apache/camel/component/google/common/GoogleApiRequestHelper.java
new file mode 100644
index 000000000000..ac81de3c0fe9
--- /dev/null
+++
b/components/camel-google/camel-google-common/src/main/java/org/apache/camel/component/google/common/GoogleApiRequestHelper.java
@@ -0,0 +1,70 @@
+/*
+ * 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.google.common;
+
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.TreeSet;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.spi.BeanIntrospection;
+import org.apache.camel.support.PluginHelper;
+
+/**
+ * Sets the options and headers of an exchange on the request that a method of
a Google API client builds, for the
+ * components that are generated from a Google API client (Calendar, Drive,
Mail).
+ */
+public final class GoogleApiRequestHelper {
+
+ /**
+ * The options that make up the credential of the client. The client sends
the credential in the Authorization
+ * header, so they are not set on the request, where accessToken would
become the access_token query parameter.
+ * p12FileName is the key file of a service account (Calendar). The names
are matched ignoring case, as a header
+ * such as CamelGoogleDrive.AccessToken sets the same property as
accessToken.
+ */
+ public static final Set<String> CREDENTIAL_OPTIONS;
+
+ static {
+ Set<String> names = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
+ names.addAll(List.of("clientId", "clientSecret", "accessToken",
"refreshToken", "serviceAccountKey", "delegate",
+ "p12FileName"));
+ CREDENTIAL_OPTIONS = Collections.unmodifiableSet(names);
+ }
+
+ private GoogleApiRequestHelper() {
+ }
+
+ /**
+ * Sets the given properties on the request, such as the optional
parameters of the method and the standard
+ * parameters of the Google API, except the options of the credential (see
{@link #CREDENTIAL_OPTIONS}).
+ *
+ * @param camelContext the Camel context
+ * @param request the request built by the method of the API client
+ * @param properties the options of the endpoint and the headers of the
exchange
+ */
+ public static void configureRequest(CamelContext camelContext, Object
request, Map<String, Object> properties)
+ throws Exception {
+ BeanIntrospection beanIntrospection =
PluginHelper.getBeanIntrospection(camelContext);
+ for (Map.Entry<String, Object> p : properties.entrySet()) {
+ if (!CREDENTIAL_OPTIONS.contains(p.getKey())) {
+ beanIntrospection.setProperty(camelContext, request,
p.getKey(), p.getValue());
+ }
+ }
+ }
+}
diff --git
a/components/camel-google/camel-google-common/src/test/java/org/apache/camel/component/google/common/GoogleApiRequestHelperTest.java
b/components/camel-google/camel-google-common/src/test/java/org/apache/camel/component/google/common/GoogleApiRequestHelperTest.java
new file mode 100644
index 000000000000..39fd3d318306
--- /dev/null
+++
b/components/camel-google/camel-google-common/src/test/java/org/apache/camel/component/google/common/GoogleApiRequestHelperTest.java
@@ -0,0 +1,75 @@
+/*
+ * 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.google.common;
+
+import java.util.Map;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.impl.DefaultCamelContext;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
+
+class GoogleApiRequestHelperTest {
+
+ @Test
+ void shouldSetTheParametersButNotTheCredentialOptions() throws Exception {
+ try (CamelContext context = new DefaultCamelContext()) {
+ context.start();
+ Request request = new Request();
+
+ GoogleApiRequestHelper.configureRequest(context, request,
+ Map.of("AccessToken", "atoken", "clientSecret", "secret",
"fields", "id,name"));
+
+ assertEquals("id,name", request.getFields());
+ assertNull(request.getAccessToken());
+ assertNull(request.getClientSecret());
+ }
+ }
+
+ public static class Request {
+
+ private String accessToken;
+ private String clientSecret;
+ private String fields;
+
+ public String getAccessToken() {
+ return accessToken;
+ }
+
+ public void setAccessToken(String accessToken) {
+ this.accessToken = accessToken;
+ }
+
+ public String getClientSecret() {
+ return clientSecret;
+ }
+
+ public void setClientSecret(String clientSecret) {
+ this.clientSecret = clientSecret;
+ }
+
+ public String getFields() {
+ return fields;
+ }
+
+ public void setFields(String fields) {
+ this.fields = fields;
+ }
+ }
+}
diff --git
a/components/camel-google/camel-google-drive/src/main/java/org/apache/camel/component/google/drive/GoogleDriveConsumer.java
b/components/camel-google/camel-google-drive/src/main/java/org/apache/camel/component/google/drive/GoogleDriveConsumer.java
index 1a18f73eeff4..4bfa611dde02 100644
---
a/components/camel-google/camel-google-drive/src/main/java/org/apache/camel/component/google/drive/GoogleDriveConsumer.java
+++
b/components/camel-google/camel-google-drive/src/main/java/org/apache/camel/component/google/drive/GoogleDriveConsumer.java
@@ -18,15 +18,13 @@ package org.apache.camel.component.google.drive;
import java.io.InputStream;
import java.util.Map;
-import java.util.Map.Entry;
import com.google.api.client.googleapis.services.AbstractGoogleClientRequest;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.RuntimeCamelException;
+import org.apache.camel.component.google.common.GoogleApiRequestHelper;
import org.apache.camel.component.google.drive.internal.GoogleDriveApiName;
-import org.apache.camel.spi.BeanIntrospection;
-import org.apache.camel.support.PluginHelper;
import org.apache.camel.support.SynchronizationAdapter;
import org.apache.camel.support.component.AbstractApiConsumer;
import org.apache.camel.util.IOHelper;
@@ -43,11 +41,7 @@ public class GoogleDriveConsumer extends
AbstractApiConsumer<GoogleDriveApiName,
protected Object doInvokeMethod(Map<String, Object> properties) throws
RuntimeCamelException {
AbstractGoogleClientRequest<?> request =
(AbstractGoogleClientRequest<?>) super.doInvokeMethod(properties);
try {
- BeanIntrospection beanIntrospection
- =
PluginHelper.getBeanIntrospection(getEndpoint().getCamelContext());
- for (Entry<String, Object> p : properties.entrySet()) {
- beanIntrospection.setProperty(getEndpoint().getCamelContext(),
request, p.getKey(), p.getValue());
- }
+
GoogleApiRequestHelper.configureRequest(getEndpoint().getCamelContext(),
request, properties);
return GoogleDriveRequestHelper.execute(request);
} catch (Exception e) {
throw new RuntimeCamelException(e);
diff --git
a/components/camel-google/camel-google-drive/src/main/java/org/apache/camel/component/google/drive/GoogleDriveProducer.java
b/components/camel-google/camel-google-drive/src/main/java/org/apache/camel/component/google/drive/GoogleDriveProducer.java
index 8f6ac342a39d..6cd0e1922df6 100644
---
a/components/camel-google/camel-google-drive/src/main/java/org/apache/camel/component/google/drive/GoogleDriveProducer.java
+++
b/components/camel-google/camel-google-drive/src/main/java/org/apache/camel/component/google/drive/GoogleDriveProducer.java
@@ -17,15 +17,13 @@
package org.apache.camel.component.google.drive;
import java.util.Map;
-import java.util.Map.Entry;
import com.google.api.client.googleapis.services.AbstractGoogleClientRequest;
import org.apache.camel.RuntimeCamelException;
+import org.apache.camel.component.google.common.GoogleApiRequestHelper;
import org.apache.camel.component.google.drive.internal.GoogleDriveApiName;
import org.apache.camel.component.google.drive.internal.GoogleDriveConstants;
import
org.apache.camel.component.google.drive.internal.GoogleDrivePropertiesHelper;
-import org.apache.camel.spi.BeanIntrospection;
-import org.apache.camel.support.PluginHelper;
import org.apache.camel.support.component.AbstractApiProducer;
import org.apache.camel.support.component.ApiMethod;
@@ -44,11 +42,7 @@ public class GoogleDriveProducer extends
AbstractApiProducer<GoogleDriveApiName,
AbstractGoogleClientRequest<?> request
= (AbstractGoogleClientRequest<?>)
super.doInvokeMethod(method, properties);
try {
- BeanIntrospection beanIntrospection
- =
PluginHelper.getBeanIntrospection(getEndpoint().getCamelContext());
- for (Entry<String, Object> p : properties.entrySet()) {
- beanIntrospection.setProperty(getEndpoint().getCamelContext(),
request, p.getKey(), p.getValue());
- }
+
GoogleApiRequestHelper.configureRequest(getEndpoint().getCamelContext(),
request, properties);
// the content of a media request is an InputStream, which the
reader of the body closes: stream caching
// in a route, or the caller of a ProducerTemplate, which may read
it after the exchange is done
return GoogleDriveRequestHelper.execute(request);
diff --git
a/components/camel-google/camel-google-drive/src/test/java/org/apache/camel/component/google/drive/GoogleDriveRequestCredentialTest.java
b/components/camel-google/camel-google-drive/src/test/java/org/apache/camel/component/google/drive/GoogleDriveRequestCredentialTest.java
new file mode 100644
index 000000000000..ed9a8d763737
--- /dev/null
+++
b/components/camel-google/camel-google-drive/src/test/java/org/apache/camel/component/google/drive/GoogleDriveRequestCredentialTest.java
@@ -0,0 +1,159 @@
+/*
+ * 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.google.drive;
+
+import java.util.Collection;
+import java.util.List;
+import java.util.concurrent.CopyOnWriteArrayList;
+
+import com.google.api.client.auth.oauth2.Credential;
+import com.google.api.client.json.Json;
+import com.google.api.client.json.JsonFactory;
+import com.google.api.client.json.gson.GsonFactory;
+import com.google.api.client.testing.http.MockHttpTransport;
+import com.google.api.client.testing.http.MockLowLevelHttpRequest;
+import com.google.api.client.testing.http.MockLowLevelHttpResponse;
+import com.google.api.services.drive.Drive;
+import org.apache.camel.CamelContext;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.google.common.GoogleCredentialsHelper;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.impl.DefaultCamelContext;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+
+/**
+ * The client sends the OAuth access token in the Authorization header; the
request URL does not carry it.
+ */
+class GoogleDriveRequestCredentialTest {
+
+ private static final String OAUTH =
"clientId=id&clientSecret=secret&accessToken=atoken";
+
+ private final RecordingTransport transport = new RecordingTransport();
+
+ @Test
+ void shouldSendTheAccessTokenOfTheProducerOnlyInTheAuthorizationHeader()
throws Exception {
+ try (CamelContext context = createContext()) {
+ Object body
+ =
context.createProducerTemplate().requestBody("google-drive://drive-files/list?"
+ OAUTH, (Object) null);
+
+ assertNotNull(body);
+ assertSentWithTheTokenInTheHeaderOnly(transport.requests.get(0));
+ }
+ }
+
+ @Test
+ void shouldNotSendAnAccessTokenHeaderOnTheRequestWhateverItsCase() throws
Exception {
+ for (String header : List.of("CamelGoogleDrive.accessToken",
"CamelGoogleDrive.AccessToken")) {
+ transport.requests.clear();
+ try (CamelContext context = createContext()) {
+ context.createProducerTemplate().requestBodyAndHeader(
+ "google-drive://drive-files/list?" + OAUTH, null,
header, "htoken");
+
+ String url = transport.requests.get(0).getUrl();
+ assertFalse(url.contains("access_token"), header + " -> " +
url);
+ assertFalse(url.contains("htoken"), header + " -> " + url);
+ }
+ }
+ }
+
+ @Test
+ void shouldSendTheAccessTokenOfTheConsumerOnlyInTheAuthorizationHeader()
throws Exception {
+ try (CamelContext context = createContext()) {
+ MockEndpoint mock = context.getEndpoint("mock:result",
MockEndpoint.class);
+ mock.expectedMinimumMessageCount(1);
+ context.addRoutes(new RouteBuilder() {
+ @Override
+ public void configure() {
+ from("google-drive://drive-files/list?delay=100&" +
OAUTH).to("mock:result");
+ }
+ });
+
+ mock.assertIsSatisfied();
+ assertSentWithTheTokenInTheHeaderOnly(transport.requests.get(0));
+ }
+ }
+
+ private static void
assertSentWithTheTokenInTheHeaderOnly(MockLowLevelHttpRequest request) {
+ assertFalse(request.getUrl().contains("access_token"),
request.getUrl());
+ assertFalse(request.getUrl().contains("atoken"), request.getUrl());
+ assertEquals("Bearer atoken",
request.getFirstHeaderValue("Authorization"));
+ }
+
+ private CamelContext createContext() {
+ CamelContext context = new DefaultCamelContext();
+ context.getComponent("google-drive",
GoogleDriveComponent.class).setClientFactory(new OAuthClientFactory(transport));
+ context.start();
+ return context;
+ }
+
+ /**
+ * Creates the client with the OAuth credential as the client factory of
the component does, on a transport that
+ * records the requests.
+ */
+ private static final class OAuthClientFactory implements
GoogleDriveClientFactory {
+
+ private final JsonFactory jsonFactory =
GsonFactory.getDefaultInstance();
+ private final MockHttpTransport transport;
+
+ private OAuthClientFactory(MockHttpTransport transport) {
+ this.transport = transport;
+ }
+
+ @Override
+ public Drive makeClient(
+ String clientId, String clientSecret, Collection<String>
scopes, String applicationName, String refreshToken,
+ String accessToken) {
+ GoogleDriveConfiguration configuration = new
GoogleDriveConfiguration();
+ configuration.setClientId(clientId);
+ configuration.setClientSecret(clientSecret);
+ configuration.setRefreshToken(refreshToken);
+ configuration.setAccessToken(accessToken);
+ try {
+ Credential credential
+ = GoogleCredentialsHelper.getOAuthCredential(null,
configuration, scopes, transport, jsonFactory);
+ return new Drive.Builder(transport, jsonFactory,
credential).setApplicationName("test").build();
+ } catch (Exception e) {
+ throw new IllegalStateException(e);
+ }
+ }
+
+ @Override
+ public Drive makeClient(
+ CamelContext camelContext, String serviceAccountKey,
Collection<String> scopes, String applicationName,
+ String delegate) {
+ throw new UnsupportedOperationException("OAuth only");
+ }
+ }
+
+ private static final class RecordingTransport extends MockHttpTransport {
+
+ private final List<MockLowLevelHttpRequest> requests = new
CopyOnWriteArrayList<>();
+
+ @Override
+ public MockLowLevelHttpRequest buildRequest(String method, String url)
{
+ MockLowLevelHttpRequest request = new MockLowLevelHttpRequest(url);
+ request.setResponse(new
MockLowLevelHttpResponse().setContentType(Json.MEDIA_TYPE)
+ .setContent("{\"files\":[]}"));
+ requests.add(request);
+ return request;
+ }
+ }
+}
diff --git
a/components/camel-google/camel-google-mail/src/main/java/org/apache/camel/component/google/mail/GoogleMailConsumer.java
b/components/camel-google/camel-google-mail/src/main/java/org/apache/camel/component/google/mail/GoogleMailConsumer.java
index c66b5c783c0e..4a536de53ca3 100644
---
a/components/camel-google/camel-google-mail/src/main/java/org/apache/camel/component/google/mail/GoogleMailConsumer.java
+++
b/components/camel-google/camel-google-mail/src/main/java/org/apache/camel/component/google/mail/GoogleMailConsumer.java
@@ -17,14 +17,12 @@
package org.apache.camel.component.google.mail;
import java.util.Map;
-import java.util.Map.Entry;
import com.google.api.client.googleapis.services.AbstractGoogleClientRequest;
import org.apache.camel.Processor;
import org.apache.camel.RuntimeCamelException;
+import org.apache.camel.component.google.common.GoogleApiRequestHelper;
import org.apache.camel.component.google.mail.internal.GoogleMailApiName;
-import org.apache.camel.spi.BeanIntrospection;
-import org.apache.camel.support.PluginHelper;
import org.apache.camel.support.component.AbstractApiConsumer;
/**
@@ -40,11 +38,7 @@ public class GoogleMailConsumer extends
AbstractApiConsumer<GoogleMailApiName, G
protected Object doInvokeMethod(Map<String, Object> properties) throws
RuntimeCamelException {
AbstractGoogleClientRequest request = (AbstractGoogleClientRequest)
super.doInvokeMethod(properties);
try {
- BeanIntrospection beanIntrospection
- =
PluginHelper.getBeanIntrospection(getEndpoint().getCamelContext());
- for (Entry<String, Object> p : properties.entrySet()) {
- beanIntrospection.setProperty(getEndpoint().getCamelContext(),
request, p.getKey(), p.getValue());
- }
+
GoogleApiRequestHelper.configureRequest(getEndpoint().getCamelContext(),
request, properties);
return request.execute();
} catch (Exception e) {
throw new RuntimeCamelException(e);
diff --git
a/components/camel-google/camel-google-mail/src/main/java/org/apache/camel/component/google/mail/GoogleMailProducer.java
b/components/camel-google/camel-google-mail/src/main/java/org/apache/camel/component/google/mail/GoogleMailProducer.java
index 447570d76d09..0b722ec599e8 100644
---
a/components/camel-google/camel-google-mail/src/main/java/org/apache/camel/component/google/mail/GoogleMailProducer.java
+++
b/components/camel-google/camel-google-mail/src/main/java/org/apache/camel/component/google/mail/GoogleMailProducer.java
@@ -17,14 +17,12 @@
package org.apache.camel.component.google.mail;
import java.util.Map;
-import java.util.Map.Entry;
import com.google.api.client.googleapis.services.AbstractGoogleClientRequest;
import org.apache.camel.RuntimeCamelException;
+import org.apache.camel.component.google.common.GoogleApiRequestHelper;
import org.apache.camel.component.google.mail.internal.GoogleMailApiName;
import
org.apache.camel.component.google.mail.internal.GoogleMailPropertiesHelper;
-import org.apache.camel.spi.BeanIntrospection;
-import org.apache.camel.support.PluginHelper;
import org.apache.camel.support.component.AbstractApiProducer;
import org.apache.camel.support.component.ApiMethod;
@@ -42,11 +40,7 @@ public class GoogleMailProducer extends
AbstractApiProducer<GoogleMailApiName, G
throws RuntimeCamelException {
AbstractGoogleClientRequest request = (AbstractGoogleClientRequest)
super.doInvokeMethod(method, properties);
try {
- BeanIntrospection beanIntrospection
- =
PluginHelper.getBeanIntrospection(getEndpoint().getCamelContext());
- for (Entry<String, Object> p : properties.entrySet()) {
- beanIntrospection.setProperty(getEndpoint().getCamelContext(),
request, p.getKey(), p.getValue());
- }
+
GoogleApiRequestHelper.configureRequest(getEndpoint().getCamelContext(),
request, properties);
return request.execute();
} catch (Exception e) {
throw new RuntimeCamelException(e);
diff --git
a/components/camel-google/camel-google-mail/src/test/java/org/apache/camel/component/google/mail/GoogleMailRequestCredentialTest.java
b/components/camel-google/camel-google-mail/src/test/java/org/apache/camel/component/google/mail/GoogleMailRequestCredentialTest.java
new file mode 100644
index 000000000000..be5c801ff5d9
--- /dev/null
+++
b/components/camel-google/camel-google-mail/src/test/java/org/apache/camel/component/google/mail/GoogleMailRequestCredentialTest.java
@@ -0,0 +1,145 @@
+/*
+ * 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.google.mail;
+
+import java.util.Collection;
+import java.util.List;
+import java.util.concurrent.CopyOnWriteArrayList;
+
+import com.google.api.client.auth.oauth2.Credential;
+import com.google.api.client.json.Json;
+import com.google.api.client.json.JsonFactory;
+import com.google.api.client.json.gson.GsonFactory;
+import com.google.api.client.testing.http.MockHttpTransport;
+import com.google.api.client.testing.http.MockLowLevelHttpRequest;
+import com.google.api.client.testing.http.MockLowLevelHttpResponse;
+import com.google.api.services.gmail.Gmail;
+import org.apache.camel.CamelContext;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.google.common.GoogleCredentialsHelper;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.impl.DefaultCamelContext;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+
+/**
+ * The client sends the OAuth access token in the Authorization header; the
request URL does not carry it.
+ */
+class GoogleMailRequestCredentialTest {
+
+ private static final String OAUTH =
"clientId=id&clientSecret=secret&accessToken=atoken";
+
+ private final RecordingTransport transport = new RecordingTransport();
+
+ @Test
+ void shouldSendTheAccessTokenOfTheProducerOnlyInTheAuthorizationHeader()
throws Exception {
+ try (CamelContext context = createContext()) {
+ Object body
+ =
context.createProducerTemplate().requestBody("google-mail://users/getProfile?userId=me&"
+ OAUTH,
+ (Object) null);
+
+ assertNotNull(body);
+ assertSentWithTheTokenInTheHeaderOnly(transport.requests.get(0));
+ }
+ }
+
+ @Test
+ void shouldSendTheAccessTokenOfTheConsumerOnlyInTheAuthorizationHeader()
throws Exception {
+ try (CamelContext context = createContext()) {
+ MockEndpoint mock = context.getEndpoint("mock:result",
MockEndpoint.class);
+ mock.expectedMinimumMessageCount(1);
+ context.addRoutes(new RouteBuilder() {
+ @Override
+ public void configure() {
+ from("google-mail://users/getProfile?userId=me&delay=100&"
+ OAUTH).to("mock:result");
+ }
+ });
+
+ mock.assertIsSatisfied();
+ assertSentWithTheTokenInTheHeaderOnly(transport.requests.get(0));
+ }
+ }
+
+ private static void
assertSentWithTheTokenInTheHeaderOnly(MockLowLevelHttpRequest request) {
+ assertFalse(request.getUrl().contains("access_token"),
request.getUrl());
+ assertFalse(request.getUrl().contains("atoken"), request.getUrl());
+ assertEquals("Bearer atoken",
request.getFirstHeaderValue("Authorization"));
+ }
+
+ private CamelContext createContext() {
+ CamelContext context = new DefaultCamelContext();
+ context.getComponent("google-mail",
GoogleMailComponent.class).setClientFactory(new OAuthClientFactory(transport));
+ context.start();
+ return context;
+ }
+
+ /**
+ * Creates the client with the OAuth credential as the client factory of
the component does, on a transport that
+ * records the requests.
+ */
+ private static final class OAuthClientFactory implements
GoogleMailClientFactory {
+
+ private final JsonFactory jsonFactory =
GsonFactory.getDefaultInstance();
+ private final MockHttpTransport transport;
+
+ private OAuthClientFactory(MockHttpTransport transport) {
+ this.transport = transport;
+ }
+
+ @Override
+ public Gmail makeClient(
+ String clientId, String clientSecret, Collection<String>
scopes, String applicationName, String refreshToken,
+ String accessToken) {
+ GoogleMailConfiguration configuration = new
GoogleMailConfiguration();
+ configuration.setClientId(clientId);
+ configuration.setClientSecret(clientSecret);
+ configuration.setRefreshToken(refreshToken);
+ configuration.setAccessToken(accessToken);
+ try {
+ Credential credential
+ = GoogleCredentialsHelper.getOAuthCredential(null,
configuration, scopes, transport, jsonFactory);
+ return new Gmail.Builder(transport, jsonFactory,
credential).setApplicationName("test").build();
+ } catch (Exception e) {
+ throw new IllegalStateException(e);
+ }
+ }
+
+ @Override
+ public Gmail makeClient(
+ CamelContext camelContext, String serviceAccountKey,
Collection<String> scopes, String applicationName,
+ String delegate) {
+ throw new UnsupportedOperationException("OAuth only");
+ }
+ }
+
+ private static final class RecordingTransport extends MockHttpTransport {
+
+ private final List<MockLowLevelHttpRequest> requests = new
CopyOnWriteArrayList<>();
+
+ @Override
+ public MockLowLevelHttpRequest buildRequest(String method, String url)
{
+ MockLowLevelHttpRequest request = new MockLowLevelHttpRequest(url);
+ request.setResponse(new
MockLowLevelHttpResponse().setContentType(Json.MEDIA_TYPE)
+ .setContent("{\"emailAddress\":\"[email protected]\"}"));
+ requests.add(request);
+ return request;
+ }
+ }
+}
diff --git
a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
index aeff077087f8..d166f581099c 100644
--- a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
+++ b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
@@ -272,13 +272,23 @@ before the transformer, for example with `setHeader` and
a JSONPath expression.
=== camel-google-sheets - the OAuth access token is sent only in the
Authorization header
-The producer and the consumer of `google-sheets` no longer copy the
`accessToken` option onto the request as the
+The producer of `google-sheets` no longer copies the `accessToken` option onto
the request as the
`access_token` query parameter of the Google API. The access token is sent in
the `Authorization` header by the OAuth
credential of the client, as before, so the request URL no longer carries it.
The other credential options
(`clientId`, `clientSecret`, `refreshToken`, `serviceAccountKey` and
`delegate`) are not set on the request either.
A `CamelGoogleSheets.accessToken` header is ignored for the same reason; use a
separate endpoint with its own
`accessToken` to call with another token.
+=== camel-google-drive, camel-google-mail - the OAuth access token is sent
only in the Authorization header
+
+The producers of `google-drive` and `google-mail` no longer copy the
`accessToken` option onto the request as the
+`access_token` query parameter of the Google API. The access token is sent in
the `Authorization` header by the OAuth
+credential of the client, as before, so the request URL no longer carries it.
The other credential options
+(`clientId`, `clientSecret`, `refreshToken`, `serviceAccountKey` and
`delegate`) are not set on the request either.
+A `CamelGoogleDrive.accessToken` or `CamelGoogleMail.accessToken` header is
ignored for the same reason; use a
+separate endpoint with its own `accessToken` to call with another token.
`google-calendar` follows the same rule; its
+requests had no `access_token` parameter.
+
=== camel-jpa - query parameters from CamelJpaParameters header are bound
literally
In previous versions, string values supplied in the `CamelJpaParameters`
header map were evaluated as Simple expressions by the JPA producer.