Skip to content

Commit

Permalink
Merge pull request #881 from RushanNanayakkara/patch_internal_2647
Browse files Browse the repository at this point in the history
Pick username java regex from alternative key
  • Loading branch information
RushanNanayakkara authored Nov 27, 2024
2 parents c0ac3af + df1b2e7 commit 99dd80d
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "[email protected]";
private final String TEST_MOBILE_CLAIM_VALUE = "0775553443";
Expand Down Expand Up @@ -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);
Expand All @@ -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);

Expand Down Expand Up @@ -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 {

Expand Down

0 comments on commit 99dd80d

Please sign in to comment.