Last active
April 20, 2018 19:18
-
-
Save AlainODea/4b7989f7411a0b4e09a7a06811a9a7de to your computer and use it in GitHub Desktop.
Add Universal Directory custom attribute and mapping to all Identity Providers in Okta with java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import com.okta.sdk.clients.AppUserTypeApiClient; | |
| import com.okta.sdk.clients.UserProfileMappingApiClient; | |
| import com.okta.sdk.clients.UserTypeApiClient; | |
| import com.okta.sdk.framework.ApiClientConfiguration; | |
| import com.okta.sdk.framework.PagedResults; | |
| import com.okta.sdk.framework.PaginationUtils; | |
| import com.okta.sdk.models.directory.AppUserType; | |
| import com.okta.sdk.models.directory.UserProfileMapping; | |
| import com.okta.sdk.models.directory.UserProfilePropertyMapping; | |
| import java.io.FileReader; | |
| import java.io.Reader; | |
| import java.util.List; | |
| import java.util.Properties; | |
| import java.util.concurrent.TimeUnit; | |
| public class CreateMissingUserRoleMappings { | |
| private static final boolean updateEnabled = false; | |
| public static final String TENANT_TYPE_PREFIX = "..."; | |
| public static final String USER_ROLE_ATTRIBUTE_NAME = "..."; | |
| public static void main(String[] args) throws Exception { | |
| Properties properties = new Properties(); | |
| try (Reader reader = new FileReader("okta.properties")) { | |
| properties.load(reader); | |
| } | |
| ApiClientConfiguration config = new ApiClientConfiguration(baseUrl, apiToken); | |
| UserProfileMappingApiClient userProfileMappingApiClient = new UserProfileMappingApiClient(config); | |
| AppUserTypeApiClient appUserTypeApiClient = new AppUserTypeApiClient(config); | |
| UserTypeApiClient userTypeApiClient = new UserTypeApiClient(config); | |
| AppUserType defaultAppUserType = userTypeApiClient.getDefaultAppUserType(); | |
| String target = defaultAppUserType.getId(); | |
| PagedResults<AppUserType> appUserTypes = appUserTypeApiClient.getAppUserTypesPagedResultsWithQuery(TENANT_TYPE_PREFIX); | |
| PaginationUtils.consumePagedResults(100, TimeUnit.MILLISECONDS, appUserTypes, appUserTypeApiClient::getAppUserTypesPagedResultsByUrl, appUserType -> { | |
| String source = appUserType.getId(); | |
| List<UserProfileMapping> userProfileMappings = userProfileMappingApiClient.getUserProfileMappings(source, target); | |
| if (userProfileMappings.isEmpty()) { | |
| System.out.println("\"" + appUserType.getDisplayName() + "\",\"ERR_NO_MAPPINGS\""); | |
| } else if (userProfileMappings.size() > 1) { | |
| System.out.println("\"" + appUserType.getDisplayName() + "\",\"ERR_TOO_MANY_MAPPINGS\""); | |
| } else { | |
| for (UserProfileMapping userProfileMapping : userProfileMappings) { | |
| List<UserProfilePropertyMapping> propertyMappings = userProfileMapping.getPropertyMappings(); | |
| boolean userRoleMappingMissing = propertyMappings.stream() | |
| .map(UserProfilePropertyMapping::getTargetField) | |
| .noneMatch(USER_ROLE_ATTRIBUTE_NAME::equals); | |
| if (userRoleMappingMissing) { | |
| UserProfilePropertyMapping userProfilePropertyMapping = new UserProfilePropertyMapping(); | |
| userProfilePropertyMapping.setTargetField("userRole"); | |
| userProfilePropertyMapping.setSourceExpression("appuser." + USER_ROLE_ATTRIBUTE_NAME); | |
| userProfilePropertyMapping.setPushStatus("PUSH"); | |
| propertyMappings.add(userProfilePropertyMapping); | |
| if (updateEnabled) { | |
| userProfileMapping = userProfileMappingApiClient.updateUserProfileMapping(userProfileMapping); | |
| userProfileMappingApiClient.reapplyUserProfileMappings(source, target); | |
| System.out.println("\"" + appUserType.getDisplayName() + "\",\"UPDATED\""); | |
| } else { | |
| System.out.println("\"" + appUserType.getDisplayName() + "\",\"WOULD_UPDATE\""); | |
| } | |
| } else { | |
| System.out.println("\"" + appUserType.getDisplayName() + "\",\"UP_TO_DATE\""); | |
| } | |
| } | |
| } | |
| }); | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Depends on my fork of the legacy Okta Java SDK:
AlainODea/oktasdk-java@4264c3f