Skip to content

Commit

Permalink
Merge pull request #865 from Malith-19/username-recovery-multichannel…
Browse files Browse the repository at this point in the history
…-support-configs

Add Username Recovery multichannel support configs
  • Loading branch information
ashensw authored Nov 14, 2024
2 parents 03ad2b8 + cf58038 commit a1146e2
Show file tree
Hide file tree
Showing 10 changed files with 962 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ public class IdentityGovernanceServiceImpl implements IdentityGovernanceService
private static final String EMAIL_LINK_PASSWORD_RECOVERY_PROPERTY
= "Recovery.Notification.Password.emailLink.Enable";
private static final String SMS_OTP_PASSWORD_RECOVERY_PROPERTY = "Recovery.Notification.Password.smsOtp.Enable";
private static final String USERNAME_RECOVERY_ENABLE = "Recovery.Notification.Username.Enable";
private static final String USERNAME_RECOVERY_EMAIL_ENABLE = "Recovery.Notification.Username.Email.Enable";
private static final String USERNAME_RECOVERY_SMS_ENABLE = "Recovery.Notification.Username.SMS.Enable";
private static final String FALSE_STRING = "false";

public void updateConfiguration(String tenantDomain, Map<String, String> configurationDetails)
Expand All @@ -70,6 +73,7 @@ public void updateConfiguration(String tenantDomain, Map<String, String> configu
updateEmailOTPNumericPropertyValue(configurationDetails);
IdPManagementUtil.validatePasswordRecoveryPropertyValues(configurationDetails);
updatePasswordRecoveryPropertyValues(configurationDetails, identityMgtProperties);
updateUsernameRecoveryPropertyValues(configurationDetails, identityMgtProperties);
for (IdentityProviderProperty identityMgtProperty : identityMgtProperties) {
IdentityProviderProperty prop = new IdentityProviderProperty();
String key = identityMgtProperty.getName();
Expand Down Expand Up @@ -407,4 +411,63 @@ private void updatePasswordRecoveryPropertyValues(Map<String, String> configurat
}
}
}

/**
* This method updates the username recovery property values based on the new configurations.
*
* @param configurationDetails Updating configuration details of the resident identity provider.
* @param identityMgtProperties Identity management properties of the resident identity provider.
*/
private void updateUsernameRecoveryPropertyValues(Map<String, String> configurationDetails,
IdentityProviderProperty[] identityMgtProperties) {

if (configurationDetails.containsKey(USERNAME_RECOVERY_ENABLE) ||
configurationDetails.containsKey(USERNAME_RECOVERY_EMAIL_ENABLE) ||
configurationDetails.containsKey(USERNAME_RECOVERY_SMS_ENABLE)) {

String usernameRecoveryProp = configurationDetails.get(USERNAME_RECOVERY_ENABLE);
String usernameRecoveryEmailProp = configurationDetails.get(USERNAME_RECOVERY_EMAIL_ENABLE);
String usernameRecoverySmsProp = configurationDetails.get(USERNAME_RECOVERY_SMS_ENABLE);

boolean usernameRecoveryProperty = Boolean.parseBoolean(usernameRecoveryProp);
boolean usernameRecoveryEmailProperty = Boolean.parseBoolean(usernameRecoveryEmailProp);
boolean usernameRecoverySmsProperty = Boolean.parseBoolean(usernameRecoverySmsProp);

if(usernameRecoveryProperty) {
configurationDetails.put(USERNAME_RECOVERY_EMAIL_ENABLE,
String.valueOf(usernameRecoveryEmailProperty ||
StringUtils.isBlank(usernameRecoveryEmailProp)));
configurationDetails.put(USERNAME_RECOVERY_SMS_ENABLE,
String.valueOf(usernameRecoverySmsProperty ||
StringUtils.isBlank(usernameRecoverySmsProp)));
} else if (StringUtils.isBlank(usernameRecoveryProp)) {
// Connector is not explicitly enabled or disabled. The connector state is derived from new and existing
// configurations.
boolean isUsernameEmailRecoveryCurrentlyEnabled = false;
boolean isUsernameSmsRecoveryCurrentlyEnabled = false;
for (IdentityProviderProperty identityMgtProperty : identityMgtProperties) {
if (USERNAME_RECOVERY_EMAIL_ENABLE.equals(identityMgtProperty.getName())) {
isUsernameEmailRecoveryCurrentlyEnabled = Boolean.parseBoolean(identityMgtProperty.getValue());
} else if (USERNAME_RECOVERY_SMS_ENABLE.equals(identityMgtProperty.getName())) {
isUsernameSmsRecoveryCurrentlyEnabled = Boolean.parseBoolean(identityMgtProperty.getValue());
}
}
boolean enableUsernameEmailRecovery = usernameRecoveryEmailProperty ||
( StringUtils.isBlank(usernameRecoveryEmailProp) &&
isUsernameEmailRecoveryCurrentlyEnabled );
boolean enableUsernameSmsRecovery = usernameRecoverySmsProperty ||
( StringUtils.isBlank(usernameRecoverySmsProp) &&
isUsernameSmsRecoveryCurrentlyEnabled );
configurationDetails.put(USERNAME_RECOVERY_EMAIL_ENABLE,
String.valueOf(enableUsernameEmailRecovery));
configurationDetails.put(USERNAME_RECOVERY_SMS_ENABLE,
String.valueOf(enableUsernameSmsRecovery));
configurationDetails.put(USERNAME_RECOVERY_ENABLE,
String.valueOf(enableUsernameEmailRecovery || enableUsernameSmsRecovery));
} else {
configurationDetails.put(USERNAME_RECOVERY_EMAIL_ENABLE, FALSE_STRING);
configurationDetails.put(USERNAME_RECOVERY_SMS_ENABLE, FALSE_STRING);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,255 @@
/*
* Copyright (c) 2024, WSO2 LLC. (https://www.wso2.org)
*
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.wso2.carbon.identity.governance;

import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.MockedStatic;
import org.mockito.MockitoAnnotations;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.wso2.carbon.identity.application.common.model.FederatedAuthenticatorConfig;
import org.wso2.carbon.identity.application.common.model.IdentityProvider;
import org.wso2.carbon.identity.application.common.model.IdentityProviderProperty;
import org.wso2.carbon.identity.governance.internal.IdentityMgtServiceDataHolder;
import org.wso2.carbon.idp.mgt.IdentityProviderManagementException;
import org.wso2.carbon.idp.mgt.IdpManager;

import java.util.HashMap;
import java.util.Map;

import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.testng.AssertJUnit.assertEquals;

public class IdentityGovernanceServiceImplTest {

// Constants.
private static final String TENANT_DOMAIN = "carbon.super";
private static final String TRUE_STRING = "true";
private static final String FALSE_STRING = "false";
private static final String USERNAME_RECOVERY_ENABLE = "Recovery.Notification.Username.Enable";
private static final String USERNAME_RECOVERY_EMAIL_ENABLE = "Recovery.Notification.Username.Email.Enable";
private static final String USERNAME_RECOVERY_SMS_ENABLE = "Recovery.Notification.Username.SMS.Enable";

@Mock
IdentityMgtServiceDataHolder identityMgtServiceDataHolder;

@Mock
IdpManager idpManager;

@Mock
IdentityProvider identityProvider;

MockedStatic<IdentityMgtServiceDataHolder> identityMgtServiceDataHolderMockedStatic;

private IdentityGovernanceServiceImpl identityGovernanceService;

@BeforeMethod
public void setup() throws IdentityProviderManagementException {

MockitoAnnotations.openMocks(this);
identityMgtServiceDataHolderMockedStatic = mockStatic(IdentityMgtServiceDataHolder.class);
identityMgtServiceDataHolderMockedStatic.when(IdentityMgtServiceDataHolder::
getInstance).thenReturn(identityMgtServiceDataHolder);
when(identityMgtServiceDataHolder.getIdpManager()).thenReturn(idpManager);
when(idpManager.getResidentIdP(TENANT_DOMAIN)).thenReturn(identityProvider);

FederatedAuthenticatorConfig[] authenticatorConfigs = new FederatedAuthenticatorConfig[0];
when(identityProvider.getFederatedAuthenticatorConfigs()).thenReturn(authenticatorConfigs);

identityGovernanceService = new IdentityGovernanceServiceImpl();
}

@AfterMethod
public void tearDown() {

identityMgtServiceDataHolderMockedStatic.close();
}

@Test(dataProvider = "updateConfigurations")
public void testUpdateConfiguration(Map<String, String> configurationDetails,
IdentityProviderProperty[] identityProviderProperties,
Map<String, String> expected) throws IdentityGovernanceException {

when(identityProvider.getIdpProperties()).thenReturn(identityProviderProperties);

identityGovernanceService.updateConfiguration(TENANT_DOMAIN, configurationDetails);

// Capture the arguments passed to setIdpProperties
ArgumentCaptor<IdentityProviderProperty[]>
argumentCaptor = ArgumentCaptor.forClass(IdentityProviderProperty[].class);
verify(identityProvider).setIdpProperties(argumentCaptor.capture());

// Assert
IdentityProviderProperty[] capturedProperties = argumentCaptor.getValue();
for (IdentityProviderProperty capturedProperty : capturedProperties) {
assertEquals(expected.get(capturedProperty.getName()), capturedProperty.getValue());
}

}

@DataProvider(name = "updateConfigurations")
public Object[][] buildConfigurations() {

// Only email config is true. Preconditions: all the configs false.
Map<String, String> usernameConfig1 = new HashMap<>();
usernameConfig1.put(USERNAME_RECOVERY_EMAIL_ENABLE, TRUE_STRING);

IdentityProviderProperty[] identityProviderProperties1 = getIdentityProviderProperties(
false, false, false);
Map<String, String> expected1 = getExpectedPropertyValues(true, true, false);

// Only sms config is true. Preconditions: all the configs false.
Map<String, String> usernameConfig2 = new HashMap<>();
usernameConfig2.put(USERNAME_RECOVERY_SMS_ENABLE, TRUE_STRING);

IdentityProviderProperty[] identityProviderProperties2 = getIdentityProviderProperties(
false, false, false);
Map<String, String> expected2 = getExpectedPropertyValues(true, false, true);

// Only sms is false. Preconditions: sms and username is true.
Map<String, String> usernameConfig3 = new HashMap<>();
usernameConfig3.put(USERNAME_RECOVERY_SMS_ENABLE, FALSE_STRING);

IdentityProviderProperty[] identityProviderProperties3 = getIdentityProviderProperties(
true, false, true);
Map<String, String> expected3 = getExpectedPropertyValues(false, false, false);

// Only email is false. Preconditions: email and username is true.
Map<String, String> usernameConfig4 = new HashMap<>();
usernameConfig4.put(USERNAME_RECOVERY_EMAIL_ENABLE, FALSE_STRING);

IdentityProviderProperty[] identityProviderProperties4 = getIdentityProviderProperties(
true, true, false);
Map<String, String> expected4 = getExpectedPropertyValues(false, false, false);

// Only email is true. Preconditions: sms and username is true.
Map<String, String> usernameConfig5 = new HashMap<>();
usernameConfig5.put(USERNAME_RECOVERY_EMAIL_ENABLE, TRUE_STRING);

IdentityProviderProperty[] identityProviderProperties5 = getIdentityProviderProperties(
true, false, true);
Map<String, String> expected5 = getExpectedPropertyValues(true, true, true);

// Only sms is true. Preconditions: email and username is true.
Map<String, String> usernameConfig6 = new HashMap<>();
usernameConfig6.put(USERNAME_RECOVERY_SMS_ENABLE, TRUE_STRING);

IdentityProviderProperty[] identityProviderProperties6 = getIdentityProviderProperties(
true, true, false);
Map<String, String> expected6 = getExpectedPropertyValues(true, true, true);

// Sms config true and email config false. Preconditions: all the configs false.
Map<String, String> usernameConfig7 = new HashMap<>();
usernameConfig7.put(USERNAME_RECOVERY_SMS_ENABLE, TRUE_STRING);
usernameConfig7.put(USERNAME_RECOVERY_EMAIL_ENABLE, FALSE_STRING);

IdentityProviderProperty[] identityProviderProperties7 = getIdentityProviderProperties(
false, false, false);
Map<String, String> expected7 = getExpectedPropertyValues(true, false, true);

// Email config true and sms config false. Preconditions: all the configs false.
Map<String, String> usernameConfig8 = new HashMap<>();
usernameConfig8.put(USERNAME_RECOVERY_EMAIL_ENABLE, TRUE_STRING);
usernameConfig8.put(USERNAME_RECOVERY_SMS_ENABLE, FALSE_STRING);

IdentityProviderProperty[] identityProviderProperties8 = getIdentityProviderProperties(
false, false, false);
Map<String, String> expected8 = getExpectedPropertyValues(true, true, false);

// Sms config true and email config true. Preconditions: all the configs false.
Map<String, String> usernameConfig9 = new HashMap<>();
usernameConfig9.put(USERNAME_RECOVERY_SMS_ENABLE, TRUE_STRING);
usernameConfig9.put(USERNAME_RECOVERY_EMAIL_ENABLE, TRUE_STRING);

IdentityProviderProperty[] identityProviderProperties9 = getIdentityProviderProperties(
false, false, false);
Map<String, String> expected9 = getExpectedPropertyValues(true, true, true);

// Only username config true. Preconditions: all the configs false.
Map<String, String> usernameConfig10 = new HashMap<>();
usernameConfig10.put(USERNAME_RECOVERY_ENABLE, TRUE_STRING);

IdentityProviderProperty[] identityProviderProperties10 = getIdentityProviderProperties(
false, false, false);
Map<String, String> expected10 = getExpectedPropertyValues(true, true, true);

// Only username config false. Preconditions: all the configs true.
Map<String, String> usernameConfig11 = new HashMap<>();
usernameConfig11.put(USERNAME_RECOVERY_ENABLE, FALSE_STRING);

IdentityProviderProperty[] identityProviderProperties11 = getIdentityProviderProperties(
true, true, true);
Map<String, String> expected11 = getExpectedPropertyValues(false, false, false);

return new Object[][]{
{usernameConfig1, identityProviderProperties1, expected1},
{usernameConfig2, identityProviderProperties2, expected2},
{usernameConfig3, identityProviderProperties3, expected3},
{usernameConfig4, identityProviderProperties4, expected4},
{usernameConfig5, identityProviderProperties5, expected5},
{usernameConfig6, identityProviderProperties6, expected6},
{usernameConfig7, identityProviderProperties7, expected7},
{usernameConfig8, identityProviderProperties8, expected8},
{usernameConfig9, identityProviderProperties9, expected9},
{usernameConfig10, identityProviderProperties10, expected10},
{usernameConfig11, identityProviderProperties11, expected11}
};

}

private IdentityProviderProperty[] getIdentityProviderProperties(boolean usernameEnable,
boolean usernameEmailEnable,
boolean usernameSmsEnable) {

IdentityProviderProperty identityProviderProperty1 = new IdentityProviderProperty();
identityProviderProperty1.setName(USERNAME_RECOVERY_ENABLE);
identityProviderProperty1.setValue(usernameEnable ? TRUE_STRING : FALSE_STRING);

IdentityProviderProperty identityProviderProperty2 = new IdentityProviderProperty();
identityProviderProperty2.setName(USERNAME_RECOVERY_SMS_ENABLE);
identityProviderProperty2.setValue(usernameSmsEnable ? TRUE_STRING : FALSE_STRING);

IdentityProviderProperty identityProviderProperty3 = new IdentityProviderProperty();
identityProviderProperty3.setName(USERNAME_RECOVERY_EMAIL_ENABLE);
identityProviderProperty3.setValue(usernameEmailEnable ? TRUE_STRING : FALSE_STRING);

return new IdentityProviderProperty[]{
identityProviderProperty1,
identityProviderProperty2,
identityProviderProperty3
};
}

private HashMap<String, String> getExpectedPropertyValues(boolean usernameEnable,
boolean usernameEmailEnable,
boolean usernameSmsEnable) {

HashMap<String, String> expected = new HashMap<>();
expected.put(USERNAME_RECOVERY_ENABLE, usernameEnable ? TRUE_STRING : FALSE_STRING);
expected.put(USERNAME_RECOVERY_EMAIL_ENABLE, usernameEmailEnable ? TRUE_STRING : FALSE_STRING);
expected.put(USERNAME_RECOVERY_SMS_ENABLE, usernameSmsEnable ? TRUE_STRING : FALSE_STRING);

return expected;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@

<test name="identity-governance-tests" preserve-order="true" parallel="false">
<classes>
<class name="org.wso2.carbon.identity.governance.listener.IdentityMgtEventListenerTest"></class>
<class name="org.wso2.carbon.identity.governance.listener.IdentityStoreEventListenerTest"></class>
<class name="org.wso2.carbon.identity.governance.IdentityGovernanceServiceImplTest"/>
<class name="org.wso2.carbon.identity.governance.listener.IdentityMgtEventListenerTest"/>
<class name="org.wso2.carbon.identity.governance.listener.IdentityStoreEventListenerTest"/>
<class name="org.wso2.carbon.identity.governance.listener.NotificationTemplateManagerTest"></class>
<class name="org.wso2.carbon.identity.governance.internal.service.impl.notification.DefaultNotificationChannelManagerTest"/>
</classes>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,8 @@ public static class ConnectorConfig {
public static final String FORCE_ADD_PW_RECOVERY_QUESTION = "Recovery.Question.Password.Forced.Enable";
public static final String FORCE_MIN_NO_QUESTION_ANSWERED = "Recovery.Question.MinQuestionsToAnswer";
public static final String USERNAME_RECOVERY_ENABLE = "Recovery.Notification.Username.Enable";
public static final String USERNAME_RECOVERY_EMAIL_ENABLE = "Recovery.Notification.Username.Email.Enable";
public static final String USERNAME_RECOVERY_SMS_ENABLE = "Recovery.Notification.Username.SMS.Enable";
public static final String USERNAME_RECOVERY_NON_UNIQUE_USERNAME = "Recovery.Notification.Username.NonUniqueUsername";
public static final String QUESTION_CHALLENGE_SEPARATOR = "Recovery.Question.Password.Separator";
public static final String QUESTION_MIN_NO_ANSWER = "Recovery.Question.Password.MinAnswers";
Expand Down
Loading

0 comments on commit a1146e2

Please sign in to comment.