Add username preprocess method considering the service provider
@@ -2895,6 +2895,38 @@ public class FrameworkUtils {
return username;
}
+ /**
+ * Pre-process user's username considering the service provider.
+ *
+ * @param username Username of the user.
+ * @param serviceProvider The service provider.
+ * @return preprocessed username
+ */
+ public static String preprocessUsername(String username, ServiceProvider serviceProvider) {
+
+ boolean isSaaSApp = serviceProvider.isSaasApp();
+ String appTenantDomain = serviceProvider.getOwner().getTenantDomain();
+
+ if (isLegacySaaSAuthenticationEnabled() && isSaaSApp) {
+ return username;
+ }
+
+ if (IdentityUtil.isEmailUsernameEnabled()) {
+ if (StringUtils.countMatches(username, "@") == 1) {
+ return username + "@" + appTenantDomain;
+ }
+ } else if (!username.endsWith(appTenantDomain)) {
+
+ // If the username is email-type (without enabling email username option) or belongs to a tenant which is
+ // not the app owner.
+ if (isSaaSApp && StringUtils.countMatches(username, "@") >= 1) {
+ return username;
+ }
+ return username + "@" + appTenantDomain;
+ }
+ return username;
+ }
+
/**
* Gets resolvedUserResult from multi attribute login identifier if enable multi attribute login.
*
@@ -2933,6 +2965,23 @@ public class FrameworkUtils {
}
}
+ /**
+ * Validate the username.
+ *
+ * @param username Username of the user.
+ * @throws InvalidCredentialsException when username is not valid.
+ */
+ public static void validateUsername(String username) throws InvalidCredentialsException {
+
+ // Validate username as an email when email username is enabled.
+ if (IdentityUtil.isEmailUsernameEnabled()) {
+ String tenantAwareUsername = MultitenantUtils.getTenantAwareUsername(username);
+ if (StringUtils.countMatches(tenantAwareUsername, "@") < 1) {
+ throw new InvalidCredentialsException("Invalid username. Username has to be an email.");
+ }
+ }
+ }
+
private static String addUserId(String username, UserStoreManager userStoreManager) {
String userId;
