|
18 | 18 |
|
19 | 19 | package org.wso2.carbon.identity.oauth2.validators.validationhandler.impl;
|
20 | 20 |
|
| 21 | +import org.apache.commons.lang.StringUtils; |
21 | 22 | import org.apache.commons.logging.Log;
|
22 | 23 | import org.apache.commons.logging.LogFactory;
|
23 | 24 | import org.wso2.carbon.identity.application.common.IdentityApplicationManagementException;
|
|
30 | 31 | import org.wso2.carbon.identity.oauth2.validators.validationhandler.ScopeValidationContext;
|
31 | 32 | import org.wso2.carbon.identity.oauth2.validators.validationhandler.ScopeValidationHandler;
|
32 | 33 | import org.wso2.carbon.identity.oauth2.validators.validationhandler.ScopeValidationHandlerException;
|
| 34 | +import org.wso2.carbon.identity.organization.management.service.exception.OrganizationManagementException; |
33 | 35 |
|
34 | 36 | import java.util.ArrayList;
|
35 | 37 | import java.util.List;
|
@@ -60,13 +62,27 @@ public List<String> validateScopes(List<String> requestedScopes, List<String> ap
|
60 | 62 | if (userRoles.isEmpty()) {
|
61 | 63 | return new ArrayList<>();
|
62 | 64 | }
|
63 |
| - List<String> filteredRoleIds = getFilteredRoleIds(userRoles, scopeValidationContext.getAppId(), |
64 |
| - scopeValidationContext.getAuthenticatedUser().getTenantDomain()); |
| 65 | + String tenantDomain = scopeValidationContext.getAuthenticatedUser().getTenantDomain(); |
| 66 | + String accessingOrganization = scopeValidationContext.getAuthenticatedUser().getAccessingOrganization(); |
| 67 | + String userResidentOrganization = scopeValidationContext.getAuthenticatedUser() |
| 68 | + .getUserResidentOrganization(); |
| 69 | + if (StringUtils.isNotEmpty(accessingOrganization)) { |
| 70 | + tenantDomain = resolveTenantDomainByOrgId(accessingOrganization); |
| 71 | + } |
| 72 | + |
| 73 | + List<String> filteredRoleIds; |
| 74 | + if (accessingOrganization != null && !accessingOrganization.equals(userResidentOrganization)) { |
| 75 | + filteredRoleIds = userRoles; |
| 76 | + } else { |
| 77 | + filteredRoleIds = |
| 78 | + getFilteredRoleIds(userRoles, scopeValidationContext.getAppId(), tenantDomain); |
| 79 | + } |
| 80 | + |
65 | 81 | if (filteredRoleIds.isEmpty()) {
|
66 | 82 | return new ArrayList<>();
|
67 | 83 | }
|
68 | 84 | List<String> associatedScopes = AuthzUtil.getAssociatedScopesForRoles(filteredRoleIds,
|
69 |
| - scopeValidationContext.getAuthenticatedUser().getTenantDomain()); |
| 85 | + tenantDomain); |
70 | 86 | List<String> filteredScopes = appAuthorizedScopes.stream().filter(associatedScopes::contains)
|
71 | 87 | .collect(Collectors.toList());
|
72 | 88 | return requestedScopes.stream().filter(filteredScopes::contains).collect(Collectors.toList());
|
@@ -124,4 +140,15 @@ public String getName() {
|
124 | 140 |
|
125 | 141 | return "RoleBasedScopeValidationHandler";
|
126 | 142 | }
|
| 143 | + |
| 144 | + private String resolveTenantDomainByOrgId(String organizationId) throws ScopeValidationHandlerException { |
| 145 | + |
| 146 | + try { |
| 147 | + return OAuth2ServiceComponentHolder.getInstance().getOrganizationManager() |
| 148 | + .resolveTenantDomain(organizationId); |
| 149 | + } catch (OrganizationManagementException e) { |
| 150 | + throw new ScopeValidationHandlerException("Error while resolving the tenant domain of the org ID: " + |
| 151 | + organizationId, e); |
| 152 | + } |
| 153 | + } |
127 | 154 | }
|
0 commit comments