CAMEL-9455: Enhance Google Calendar service account with user impersonation support. Thanks to Alvin Kwekel for the patch.
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/76cf2f82 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/76cf2f82 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/76cf2f82 Branch: refs/heads/camel-2.16.x Commit: 76cf2f8279349fa32aeef0134aea969b6275a473 Parents: c4d4338 Author: Claus Ibsen <davscl...@apache.org> Authored: Mon Dec 28 14:15:09 2015 +0100 Committer: Claus Ibsen <davscl...@apache.org> Committed: Mon Dec 28 14:15:35 2015 +0100 ---------------------------------------------------------------------- .../BatchGoogleCalendarClientFactory.java | 34 ++++++++++---------- .../calendar/GoogleCalendarClientFactory.java | 4 +-- .../calendar/GoogleCalendarComponent.java | 17 ++++------ .../calendar/GoogleCalendarConfiguration.java | 23 ++++++++++--- 4 files changed, 45 insertions(+), 33 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/76cf2f82/components/camel-google-calendar/src/main/java/org/apache/camel/component/google/calendar/BatchGoogleCalendarClientFactory.java ---------------------------------------------------------------------- diff --git a/components/camel-google-calendar/src/main/java/org/apache/camel/component/google/calendar/BatchGoogleCalendarClientFactory.java b/components/camel-google-calendar/src/main/java/org/apache/camel/component/google/calendar/BatchGoogleCalendarClientFactory.java index 67d94f7..5783e89 100644 --- a/components/camel-google-calendar/src/main/java/org/apache/camel/component/google/calendar/BatchGoogleCalendarClientFactory.java +++ b/components/camel-google-calendar/src/main/java/org/apache/camel/component/google/calendar/BatchGoogleCalendarClientFactory.java @@ -26,8 +26,6 @@ import com.google.api.client.http.HttpTransport; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.jackson2.JacksonFactory; import com.google.api.services.calendar.Calendar; - - import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -44,49 +42,51 @@ public class BatchGoogleCalendarClientFactory implements GoogleCalendarClientFac @Override public Calendar makeClient(String clientId, String clientSecret, - Collection<String> scopes, String applicationName, String refreshToken, - String accessToken, String emailAddress, String p12FileName) { - + Collection<String> scopes, String applicationName, String refreshToken, + String accessToken, String emailAddress, String p12FileName, String user) { + Credential credential; try { - // if emailAddress and p12FileName values are present, assume Google Service Account + // if emailAddress and p12FileName values are present, assume Google Service Account if (null != emailAddress && !"".equals(emailAddress) && null != p12FileName && !"".equals(p12FileName)) { - credential = authorizeServiceAccount(emailAddress, p12FileName, scopes); + credential = authorizeServiceAccount(emailAddress, p12FileName, scopes, user); } else { credential = authorize(clientId, clientSecret, scopes); if (refreshToken != null && !"".equals(refreshToken)) { credential.setRefreshToken(refreshToken); - } + } if (accessToken != null && !"".equals(accessToken)) { credential.setAccessToken(accessToken); } } return new Calendar.Builder(transport, jsonFactory, credential).setApplicationName(applicationName).build(); } catch (Exception e) { - LOG.error("Could not create Google Drive client.", e); + LOG.error("Could not create Google Drive client.", e); } return null; } - + // Authorizes the installed application to access user's protected data. private Credential authorize(String clientId, String clientSecret, Collection<String> scopes) throws Exception { // authorize return new GoogleCredential.Builder() - .setJsonFactory(jsonFactory) - .setTransport(transport) - .setClientSecrets(clientId, clientSecret) - .build(); + .setJsonFactory(jsonFactory) + .setTransport(transport) + .setClientSecrets(clientId, clientSecret) + .build(); } - - private Credential authorizeServiceAccount(String emailAddress, String p12FileName, Collection<String> scopes) throws Exception { + + private Credential authorizeServiceAccount(String emailAddress, String p12FileName, Collection<String> scopes, String user) throws Exception { HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport(); + // set the service account user when provided GoogleCredential credential = new GoogleCredential.Builder() .setTransport(httpTransport) .setJsonFactory(jsonFactory) .setServiceAccountId(emailAddress) .setServiceAccountPrivateKeyFromP12File(new File(p12FileName)) .setServiceAccountScopes(scopes) + .setServiceAccountUser(user) .build(); return credential; } -} \ No newline at end of file +} http://git-wip-us.apache.org/repos/asf/camel/blob/76cf2f82/components/camel-google-calendar/src/main/java/org/apache/camel/component/google/calendar/GoogleCalendarClientFactory.java ---------------------------------------------------------------------- diff --git a/components/camel-google-calendar/src/main/java/org/apache/camel/component/google/calendar/GoogleCalendarClientFactory.java b/components/camel-google-calendar/src/main/java/org/apache/camel/component/google/calendar/GoogleCalendarClientFactory.java index 74347a2..c187db0 100644 --- a/components/camel-google-calendar/src/main/java/org/apache/camel/component/google/calendar/GoogleCalendarClientFactory.java +++ b/components/camel-google-calendar/src/main/java/org/apache/camel/component/google/calendar/GoogleCalendarClientFactory.java @@ -23,7 +23,7 @@ import com.google.api.services.calendar.Calendar; public interface GoogleCalendarClientFactory { Calendar makeClient(String clientId, String clientSecret, Collection<String> scopes, - String applicationName, String refreshToken, String accessToken, - String emailAddress, String p12FileName); + String applicationName, String refreshToken, String accessToken, + String emailAddress, String p12FileName, String user); } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/76cf2f82/components/camel-google-calendar/src/main/java/org/apache/camel/component/google/calendar/GoogleCalendarComponent.java ---------------------------------------------------------------------- diff --git a/components/camel-google-calendar/src/main/java/org/apache/camel/component/google/calendar/GoogleCalendarComponent.java b/components/camel-google-calendar/src/main/java/org/apache/camel/component/google/calendar/GoogleCalendarComponent.java index 6cd83dc..d140ee4 100644 --- a/components/camel-google-calendar/src/main/java/org/apache/camel/component/google/calendar/GoogleCalendarComponent.java +++ b/components/camel-google-calendar/src/main/java/org/apache/camel/component/google/calendar/GoogleCalendarComponent.java @@ -17,7 +17,6 @@ package org.apache.camel.component.google.calendar; import com.google.api.services.calendar.Calendar; - import org.apache.camel.CamelContext; import org.apache.camel.Endpoint; import org.apache.camel.component.google.calendar.internal.GoogleCalendarApiCollection; @@ -47,15 +46,15 @@ public class GoogleCalendarComponent extends AbstractApiComponent<GoogleCalendar public Calendar getClient() { if (client == null) { - client = getClientFactory().makeClient(configuration.getClientId(), - configuration.getClientSecret(), configuration.getScopes(), - configuration.getApplicationName(), configuration.getRefreshToken(), - configuration.getAccessToken(), configuration.getEmailAddress(), - configuration.getP12FileName()); + client = getClientFactory().makeClient(configuration.getClientId(), + configuration.getClientSecret(), configuration.getScopes(), + configuration.getApplicationName(), configuration.getRefreshToken(), + configuration.getAccessToken(), configuration.getEmailAddress(), + configuration.getP12FileName(), configuration.getUser()); } return client; } - + public GoogleCalendarClientFactory getClientFactory() { if (clientFactory == null) { clientFactory = new BatchGoogleCalendarClientFactory(); @@ -70,7 +69,6 @@ public class GoogleCalendarComponent extends AbstractApiComponent<GoogleCalendar /** * To use the shared configuration - * @param configuration */ @Override public void setConfiguration(GoogleCalendarConfiguration configuration) { @@ -80,12 +78,11 @@ public class GoogleCalendarComponent extends AbstractApiComponent<GoogleCalendar /** * To use the GoogleCalendarClientFactory as factory for creating the client. * Will by default use {@link BatchGoogleCalendarClientFactory} - * @param clientFactory */ public void setClientFactory(GoogleCalendarClientFactory clientFactory) { this.clientFactory = clientFactory; } - + @Override protected Endpoint createEndpoint(String uri, String methodName, GoogleCalendarApiName apiName, GoogleCalendarConfiguration endpointConfiguration) { http://git-wip-us.apache.org/repos/asf/camel/blob/76cf2f82/components/camel-google-calendar/src/main/java/org/apache/camel/component/google/calendar/GoogleCalendarConfiguration.java ---------------------------------------------------------------------- diff --git a/components/camel-google-calendar/src/main/java/org/apache/camel/component/google/calendar/GoogleCalendarConfiguration.java b/components/camel-google-calendar/src/main/java/org/apache/camel/component/google/calendar/GoogleCalendarConfiguration.java index 80ba96b..dae776c 100644 --- a/components/camel-google-calendar/src/main/java/org/apache/camel/component/google/calendar/GoogleCalendarConfiguration.java +++ b/components/camel-google-calendar/src/main/java/org/apache/camel/component/google/calendar/GoogleCalendarConfiguration.java @@ -31,9 +31,10 @@ import org.apache.camel.spi.UriPath; */ @UriParams public class GoogleCalendarConfiguration { - private static final List<String> DEFAULT_SCOPES = Arrays.asList(CalendarScopes.CALENDAR); + private static final List<String> DEFAULT_SCOPES = Arrays.asList(CalendarScopes.CALENDAR); - @UriPath @Metadata(required = "true") + @UriPath + @Metadata(required = "true") private GoogleCalendarApiName apiName; @UriPath(enums = "calendarImport,clear,delete,get,insert,instances,list,move,patch,query,quickAdd,stop,update,watch") @@ -42,7 +43,7 @@ public class GoogleCalendarConfiguration { @UriParam(defaultValue = CalendarScopes.CALENDAR) private List<String> scopes = DEFAULT_SCOPES; - + @UriParam private String clientId; @@ -64,6 +65,9 @@ public class GoogleCalendarConfiguration { @UriParam private String p12FileName; + @UriParam + private String user; + public GoogleCalendarApiName getApiName() { return apiName; } @@ -151,7 +155,7 @@ public class GoogleCalendarConfiguration { public void setApplicationName(String applicationName) { this.applicationName = applicationName; } - + public List<String> getScopes() { return scopes; } @@ -174,4 +178,15 @@ public class GoogleCalendarConfiguration { this.p12FileName = p12FileName; } + public String getUser() { + return user; + } + + /** + * The email address of the user the application is trying to impersonate in the service account flow + */ + public void setUser(String user) { + this.user = user; + } + }