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 Organization SSO user ID resolving logic to add to the carbon context #277

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
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@
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;

/**
* This is the abstract class for custom authentication handlers.
Expand Down Expand Up @@ -107,76 +105,64 @@ protected void postAuthenticate(MessageContext messageContext, AuthenticationRes

User user = authenticationContext.getUser();
if (user != null) {
// Set the user in to the Carbon context if the user belongs to same tenant or else if the accessing
// organization is authorized to access. Skip this for cross tenant scenarios.

String authorizedOrganization = null;
String userResidentOrganization = null;
if (user instanceof AuthenticatedUser) {
authorizedOrganization = ((AuthenticatedUser) user).getAccessingOrganization();
userResidentOrganization = ((AuthenticatedUser) user).getUserResidentOrganization();
}
// Set the user in to the Carbon context if the user belongs to same tenant. Skip this for cross tenant
// scenarios.

if (user.getTenantDomain() != null && (user.getTenantDomain()
.equalsIgnoreCase(PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain()))) {
if (user.getTenantDomain() != null && user.getTenantDomain().equalsIgnoreCase(PrivilegedCarbonContext
.getThreadLocalCarbonContext().getTenantDomain())) {
PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername(IdentityUtil.addDomainToName
(user.getUserName(), user.getUserStoreDomain()));
}

// Set the user id to the Carbon context if the user authentication is succeeded.
try {
AuthenticatedUser authenticatedUser;
if (user instanceof AuthenticatedUser) {
authenticatedUser = (AuthenticatedUser) user;
// For B2B organization users, set the user ID which is set as username in user object.
if (authenticatedUser.isFederatedUser() && StringUtils.isNotEmpty(authorizedOrganization)) {
String userName = MultitenantUtils.getTenantAwareUsername(authenticatedUser.getUserName());
userName = UserCoreUtil.removeDomainFromName(userName);
PrivilegedCarbonContext.getThreadLocalCarbonContext().setUserId(userName);
} else {
PrivilegedCarbonContext.getThreadLocalCarbonContext()
.setUserId(authenticatedUser.getUserId());
}
PrivilegedCarbonContext.getThreadLocalCarbonContext()
.setUserId(((AuthenticatedUser) user).getUserId());
} else {
authenticatedUser = new AuthenticatedUser(user);
PrivilegedCarbonContext.getThreadLocalCarbonContext().setUserId(authenticatedUser.getUserId());
AuthenticatedUser authenticatedUser = new AuthenticatedUser(user);
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
// Set the username in to the Carbon context if an organization user.
if (user instanceof AuthenticatedUser && ((AuthenticatedUser) user).isOrganizationUser()) {
String username = resolveUserNameForOrganizationUser((AuthenticatedUser) user);
PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername(username);
}

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

private void updateUserNameInContextForOrganizationSsoUsers(String userResidentOrganization) {
private String resolveUserNameForOrganizationUser(AuthenticatedUser authenticatedUser) {

try {
String tenantDomain = AuthenticationServiceHolder.getInstance().getOrganizationManager()
.resolveTenantDomain(userResidentOrganization);
.resolveTenantDomain(authenticatedUser.getUserResidentOrganization());
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.getDomainQualifiedUsername());
}
}
} catch (OrganizationManagementException | UserStoreException e) {
AbstractUserStoreManager userStoreManager = getAbstractUserStoreManager(tenantId);
return userStoreManager.getUserNameFromUserID(authenticatedUser.getUserId());
} catch (OrganizationManagementException | UserStoreException | UserIdNotFoundException e) {
LOG.error("Authenticated user's username could not be resolved.", e);
}
return StringUtils.EMPTY;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Handle from the invocation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the previous implementation, when username didn't get resolved, it simply skip setting the username in the context. In that cases, username might have been set with user-id which is wrong.

Here if fails to resolve the username, it set the username as empty, so that if any downstream task requires to fetch the username, it will not relay on the username in the context as it is set to empty, hence might rely on the user-id if it is properly set in the context.

As setting username, user-id in the context are not directly related to post authentication, but added for any usecase where the request initiator is required (ex; resource creation, auditing etc.), only added an error log instead of throwing an exception.

}

private AbstractUserStoreManager getAbstractUserStoreManager(int tenantId) throws UserStoreException {

RealmService realmService = AuthenticationServiceHolder.getInstance().getRealmService();
UserRealm tenantUserRealm = realmService.getTenantUserRealm(tenantId);
return (AbstractUserStoreManager) tenantUserRealm.getUserStoreManager();
}
}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@
<org.wso2.carbon.identity.cors.valve.version>${project.version}</org.wso2.carbon.identity.cors.valve.version>

<!--Carbon identity version-->
<identity.framework.version>5.25.652</identity.framework.version>
<identity.framework.version>7.3.13</identity.framework.version>
<carbon.identity.package.import.version.range>[5.17.8, 8.0.0)</carbon.identity.package.import.version.range>

<org.wso2.carbon.identity.oauth.version>7.0.65</org.wso2.carbon.identity.oauth.version>
Expand Down
Loading