This is an automated email from the ASF dual-hosted git repository. acosentino pushed a commit to branch 23038 in repository https://gitbox.apache.org/repos/asf/camel.git
commit 750017fb77d637026983afa65510bc3a8adcaa8b Author: Andrea Cosentino <[email protected]> AuthorDate: Tue Feb 24 13:14:27 2026 +0100 CAMEL-23038 - Generalize Google services authentication with common module - Google Calendar Signed-off-by: Andrea Cosentino <[email protected]> --- .../camel-google/camel-google-calendar/pom.xml | 4 ++ .../calendar/BatchGoogleCalendarClientFactory.java | 63 ++++++++-------------- .../calendar/GoogleCalendarConfiguration.java | 10 +++- .../stream/GoogleCalendarStreamConfiguration.java | 10 +++- 4 files changed, 44 insertions(+), 43 deletions(-) diff --git a/components/camel-google/camel-google-calendar/pom.xml b/components/camel-google/camel-google-calendar/pom.xml index 172e5d9b500b..4ed04c787f5d 100644 --- a/components/camel-google/camel-google-calendar/pom.xml +++ b/components/camel-google/camel-google-calendar/pom.xml @@ -41,6 +41,10 @@ </properties> <dependencies> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-google-common</artifactId> + </dependency> <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-support</artifactId> diff --git a/components/camel-google/camel-google-calendar/src/main/java/org/apache/camel/component/google/calendar/BatchGoogleCalendarClientFactory.java b/components/camel-google/camel-google-calendar/src/main/java/org/apache/camel/component/google/calendar/BatchGoogleCalendarClientFactory.java index d815b9a55841..461267497b08 100644 --- a/components/camel-google/camel-google-calendar/src/main/java/org/apache/camel/component/google/calendar/BatchGoogleCalendarClientFactory.java +++ b/components/camel-google/camel-google-calendar/src/main/java/org/apache/camel/component/google/calendar/BatchGoogleCalendarClientFactory.java @@ -17,7 +17,6 @@ package org.apache.camel.component.google.calendar; import java.io.File; -import java.io.IOException; import java.util.Collection; import com.google.api.client.auth.oauth2.Credential; @@ -29,7 +28,7 @@ import com.google.api.client.json.jackson2.JacksonFactory; import com.google.api.services.calendar.Calendar; import org.apache.camel.CamelContext; import org.apache.camel.RuntimeCamelException; -import org.apache.camel.support.ResourceHelper; +import org.apache.camel.component.google.common.GoogleCredentialsHelper; public class BatchGoogleCalendarClientFactory implements GoogleCalendarClientFactory { private NetHttpTransport transport; @@ -46,7 +45,7 @@ public class BatchGoogleCalendarClientFactory implements GoogleCalendarClientFac String accessToken, String emailAddress, String p12FileName, String user) { // if emailAddress and p12FileName values are present, assume Google - // Service Account + // Service Account with P12 file (legacy) boolean serviceAccount = null != emailAddress && !emailAddress.isEmpty() && null != p12FileName && !p12FileName.isEmpty(); @@ -57,15 +56,17 @@ public class BatchGoogleCalendarClientFactory implements GoogleCalendarClientFac try { Credential credential; if (serviceAccount) { - credential = authorizeServiceAccount(emailAddress, p12FileName, scopes, user); + // Legacy P12 file authentication - keep as is since GoogleCredentialsHelper doesn't support P12 + credential = authorizeServiceAccountWithP12(emailAddress, p12FileName, scopes, user); } else { - credential = authorize(clientId, clientSecret); - if (refreshToken != null && !refreshToken.isEmpty()) { - credential.setRefreshToken(refreshToken); - } - if (accessToken != null && !accessToken.isEmpty()) { - credential.setAccessToken(accessToken); - } + // Use GoogleCredentialsHelper for OAuth credentials + GoogleCalendarConfiguration tempConfig = new GoogleCalendarConfiguration(); + tempConfig.setClientId(clientId); + tempConfig.setClientSecret(clientSecret); + tempConfig.setRefreshToken(refreshToken); + tempConfig.setAccessToken(accessToken); + + credential = GoogleCredentialsHelper.getOAuthCredential(null, tempConfig, scopes, transport, jsonFactory); } return new Calendar.Builder(transport, jsonFactory, credential).setApplicationName(applicationName).build(); } catch (Exception e) { @@ -73,18 +74,9 @@ public class BatchGoogleCalendarClientFactory implements GoogleCalendarClientFac } } - // Authorizes the installed application to access user's protected data. - private Credential authorize(String clientId, String clientSecret) { - // authorize - return new GoogleCredential.Builder() - .setJsonFactory(jsonFactory) - .setTransport(transport) - .setClientSecrets(clientId, clientSecret) - .build(); - } - - // authorize with P12-Certificate file - private Credential authorizeServiceAccount(String emailAddress, String p12FileName, Collection<String> scopes, String user) + // authorize with P12-Certificate file (legacy method - kept for backward compatibility) + private Credential authorizeServiceAccountWithP12( + String emailAddress, String p12FileName, Collection<String> scopes, String user) throws Exception { HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport(); // set the service account user when provided @@ -106,27 +98,16 @@ public class BatchGoogleCalendarClientFactory implements GoogleCalendarClientFac throw new IllegalArgumentException("serviceAccountKey is required to create Google Calendar client."); } try { - Credential credential = authorizeServiceAccount(camelContext, serviceAccountKey, delegate, scopes); + // Use GoogleCredentialsHelper for service account JSON key + GoogleCalendarConfiguration tempConfig = new GoogleCalendarConfiguration(); + tempConfig.setServiceAccountKey(serviceAccountKey); + tempConfig.setDelegate(delegate); + + Credential credential + = GoogleCredentialsHelper.getOAuthCredential(camelContext, tempConfig, scopes, transport, jsonFactory); return new Calendar.Builder(transport, jsonFactory, credential).setApplicationName(applicationName).build(); } catch (Exception e) { throw new RuntimeCamelException("Could not create Google Calendar client.", e); } } - - // authorize with JSON-Credentials - private Credential authorizeServiceAccount( - CamelContext camelContext, String serviceAccountKey, String delegate, Collection<String> scopes) { - // authorize - try { - GoogleCredential cred = GoogleCredential - .fromStream(ResourceHelper.resolveMandatoryResourceAsInputStream(camelContext, serviceAccountKey), - transport, - jsonFactory) - .createScoped(scopes != null && !scopes.isEmpty() ? scopes : null).createDelegated(delegate); - cred.refreshToken(); - return cred; - } catch (IOException e) { - throw new RuntimeException(e); - } - } } diff --git a/components/camel-google/camel-google-calendar/src/main/java/org/apache/camel/component/google/calendar/GoogleCalendarConfiguration.java b/components/camel-google/camel-google-calendar/src/main/java/org/apache/camel/component/google/calendar/GoogleCalendarConfiguration.java index ee56a80f4d6e..1ac849eb2404 100644 --- a/components/camel-google/camel-google-calendar/src/main/java/org/apache/camel/component/google/calendar/GoogleCalendarConfiguration.java +++ b/components/camel-google/camel-google-calendar/src/main/java/org/apache/camel/component/google/calendar/GoogleCalendarConfiguration.java @@ -21,6 +21,7 @@ import java.util.List; import com.google.api.services.calendar.CalendarScopes; import org.apache.camel.component.google.calendar.internal.GoogleCalendarApiName; +import org.apache.camel.component.google.common.GoogleCommonConfiguration; import org.apache.camel.spi.Configurer; import org.apache.camel.spi.Metadata; import org.apache.camel.spi.UriParam; @@ -32,7 +33,7 @@ import org.apache.camel.spi.UriPath; */ @UriParams @Configurer(extended = true) -public class GoogleCalendarConfiguration { +public class GoogleCalendarConfiguration implements GoogleCommonConfiguration { @UriPath @Metadata(required = true) @@ -86,6 +87,7 @@ public class GoogleCalendarConfiguration { this.methodName = methodName; } + @Override public String getClientId() { return clientId; } @@ -108,6 +110,7 @@ public class GoogleCalendarConfiguration { this.emailAddress = emailAddress; } + @Override public String getClientSecret() { return clientSecret; } @@ -119,6 +122,7 @@ public class GoogleCalendarConfiguration { this.clientSecret = clientSecret; } + @Override public String getAccessToken() { return accessToken; } @@ -130,6 +134,7 @@ public class GoogleCalendarConfiguration { this.accessToken = accessToken; } + @Override public String getRefreshToken() { return refreshToken; } @@ -157,6 +162,7 @@ public class GoogleCalendarConfiguration { return scopes; } + @Override public Collection<String> getScopesAsList() { if (scopes != null) { return List.of(scopes.split(",")); @@ -198,6 +204,7 @@ public class GoogleCalendarConfiguration { this.user = user; } + @Override public String getServiceAccountKey() { return serviceAccountKey; } @@ -212,6 +219,7 @@ public class GoogleCalendarConfiguration { this.serviceAccountKey = serviceAccountKey; } + @Override public String getDelegate() { return delegate; } diff --git a/components/camel-google/camel-google-calendar/src/main/java/org/apache/camel/component/google/calendar/stream/GoogleCalendarStreamConfiguration.java b/components/camel-google/camel-google-calendar/src/main/java/org/apache/camel/component/google/calendar/stream/GoogleCalendarStreamConfiguration.java index b546bd700c71..c1059fec5484 100644 --- a/components/camel-google/camel-google-calendar/src/main/java/org/apache/camel/component/google/calendar/stream/GoogleCalendarStreamConfiguration.java +++ b/components/camel-google/camel-google-calendar/src/main/java/org/apache/camel/component/google/calendar/stream/GoogleCalendarStreamConfiguration.java @@ -21,6 +21,7 @@ import java.util.List; import com.google.api.services.calendar.CalendarScopes; import org.apache.camel.RuntimeCamelException; +import org.apache.camel.component.google.common.GoogleCommonConfiguration; import org.apache.camel.spi.Metadata; import org.apache.camel.spi.UriParam; import org.apache.camel.spi.UriParams; @@ -30,7 +31,7 @@ import org.apache.camel.spi.UriPath; * Component configuration for GoogleCalendar stream component. */ @UriParams -public class GoogleCalendarStreamConfiguration implements Cloneable { +public class GoogleCalendarStreamConfiguration implements Cloneable, GoogleCommonConfiguration { private static final String DEFAULT_SCOPES = CalendarScopes.CALENDAR; @UriPath @@ -72,6 +73,7 @@ public class GoogleCalendarStreamConfiguration implements Cloneable { @UriParam private String delegate; + @Override public String getClientId() { return clientId; } @@ -94,6 +96,7 @@ public class GoogleCalendarStreamConfiguration implements Cloneable { this.emailAddress = emailAddress; } + @Override public String getClientSecret() { return clientSecret; } @@ -105,6 +108,7 @@ public class GoogleCalendarStreamConfiguration implements Cloneable { this.clientSecret = clientSecret; } + @Override public String getAccessToken() { return accessToken; } @@ -116,6 +120,7 @@ public class GoogleCalendarStreamConfiguration implements Cloneable { this.accessToken = accessToken; } + @Override public String getRefreshToken() { return refreshToken; } @@ -154,6 +159,7 @@ public class GoogleCalendarStreamConfiguration implements Cloneable { this.scopes = scopes; } + @Override public Collection<String> getScopesAsList() { if (scopes != null) { return List.of(scopes.split(",")); @@ -263,6 +269,7 @@ public class GoogleCalendarStreamConfiguration implements Cloneable { this.considerLastUpdate = considerLastUpdate; } + @Override public String getServiceAccountKey() { return serviceAccountKey; } @@ -277,6 +284,7 @@ public class GoogleCalendarStreamConfiguration implements Cloneable { this.serviceAccountKey = serviceAccountKey; } + @Override public String getDelegate() { return delegate; }
