Skip to content

Commit

Permalink
Refactor code to remove unnecessary iterations
Browse files Browse the repository at this point in the history
  • Loading branch information
SujanSanjula96 committed Dec 3, 2024
1 parent 74b2650 commit a82357a
Showing 1 changed file with 26 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@
import org.wso2.carbon.identity.application.authentication.framework.model.AuthenticatedUser;
import org.wso2.carbon.identity.application.common.IdentityApplicationManagementException;
import org.wso2.carbon.identity.application.common.model.IdentityProvider;
import org.wso2.carbon.identity.application.common.model.InboundAuthenticationRequestConfig;
import org.wso2.carbon.identity.application.common.model.ServiceProvider;
import org.wso2.carbon.identity.application.common.model.User;
import org.wso2.carbon.identity.application.mgt.ApplicationConstants;
import org.wso2.carbon.identity.application.mgt.ApplicationConstants.StandardInboundProtocols;
import org.wso2.carbon.identity.application.mgt.ApplicationManagementService;
import org.wso2.carbon.identity.base.IdentityConstants;
import org.wso2.carbon.identity.central.log.mgt.utils.LoggerUtils;
Expand Down Expand Up @@ -778,47 +779,38 @@ private static AuthenticatedUser buildAuthenticatedUser(UserStoreManager userSto
* @param authenticatedUser Authenticated user.
* @return Set of clientIds of associated applications.
*/
private static Set<String> getClientIdsOfAssociatedApplications(RoleBasicInfo role,
AuthenticatedUser authenticatedUser)
private static Optional<String> getClientIdOfAssociatedApplication(RoleBasicInfo role,
AuthenticatedUser authenticatedUser)
throws UserStoreException {

ApplicationManagementService applicationManagementService =
OAuthComponentServiceHolder.getInstance().getApplicationManagementService();
List<String> associatedApplications = Collections.singletonList(role.getAudienceId());
String associatedApplication = role.getAudienceId();
try {
if (authenticatedUser.getUserResidentOrganization() != null) {
List<String> newAssociatedApplications = new ArrayList<>();
for (String app : associatedApplications) {
newAssociatedApplications.add(
SharedAppResolveDAO.getMainApplication(app, authenticatedUser.getAccessingOrganization()));
}
associatedApplications = newAssociatedApplications;
associatedApplication = SharedAppResolveDAO.getMainApplication(
associatedApplication, authenticatedUser.getAccessingOrganization());
}
} catch (IdentityOAuth2Exception e) {
throw new UserStoreException("Error occurred while getting the main applications of the shared apps.", e);
}
Set<String> clientIds = new HashSet<>();
associatedApplications.forEach(associatedApplication -> {
try {
ServiceProvider application = applicationManagementService
.getApplicationByResourceId(associatedApplication, authenticatedUser.getTenantDomain());
if (application == null || application.getInboundAuthenticationConfig() == null) {
return;
}
Arrays.stream(application.getInboundAuthenticationConfig().getInboundAuthenticationRequestConfigs())
.forEach(inboundAuthenticationRequestConfig -> {
if (ApplicationConstants.StandardInboundProtocols.OAUTH2.equals(
inboundAuthenticationRequestConfig.getInboundAuthType())) {
clientIds.add(inboundAuthenticationRequestConfig.getInboundAuthKey());
}
});
} catch (IdentityApplicationManagementException e) {
String errorMessage = "Error occurred while retrieving application of id : " +
associatedApplication;
LOG.error(errorMessage);
try {
ServiceProvider application = applicationManagementService
.getApplicationByResourceId(associatedApplication, authenticatedUser.getTenantDomain());
if (application != null && application.getInboundAuthenticationConfig() != null) {
InboundAuthenticationRequestConfig[] inboundAuthenticationRequestConfigs =
application.getInboundAuthenticationConfig().getInboundAuthenticationRequestConfigs();
return Arrays.stream(inboundAuthenticationRequestConfigs)
.filter(config -> StandardInboundProtocols.OAUTH2.equals(config.getInboundAuthType()))
.map(InboundAuthenticationRequestConfig::getInboundAuthKey)
.findFirst();
}
});
return clientIds;
} catch (IdentityApplicationManagementException e) {
String errorMessage = "Error occurred while retrieving application of id : " +
associatedApplication;
LOG.error(errorMessage);
}
return Optional.empty();
}

private static Set<String> filterClientIdsWithOrganizationAudience(List<String> clientIds, String tenantDomain) {
Expand Down Expand Up @@ -1010,7 +1002,7 @@ public static boolean revokeTokens(String username, UserStoreManager userStoreMa
}

// Get details about the role to identify the audience and associated applications.
Set<String> clientIds = null;
Set<String> clientIds = new HashSet<>();;
RoleBasicInfo role = null;
boolean getClientIdsFromUser = false;
if (roleId != null) {
Expand All @@ -1021,7 +1013,8 @@ public static boolean revokeTokens(String username, UserStoreManager userStoreMa
LOG.debug("Get clientIds of associated applications for the application role: "
+ role.getName());
}
clientIds = getClientIdsOfAssociatedApplications(role, authenticatedUser);
getClientIdOfAssociatedApplication(role, authenticatedUser)
.ifPresent(clientIds::add);
} else {
// Get all the distinct client Ids authorized by this user since this is an organization role.
if (LOG.isDebugEnabled()) {
Expand Down

0 comments on commit a82357a

Please sign in to comment.