Skip to content

Commit

Permalink
Merge pull request #5 from openstandia/dev
Browse files Browse the repository at this point in the history
fix: some improvements
  • Loading branch information
wadahiro authored Jun 30, 2022
2 parents e70fab1 + 254d2b1 commit 103a1ba
Show file tree
Hide file tree
Showing 10 changed files with 657 additions and 129 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@
<configuration>
<serverId>ossrh</serverId>
<nexusUrl>https://oss.sonatype.org/</nexusUrl>
<autoReleaseAfterClose>false</autoReleaseAfterClose>
<autoReleaseAfterClose>true</autoReleaseAfterClose>
</configuration>
</plugin>
<plugin>
Expand Down Expand Up @@ -194,7 +194,7 @@
<dependency>
<groupId>com.evolveum.midpoint.gui</groupId>
<artifactId>admin-gui</artifactId>
<version>4.4.1</version>
<version>4.4.2</version>
<type>jar</type>
<classifier>classes</classifier>
<scope>provided</scope>
Expand Down
223 changes: 150 additions & 73 deletions src/main/java/jp/openstandia/connector/auth0/Auth0Client.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ public class Auth0Configuration extends AbstractConfiguration {
private String domain;
private String clientId;
private GuardedString clientSecret;
private Integer connectionTimeoutInSeconds = 10;
private Integer readTimeoutInSeconds = 10;
private Integer maxRetries = 3;
private int connectionTimeoutInSeconds = 10;
private int readTimeoutInSeconds = 10;
private int maxRetries = 3;
private String httpProxyHost;
private int httpProxyPort;
private String httpProxyUser;
private GuardedString httpProxyPassword;
private Integer defaultQueryPageSize = 50;
private int defaultQueryPageSize = 50;
private String[] connectionFilter = new String[]{};

@ConfigurationProperty(
Expand Down Expand Up @@ -96,11 +96,11 @@ public void setConnectionFilter(String[] connectionFilter) {
helpMessageKey = "Connection timeout when connecting to Auth0. (Default: 10)",
required = false,
confidential = false)
public Integer getConnectionTimeoutInSeconds() {
public int getConnectionTimeoutInSeconds() {
return connectionTimeoutInSeconds;
}

public void setConnectionTimeoutInSeconds(Integer connectionTimeoutInSeconds) {
public void setConnectionTimeoutInSeconds(int connectionTimeoutInSeconds) {
this.connectionTimeoutInSeconds = connectionTimeoutInSeconds;
}

Expand All @@ -110,11 +110,11 @@ public void setConnectionTimeoutInSeconds(Integer connectionTimeoutInSeconds) {
helpMessageKey = "Read timeout when fetching data from Auth0. (Default: 10)",
required = false,
confidential = false)
public Integer getReadTimeoutInSeconds() {
public int getReadTimeoutInSeconds() {
return readTimeoutInSeconds;
}

public void setReadTimeoutInSeconds(Integer readTimeoutInSeconds) {
public void setReadTimeoutInSeconds(int readTimeoutInSeconds) {
this.readTimeoutInSeconds = readTimeoutInSeconds;
}

Expand All @@ -124,11 +124,11 @@ public void setReadTimeoutInSeconds(Integer readTimeoutInSeconds) {
helpMessageKey = "Sets the maximum number of consecutive retries for Auth0 Management API requests that fail due to rate-limits being reached. (Default: 3)",
required = false,
confidential = false)
public Integer getMaxRetries() {
public int getMaxRetries() {
return maxRetries;
}

public void setMaxRetries(Integer maxRetries) {
public void setMaxRetries(int maxRetries) {
this.maxRetries = maxRetries;
}

Expand Down Expand Up @@ -194,11 +194,11 @@ public void setHttpProxyPassword(GuardedString httpProxyPassword) {
helpMessageKey = "Set default query page size. Default: 50",
required = false,
confidential = false)
public Integer getDefaultQueryPageSize() {
public int getDefaultQueryPageSize() {
return defaultQueryPageSize;
}

public void setDefaultQueryPageSize(Integer defaultQueryPageSize) {
public void setDefaultQueryPageSize(int defaultQueryPageSize) {
this.defaultQueryPageSize = defaultQueryPageSize;
}

Expand Down
31 changes: 23 additions & 8 deletions src/main/java/jp/openstandia/connector/auth0/Auth0Connector.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@
import org.identityconnectors.framework.common.exceptions.*;
import org.identityconnectors.framework.common.objects.*;
import org.identityconnectors.framework.common.objects.filter.FilterTranslator;
import org.identityconnectors.framework.spi.Configuration;
import org.identityconnectors.framework.spi.ConnectorClass;
import org.identityconnectors.framework.spi.InstanceNameAware;
import org.identityconnectors.framework.spi.PoolableConnector;
import org.identityconnectors.framework.spi.*;
import org.identityconnectors.framework.spi.operations.*;

import java.util.*;
Expand All @@ -35,6 +32,8 @@
import static jp.openstandia.connector.auth0.Auth0OrganizationHandler.ORGANIZATION_OBJECT_CLASS;
import static jp.openstandia.connector.auth0.Auth0RoleHandler.ROLE_OBJECT_CLASS;
import static jp.openstandia.connector.auth0.Auth0UserHandler.USER_OBJECT_CLASS_PREFIX;
import static jp.openstandia.connector.auth0.Auth0Utils.resolvePageOffset;
import static jp.openstandia.connector.auth0.Auth0Utils.resolvePageSize;

@ConnectorClass(configurationClass = Auth0Configuration.class, displayNameKey = "Auth0 Connector")
public class Auth0Connector implements PoolableConnector, CreateOp, UpdateDeltaOp, DeleteOp, SchemaOp, TestOp, SearchOp<Auth0Filter>, InstanceNameAware {
Expand Down Expand Up @@ -105,7 +104,8 @@ public Schema schema() {

schemaBuilder.defineOperationOption(OperationOptionInfoBuilder.buildAttributesToGet(), SearchOp.class);
schemaBuilder.defineOperationOption(OperationOptionInfoBuilder.buildReturnDefaultAttributes(), SearchOp.class);

schemaBuilder.defineOperationOption(OperationOptionInfoBuilder.buildPageSize(), SearchOp.class);
schemaBuilder.defineOperationOption(OperationOptionInfoBuilder.buildPagedResultsOffset(), SearchOp.class);

Map<String, AttributeInfo> roleSchemaMap = new HashMap<>();
for (AttributeInfo a : roleSchemaInfo.getAttributeInfo()) {
Expand Down Expand Up @@ -231,21 +231,36 @@ public FilterTranslator<Auth0Filter> createFilterTranslator(ObjectClass objectCl
@Override
public void executeQuery(ObjectClass objectClass, Auth0Filter filter, ResultsHandler resultsHandler, OperationOptions options) {
try {
int pageSize = resolvePageSize(configuration, options);
int pageOffset = resolvePageOffset(options);

int total = 0;

if (objectClass.getObjectClassValue().startsWith(USER_OBJECT_CLASS_PREFIX)) {
Auth0UserHandler userHandler = new Auth0UserHandler(configuration, client, getSchemaMap(objectClass), resolveDatabaseConnection(objectClass));
userHandler.getUsers(filter, resultsHandler, options);
total = userHandler.getUsers(filter, resultsHandler, options);

} else if (objectClass.equals(ROLE_OBJECT_CLASS)) {
Auth0RoleHandler roleHandler = new Auth0RoleHandler(configuration, client, getSchemaMap(objectClass));
roleHandler.getRoles(filter, resultsHandler, options);
total = roleHandler.getRoles(filter, resultsHandler, options);

} else if (objectClass.equals(ORGANIZATION_OBJECT_CLASS)) {
Auth0OrganizationHandler organizationHandler = new Auth0OrganizationHandler(configuration, client, getSchemaMap(objectClass));
organizationHandler.query(filter, resultsHandler, options);
total = organizationHandler.query(filter, resultsHandler, options);

} else {
throw new InvalidAttributeValueException("Unsupported object class " + objectClass);
}

if (resultsHandler instanceof SearchResultsHandler &&
pageOffset > 0) {

int remaining = total - (pageSize * pageOffset);

SearchResultsHandler searchResultsHandler = (SearchResultsHandler) resultsHandler;
SearchResult searchResult = new SearchResult(null, remaining);
searchResultsHandler.handleResult(searchResult);
}
} catch (Exception e) {
throw processException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,40 +239,44 @@ public void delete(Uid uid, OperationOptions options) throws Auth0Exception {
* @param filter
* @param resultsHandler
* @param options
* @return
* @throws Auth0Exception
*/
public void query(Auth0Filter filter,
ResultsHandler resultsHandler, OperationOptions options) throws Auth0Exception {
public int query(Auth0Filter filter,
ResultsHandler resultsHandler, OperationOptions options) throws Auth0Exception {
// Create full attributesToGet by RETURN_DEFAULT_ATTRIBUTES + ATTRIBUTES_TO_GET
Set<String> attributesToGet = createFullAttributesToGet(schema, options);
boolean allowPartialAttributeValues = shouldAllowPartialAttributeValues(options);

if (filter != null) {
if (filter.isByName()) {
// Filter by __NANE__
getOrganizationByName(filter.attributeValue, resultsHandler, attributesToGet, allowPartialAttributeValues);
return getOrganizationByName(filter.attributeValue, resultsHandler, attributesToGet, allowPartialAttributeValues);
} else {
// Filter by __UID__
getOrganizationByUid(filter.attributeValue, resultsHandler, attributesToGet, allowPartialAttributeValues);
return getOrganizationByUid(filter.attributeValue, resultsHandler, attributesToGet, allowPartialAttributeValues);
}
return;
}

client.getOrganizations(options, (org) -> resultsHandler.handle(toConnectorObject(org, attributesToGet, allowPartialAttributeValues)));
return client.getOrganizations(options, (org) -> resultsHandler.handle(toConnectorObject(org, attributesToGet, allowPartialAttributeValues)));
}

private void getOrganizationByName(String orgName,
ResultsHandler resultsHandler, Set<String> attributesToGet, boolean allowPartialAttributeValues) throws Auth0Exception {
private int getOrganizationByName(String orgName,
ResultsHandler resultsHandler, Set<String> attributesToGet, boolean allowPartialAttributeValues) throws Auth0Exception {
Organization org = client.getOrganizationByName(orgName);

resultsHandler.handle(toConnectorObject(org, attributesToGet, allowPartialAttributeValues));

return 1;
}

private void getOrganizationByUid(String orgId,
ResultsHandler resultsHandler, Set<String> attributesToGet, boolean allowPartialAttributeValues) throws Auth0Exception {
private int getOrganizationByUid(String orgId,
ResultsHandler resultsHandler, Set<String> attributesToGet, boolean allowPartialAttributeValues) throws Auth0Exception {
Organization org = client.getOrganizationByUid(orgId);

resultsHandler.handle(toConnectorObject(org, attributesToGet, allowPartialAttributeValues));

return 1;
}

private ConnectorObject toConnectorObject(Organization org, Set<String> attributesToGet, boolean allowPartialAttributeValues) throws Auth0Exception {
Expand Down
24 changes: 14 additions & 10 deletions src/main/java/jp/openstandia/connector/auth0/Auth0RoleHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -212,41 +212,45 @@ public void deleteRole(Uid uid, OperationOptions options) throws Auth0Exception
* @param resultsHandler
* @param options
* @throws Auth0Exception
* @return
*/
public void getRoles(Auth0Filter filter,
ResultsHandler resultsHandler, OperationOptions options) throws Auth0Exception {
public int getRoles(Auth0Filter filter,
ResultsHandler resultsHandler, OperationOptions options) throws Auth0Exception {
// Create full attributesToGet by RETURN_DEFAULT_ATTRIBUTES + ATTRIBUTES_TO_GET
Set<String> attributesToGet = createFullAttributesToGet(schema, options);
boolean allowPartialAttributeValues = shouldAllowPartialAttributeValues(options);

if (filter != null) {
if (filter.isByName()) {
// Filter by __NANE__
getRoleByName(filter.attributeValue, resultsHandler, attributesToGet, allowPartialAttributeValues);
return getRoleByName(filter.attributeValue, resultsHandler, attributesToGet, allowPartialAttributeValues);
} else {
// Filter by __UID__
getRoleByUid(filter.attributeValue, resultsHandler, attributesToGet, allowPartialAttributeValues);
return getRoleByUid(filter.attributeValue, resultsHandler, attributesToGet, allowPartialAttributeValues);
}
return;
}

client.getRoles(options, (role) -> resultsHandler.handle(toConnectorObject(role, attributesToGet, allowPartialAttributeValues)));
return client.getRoles(options, (role) -> resultsHandler.handle(toConnectorObject(role, attributesToGet, allowPartialAttributeValues)));
}

private void getRoleByName(String roleName,
ResultsHandler resultsHandler, Set<String> attributesToGet, boolean allowPartialAttributeValues) throws Auth0Exception {
private int getRoleByName(String roleName,
ResultsHandler resultsHandler, Set<String> attributesToGet, boolean allowPartialAttributeValues) throws Auth0Exception {
List<Role> response = client.getRoleByName(roleName);

for (Role role : response) {
resultsHandler.handle(toConnectorObject(role, attributesToGet, allowPartialAttributeValues));
}

return response.size();
}

private void getRoleByUid(String roleId,
ResultsHandler resultsHandler, Set<String> attributesToGet, boolean allowPartialAttributeValues) throws Auth0Exception {
private int getRoleByUid(String roleId,
ResultsHandler resultsHandler, Set<String> attributesToGet, boolean allowPartialAttributeValues) throws Auth0Exception {
Role role = client.getRoleByUid(roleId);

resultsHandler.handle(toConnectorObject(role, attributesToGet, allowPartialAttributeValues));

return 1;
}

private ConnectorObject toConnectorObject(Role role, Set<String> attributesToGet, boolean allowPartialAttributeValues) throws Auth0Exception {
Expand Down
33 changes: 20 additions & 13 deletions src/main/java/jp/openstandia/connector/auth0/Auth0UserHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ public void deleteUser(Uid uid, OperationOptions options) throws Auth0Exception
client.deleteUser(uid);
}

public void getUsers(Auth0Filter filter, ResultsHandler resultsHandler, OperationOptions options) throws
public int getUsers(Auth0Filter filter, ResultsHandler resultsHandler, OperationOptions options) throws
Auth0Exception {
// Create full attributesToGet by RETURN_DEFAULT_ATTRIBUTES + ATTRIBUTES_TO_GET
Set<String> attributesToGet = createFullAttributesToGet(schema, options);
Expand All @@ -574,33 +574,34 @@ public void getUsers(Auth0Filter filter, ResultsHandler resultsHandler, Operatio
if (filter.isByName()) {
// Filter by __NANE__
if (isSMS()) {
getUserByPhoneNumber(filter.attributeValue, resultsHandler, attributesToGet, allowPartialAttributeValues);
return getUserByPhoneNumber(filter.attributeValue, resultsHandler, attributesToGet, allowPartialAttributeValues);
} else {
getUserByEmail(filter.attributeValue, resultsHandler, attributesToGet, allowPartialAttributeValues);
return getUserByEmail(filter.attributeValue, resultsHandler, attributesToGet, allowPartialAttributeValues);
}
} else {
// Filter by __UID__
getUserByUid(filter.attributeValue, resultsHandler, attributesToGet, allowPartialAttributeValues);
return getUserByUid(filter.attributeValue, resultsHandler, attributesToGet, allowPartialAttributeValues);
}
return;
}

UserFilter userFilter = applyFieldsFilter(attributesToGet, new UserFilter(), ADDITIONAL_ALLOWED_FIELDS_SET)
.withQuery("identities.connection:\"" + connection + "\"");

client.getUsers(userFilter, options, (user) -> resultsHandler.handle(toConnectorObject(user, attributesToGet, allowPartialAttributeValues)));
return client.getUsers(userFilter, options, (user) -> resultsHandler.handle(toConnectorObject(user, attributesToGet, allowPartialAttributeValues)));
}

private void getUserByUid(String userId, ResultsHandler resultsHandler, Set<String> attributesToGet,
boolean allowPartialAttributeValues) throws Auth0Exception {
private int getUserByUid(String userId, ResultsHandler resultsHandler, Set<String> attributesToGet,
boolean allowPartialAttributeValues) throws Auth0Exception {
UserFilter filter = applyFieldsFilter(attributesToGet, new UserFilter(), Collections.emptySet());
User user = client.getUserByUid(userId, filter);

resultsHandler.handle(toConnectorObject(user, attributesToGet, allowPartialAttributeValues));

return 1;
}

private void getUserByPhoneNumber(String attrValue, ResultsHandler resultsHandler, Set<String> attributesToGet,
boolean allowPartialAttributeValues) throws Auth0Exception {
private int getUserByPhoneNumber(String attrValue, ResultsHandler resultsHandler, Set<String> attributesToGet,
boolean allowPartialAttributeValues) throws Auth0Exception {
attrValue = attrValue.replace("\"", "\\\"");
UserFilter filter = new UserFilter()
.withPage(0, 1)
Expand All @@ -612,10 +613,12 @@ private void getUserByPhoneNumber(String attrValue, ResultsHandler resultsHandle
for (User user : response) {
resultsHandler.handle(toConnectorObject(user, attributesToGet, allowPartialAttributeValues));
}

return response.size();
}

private void getUserByEmail(String email, ResultsHandler resultsHandler, Set<String> attributesToGet,
boolean allowPartialAttributeValues) throws Auth0Exception {
private int getUserByEmail(String email, ResultsHandler resultsHandler, Set<String> attributesToGet,
boolean allowPartialAttributeValues) throws Auth0Exception {
email = email.replace("\"", "\\\"");
UserFilter filter = new UserFilter()
.withPage(0, 1)
Expand All @@ -628,6 +631,8 @@ private void getUserByEmail(String email, ResultsHandler resultsHandler, Set<Str
for (User user : response) {
resultsHandler.handle(toConnectorObject(user, attributesToGet, allowPartialAttributeValues));
}

return response.size();
}

private ConnectorObject toConnectorObject(User user, Set<String> attributesToGet,
Expand Down Expand Up @@ -662,7 +667,9 @@ private ConnectorObject toConnectorObject(User user, Set<String> attributesToGet
builderWrapper.apply(ATTR_NICKNAME, user.getNickname());
builderWrapper.apply(ATTR_GIVEN_NAME, user.getGivenName());
builderWrapper.apply(ATTR_FAMILY_NAME, user.getFamilyName());
builderWrapper.apply(ATTR_CONNECTION, user.getIdentities().stream().map(i -> i.getConnection()).collect(Collectors.toList()));
builderWrapper.apply(ATTR_CONNECTION, user.getIdentities() != null ?
user.getIdentities().stream().map(i -> i.getConnection()).collect(Collectors.toList()) :
null);

if (allowPartialAttributeValues) {
// Suppress fetching association
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ public static Set<String> createFullAttributesToGet(Map<String, AttributeInfo> s
for (String a : options.getAttributesToGet()) {
attributesToGet.add(a);
}
attributesToGet.add(Uid.NAME);
}
return attributesToGet;
}
Expand Down
Loading

0 comments on commit 103a1ba

Please sign in to comment.