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 55d4dd19ed6b CAMEL-27187: camel-google - OAuth login fails when scopes 
are set (#30501)
55d4dd19ed6b is described below

commit 55d4dd19ed6b6a5a2009466525525dc69194cc31
Author: Guillaume Nodet <[email protected]>
AuthorDate: Mon Nov 23 12:13:07 2026 +0100

    CAMEL-27187: camel-google - OAuth login fails when scopes are set (#30501)
    
    createOAuthCredential() called .setServiceAccountScopes(scopes) on a
    GoogleCredential.Builder that has no service account private key.
    GoogleCredential's constructor enforces that serviceAccountScopes must
    be null when serviceAccountPrivateKey is null, so any non-null scope
    collection triggered an IllegalArgumentException.
    
    For OAuth credentials the scopes are fixed at consent time (when the
    refresh token is obtained); they are not passed when refreshing an
    access token. Remove the .setServiceAccountScopes(scopes) call. The
    @param scopes Javadoc of both getOAuthCredential overloads says the
    scopes only apply to service account and default credentials.
    
    This always broke google-calendar-stream with OAuth (its configuration
    defaults scopes to CalendarScopes.CALENDAR), and broke all other Google
    components (mail, sheets, drive, calendar) whenever the user set scopes
    together with OAuth credentials.
    
    Co-authored-by: Claude Sonnet 4.6 <[email protected]>
---
 .../google/common/GoogleCredentialsHelper.java     |  16 ++--
 .../google/common/GoogleCredentialsHelperTest.java | 103 ++++++++++++++++++++-
 2 files changed, 112 insertions(+), 7 deletions(-)

diff --git 
a/components/camel-google/camel-google-common/src/main/java/org/apache/camel/component/google/common/GoogleCredentialsHelper.java
 
b/components/camel-google/camel-google-common/src/main/java/org/apache/camel/component/google/common/GoogleCredentialsHelper.java
index 4c32d5ad8d65..2e591f460ee4 100644
--- 
a/components/camel-google/camel-google-common/src/main/java/org/apache/camel/component/google/common/GoogleCredentialsHelper.java
+++ 
b/components/camel-google/camel-google-common/src/main/java/org/apache/camel/component/google/common/GoogleCredentialsHelper.java
@@ -143,7 +143,9 @@ public final class GoogleCredentialsHelper {
      *
      * @param  context          the Camel context for resource resolution
      * @param  config           the component configuration
-     * @param  scopes           OAuth scopes to apply
+     * @param  scopes           OAuth scopes to apply to service account and 
Application Default Credentials; ignored
+     *                          for OAuth 2.0 credentials (client ID/secret) 
because their granted scopes are fixed at
+     *                          consent time
      * @param  httpTransport    HTTP transport to use (pass null for default)
      * @param  jsonFactory      JSON factory to use (pass null for default)
      * @return                  Google credential for use with legacy API 
clients
@@ -163,7 +165,7 @@ public final class GoogleCredentialsHelper {
 
         // Check for OAuth 2.0 credentials first
         if (hasOAuthCredentials(config)) {
-            return createOAuthCredential(config, scopes, transport, factory);
+            return createOAuthCredential(config, transport, factory);
         }
 
         // Check for Service Account credentials
@@ -186,7 +188,9 @@ public final class GoogleCredentialsHelper {
      *
      * @param  context     the Camel context for resource resolution
      * @param  config      the component configuration
-     * @param  scopes      OAuth scopes to apply
+     * @param  scopes      OAuth scopes to apply to service account and 
Application Default Credentials; ignored for
+     *                     OAuth 2.0 credentials (client ID/secret) because 
their granted scopes are fixed at consent
+     *                     time
      * @return             Google credential for use with legacy API clients
      * @throws IOException if credentials cannot be loaded
      */
@@ -209,11 +213,12 @@ public final class GoogleCredentialsHelper {
     }
 
     /**
-     * Creates OAuth 2.0 credential from client ID, secret, and tokens.
+     * Creates OAuth 2.0 credential from client ID, secret, and tokens. Scopes 
are not set here because for OAuth
+     * credentials the granted scopes are fixed at consent time (when the 
refresh token was obtained) and are not needed
+     * when building or refreshing the credential.
      */
     private static Credential createOAuthCredential(
             GoogleCommonConfiguration config,
-            Collection<String> scopes,
             HttpTransport transport,
             JsonFactory jsonFactory) {
 
@@ -221,7 +226,6 @@ public final class GoogleCredentialsHelper {
                 .setJsonFactory(jsonFactory)
                 .setTransport(transport)
                 .setClientSecrets(config.getClientId(), 
config.getClientSecret())
-                .setServiceAccountScopes(scopes)
                 .build();
 
         if (ObjectHelper.isNotEmpty(config.getRefreshToken())) {
diff --git 
a/components/camel-google/camel-google-common/src/test/java/org/apache/camel/component/google/common/GoogleCredentialsHelperTest.java
 
b/components/camel-google/camel-google-common/src/test/java/org/apache/camel/component/google/common/GoogleCredentialsHelperTest.java
index a29e7004fb7a..3264f69b525f 100644
--- 
a/components/camel-google/camel-google-common/src/test/java/org/apache/camel/component/google/common/GoogleCredentialsHelperTest.java
+++ 
b/components/camel-google/camel-google-common/src/test/java/org/apache/camel/component/google/common/GoogleCredentialsHelperTest.java
@@ -20,6 +20,7 @@ import java.util.Arrays;
 import java.util.Collection;
 import java.util.List;
 
+import com.google.api.client.auth.oauth2.Credential;
 import com.google.auth.Credentials;
 import com.google.auth.oauth2.ExternalAccountCredentials;
 import com.google.auth.oauth2.ImpersonatedCredentials;
@@ -29,6 +30,7 @@ import org.junit.jupiter.api.AfterEach;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 
+import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertInstanceOf;
 import static org.junit.jupiter.api.Assertions.assertNotNull;
 import static org.junit.jupiter.api.Assertions.assertNull;
@@ -107,8 +109,67 @@ class GoogleCredentialsHelperTest {
         assertNull(config.getImpersonatedServiceAccount());
     }
 
+    // ==================== OAuth regression tests (CAMEL-27187) 
====================
+
+    /**
+     * Regression test for CAMEL-27187: createOAuthCredential must not call 
setServiceAccountScopes when building an
+     * OAuth credential. GoogleCredential rejects service-account scopes when 
no private key is present, throwing
+     * IllegalArgumentException.
+     * <p>
+     * This test covers the base case: OAuth with explicit scopes must succeed.
+     */
+    @Test
+    void testOAuthCredentialWithScopesDoesNotThrow() throws Exception {
+        TestConfig config = new TestConfig();
+        config.setClientId("test-client-id");
+        config.setClientSecret("test-client-secret");
+        config.setRefreshToken("test-refresh-token");
+
+        List<String> scopes = 
Arrays.asList("https://www.googleapis.com/auth/calendar";);
+        // Before the fix this threw IllegalArgumentException from 
GoogleCredential.<init>
+        Credential credential = 
GoogleCredentialsHelper.getOAuthCredential(context, config, scopes);
+
+        assertNotNull(credential);
+        assertEquals("test-refresh-token", credential.getRefreshToken());
+    }
+
     /**
-     * Test configuration implementing GoogleCommonConfiguration with WIF 
support.
+     * Regression test for CAMEL-27187: google-calendar-stream always provides 
non-null scopes (defaulting to
+     * CalendarScopes.CALENDAR), so every OAuth login failed before the fix.
+     */
+    @Test
+    void testOAuthCredentialWithMultipleScopesDoesNotThrow() throws Exception {
+        TestConfig config = new TestConfig();
+        config.setClientId("test-client-id");
+        config.setClientSecret("test-client-secret");
+        config.setAccessToken("test-access-token");
+
+        List<String> scopes = Arrays.asList(
+                "https://www.googleapis.com/auth/calendar";,
+                "https://www.googleapis.com/auth/gmail.modify";,
+                "https://www.googleapis.com/auth/drive";);
+        Credential credential = 
GoogleCredentialsHelper.getOAuthCredential(context, config, scopes);
+
+        assertNotNull(credential);
+    }
+
+    /**
+     * OAuth with null scopes must continue to work (pre-existing behaviour).
+     */
+    @Test
+    void testOAuthCredentialWithNullScopesDoesNotThrow() throws Exception {
+        TestConfig config = new TestConfig();
+        config.setClientId("test-client-id");
+        config.setClientSecret("test-client-secret");
+        config.setRefreshToken("test-refresh-token");
+
+        Credential credential = 
GoogleCredentialsHelper.getOAuthCredential(context, config, null);
+
+        assertNotNull(credential);
+    }
+
+    /**
+     * Test configuration implementing GoogleCommonConfiguration with WIF and 
OAuth support.
      */
     static class TestConfig implements GoogleCommonConfiguration {
         private String serviceAccountKey;
@@ -116,6 +177,10 @@ class GoogleCredentialsHelperTest {
         private String workloadIdentityConfig;
         private String impersonatedServiceAccount;
         private boolean authenticate = true;
+        private String clientId;
+        private String clientSecret;
+        private String accessToken;
+        private String refreshToken;
 
         @Override
         public String getServiceAccountKey() {
@@ -162,6 +227,42 @@ class GoogleCredentialsHelperTest {
             this.authenticate = authenticate;
         }
 
+        @Override
+        public String getClientId() {
+            return clientId;
+        }
+
+        public void setClientId(String clientId) {
+            this.clientId = clientId;
+        }
+
+        @Override
+        public String getClientSecret() {
+            return clientSecret;
+        }
+
+        public void setClientSecret(String clientSecret) {
+            this.clientSecret = clientSecret;
+        }
+
+        @Override
+        public String getAccessToken() {
+            return accessToken;
+        }
+
+        public void setAccessToken(String accessToken) {
+            this.accessToken = accessToken;
+        }
+
+        @Override
+        public String getRefreshToken() {
+            return refreshToken;
+        }
+
+        public void setRefreshToken(String refreshToken) {
+            this.refreshToken = refreshToken;
+        }
+
         @Override
         public Collection<String> getScopesAsList() {
             return null;

Reply via email to