Skip to content

Commit

Permalink
[GOOGLEAPPS-20] #resolve
Browse files Browse the repository at this point in the history
  • Loading branch information
ilgrosso committed May 22, 2024
1 parent 101f3dc commit be3d668
Show file tree
Hide file tree
Showing 15 changed files with 640 additions and 626 deletions.
3 changes: 1 addition & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
<google-auth-library-oauth2-http.version>1.23.0</google-auth-library-oauth2-http.version>
<jackson.version>2.17.1</jackson.version>

<spring-boot.version>2.6.15</spring-boot.version>
<spring-boot.version>2.7.18</spring-boot.version>

<java.version>1.8</java.version>

Expand Down Expand Up @@ -159,7 +159,6 @@
<version>${connid.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
import org.identityconnectors.common.security.GuardedString;
import org.identityconnectors.common.security.SecurityUtil;
import org.identityconnectors.framework.common.exceptions.ConfigurationException;
import org.identityconnectors.framework.common.exceptions.ConnectorException;
import org.identityconnectors.framework.spi.AbstractConfiguration;
import org.identityconnectors.framework.spi.ConfigurationProperty;
import org.identityconnectors.framework.spi.StatefulConfiguration;
Expand All @@ -66,6 +65,8 @@ public class GoogleAppsConfiguration extends AbstractConfiguration implements St
*/
private static final JsonFactory JSON_FACTORY = new GsonFactory();

private static final String APPLICATION_NAME = "ConnId";

private String domain = null;

/**
Expand All @@ -80,7 +81,7 @@ public class GoogleAppsConfiguration extends AbstractConfiguration implements St

private GuardedString refreshToken = null;

private GoogleCredentials credentials = null;
private GoogleCredentials googleCredentials = null;

private Directory directory;

Expand Down Expand Up @@ -222,52 +223,55 @@ public void validate() {
}
}

public void getGoogleCredential() {
private void initGoogleCredentials() {
synchronized (this) {
if (null == credentials) {
final UserCredentials.Builder credentialsBuilder =
UserCredentials.newBuilder()
.setClientId(getClientId())
.setClientSecret(SecurityUtil.decrypt(getClientSecret()));
if (null == googleCredentials) {
UserCredentials.Builder credentialsBuilder = UserCredentials.newBuilder()
.setClientId(getClientId())
.setClientSecret(SecurityUtil.decrypt(getClientSecret()));

getRefreshToken().access(chars -> credentialsBuilder.setRefreshToken(new String(chars)));

final UserCredentials userCredentials = credentialsBuilder.build();
credentials = userCredentials.createScoped(
Arrays.asList(DirectoryScopes.ADMIN_DIRECTORY_USER,
DirectoryScopes.ADMIN_DIRECTORY_USER_ALIAS,
DirectoryScopes.ADMIN_DIRECTORY_USERSCHEMA,
DirectoryScopes.ADMIN_DIRECTORY_ORGUNIT,
DirectoryScopes.ADMIN_DIRECTORY_DOMAIN,
DirectoryScopes.ADMIN_DIRECTORY_NOTIFICATIONS,
DirectoryScopes.ADMIN_DIRECTORY_GROUP,
DirectoryScopes.ADMIN_DIRECTORY_GROUP_MEMBER));

HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(credentials);
UserCredentials userCredentials = credentialsBuilder.build();

googleCredentials = userCredentials.createScoped(Arrays.asList(
DirectoryScopes.ADMIN_DIRECTORY_USER,
DirectoryScopes.ADMIN_DIRECTORY_USER_ALIAS,
DirectoryScopes.ADMIN_DIRECTORY_USERSCHEMA,
DirectoryScopes.ADMIN_DIRECTORY_ORGUNIT,
DirectoryScopes.ADMIN_DIRECTORY_DOMAIN,
DirectoryScopes.ADMIN_DIRECTORY_NOTIFICATIONS,
DirectoryScopes.ADMIN_DIRECTORY_GROUP,
DirectoryScopes.ADMIN_DIRECTORY_GROUP_MEMBER));

HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(googleCredentials);
directory = new Directory.Builder(HTTP_TRANSPORT, JSON_FACTORY, requestInitializer).
setApplicationName("ConnId").
setApplicationName(APPLICATION_NAME).
build();
licensing = new Licensing.Builder(HTTP_TRANSPORT, JSON_FACTORY, requestInitializer).
setApplicationName("ConnId").
setApplicationName(APPLICATION_NAME).
build();
}
}
}

public void test() throws IOException {
initGoogleCredentials();
googleCredentials.refreshIfExpired();
}

@Override
public void release() {
googleCredentials = null;
}

public Directory getDirectory() {
getGoogleCredential();
initGoogleCredentials();
return directory;
}

public Licensing getLicensing() {
getGoogleCredential();
if (null == licensing) {
throw new ConnectorException("Licensing is not enabled");
}
initGoogleCredentials();
return licensing;
}
}
Loading

0 comments on commit be3d668

Please sign in to comment.