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

Pick username java regex from alternative key #881

Merged
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 @@ -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 = "dummyuser@wso2.com";
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
Loading