Skip to content

Commit

Permalink
Upgrading Okta SDK to 18.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ilgrosso committed Aug 11, 2024
1 parent 6df16a6 commit c3891ac
Show file tree
Hide file tree
Showing 10 changed files with 18,740 additions and 2,509 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
import com.okta.sdk.resource.model.PasswordCredential;
import com.okta.sdk.resource.model.UpdateUserRequest;
import com.okta.sdk.resource.model.User;
import com.okta.sdk.resource.model.UserGetSingleton;
import com.okta.sdk.resource.model.UserProfile;
import com.okta.sdk.resource.model.UserStatus;
import com.okta.sdk.resource.user.UserBuilder;
import java.time.OffsetDateTime;
Expand Down Expand Up @@ -343,7 +345,7 @@ public Uid update(
AttributesAccessor accessor = new AttributesAccessor(replaceAttributes);
if (ObjectClass.ACCOUNT.equals(objectClass)) {
Uid returnUid = uid;
User user = userApi.getUser(uid.getUidValue());
UserGetSingleton user = userApi.getUser(uid.getUidValue(), null);

// 1. update password
Optional.ofNullable(accessor.getPassword()).
Expand All @@ -357,7 +359,7 @@ public Uid update(

// 2. update attributes
try {
updateUserAttributes(user, replaceAttributes);
updateUserProfile(user.getProfile(), replaceAttributes);
UpdateUserRequest req = new UpdateUserRequest();
req.setProfile(user.getProfile());

Expand All @@ -375,7 +377,7 @@ public Uid update(
List<Object> groupsToAssign =
CollectionUtil.nullAsEmpty(accessor.findList(OktaAttribute.OKTA_GROUPS));

Set<String> assignedGroups = userApi.listUserGroups(user.getId()).stream()
Set<String> assignedGroups = userApi.listUserGroups(user.getId(), null, null).stream()
.filter(item -> !OktaAttribute.isDefaultEveryoneGroup(item))
.map(Group::getId).collect(Collectors.toSet());

Expand Down Expand Up @@ -539,7 +541,7 @@ public void sync(
} else {
try {
if (ObjectClass.ACCOUNT.equals(objectClass)) {
User user = userApi.getUser(item.getTarget().get(0).getId());
UserGetSingleton user = userApi.getUser(item.getTarget().get(0).getId(), null);
connObj = fromUser(user, attributesToGet);
} else if (ObjectClass.GROUP.equals(objectClass)) {
Group group = groupApi.getGroup(item.getTarget().get(0).getId());
Expand Down Expand Up @@ -590,24 +592,25 @@ public FilterTranslator<OktaFilter> createFilterTranslator(
return new OktaFilterTranslator(oclass);
}

private <T> void doExecuteQuery(
private <T, S> void doExecuteQuery(
final ObjectClass objectClass,
final OktaFilter filter,
final Integer pageSize,
final String beforeCookie,
final ResultsHandler handler,
final Function<String, T> getFunction,
final Function<String, S> getFunction,
final Function<String, List<T>> pagedSearchFunction,
final Function<String, List<T>> searchFunction,
final Function<T, ConnectorObject> fromFunction) {
final Function<T, ConnectorObject> fromObject,
final Function<S, ConnectorObject> fromObjectSingletonGet) {

if (filter != null && filter.getFilters() == null
&& OktaAttribute.ID.equals(filter.getAttribute())
&& OktaFilterOp.EQUALS.equals(filter.getFilterOp())) {

try {
T object = getFunction.apply(filter.getValue());
handler.handle(fromFunction.apply(object));
S object = getFunction.apply(filter.getValue());
handler.handle(fromObjectSingletonGet.apply(object));
} catch (Exception e) {
OktaUtils.wrapGeneralError(
"While getting " + objectClass.getObjectClassValue() + " with filter: " + filter, e);
Expand All @@ -632,12 +635,10 @@ private <T> void doExecuteQuery(
"While getting " + objectClass.getObjectClassValue() + " with filter: " + filter, e);
}

if (objects != null) {
for (T object : objects) {
if (!handler.handle(fromFunction.apply(object))) {
LOG.ok("Stop processing of the result set");
break;
}
for (T object : CollectionUtil.nullAsEmpty(objects)) {
if (!handler.handle(fromObject.apply(object))) {
LOG.ok("Stop processing of the result set");
break;
}
}

Expand Down Expand Up @@ -673,14 +674,16 @@ public void executeQuery(
options.getPageSize(),
options.getPagedResultsCookie(),
handler,
userApi::getUser,
userId -> userApi.getUser(userId, null),
f -> userApi.listUsers(
null, options.getPagedResultsCookie(), options.getPageSize(), f, null, null, null),
f -> userApi.listUsers(
null, null, null, f, null, null, null),
o -> fromUser(o, attributesToGet));
user -> fromUser(user, attributesToGet),
userGetSingleton -> fromUser(userGetSingleton, attributesToGet));
} else if (APPLICATION.equals(objectClass)) {
doExecuteQuery(objectClass,
doExecuteQuery(
objectClass,
filter,
options.getPageSize(),
options.getPagedResultsCookie(),
Expand All @@ -690,6 +693,7 @@ public void executeQuery(
null, options.getPagedResultsCookie(), options.getPageSize(), f, null, null),
f -> appApi.listApplications(
null, null, null, f, null, null),
o -> fromApplication(o, attributesToGet),
o -> fromApplication(o, attributesToGet));
} else if (ObjectClass.GROUP.equals(objectClass)) {
doExecuteQuery(
Expand All @@ -703,6 +707,7 @@ public void executeQuery(
null, f, options.getPagedResultsCookie(), options.getPageSize(), null, null, null, null),
f -> groupApi.listGroups(
null, f, null, null, null, null, null, null),
o -> fromGroup(o, attributesToGet),
o -> fromGroup(o, attributesToGet));
} else {
throw new UnsupportedOperationException(
Expand Down Expand Up @@ -761,13 +766,41 @@ private ConnectorObject fromLogEvent(final String id, final long lastUpdate, fin
return builder.build();
}

private ConnectorObject fromUser(final User user, final Set<String> attributesToGet) {
private ConnectorObject fromUser(
final UserGetSingleton user,
final Set<String> attributesToGet) {

ConnectorObjectBuilder builder = new ConnectorObjectBuilder();
builder.setObjectClass(ObjectClass.ACCOUNT);
builder.setUid(user.getId());
builder.setName(user.getProfile().getLogin());
builder.addAttributes(OktaAttribute.buildUserAttributes(
userApi, user, schema.getSchema(), attributesToGet));
userApi,
user.getId(),
user.getStatus(),
user.getLastUpdated(),
user.getProfile(),
schema.getSchema(),
attributesToGet));
return builder.build();
}

private ConnectorObject fromUser(
final User user,
final Set<String> attributesToGet) {

ConnectorObjectBuilder builder = new ConnectorObjectBuilder();
builder.setObjectClass(ObjectClass.ACCOUNT);
builder.setUid(user.getId());
builder.setName(user.getProfile().getLogin());
builder.addAttributes(OktaAttribute.buildUserAttributes(
userApi,
user.getId(),
user.getStatus(),
user.getLastUpdated(),
user.getProfile(),
schema.getSchema(),
attributesToGet));
return builder.build();
}

Expand Down Expand Up @@ -904,7 +937,7 @@ private void buildProfile(
}));
}

private void updateUserAttributes(final User user, final Set<Attribute> replaceAttributes) {
private void updateUserProfile(final UserProfile userProfile, final Set<Attribute> replaceAttributes) {
ObjectClassInfo objectClassInfo = schema.getSchema().findObjectClassInfo(ObjectClass.ACCOUNT_NAME);
replaceAttributes.stream().
filter(attribute -> !NOT_FOR_PROFILE.contains(attribute.getName())).
Expand All @@ -919,33 +952,33 @@ private void updateUserAttributes(final User user, final Set<Attribute> replaceA

switch (attrInfo.getName()) {
case OktaAttribute.FIRSTNAME:
user.getProfile().setFirstName(value);
userProfile.setFirstName(value);
break;

case OktaAttribute.LASTNAME:
user.getProfile().setLastName(value);
userProfile.setLastName(value);
break;

case OktaAttribute.EMAIL:
user.getProfile().setEmail(value);
userProfile.setEmail(value);
break;

case OktaAttribute.LOGIN:
user.getProfile().setLogin(value);
userProfile.setLogin(value);
break;

case OktaAttribute.MOBILEPHONE:
user.getProfile().setMobilePhone(value);
userProfile.setMobilePhone(value);
break;

case OktaAttribute.SECOND_EMAIL:
user.getProfile().setSecondEmail(value);
userProfile.setSecondEmail(value);
break;

default:
}
} else {
user.getProfile().getAdditionalProperties().put(
userProfile.getAdditionalProperties().put(
attr.getName(),
CollectionUtil.isEmpty(attr.getValue())
? null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@
import com.okta.sdk.resource.model.ApplicationLifecycleStatus;
import com.okta.sdk.resource.model.Group;
import com.okta.sdk.resource.model.GroupType;
import com.okta.sdk.resource.model.User;
import com.okta.sdk.resource.model.UserProfile;
import com.okta.sdk.resource.model.UserStatus;
import java.time.OffsetDateTime;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import net.tirasa.connid.bundles.okta.OktaConnector;
Expand Down Expand Up @@ -115,34 +116,35 @@ public static Class<?> getType(final String type) {

public static Set<Attribute> buildUserAttributes(
final UserApi userApi,
final User user,
final String userId,
final UserStatus userStatus,
final OffsetDateTime userLastUpdated,
final UserProfile userProfile,
final Schema schema,
final Set<String> attributesToGet) {

Set<Attribute> attributes = new HashSet<>();
ObjectClassInfo objectClassInfo = schema.findObjectClassInfo(ObjectClass.ACCOUNT_NAME);
UserProfile userProfile = user.getProfile();
attributesToGet.stream().filter(name -> !Name.NAME.equals(name) && !Uid.NAME.equals(name)).forEach(name -> {
if (ID.equals(name)) {
attributes.add(AttributeBuilder.build(name, user.getId()));
attributes.add(AttributeBuilder.build(name, userId));
} else if (STATUS.equals(name)) {
attributes.add(buildAttribute(user.getStatus().toString(), name, String.class).build());
attributes.add(buildAttribute(userStatus.toString(), name, String.class).build());
} else if (OperationalAttributes.ENABLE_NAME.equals(name)) {
attributes.add(buildAttribute(user.getStatus() == UserStatus.ACTIVE, name, Boolean.class).build());
attributes.add(buildAttribute(userStatus == UserStatus.ACTIVE, name, Boolean.class).build());
} else if (OKTA_GROUPS.equals(name)) {
try {
List<String> assignedGroups = userApi.listUserGroups(user.getId()).stream()
List<String> assignedGroups = userApi.listUserGroups(userId, null, null).stream()
.filter(item -> !isDefaultEveryoneGroup(item))
.map(Group::getId)
.collect(Collectors.toList());
attributes.add(buildAttribute(assignedGroups, name, Set.class).build());
} catch (Exception ex) {
LOG.error(ex, "Could not list groups for User {0}", user.getId());
LOG.error(ex, "Could not list groups for User {0}", userId);
}
} else if (LASTUPDATED.equals(name)) {
attributes.add(buildAttribute(user.getLastUpdated() != null
? user.getLastUpdated().toInstant().toEpochMilli()
: 0L,
attributes.add(buildAttribute(
Optional.ofNullable(userLastUpdated).map(u -> u.toInstant().toEpochMilli()).orElse(0L),
name, Long.class).build());
} else {
objectClassInfo.getAttributeInfo().stream().
Expand Down Expand Up @@ -293,14 +295,23 @@ public static Set<Attribute> buildGroupAttributes(

Set<Attribute> attributes = new HashSet<>();
attributesToGet.stream().filter(name -> !Name.NAME.equals(name) && !Uid.NAME.equals(name)).forEach(name -> {
if (ID.equals(name)) {
attributes.add(AttributeBuilder.build(name, group.getId()));
} else if (DESCRIPTION.equals(name)) {
attributes.add(AttributeBuilder.build(name, group.getProfile().getDescription()));
} else if (LASTUPDATED.equals(name)) {
attributes.add(buildAttribute(group.getLastUpdated() != null
? group.getLastUpdated().toInstant().toEpochMilli() : 0L,
name, Long.class).build());
switch (name) {
case ID:
attributes.add(AttributeBuilder.build(name, group.getId()));
break;

case DESCRIPTION:
attributes.add(AttributeBuilder.build(name, group.getProfile().getDescription()));
break;

case LASTUPDATED:
attributes.add(buildAttribute(
Optional.ofNullable(group.getLastUpdated()).
map(t -> t.toInstant().toEpochMilli()).orElse(0L),
name, Long.class).build());
break;

default:
}
});
return attributes;
Expand All @@ -322,16 +333,15 @@ public static Set<Attribute> buildApplicationAttributes(
attributeBuilder.setName(name);
attributeBuilder.addValue(application.getStatus());
attributes.add(attributeBuilder.build());
} else if (OperationalAttributes.ENABLE_NAME.equals(name)
|| STATUS.equals(name)) {

} else if (OperationalAttributes.ENABLE_NAME.equals(name) || STATUS.equals(name)) {
AttributeBuilder attributeBuilder = new AttributeBuilder();
attributeBuilder.setName(name);
attributeBuilder.addValue(application.getStatus() == ApplicationLifecycleStatus.ACTIVE);
attributes.add(attributeBuilder.build());
} else if (LASTUPDATED.equals(name)) {
attributes.add(buildAttribute(application.getLastUpdated() != null
? application.getLastUpdated().toInstant().toEpochMilli() : 0L,
attributes.add(buildAttribute(
Optional.ofNullable(application.getLastUpdated()).
map(t -> t.toInstant().toEpochMilli()).orElse(0L),
name, Long.class).build());
} else {
objectClassInfo.getAttributeInfo().stream().
Expand All @@ -348,7 +358,8 @@ public static AttributeBuilder buildAttribute(final Object value, final String n
return buildAttribute(value, name, clazz, new AttributeBuilder());
}

public static AttributeBuilder buildAttribute(final Object value,
public static AttributeBuilder buildAttribute(
final Object value,
final String name,
final Class<?> clazz,
final AttributeBuilder attributeBuilder) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import com.okta.sdk.resource.model.ApplicationSignOnMode;
import com.okta.sdk.resource.model.BasicAuthApplication;
import com.okta.sdk.resource.model.Group;
import com.okta.sdk.resource.model.User;
import com.okta.sdk.resource.model.UserGetSingleton;
import com.okta.sdk.resource.model.UserStatus;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -80,7 +80,7 @@ public class OktaConnectorTests extends AbstractConnectorTests {
private Set<String> getUserGroups(final UserApi client, final String userId) {
Set<String> assignedGroups = new HashSet<>();
try {
for (Group grpItem : client.listUserGroups(userId)) {
for (Group grpItem : client.listUserGroups(userId, null, null)) {
assignedGroups.add(grpItem.getId());
}
} catch (Exception ex) {
Expand Down Expand Up @@ -168,10 +168,12 @@ public static void setupData() {

Group groupTest = createGroup(CONN.getGroupApi());
assertNotNull(groupTest);
assertNotNull(groupTest.getProfile().getName());
GROUPS.add(groupTest.getId());

groupTest = createGroup(CONN.getGroupApi());
assertNotNull(groupTest);
assertNotNull(groupTest.getProfile().getName());
GROUPS.add(groupTest.getId());

Application app = createApplication(CONN.getApplicationApi());
Expand Down Expand Up @@ -739,7 +741,7 @@ public void createUserWithStatusStaged() {
USERS.add(created.getUidValue());
assertNotNull(created);

User user = CONN.getUserApi().getUser(created.getUidValue());
UserGetSingleton user = CONN.getUserApi().getUser(created.getUidValue(), null);
assertEquals(UserStatus.STAGED, user.getStatus());
}

Expand Down
Loading

0 comments on commit c3891ac

Please sign in to comment.