Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve the post authentication handler to set the correct username in the carbon context for organization SSO users #254

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions components/org.wso2.carbon.identity.auth.service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,9 @@
org.osgi.util.tracker; version="${osgi.util.tracker.imp.pkg.version.range}",
org.wso2.carbon.core.*; version="${carbon.kernel.imp.pkg.version.range}",
org.wso2.carbon.user.core.util; version="${carbon.kernel.imp.pkg.version.range}",
org.wso2.carbon.user.core.common; version="${carbon.kernel.imp.pkg.version.range}",
org.wso2.carbon.user.core.service; version="${carbon.kernel.imp.pkg.version.range}",
org.wso2.carbon.user.api; version="${carbon.user.api.imp.pkg.version.range}",
org.wso2.carbon.utils.*; version="${carbon.kernel.imp.pkg.version.range}",
org.wso2.carbon.identity.application.common.model.*;
version="${carbon.identity.package.import.version.range}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,15 @@
import org.wso2.carbon.identity.auth.service.exception.AuthClientException;
import org.wso2.carbon.identity.auth.service.exception.AuthServerException;
import org.wso2.carbon.identity.auth.service.exception.AuthenticationFailException;
import org.wso2.carbon.identity.auth.service.internal.AuthenticationServiceHolder;
import org.wso2.carbon.identity.core.bean.context.MessageContext;
import org.wso2.carbon.identity.core.handler.AbstractIdentityMessageHandler;
import org.wso2.carbon.identity.core.util.IdentityUtil;
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.service.RealmService;
import org.wso2.carbon.user.core.util.UserCoreUtil;
import org.wso2.carbon.utils.multitenancy.MultitenantUtils;

Expand Down Expand Up @@ -112,14 +118,11 @@ protected void postAuthenticate(MessageContext messageContext, AuthenticationRes
}

if (user.getTenantDomain() != null && (user.getTenantDomain()
.equalsIgnoreCase(PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain()) ||
StringUtils.isNotEmpty(authorizedOrganization))) {
.equalsIgnoreCase(PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain()))) {
PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername(IdentityUtil.addDomainToName
(user.getUserName(), user.getUserStoreDomain()));
// Set the user's resident organization if user is accessing an organization
PrivilegedCarbonContext.getThreadLocalCarbonContext()
.setUserResidentOrganizationId(userResidentOrganization);
}

// Set the user id to the Carbon context if the user authentication is succeeded.
try {
AuthenticatedUser authenticatedUser;
Expand All @@ -130,16 +133,49 @@ protected void postAuthenticate(MessageContext messageContext, AuthenticationRes
String userName = MultitenantUtils.getTenantAwareUsername(authenticatedUser.getUserName());
userName = UserCoreUtil.removeDomainFromName(userName);
PrivilegedCarbonContext.getThreadLocalCarbonContext().setUserId(userName);
return;
} else {
PrivilegedCarbonContext.getThreadLocalCarbonContext()
.setUserId(authenticatedUser.getUserId());
}
} else {
authenticatedUser = new AuthenticatedUser(user);
PrivilegedCarbonContext.getThreadLocalCarbonContext().setUserId(authenticatedUser.getUserId());
}
PrivilegedCarbonContext.getThreadLocalCarbonContext().setUserId(authenticatedUser.getUserId());
} catch (UserIdNotFoundException e) {
LOG.error("User id not found for user: " + user.getLoggableMaskedUserId());
}

if (StringUtils.isNotEmpty(authorizedOrganization)) {
// Set the user's resident organization if user is accessing an organization
PrivilegedCarbonContext.getThreadLocalCarbonContext()
.setUserResidentOrganizationId(userResidentOrganization);
if (((AuthenticatedUser) user).isFederatedUser()) {
updateUserNameInContextForOrganizationSsoUsers(userResidentOrganization);
}
}
}
}
}

private void updateUserNameInContextForOrganizationSsoUsers(String userResidentOrganization) {

try {
String tenantDomain = AuthenticationServiceHolder.getInstance().getOrganizationManager()
.resolveTenantDomain(userResidentOrganization);
int tenantId = AuthenticationServiceHolder.getInstance().getRealmService().getTenantManager()
.getTenantId(tenantDomain);
RealmService realmService = AuthenticationServiceHolder.getInstance().getRealmService();
UserRealm tenantUserRealm = realmService.getTenantUserRealm(tenantId);
if (tenantUserRealm != null) {
String userId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUserId();
org.wso2.carbon.user.core.common.User user =
((AbstractUserStoreManager) tenantUserRealm.getUserStoreManager()).getUser(userId, null);
if (user != null && StringUtils.isNotEmpty(user.getUsername())) {
PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername(user.getUsername());
}
}
} catch (OrganizationManagementException | UserStoreException e) {
LOG.error("Authenticated user's username could not be resolved.", e);
}
}
}
Loading