Skip to content

Commit

Permalink
remove b2b flow user resolver path
Browse files Browse the repository at this point in the history
  • Loading branch information
Yoshani committed Nov 17, 2022
1 parent d361284 commit e4c7d6e
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -456,12 +456,6 @@ private User resolveUser(HttpServletRequest request, AuthenticationContext conte
user = multiAttributeLoginUser.get();
}

Optional<User> orgUser = UserResolver.resolveUserFromOrganizationHierarchy(context, tenantAwareUsername,
username, tenantDomain);
if (orgUser.isPresent()) {
user = orgUser.get();
}

if (StringUtils.isBlank(user.getUserID())) {
Optional<User> userStoreUser = UserResolver.resolveUserFromUserStore(tenantAwareUsername,
username, tenantDomain);
Expand All @@ -479,7 +473,8 @@ private void setResolvedUserInContext(AuthenticationContext context, User user)
context.setProperties(authProperties);
}

String username = FrameworkUtils.prependUserStoreDomainToName(user.getPreferredUsername());
String username = UserCoreUtil.addTenantDomainToEntry(user.getUsername(), context.getTenantDomain());
username = FrameworkUtils.prependUserStoreDomainToName(username);
authProperties.put(MagicLinkAuthenticatorConstants.USER_NAME, username);
addUsernameToContext(context, username);
setSubjectInContextWithUserId(context, user);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,17 @@
*/
package org.wso2.carbon.identity.application.authenticator.magiclink;

import org.apache.commons.lang.StringUtils;
import org.wso2.carbon.identity.application.authentication.framework.context.AuthenticationContext;
import org.wso2.carbon.identity.application.authentication.framework.exception.AuthenticationFailedException;
import org.wso2.carbon.identity.application.authentication.framework.exception.InvalidCredentialsException;
import org.wso2.carbon.identity.application.authenticator.magiclink.internal.MagicLinkServiceDataHolder;
import org.wso2.carbon.identity.application.authenticator.magiclink.util.MagicLinkAuthErrorConstants;
import org.wso2.carbon.identity.base.IdentityRuntimeException;
import org.wso2.carbon.identity.core.util.IdentityTenantUtil;
import org.wso2.carbon.identity.multi.attribute.login.mgt.ResolvedUserResult;
import org.wso2.carbon.identity.organization.management.service.exception.OrganizationManagementException;
import org.wso2.carbon.user.api.UserRealm;
import org.wso2.carbon.user.api.UserStoreException;
import org.wso2.carbon.user.core.common.AbstractUserStoreManager;
import org.wso2.carbon.user.core.common.User;
import org.wso2.carbon.user.core.tenant.Tenant;
import org.wso2.carbon.user.core.util.UserCoreUtil;
import org.wso2.carbon.utils.multitenancy.MultitenantConstants;
import org.wso2.carbon.utils.multitenancy.MultitenantUtils;

import java.util.Optional;
Expand Down Expand Up @@ -85,64 +79,6 @@ public static Optional<User> resolveUserFromMultiAttributeLogin(AuthenticationCo
org.wso2.carbon.identity.application.common.model.User.getUserFromUserName(username));
}

/**
* This method resolves the user if it is a B2B flow.
*
* @param context The authentication context.
* @param tenantAwareUsername The tenant-aware username.
* @param username The username.
* @throws AuthenticationFailedException In occasions of failing to resolve user.
*/
public static Optional<User> resolveUserFromOrganizationHierarchy(AuthenticationContext context,
String tenantAwareUsername, String username,
String tenantDomain)
throws AuthenticationFailedException {

if (!canResolveUserFromOrganizationHierarchy(context)) {
return Optional.empty();
}
String requestTenantDomain = context.getUserTenantDomain();
try {
int tenantId = IdentityTenantUtil.getTenantId(requestTenantDomain);
Tenant tenant = (Tenant) MagicLinkServiceDataHolder.getInstance().getRealmService().getTenantManager()
.getTenant(tenantId);
if (tenant != null && StringUtils.isNotBlank(tenant.getAssociatedOrganizationUUID())) {
User user = MagicLinkServiceDataHolder.getInstance()
.getOrganizationUserResidentResolverService()
.resolveUserFromResidentOrganization(tenantAwareUsername, null,
tenant.getAssociatedOrganizationUUID())
.orElseThrow(() -> new AuthenticationFailedException(
MagicLinkAuthErrorConstants.ErrorMessages.USER_NOT_IDENTIFIED_IN_HIERARCHY.getCode()));

tenantAwareUsername = user.getUsername();
username = UserCoreUtil.addTenantDomainToEntry(tenantAwareUsername, user.getTenantDomain());
setUserParams(user, tenantAwareUsername, username, tenantDomain);
return Optional.of(user);
}
} catch (OrganizationManagementException e) {
throw new AuthenticationFailedException(
MagicLinkAuthErrorConstants.ErrorMessages
.ORGANIZATION_MGT_EXCEPTION_WHILE_TRYING_TO_RESOLVE_RESIDENT_ORG.getCode(), e.getMessage(),
org.wso2.carbon.identity.application.common.model.User.getUserFromUserName(username), e);
} catch (UserStoreException e) {
throw new AuthenticationFailedException(
MagicLinkAuthErrorConstants.ErrorMessages
.USER_STORE_EXCEPTION_WHILE_TRYING_TO_AUTHENTICATE.getCode(), e.getMessage(),
org.wso2.carbon.identity.application.common.model.User.getUserFromUserName(username), e);
}
return Optional.empty();
}

private static boolean canResolveUserFromOrganizationHierarchy(AuthenticationContext context) {

if (context.getCallerPath() != null && context.getCallerPath().startsWith("/t/")) {
return true;
}
String requestTenantDomain = context.getUserTenantDomain();
return StringUtils.isNotBlank(requestTenantDomain) &&
!MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equalsIgnoreCase(requestTenantDomain);
}

/**
* This method resolves the user from user store.
*
Expand Down Expand Up @@ -189,9 +125,9 @@ public static Optional<User> resolveUserFromUserStore(String tenantAwareUsername
}

private static void setUserParams(User user, String tenantAwareUsername, String username, String tenantDomain) {

user.setUsername(tenantAwareUsername);
user.setUserStoreDomain(UserCoreUtil.extractDomainFromName(username));
user.setPreferredUsername(username);
user.setTenantDomain(tenantDomain);
}
}

0 comments on commit e4c7d6e

Please sign in to comment.