Skip to content

Commit

Permalink
chore(google): migrate G-connectors to generator (#1966)
Browse files Browse the repository at this point in the history
  • Loading branch information
igpetrov authored Feb 29, 2024
1 parent 8db0493 commit 48e29a9
Show file tree
Hide file tree
Showing 58 changed files with 3,271 additions and 2,013 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,115 +6,86 @@
*/
package io.camunda.google.model;

import com.google.api.services.drive.DriveScopes;
import com.google.auth.oauth2.AccessToken;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.auth.oauth2.UserCredentials;
import jakarta.validation.constraints.AssertTrue;
import jakarta.validation.constraints.NotNull;
import java.util.Objects;

public class Authentication {

@NotNull private AuthenticationType authType;
private String bearerToken;
private String oauthClientId;
private String oauthClientSecret;
private String oauthRefreshToken;

@AssertTrue
private boolean isHasAuthData() {
if (authType == AuthenticationType.BEARER) {
return bearerToken != null;
} else if (authType == AuthenticationType.REFRESH) {
return oauthClientId != null && oauthClientSecret != null && oauthRefreshToken != null;
} else {
return false;
}
}

public GoogleCredentials fetchCredentials() {
if (authType == AuthenticationType.BEARER) {
AccessToken accessToken = new AccessToken(bearerToken, null);
return new GoogleCredentials(accessToken).createScoped(DriveScopes.DRIVE);
}

if (authType == AuthenticationType.REFRESH) {
return UserCredentials.newBuilder()
.setClientId(oauthClientId)
.setClientSecret(oauthClientSecret)
.setRefreshToken(oauthRefreshToken)
.build();
}

throw new RuntimeException("Unsupported authentication type");
}

public AuthenticationType getAuthType() {
return authType;
}

public void setAuthType(AuthenticationType authType) {
this.authType = authType;
}

public String getBearerToken() {
return bearerToken;
}

public void setBearerToken(String bearerToken) {
this.bearerToken = bearerToken;
}

public String getOauthClientId() {
return oauthClientId;
}

public void setOauthClientId(String oauthClientId) {
this.oauthClientId = oauthClientId;
}

public String getOauthClientSecret() {
return oauthClientSecret;
}

public void setOauthClientSecret(String oauthClientSecret) {
this.oauthClientSecret = oauthClientSecret;
}

public String getOauthRefreshToken() {
return oauthRefreshToken;
}
import static io.camunda.connector.generator.java.annotation.TemplateProperty.PropertyType.Dropdown;

public void setOauthRefreshToken(String oauthRefreshToken) {
this.oauthRefreshToken = oauthRefreshToken;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}

if (o == null || getClass() != o.getClass()) {
return false;
}

Authentication that = (Authentication) o;
return Objects.equals(authType, that.authType)
&& Objects.equals(bearerToken, that.bearerToken)
&& Objects.equals(oauthClientId, that.oauthClientId)
&& Objects.equals(oauthClientSecret, that.oauthClientSecret)
&& Objects.equals(oauthRefreshToken, that.oauthRefreshToken);
}

@Override
public int hashCode() {
return Objects.hash(authType, bearerToken, oauthClientId, oauthClientSecret, oauthRefreshToken);
import io.camunda.connector.generator.dsl.Property.FeelMode;
import io.camunda.connector.generator.java.annotation.TemplateProperty;
import io.camunda.connector.generator.java.annotation.TemplateProperty.DropdownPropertyChoice;
import io.camunda.connector.generator.java.annotation.TemplateProperty.PropertyCondition;
import io.camunda.connector.generator.java.annotation.TemplateProperty.PropertyConstraints;
import jakarta.validation.constraints.AssertTrue;
import org.apache.commons.lang3.StringUtils;

public record Authentication(
@TemplateProperty(
id = "authType",
label = "Type",
group = "authentication",
type = Dropdown,
defaultValue = "BEARER",
constraints = @PropertyConstraints(notEmpty = true),
choices = {
@DropdownPropertyChoice(label = "Bearer token", value = "BEARER"),
@DropdownPropertyChoice(label = "Refresh token", value = "REFRESH")
})
AuthenticationType authType,
@TemplateProperty(
id = "bearerToken",
label = "Bearer token",
description = "Enter a valid Google API Bearer token",
group = "authentication",
feel = FeelMode.optional,
constraints = @PropertyConstraints(notEmpty = true),
condition = @PropertyCondition(property = "authentication.authType", equals = "BEARER"))
String bearerToken,
@TemplateProperty(
id = "oauthClientId",
label = "Client ID",
description = "Enter Google API Client ID",
group = "authentication",
feel = FeelMode.optional,
constraints = @PropertyConstraints(notEmpty = true),
condition =
@PropertyCondition(property = "authentication.authType", equals = "REFRESH"))
String oauthClientId,
@TemplateProperty(
id = "oauthClientSecret",
label = "Client secret",
description = "Enter Google API client Secret",
group = "authentication",
feel = FeelMode.optional,
constraints = @PropertyConstraints(notEmpty = true),
condition =
@PropertyCondition(property = "authentication.authType", equals = "REFRESH"))
String oauthClientSecret,
@TemplateProperty(
id = "oauthRefreshToken",
label = "Refresh token",
description = "Enter a valid Google API refresh token",
group = "authentication",
feel = FeelMode.optional,
constraints = @PropertyConstraints(notEmpty = true),
condition =
@PropertyCondition(property = "authentication.authType", equals = "REFRESH"))
String oauthRefreshToken) {

@AssertTrue(message = "Credentials were incorrect")
private boolean isCredentialsSupplied() {
return StringUtils.isNotBlank(bearerToken)
|| (StringUtils.isNotBlank(oauthClientId)
&& StringUtils.isNotBlank(oauthClientSecret)
&& StringUtils.isNotBlank(oauthRefreshToken));
}

@Override
public String toString() {
return "Authentication{" + "authType='" + authType + '\'' + '}';
return "Authentication{"
+ "authType="
+ authType
+ ", bearerToken=[REDACTED]"
+ ", oauthClientId=[REDACTED]"
+ ", oauthClientSecret=[REDACTED]"
+ ", oauthRefreshToken=[REDACTED]"
+ '}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import static io.camunda.google.supplier.util.GoogleServiceSupplierUtil.getHttpHttpCredentialsAdapter;
import static io.camunda.google.supplier.util.GoogleServiceSupplierUtil.getNetHttpTransport;

import com.google.api.client.json.gson.GsonFactory;
import com.google.api.services.drive.Drive;
import io.camunda.google.model.Authentication;
import org.slf4j.Logger;
Expand All @@ -24,7 +25,7 @@ public static Drive createDriveClientInstance(final Authentication authenticatio
Drive drive =
new Drive.Builder(
getNetHttpTransport(),
GsonComponentSupplier.gsonFactoryInstance(),
GsonFactory.getDefaultInstance(),
getHttpHttpCredentialsAdapter(authentication))
.build();
LOGGER.debug("Google drive service was successfully initialized");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@

import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.services.drive.DriveScopes;
import com.google.auth.Credentials;
import com.google.auth.http.HttpCredentialsAdapter;
import com.google.auth.oauth2.AccessToken;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.auth.oauth2.UserCredentials;
import io.camunda.google.model.Authentication;
import io.camunda.google.model.AuthenticationType;
import java.io.IOException;
import java.security.GeneralSecurityException;

Expand All @@ -18,7 +24,22 @@ public final class GoogleServiceSupplierUtil {
private GoogleServiceSupplierUtil() {}

public static HttpCredentialsAdapter getHttpHttpCredentialsAdapter(final Authentication auth) {
return new HttpCredentialsAdapter(auth.fetchCredentials());
Credentials creds = null;
if (auth.authType() == AuthenticationType.BEARER) {
AccessToken accessToken = new AccessToken(auth.bearerToken(), null);
creds = new GoogleCredentials(accessToken).createScoped(DriveScopes.DRIVE);
}

if (auth.authType() == AuthenticationType.REFRESH) {
creds =
UserCredentials.newBuilder()
.setClientId(auth.oauthClientId())
.setClientSecret(auth.oauthClientSecret())
.setRefreshToken(auth.oauthRefreshToken())
.build();
}

return new HttpCredentialsAdapter(creds);
}

public static NetHttpTransport getNetHttpTransport() {
Expand Down
Loading

0 comments on commit 48e29a9

Please sign in to comment.