use of org.wso2.carbon.identity.recovery.bean.NotificationResponseBean in project identity-governance by wso2-extensions.
the class MeApiServiceImpl method buildSuccessResponseForExternalChannel.
/**
* Build the successResponseDTO for successful user identification and channel retrieve when the notifications
* are managed externally.
*
* @param notificationResponseBean NotificationResponseBean
* @return SuccessfulUserCreationExternalResponseDTO
*/
private SuccessfulUserCreationExternalResponseDTO buildSuccessResponseForExternalChannel(NotificationResponseBean notificationResponseBean) {
SuccessfulUserCreationExternalResponseDTO successDTO = new SuccessfulUserCreationExternalResponseDTO();
successDTO.setCode(notificationResponseBean.getCode());
successDTO.setMessage(notificationResponseBean.getMessage());
successDTO.setNotificationChannel(notificationResponseBean.getNotificationChannel());
successDTO.setConfirmationCode(notificationResponseBean.getRecoveryId());
return successDTO;
}
use of org.wso2.carbon.identity.recovery.bean.NotificationResponseBean in project identity-governance by wso2-extensions.
the class MeApiServiceImpl method buildSuccessResponseForInternalChannels.
/**
* Build the successResponseDTO for successful user identification and channel retrieve when the notifications
* are managed internally.
*
* @param notificationResponseBean NotificationResponseBean
* @return SuccessfulUserCreationDTO
*/
private SuccessfulUserCreationDTO buildSuccessResponseForInternalChannels(NotificationResponseBean notificationResponseBean) {
SuccessfulUserCreationDTO successDTO = new SuccessfulUserCreationDTO();
successDTO.setCode(notificationResponseBean.getCode());
successDTO.setMessage(notificationResponseBean.getMessage());
successDTO.setNotificationChannel(notificationResponseBean.getNotificationChannel());
return successDTO;
}
use of org.wso2.carbon.identity.recovery.bean.NotificationResponseBean in project identity-governance by wso2-extensions.
the class MeApiServiceImplTest method testMeResendCodePost.
@Test
public void testMeResendCodePost() throws IdentityRecoveryException {
try {
String carbonHome = Paths.get(System.getProperty("user.dir"), "src", "test", "resources").toString();
System.setProperty(CarbonBaseConstants.CARBON_HOME, carbonHome);
PrivilegedCarbonContext.startTenantFlow();
PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername(USERNAME);
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(-1234);
Mockito.when(resendConfirmationManager.resendConfirmationCode(isNull(), anyString(), anyString(), anyString(), isNull())).thenReturn(notificationResponseBean);
mockedUtils.when(() -> Utils.getUserRecoveryData(any(ResendCodeRequestDTO.class), anyString())).thenReturn(userRecoveryData);
mockedUtils.when(Utils::getResendConfirmationManager).thenReturn(resendConfirmationManager);
Mockito.when(userRecoveryData.getRecoveryScenario()).thenReturn(RecoveryScenarios.getRecoveryScenario("MOBILE_VERIFICATION_ON_UPDATE"));
Mockito.when(userRecoveryData.getRecoveryStep()).thenReturn(RecoverySteps.getRecoveryStep("VERIFY_MOBILE_NUMBER"));
assertEquals(meApiService.meResendCodePost(meResendCodeRequestDTO()).getStatus(), 201);
assertEquals(meApiService.meResendCodePost(meResendCodeRequestDTOWithInvalidScenarioProperty()).getStatus(), 400);
mockedUtils.when(() -> Utils.getUserRecoveryData(any(ResendCodeRequestDTO.class), anyString())).thenReturn(null);
assertEquals(meApiService.meResendCodePost(meResendCodeRequestDTO()).getStatus(), 400);
Mockito.when(userRecoveryData.getRecoveryScenario()).thenReturn(RecoveryScenarios.getRecoveryScenario("ASK_PASSWORD"));
assertEquals(meApiService.meResendCodePost(meResendCodeRequestDTO()).getStatus(), 400);
} finally {
PrivilegedCarbonContext.endTenantFlow();
}
}
use of org.wso2.carbon.identity.recovery.bean.NotificationResponseBean in project identity-governance by wso2-extensions.
the class ResendConfirmationManager method resendAccountRecoveryNotification.
/**
* Resend account recovery information to the user.
*
* @param user User object
* @param code Previous confirmation code
* @param recoveryScenario Recovery scenario
* @param recoveryStep Recovery step
* @param notificationType Notification type
* @param properties Event properties
* @return NotificationResponseBean
* @throws IdentityRecoveryException If an error occurred while sending notifications.
*/
private NotificationResponseBean resendAccountRecoveryNotification(User user, String code, String recoveryScenario, String recoveryStep, String notificationType, Property[] properties) throws IdentityRecoveryException {
validateRequestParameters(user, recoveryScenario, recoveryStep, notificationType);
// Resolve the tenant domain and the userstore domain name of the user.
resolveUserAttributes(user);
boolean notificationInternallyManage = isNotificationInternallyManage(user);
NotificationResponseBean notificationResponseBean = new NotificationResponseBean(user);
UserRecoveryDataStore userRecoveryDataStore = JDBCRecoveryDataStore.getInstance();
UserRecoveryData userRecoveryData = userRecoveryDataStore.loadWithoutCodeExpiryValidation(user, RecoveryScenarios.getRecoveryScenario(recoveryScenario));
// Validate the previous confirmation code with the data retrieved by the user recovery information.
validateWithOldConfirmationCode(code, recoveryScenario, recoveryStep, userRecoveryData);
// Get the notification channel details stored in the remainingSetIds.
String storedNotificationChannel = userRecoveryData.getRemainingSetIds();
String preferredChannel = StringUtils.EMPTY;
/* Having a not supported storedNotificationChannel implies that the particular recovery scenario does not store
the notification channel in remainingSetIds column. In that case the notification channel should be EMAIL.*/
if (isServerSupportedNotificationChannel(storedNotificationChannel)) {
preferredChannel = storedNotificationChannel;
if (!notificationInternallyManage) {
preferredChannel = NotificationChannels.EXTERNAL_CHANNEL.getChannelType();
}
}
if (RecoveryScenarios.MOBILE_VERIFICATION_ON_UPDATE.toString().equals(recoveryScenario)) {
preferredChannel = NotificationChannels.SMS_CHANNEL.getChannelType();
}
String secretKey;
if (Utils.reIssueExistingConfirmationCode(userRecoveryData, preferredChannel)) {
secretKey = userRecoveryData.getSecret();
} else {
// Invalid previous confirmation code.
userRecoveryDataStore.invalidate(userRecoveryData.getSecret());
secretKey = Utils.generateSecretKey(preferredChannel, user.getTenantDomain(), recoveryScenario);
UserRecoveryData recoveryDataDO = new UserRecoveryData(user, secretKey, RecoveryScenarios.getRecoveryScenario(recoveryScenario), RecoverySteps.getRecoveryStep(recoveryStep));
/* Notified channel is stored in remaining setIds for recovery purposes. Having a EMPTY preferred channel
states that the notification channel should not be stored. */
if (StringUtils.isNotBlank(preferredChannel)) {
recoveryDataDO.setRemainingSetIds(preferredChannel);
notificationResponseBean.setNotificationChannel(preferredChannel);
}
if (RecoveryScenarios.EMAIL_VERIFICATION_ON_UPDATE.toString().equals(recoveryScenario) && RecoverySteps.VERIFY_EMAIL.toString().equals(recoveryStep)) {
String verificationPendingEmailClaimValue = userRecoveryData.getRemainingSetIds();
properties = new Property[] { new Property(IdentityRecoveryConstants.SEND_TO, verificationPendingEmailClaimValue) };
recoveryDataDO.setRemainingSetIds(verificationPendingEmailClaimValue);
} else if (RecoveryScenarios.MOBILE_VERIFICATION_ON_UPDATE.toString().equals(recoveryScenario) && RecoverySteps.VERIFY_MOBILE_NUMBER.toString().equals(recoveryStep)) {
String verificationPendingMobileNumber = userRecoveryData.getRemainingSetIds();
properties = new Property[] { new Property(IdentityRecoveryConstants.SEND_TO, verificationPendingMobileNumber) };
recoveryDataDO.setRemainingSetIds(verificationPendingMobileNumber);
}
userRecoveryDataStore.store(recoveryDataDO);
}
if (notificationInternallyManage) {
String eventName = resolveEventName(preferredChannel, user.getUserName(), user.getUserStoreDomain(), user.getTenantDomain());
triggerNotification(user, preferredChannel, notificationType, secretKey, eventName, properties);
} else {
notificationResponseBean.setKey(secretKey);
}
return notificationResponseBean;
}
use of org.wso2.carbon.identity.recovery.bean.NotificationResponseBean in project identity-governance by wso2-extensions.
the class NotificationPasswordRecoveryManager method sendRecoveryNotification.
/**
* Send password recovery information to the user.
*
* @param user User
* @param type Notification Type
* @param notify Manage notifications internally
* @param properties Meta properties
* @return NotificationResponseBean
* @throws IdentityRecoveryException Error while sending recovery information.
*/
public NotificationResponseBean sendRecoveryNotification(User user, String type, Boolean notify, Property[] properties) throws IdentityRecoveryException {
publishEvent(user, String.valueOf(notify), null, null, properties, IdentityEventConstants.Event.PRE_SEND_RECOVERY_NOTIFICATION, new UserRecoveryData(user, null, RecoveryScenarios.NOTIFICATION_BASED_PW_RECOVERY, RecoverySteps.UPDATE_PASSWORD));
validateUserStoreDomain(user);
Utils.validateEmailUsername(user.getUserName());
// Resolve user attributes.
resolveUserAttributes(user);
validatePasswordRecoveryConfiguration(user.getTenantDomain());
validateCallback(properties, user.getTenantDomain());
// Build a property map from the properties in the request.
HashMap<String, String> propertyMap = buildPropertyMap(properties);
String notificationChannel = getNotificationChannelFromProperties(propertyMap);
// Check whether to manage notifications internally.
boolean isNotificationInternallyManage = isNotificationsInternallyManaged(user.getTenantDomain(), notify);
if (!isNotificationInternallyManage) {
notificationChannel = NotificationChannels.EXTERNAL_CHANNEL.getChannelType();
}
// Check whether the user is already verified ( NOTE: This property is set by the new recovery API).
if (!isUserVerified(propertyMap)) {
if (!isExistingUser(user)) {
/* If the user does not exist, Check for NOTIFY_USER_EXISTENCE property. If the property is not
enabled, notify with an empty NotificationResponseBean.*/
boolean notifyUserExistence = Boolean.parseBoolean(IdentityUtil.getProperty(IdentityRecoveryConstants.ConnectorConfig.NOTIFY_USER_EXISTENCE));
if (notifyUserExistence) {
throw Utils.handleClientException(IdentityRecoveryConstants.ErrorMessages.ERROR_CODE_INVALID_USER, user.getUserName());
}
return new NotificationResponseBean(user);
}
}
// Check if the user has a local credential to recover. If not skip sending the recovery mail.
if (!isLocalCredentialAvailable(user)) {
throw Utils.handleClientException(IdentityRecoveryConstants.ErrorMessages.ERROR_CODE_FEDERATED_USER, user.getUserName());
}
if (Utils.isAccountDisabled(user)) {
// If the NotifyUserAccountStatus is disabled, notify with an empty NotificationResponseBean.
if (getNotifyUserAccountStatus()) {
throw Utils.handleClientException(IdentityRecoveryConstants.ErrorMessages.ERROR_CODE_DISABLED_ACCOUNT, user.getUserName());
}
return new NotificationResponseBean(user);
} else if (Utils.isAccountLocked(user)) {
// Check user in PENDING_SR or PENDING_AP status.
checkAccountPendingStatus(user);
// If the NotifyUserAccountStatus is disabled, notify with an empty NotificationResponseBean.
if (getNotifyUserAccountStatus()) {
throw Utils.handleClientException(IdentityRecoveryConstants.ErrorMessages.ERROR_CODE_LOCKED_ACCOUNT, user.getUserName());
}
return new NotificationResponseBean(user);
}
UserRecoveryDataStore userRecoveryDataStore = JDBCRecoveryDataStore.getInstance();
String secretKey;
UserRecoveryData recoveryDataDO;
// Loading the existing user recovery details with the code created timestamp.
recoveryDataDO = userRecoveryDataStore.loadWithoutCodeExpiryValidation(user, RecoveryScenarios.NOTIFICATION_BASED_PW_RECOVERY, RecoverySteps.UPDATE_PASSWORD);
/* Checking whether the existing confirmation code can be used based on the email confirmation code tolerance
and the existing recovery details. */
if (!Utils.reIssueExistingConfirmationCode(recoveryDataDO, notificationChannel)) {
recoveryDataDO = generateNewConfirmationCode(user, notificationChannel);
}
secretKey = recoveryDataDO.getSecret();
NotificationResponseBean notificationResponseBean = new NotificationResponseBean(user);
if (isNotificationInternallyManage) {
// Manage notifications by the identity server.
String eventName = Utils.resolveEventName(notificationChannel);
triggerNotification(user, notificationChannel, IdentityRecoveryConstants.NOTIFICATION_TYPE_PASSWORD_RESET, secretKey, eventName, properties, recoveryDataDO);
} else {
// Set password recovery key since the notifications are managed by an external mechanism.
notificationResponseBean.setKey(secretKey);
}
publishEvent(user, String.valueOf(notify), secretKey, null, properties, IdentityEventConstants.Event.POST_SEND_RECOVERY_NOTIFICATION, recoveryDataDO);
return notificationResponseBean;
}
Aggregations