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

Backport 6.5.x/task 69432 69702 #39

Merged
merged 2 commits into from
Feb 20, 2024
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

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
import org.apache.catalina.connector.Response;
import org.apache.catalina.realm.GenericPrincipal;
import org.apache.tomcat.util.descriptor.web.LoginConfig;
import org.exoplatform.commons.utils.ListAccess;
import org.exoplatform.services.organization.Query;
import org.exoplatform.services.organization.User;
import org.exoplatform.services.organization.UserHandler;
import org.exoplatform.container.ExoContainer;
import org.exoplatform.container.ExoContainerContext;
import org.exoplatform.container.PortalContainer;
Expand Down Expand Up @@ -528,6 +532,8 @@ private boolean handleSAML2Response(Request request, Response response, LoginCon

String username = principal.getName();
String password = ServiceProviderSAMLContext.EMPTY_PASSWORD;

username = checkForEmail(username);

roles.addAll(extractGateinRoles(username));
if (logger.isTraceEnabled()) {
Expand Down Expand Up @@ -784,4 +790,26 @@ private boolean isAjaxRequest(Request request) {
String requestedWithHeader = request.getHeader(GeneralConstants.HTTP_HEADER_X_REQUESTED_WITH);
return requestedWithHeader != null && "XMLHttpRequest".equalsIgnoreCase(requestedWithHeader);
}

private String checkForEmail(String username) {
//allow to use email as identifier in SAML assertion
//if username is an email, we read the related user, and return his username, if there is only one result with this email
try {
if (username.contains("@")) {
OrganizationService organizationService = PortalContainer.getInstance().getComponentInstanceOfType(OrganizationService.class);
UserHandler userHandler = organizationService.getUserHandler();
Query emailQuery = new Query();
emailQuery.setEmail(username);
ListAccess<User> users;
users = userHandler.findUsersByQuery(emailQuery);
if (users != null && users.getSize() == 1) {
return users.load(0, 1)[0].getUserName();
}
}
} catch (Exception e) {
logger.samlSPHandleRequestError(e);
return null;
}
return username;
}
}
Loading