|
62 | 62 | import org.wso2.carbon.identity.core.util.IdentityUtil; |
63 | 63 | import org.wso2.carbon.identity.governance.IdentityGovernanceException; |
64 | 64 | import org.wso2.carbon.identity.multi.attribute.login.mgt.ResolvedUserResult; |
| 65 | +import org.wso2.carbon.identity.organization.management.organization.user.sharing.models.UserAssociation; |
| 66 | +import org.wso2.carbon.identity.organization.management.service.exception.OrganizationManagementException; |
| 67 | +import org.wso2.carbon.identity.organization.management.service.util.OrganizationManagementUtil; |
65 | 68 | import org.wso2.carbon.identity.recovery.RecoveryScenarios; |
66 | 69 | import org.wso2.carbon.user.api.UserRealm; |
67 | 70 | import org.wso2.carbon.user.core.UserCoreConstants; |
@@ -735,8 +738,41 @@ protected void processAuthenticationResponse(HttpServletRequest request, |
735 | 738 | if (userId != null) { |
736 | 739 | authenticationResult = userStoreManager.authenticateWithID(userId, password); |
737 | 740 | } else { |
738 | | - authenticationResult = userStoreManager.authenticateWithID(UserCoreClaimConstants.USERNAME_CLAIM_URI, |
739 | | - tenantAwareUsername, password, UserCoreConstants.DEFAULT_PROFILE); |
| 741 | + // Check whether the user is a shared user, if yes authenticate from the user resident org. |
| 742 | + org.wso2.carbon.user.core.common.User user = userStoreManager.getUser(null, tenantAwareUsername); |
| 743 | + if (user != null) { |
| 744 | + String userID = user.getUserID(); |
| 745 | + boolean sharedUserProfile = isSharedUserProfile(userID, requestTenantDomain); |
| 746 | + if (sharedUserProfile) { |
| 747 | + // Get user's resident org. |
| 748 | + String currentOrganizationId = |
| 749 | + BasicAuthenticatorDataHolder.getInstance().getOrganizationManager() |
| 750 | + .resolveOrganizationId(requestTenantDomain); |
| 751 | + UserAssociation userAssociation = |
| 752 | + BasicAuthenticatorDataHolder.getInstance().getOrganizationUserSharingService() |
| 753 | + .getUserAssociation(userID, currentOrganizationId); |
| 754 | + String associationUserResidentOrganizationId = userAssociation.getUserResidentOrganizationId(); |
| 755 | + String associationUserTenantDomain = |
| 756 | + BasicAuthenticatorDataHolder.getInstance().getOrganizationManager() |
| 757 | + .resolveTenantDomain(associationUserResidentOrganizationId); |
| 758 | + AbstractUserStoreManager residentOrguserStoreManager = |
| 759 | + getUserStoreManager(username, associationUserTenantDomain); |
| 760 | + authenticationResult = residentOrguserStoreManager.authenticateWithID( |
| 761 | + UserCoreClaimConstants.USERNAME_CLAIM_URI, |
| 762 | + tenantAwareUsername, password, UserCoreConstants.DEFAULT_PROFILE); |
| 763 | + if (AuthenticationResult.AuthenticationStatus.SUCCESS == authenticationResult.getAuthenticationStatus()) { |
| 764 | + authenticationResult.setAuthenticatedUser(user); |
| 765 | + } |
| 766 | + } else { |
| 767 | + authenticationResult = |
| 768 | + userStoreManager.authenticateWithID(UserCoreClaimConstants.USERNAME_CLAIM_URI, |
| 769 | + tenantAwareUsername, password, UserCoreConstants.DEFAULT_PROFILE); |
| 770 | + } |
| 771 | + } else { |
| 772 | + authenticationResult = |
| 773 | + userStoreManager.authenticateWithID(UserCoreClaimConstants.USERNAME_CLAIM_URI, |
| 774 | + tenantAwareUsername, password, UserCoreConstants.DEFAULT_PROFILE); |
| 775 | + } |
740 | 776 | } |
741 | 777 | if (AuthenticationResult.AuthenticationStatus.SUCCESS == authenticationResult.getAuthenticationStatus() |
742 | 778 | && authenticationResult.getAuthenticatedUser().isPresent()) { |
@@ -825,6 +861,8 @@ protected void processAuthenticationResponse(HttpServletRequest request, |
825 | 861 | ErrorMessages.USER_STORE_EXCEPTION_WHILE_TRYING_TO_AUTHENTICATE.getCode(), e.getMessage(), |
826 | 862 | e); |
827 | 863 | } |
| 864 | + } catch (OrganizationManagementException e) { |
| 865 | + throw new AuthenticationFailedException("Error while resolving organization", e); |
828 | 866 | } finally { |
829 | 867 | clearUserExistThreadLocal(); |
830 | 868 | } |
@@ -1323,4 +1361,32 @@ public String getI18nKey() { |
1323 | 1361 | return AUTHENTICATOR_BASIC; |
1324 | 1362 | } |
1325 | 1363 |
|
| 1364 | + private static boolean isSharedUserProfile(String userID, String currentTenantDomain) |
| 1365 | + throws UserStoreClientException { |
| 1366 | + |
| 1367 | + String currentOrganizationId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getOrganizationId(); |
| 1368 | + try { |
| 1369 | + if (!OrganizationManagementUtil.isOrganization(currentTenantDomain)) { |
| 1370 | + // There is no shared users in root organizations. Hence, return false. |
| 1371 | + return false; |
| 1372 | + } |
| 1373 | + if (StringUtils.isBlank(currentOrganizationId)) { |
| 1374 | + currentOrganizationId = BasicAuthenticatorDataHolder.getInstance().getOrganizationManager() |
| 1375 | + .resolveOrganizationId(currentTenantDomain); |
| 1376 | + } |
| 1377 | + UserAssociation userAssociation = |
| 1378 | + BasicAuthenticatorDataHolder.getInstance().getOrganizationUserSharingService() |
| 1379 | + .getUserAssociation(userID, currentOrganizationId); |
| 1380 | + if (userAssociation == null) { |
| 1381 | + // User is not a shared user. Hence, return false. |
| 1382 | + return false; |
| 1383 | + } |
| 1384 | + } catch (OrganizationManagementException e) { |
| 1385 | + throw new UserStoreClientException( |
| 1386 | + "Error while checking the user association of the user: " + userID + " with the organization: " + |
| 1387 | + currentOrganizationId, e); |
| 1388 | + } |
| 1389 | + return true; |
| 1390 | + } |
| 1391 | + |
1326 | 1392 | } |
0 commit comments