From 36903948d005247139cda3c72db52d083a8f24b8 Mon Sep 17 00:00:00 2001 From: rushannanayakkara Date: Tue, 26 Nov 2024 07:59:03 +0530 Subject: [PATCH 1/2] Pick username java regex from alternative key if not resolved for non-primary user stores. --- .../identity/recovery/signup/UserSelfRegistrationManager.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/components/org.wso2.carbon.identity.recovery/src/main/java/org/wso2/carbon/identity/recovery/signup/UserSelfRegistrationManager.java b/components/org.wso2.carbon.identity.recovery/src/main/java/org/wso2/carbon/identity/recovery/signup/UserSelfRegistrationManager.java index 6577ac269..56aa56fe3 100644 --- a/components/org.wso2.carbon.identity.recovery/src/main/java/org/wso2/carbon/identity/recovery/signup/UserSelfRegistrationManager.java +++ b/components/org.wso2.carbon.identity.recovery/src/main/java/org/wso2/carbon/identity/recovery/signup/UserSelfRegistrationManager.java @@ -1642,6 +1642,10 @@ private boolean validateAgainstRegex(String userName, RealmConfiguration realmCo String regularExpression = realmConfig .getUserStoreProperty(UserCoreConstants.RealmConfig.PROPERTY_USER_NAME_JAVA_REG_EX); + if (regularExpression == null && !realmConfig.isPrimary()) { + regularExpression = realmConfig.getUserStoreProperty( + UserCoreConstants.RealmConfig.PROPERTY_USER_NAME_JAVA_REG); + } if (MultitenantUtils.isEmailUserName()) { regularExpression = realmConfig From df1b2e7b98ee03e3d2d709047e9954dcabf4e52c Mon Sep 17 00:00:00 2001 From: rushannanayakkara Date: Tue, 26 Nov 2024 10:07:21 +0530 Subject: [PATCH 2/2] Add unit tests for isMatchUserNameRegex function for secondary user stores. --- .../UserSelfRegistrationManagerTest.java | 67 ++++++++++++++++++- 1 file changed, 66 insertions(+), 1 deletion(-) diff --git a/components/org.wso2.carbon.identity.recovery/src/test/java/org/wso2/carbon/identity/recovery/signup/UserSelfRegistrationManagerTest.java b/components/org.wso2.carbon.identity.recovery/src/test/java/org/wso2/carbon/identity/recovery/signup/UserSelfRegistrationManagerTest.java index 9d50cf949..639d0cbf9 100644 --- a/components/org.wso2.carbon.identity.recovery/src/test/java/org/wso2/carbon/identity/recovery/signup/UserSelfRegistrationManagerTest.java +++ b/components/org.wso2.carbon.identity.recovery/src/test/java/org/wso2/carbon/identity/recovery/signup/UserSelfRegistrationManagerTest.java @@ -236,7 +236,9 @@ public class UserSelfRegistrationManagerTest { private final String TEST_TENANT_DOMAIN_NAME = "carbon.super"; private final int TEST_TENANT_ID = 12; private final String TEST_USERSTORE_DOMAIN = "PRIMARY"; + private final String TEST_SECONDARY_USERSTORE_DOMAIN = "SECONDARY"; private final String TEST_USER_NAME = "dummyUser"; + private final String TEST_INVALID_USER_NAME = "IS"; private final String TEST_CLAIM_URI = "ttp://wso2.org/claims/emailaddress"; private final String TEST_CLAIM_VALUE = "dummyuser@wso2.com"; private final String TEST_MOBILE_CLAIM_VALUE = "0775553443"; @@ -1429,6 +1431,7 @@ public void testIsMatchUserNameRegex() mockedUserCoreUtil.when(() -> UserCoreUtil.removeDomainFromName(anyString())).thenReturn(TEST_USER_NAME); mockedIdentityUtil.when(() -> IdentityUtil.extractDomainFromName(anyString())) .thenReturn(TEST_USERSTORE_DOMAIN); + when(realmConfiguration.isPrimary()).thenReturn(true); when(realmConfiguration.getTenantId()).thenReturn(TEST_TENANT_ID); mockedIdentityUtil.when(() -> IdentityTenantUtil.getTenantDomain(TEST_TENANT_ID)) .thenReturn(TEST_TENANT_DOMAIN_NAME); @@ -1438,10 +1441,16 @@ public void testIsMatchUserNameRegex() when(validationConfiguration.getField()).thenReturn("username"); mockedIdentityUtil.when(() -> IdentityUtil.getProperty(Constants.INPUT_VALIDATION_USERNAME_ENABLED_CONFIG)) .thenReturn("false"); + when(realmConfiguration.getUserStoreProperty(UserCoreConstants.RealmConfig.PROPERTY_USER_NAME_JAVA_REG_EX)) + .thenReturn(null); + boolean isMatchUsernameRegex = + userSelfRegistrationManager.isMatchUserNameRegex(TEST_TENANT_DOMAIN_NAME, TEST_USER_NAME); + assertTrue(isMatchUsernameRegex); + when(realmConfiguration.getUserStoreProperty(UserCoreConstants.RealmConfig.PROPERTY_USER_NAME_JAVA_REG_EX)) .thenReturn("^[\\S]{5,30}$"); - boolean isMatchUsernameRegex = + isMatchUsernameRegex = userSelfRegistrationManager.isMatchUserNameRegex(TEST_TENANT_DOMAIN_NAME, TEST_USER_NAME); assertTrue(isMatchUsernameRegex); @@ -1486,6 +1495,62 @@ public void testIsMatchUserNameRegex() } } + @Test + public void testIsMatchUserNameRegexForSecondaryUserStore() + throws IdentityRecoveryException, InputValidationMgtException, + UserStoreException { + + mockedMultiTenantUtils.when(() -> MultitenantUtils + .getTenantAwareUsername(eq(TEST_USER_NAME))).thenReturn(TEST_USER_NAME); + mockedUserCoreUtil.when(() -> UserCoreUtil.removeDomainFromName(eq(TEST_USER_NAME))).thenReturn(TEST_USER_NAME); + mockedIdentityUtil.when(() -> IdentityUtil.extractDomainFromName(anyString())) + .thenReturn(TEST_SECONDARY_USERSTORE_DOMAIN); + when(realmConfiguration.getTenantId()).thenReturn(TEST_TENANT_ID); + mockedIdentityUtil.when(() -> IdentityTenantUtil.getTenantDomain(TEST_TENANT_ID)) + .thenReturn(TEST_TENANT_DOMAIN_NAME); + + when(inputValidationManagementService.getInputValidationConfiguration(anyString())) + .thenReturn(Arrays.asList(validationConfiguration)); + when(validationConfiguration.getField()).thenReturn("username"); + mockedIdentityUtil.when(() -> IdentityUtil.getProperty(Constants.INPUT_VALIDATION_USERNAME_ENABLED_CONFIG)) + .thenReturn("false"); + when(realmConfiguration.getUserStoreProperty(UserCoreConstants.RealmConfig.PROPERTY_USER_NAME_JAVA_REG_EX)) + .thenReturn(null); + when(realmConfiguration.getUserStoreProperty(UserCoreConstants.RealmConfig.PROPERTY_USER_NAME_JAVA_REG)) + .thenReturn("^[\\S]{5,30}$"); + + // regex read from fallback key + boolean isMatchUsernameRegex = + userSelfRegistrationManager.isMatchUserNameRegex(TEST_TENANT_DOMAIN_NAME, TEST_USER_NAME); + assertTrue(isMatchUsernameRegex); + + mockedMultiTenantUtils.when(() -> MultitenantUtils + .getTenantAwareUsername(eq(TEST_INVALID_USER_NAME))).thenReturn(TEST_INVALID_USER_NAME); + mockedUserCoreUtil.when(() -> UserCoreUtil.removeDomainFromName(eq(TEST_INVALID_USER_NAME))) + .thenReturn(TEST_INVALID_USER_NAME); + isMatchUsernameRegex = + userSelfRegistrationManager.isMatchUserNameRegex(TEST_TENANT_DOMAIN_NAME, TEST_INVALID_USER_NAME); + assertFalse(isMatchUsernameRegex); + + // regex read main key + when(realmConfiguration.getUserStoreProperty(UserCoreConstants.RealmConfig.PROPERTY_USER_NAME_JAVA_REG_EX)) + .thenReturn("^[\\S]{5,30}$"); + when(realmConfiguration.getUserStoreProperty(UserCoreConstants.RealmConfig.PROPERTY_USER_NAME_JAVA_REG)) + .thenReturn(null); + isMatchUsernameRegex = + userSelfRegistrationManager.isMatchUserNameRegex(TEST_TENANT_DOMAIN_NAME, TEST_USER_NAME); + assertTrue(isMatchUsernameRegex); + + mockedMultiTenantUtils.when(() -> MultitenantUtils + .getTenantAwareUsername(eq(TEST_INVALID_USER_NAME))).thenReturn(TEST_INVALID_USER_NAME); + mockedUserCoreUtil.when(() -> UserCoreUtil.removeDomainFromName(eq(TEST_INVALID_USER_NAME))) + .thenReturn(TEST_INVALID_USER_NAME); + isMatchUsernameRegex = + userSelfRegistrationManager.isMatchUserNameRegex(TEST_TENANT_DOMAIN_NAME, TEST_INVALID_USER_NAME); + assertFalse(isMatchUsernameRegex); + + } + @Test public void testPreValidatePasswordWithConfirmationKey() throws Exception {