Search in sources :

Example 46 with UserDTO

use of org.wso2.carbon.apimgt.rest.api.store.dto.UserDTO in project carbon-identity-framework by wso2.

the class UserInformationRecoveryService method verifyUser.

public VerificationBean verifyUser(String username, CaptchaInfoBean captcha) throws IdentityMgtServiceException {
    UserDTO userDTO;
    VerificationBean bean;
    if (log.isDebugEnabled()) {
        log.debug("User verification request received with username : " + username);
    }
    if (IdentityMgtConfig.getInstance().isCaptchaVerificationInternallyManaged()) {
        try {
            CaptchaUtil.processCaptchaInfoBean(captcha);
        } catch (Exception e) {
            bean = handleError(VerificationBean.ERROR_CODE_INVALID_CAPTCHA + " Error while validating captcha", e);
            return bean;
        }
    }
    try {
        userDTO = Utils.processUserId(username);
    } catch (IdentityException e) {
        bean = handleError(VerificationBean.ERROR_CODE_INVALID_USER + " invalid user : " + username, e);
        return bean;
    }
    try {
        if (IdentityMgtConfig.getInstance().isSaasEnabled()) {
            PrivilegedCarbonContext.startTenantFlow();
            PrivilegedCarbonContext carbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext();
            carbonContext.setTenantId(userDTO.getTenantId());
            carbonContext.setTenantDomain(userDTO.getTenantDomain());
        }
        RecoveryProcessor processor = IdentityMgtServiceComponent.getRecoveryProcessor();
        bean = processor.verifyUserForRecovery(1, userDTO);
        if (bean.getError() != null) {
            if (bean.getError().contains(VerificationBean.ERROR_CODE_INVALID_USER)) {
                bean = handleError(VerificationBean.ERROR_CODE_INVALID_USER + " User does not exist : " + username, null);
            } else if (bean.getError().contains(VerificationBean.ERROR_CODE_DISABLED_ACCOUNT)) {
                bean = handleError(VerificationBean.ERROR_CODE_DISABLED_ACCOUNT + " Account is disabled for user " + username + ". Can not allow to recover.", null);
            } else {
                bean = handleError(VerificationBean.ERROR_CODE_UNEXPECTED + " Error verifying user : " + username, null);
            }
        }
        return bean;
    } finally {
        if (IdentityMgtConfig.getInstance().isSaasEnabled()) {
            PrivilegedCarbonContext.endTenantFlow();
        }
    }
}
Also used : VerificationBean(org.wso2.carbon.identity.mgt.beans.VerificationBean) UserDTO(org.wso2.carbon.identity.mgt.dto.UserDTO) RecoveryProcessor(org.wso2.carbon.identity.mgt.RecoveryProcessor) PrivilegedCarbonContext(org.wso2.carbon.context.PrivilegedCarbonContext) IdentityException(org.wso2.carbon.identity.base.IdentityException) UserStoreException(org.wso2.carbon.user.api.UserStoreException) IdentityMgtServiceException(org.wso2.carbon.identity.mgt.IdentityMgtServiceException) IdentityException(org.wso2.carbon.identity.base.IdentityException)

Example 47 with UserDTO

use of org.wso2.carbon.apimgt.rest.api.store.dto.UserDTO in project carbon-identity-framework by wso2.

the class UserInformationRecoveryService method updatePassword.

/**
 * This method is used to update the password in the system for password
 * recovery process. Before calling this method caller needs to call
 * verifyConfirmationCode and get the newly generated confirmation code.
 *
 * @param username         - username
 * @param confirmationCode - newly generated confirmation code
 * @param newPassword      - new password
 * @return - VerificationBean with operation status true or false.
 * @throws IdentityMgtServiceException
 */
public VerificationBean updatePassword(String username, String confirmationCode, String newPassword) throws IdentityMgtServiceException {
    RecoveryProcessor recoveryProcessor = IdentityMgtServiceComponent.getRecoveryProcessor();
    VerificationBean bean = null;
    if (log.isDebugEnabled()) {
        log.debug("User update password request received with username: " + username);
    }
    try {
        UserDTO userDTO = Utils.processUserId(username);
        if (IdentityMgtConfig.getInstance().isSaasEnabled()) {
            PrivilegedCarbonContext.startTenantFlow();
            PrivilegedCarbonContext carbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext();
            carbonContext.setTenantId(userDTO.getTenantId());
            carbonContext.setTenantDomain(userDTO.getTenantDomain());
        }
        TenantManager tenantManager = IdentityMgtServiceComponent.getRealmService().getTenantManager();
        int tenantId = 0;
        try {
            tenantId = tenantManager.getTenantId(userDTO.getTenantDomain());
        } catch (UserStoreException e) {
            log.warn("No Tenant id for tenant domain " + userDTO.getTenantDomain());
            return handleError(VerificationBean.ERROR_CODE_INVALID_TENANT + " No Tenant id for tenant domain : " + userDTO.getTenantDomain(), e);
        }
        if (recoveryProcessor.verifyConfirmationCode(30, userDTO.getUserId(), confirmationCode).isVerified()) {
            Utils.updatePassword(userDTO.getUserId(), tenantId, newPassword);
            log.info("Credential is updated for user : " + userDTO.getUserId() + " and tenant domain : " + userDTO.getTenantDomain());
            IdentityMgtConfig.getInstance().getRecoveryDataStore().invalidate(userDTO.getUserId(), tenantId);
            bean = new VerificationBean(true);
        } else if (recoveryProcessor.verifyConfirmationCode(3, userDTO.getUserId(), confirmationCode).isVerified()) {
            Utils.updatePassword(userDTO.getUserId(), tenantId, newPassword);
            log.info("Credential is updated for user : " + userDTO.getUserId() + " and tenant domain : " + userDTO.getTenantDomain());
            IdentityMgtConfig.getInstance().getRecoveryDataStore().invalidate(userDTO.getUserId(), tenantId);
            bean = new VerificationBean(true);
        } else {
            String msg = "Invalid user tried to update credential with user Id : " + userDTO.getUserId() + " and tenant domain : " + userDTO.getTenantDomain();
            bean = new VerificationBean(VerificationBean.ERROR_CODE_INVALID_USER + " " + msg);
            bean.setVerified(false);
            log.error(msg);
        }
    } catch (IdentityException e) {
        bean = UserIdentityManagementUtil.getCustomErrorMessagesToVerifyCode(e, username);
        if (bean.getError() == null) {
            bean = handleError(VerificationBean.ERROR_CODE_UNEXPECTED + " Error while updating credential " + "for user: " + username, e);
        }
        return bean;
    } finally {
        if (IdentityMgtConfig.getInstance().isSaasEnabled()) {
            PrivilegedCarbonContext.endTenantFlow();
        }
    }
    return bean;
}
Also used : VerificationBean(org.wso2.carbon.identity.mgt.beans.VerificationBean) UserDTO(org.wso2.carbon.identity.mgt.dto.UserDTO) UserStoreException(org.wso2.carbon.user.api.UserStoreException) RecoveryProcessor(org.wso2.carbon.identity.mgt.RecoveryProcessor) PrivilegedCarbonContext(org.wso2.carbon.context.PrivilegedCarbonContext) IdentityException(org.wso2.carbon.identity.base.IdentityException) TenantManager(org.wso2.carbon.user.core.tenant.TenantManager)

Example 48 with UserDTO

use of org.wso2.carbon.apimgt.rest.api.store.dto.UserDTO in project carbon-identity-framework by wso2.

the class UserInformationRecoveryService method getUserChallengeQuestions.

/**
 * Returns all the challenge questions configured for the user.
 *
 * @param userName     username of the user
 * @param confirmation confirmation code
 * @return an instance of UserChallengesCollectionDTO which holds the challenge questions and status
 * @throws IdentityMgtServiceException
 */
public UserChallengesCollectionDTO getUserChallengeQuestions(String userName, String confirmation) throws IdentityMgtServiceException {
    UserDTO userDTO = null;
    UserChallengesCollectionDTO userChallengesCollectionDTO = new UserChallengesCollectionDTO();
    if (log.isDebugEnabled()) {
        log.debug("User challenge question request received with username :" + userName);
    }
    try {
        userDTO = Utils.processUserId(userName);
    } catch (IdentityException e) {
        log.error("Error while validating user " + userName, e);
        return UserIdentityManagementUtil.handleChallengeQuestionSetError(VerificationBean.ERROR_CODE_INVALID_USER + " Error validating user : " + userName, null);
    }
    try {
        if (IdentityMgtConfig.getInstance().isSaasEnabled()) {
            PrivilegedCarbonContext.startTenantFlow();
            PrivilegedCarbonContext carbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext();
            carbonContext.setTenantId(userDTO.getTenantId());
            carbonContext.setTenantDomain(userDTO.getTenantDomain());
        }
        RecoveryProcessor processor = IdentityMgtServiceComponent.getRecoveryProcessor();
        VerificationBean bean;
        try {
            bean = processor.verifyConfirmationCode(1, userDTO.getUserId(), confirmation);
            if (bean.isVerified()) {
                bean = processor.updateConfirmationCode(20, userDTO.getUserId(), userDTO.getTenantId());
            } else {
                bean.setVerified(false);
            }
        } catch (IdentityException e) {
            log.error("Error while verifying confirmation code.", e);
            return UserIdentityManagementUtil.getCustomErrorMessagesForChallengeQuestionSet(e, userName);
        }
        if (bean.isVerified()) {
            UserChallengesDTO[] userChallengesDTOs = null;
            try {
                userChallengesDTOs = processor.getQuestionProcessor().getUserChallengeQuestions(userDTO.getUserId(), userDTO.getTenantId());
                userChallengesCollectionDTO.setKey(bean.getKey());
                userChallengesCollectionDTO.setUserChallengesDTOs(userChallengesDTOs);
            } catch (IdentityException e) {
                log.error("Error while retrieving challenge questions of the user " + userName, e);
                return UserIdentityManagementUtil.handleChallengeQuestionSetError(VerificationBean.ERROR_CODE_CHALLENGE_QUESTION_NOT_FOUND + " No associated challenge " + "questions found for the user : " + userName, null);
            }
            if (log.isDebugEnabled()) {
                log.debug("User challenge questions retrieved successfully");
            }
        } else {
            if (log.isDebugEnabled()) {
                log.debug("Verification failed for user. Error : " + bean.getError());
            }
            userChallengesCollectionDTO.setError(VerificationBean.ERROR_CODE_INVALID_USER + " " + bean.getError());
        }
    } finally {
        if (IdentityMgtConfig.getInstance().isSaasEnabled()) {
            PrivilegedCarbonContext.endTenantFlow();
        }
    }
    return userChallengesCollectionDTO;
}
Also used : VerificationBean(org.wso2.carbon.identity.mgt.beans.VerificationBean) UserChallengesDTO(org.wso2.carbon.identity.mgt.dto.UserChallengesDTO) UserChallengesCollectionDTO(org.wso2.carbon.identity.mgt.dto.UserChallengesCollectionDTO) UserDTO(org.wso2.carbon.identity.mgt.dto.UserDTO) RecoveryProcessor(org.wso2.carbon.identity.mgt.RecoveryProcessor) PrivilegedCarbonContext(org.wso2.carbon.context.PrivilegedCarbonContext) IdentityException(org.wso2.carbon.identity.base.IdentityException)

Example 49 with UserDTO

use of org.wso2.carbon.apimgt.rest.api.store.dto.UserDTO in project carbon-identity-framework by wso2.

the class UserInformationRecoveryService method confirmUserSelfRegistration.

/**
 * This method used to confirm the self registered user account and unlock it.
 *
 * @param username
 * @param code
 * @param captcha
 * @param tenantDomain
 * @return
 * @throws IdentityMgtServiceException
 */
public VerificationBean confirmUserSelfRegistration(String username, String code, CaptchaInfoBean captcha, String tenantDomain) throws IdentityMgtServiceException {
    VerificationBean bean = new VerificationBean();
    if (log.isDebugEnabled()) {
        log.debug("User registration verification request received with username :" + username);
    }
    if (IdentityMgtConfig.getInstance().isCaptchaVerificationInternallyManaged()) {
        try {
            CaptchaUtil.processCaptchaInfoBean(captcha);
        } catch (Exception e) {
            bean = handleError(VerificationBean.ERROR_CODE_INVALID_CAPTCHA + " Error while validating captcha for user : " + username, e);
            return bean;
        }
    }
    if (!IdentityMgtConfig.getInstance().isSaasEnabled()) {
        String loggedInTenant = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain();
        if (tenantDomain != null && !tenantDomain.isEmpty() && !loggedInTenant.equals(tenantDomain)) {
            String msg = "Trying to confirm users in unauthorized tenant space";
            log.error(msg);
            return handleError(VerificationBean.ERROR_CODE_INVALID_TENANT + " " + msg, null);
        }
        if (tenantDomain == null || tenantDomain.isEmpty()) {
            tenantDomain = loggedInTenant;
        }
    }
    UserDTO userDTO = null;
    try {
        userDTO = Utils.processUserId(username + "@" + tenantDomain);
    } catch (IdentityException e) {
        bean = handleError(VerificationBean.ERROR_CODE_INVALID_USER + " Error verifying user account for user : " + username, e);
        return bean;
    }
    try {
        if (IdentityMgtConfig.getInstance().isSaasEnabled()) {
            PrivilegedCarbonContext.startTenantFlow();
            PrivilegedCarbonContext carbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext();
            carbonContext.setTenantId(userDTO.getTenantId());
            carbonContext.setTenantDomain(userDTO.getTenantDomain());
        }
        RecoveryProcessor processor = IdentityMgtServiceComponent.getRecoveryProcessor();
        org.wso2.carbon.user.core.UserStoreManager userStoreManager = null;
        RealmService realmService = IdentityMgtServiceComponent.getRealmService();
        int tenantId;
        try {
            tenantId = Utils.getTenantId(tenantDomain);
            if (realmService.getTenantUserRealm(tenantId) != null) {
                userStoreManager = (org.wso2.carbon.user.core.UserStoreManager) realmService.getTenantUserRealm(tenantId).getUserStoreManager();
                if (username != null && username.contains(UserCoreConstants.DOMAIN_SEPARATOR)) {
                    userStoreManager = userStoreManager.getSecondaryUserStoreManager(Utils.getUserStoreDomainName(username));
                }
            }
        } catch (Exception e) {
            bean = handleError(VerificationBean.ERROR_CODE_UNEXPECTED + "Error retrieving the user store manager" + " for the tenant : " + tenantDomain, e);
            return bean;
        }
        try {
            bean = processor.verifyConfirmationCode(1, username, code);
            if (bean.isVerified()) {
                UserIdentityManagementUtil.unlockUserAccount(username, userStoreManager);
                bean.setVerified(true);
            } else {
                bean.setVerified(false);
                bean.setKey("");
                log.error("User verification failed against the given confirmation code");
            }
        } catch (IdentityException e) {
            bean = UserIdentityManagementUtil.getCustomErrorMessagesToVerifyCode(e, username);
            if (bean.getError() == null) {
                bean = handleError("Error while validating confirmation code for user : " + username, e);
            }
            return bean;
        }
    } finally {
        if (IdentityMgtConfig.getInstance().isSaasEnabled()) {
            PrivilegedCarbonContext.endTenantFlow();
        }
    }
    return bean;
}
Also used : VerificationBean(org.wso2.carbon.identity.mgt.beans.VerificationBean) UserDTO(org.wso2.carbon.identity.mgt.dto.UserDTO) RecoveryProcessor(org.wso2.carbon.identity.mgt.RecoveryProcessor) PrivilegedCarbonContext(org.wso2.carbon.context.PrivilegedCarbonContext) IdentityException(org.wso2.carbon.identity.base.IdentityException) UserStoreException(org.wso2.carbon.user.api.UserStoreException) IdentityMgtServiceException(org.wso2.carbon.identity.mgt.IdentityMgtServiceException) IdentityException(org.wso2.carbon.identity.base.IdentityException) RealmService(org.wso2.carbon.user.core.service.RealmService)

Example 50 with UserDTO

use of org.wso2.carbon.apimgt.rest.api.store.dto.UserDTO in project carbon-identity-framework by wso2.

the class UserInformationRecoveryService method verifyAccount.

/**
 * Verifies the user against the provided claims and captcha information.
 *
 * @param claims
 * @param captcha
 * @param tenantDomain
 * @return
 * @throws IdentityMgtServiceException
 */
public VerificationBean verifyAccount(UserIdentityClaimDTO[] claims, CaptchaInfoBean captcha, String tenantDomain) throws IdentityMgtServiceException {
    VerificationBean vBean = new VerificationBean();
    if (IdentityMgtConfig.getInstance().isCaptchaVerificationInternallyManaged()) {
        try {
            CaptchaUtil.processCaptchaInfoBean(captcha);
        } catch (Exception e) {
            vBean = handleError(VerificationBean.ERROR_CODE_INVALID_CAPTCHA + " Error processing captcha", e);
            return vBean;
        }
    }
    if (!IdentityMgtConfig.getInstance().isSaasEnabled()) {
        String loggedInTenant = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain();
        if (tenantDomain != null && !tenantDomain.isEmpty() && !loggedInTenant.equals(tenantDomain)) {
            String msg = "Trying to verify account unauthorized tenant space";
            log.error(msg);
            throw new IdentityMgtServiceException(msg);
        }
        if (tenantDomain == null || tenantDomain.isEmpty()) {
            tenantDomain = loggedInTenant;
        }
    }
    try {
        int tenantId = Utils.getTenantId(tenantDomain);
        String userName = UserIdentityManagementUtil.getUsernameByClaims(claims, tenantId);
        if (userName != null) {
            UserDTO userDTO = new UserDTO(UserCoreUtil.addTenantDomainToEntry(userName, tenantDomain));
            userDTO.setTenantId(tenantId);
            UserRecoveryDTO dto = new UserRecoveryDTO(userDTO);
            dto.setNotification(IdentityMgtConstants.Notification.ACCOUNT_ID_RECOVERY);
            dto.setNotificationType("EMAIL");
            RecoveryProcessor processor = IdentityMgtServiceComponent.getRecoveryProcessor();
            NotificationDataDTO notificationDto = processor.notifyWithEmail(dto);
            vBean.setVerified(notificationDto.isNotificationSent());
            // Send email data only if not internally managed.
            if (!(IdentityMgtConfig.getInstance().isNotificationInternallyManaged())) {
                vBean.setNotificationData(notificationDto);
            }
        } else {
            vBean.setError("User not found");
            vBean.setVerified(false);
        }
    } catch (Exception e) {
        vBean = handleError(VerificationBean.ERROR_CODE_INVALID_USER + " Error verifying user account", e);
        return vBean;
    }
    return vBean;
}
Also used : VerificationBean(org.wso2.carbon.identity.mgt.beans.VerificationBean) IdentityMgtServiceException(org.wso2.carbon.identity.mgt.IdentityMgtServiceException) UserDTO(org.wso2.carbon.identity.mgt.dto.UserDTO) NotificationDataDTO(org.wso2.carbon.identity.mgt.dto.NotificationDataDTO) RecoveryProcessor(org.wso2.carbon.identity.mgt.RecoveryProcessor) UserRecoveryDTO(org.wso2.carbon.identity.mgt.dto.UserRecoveryDTO) UserStoreException(org.wso2.carbon.user.api.UserStoreException) IdentityMgtServiceException(org.wso2.carbon.identity.mgt.IdentityMgtServiceException) IdentityException(org.wso2.carbon.identity.base.IdentityException)

Aggregations

IdentityException (org.wso2.carbon.identity.base.IdentityException)23 UserDTO (org.wso2.carbon.identity.mgt.dto.UserDTO)22 VerificationBean (org.wso2.carbon.identity.mgt.beans.VerificationBean)19 RecoveryProcessor (org.wso2.carbon.identity.mgt.RecoveryProcessor)17 UserStoreException (org.wso2.carbon.user.api.UserStoreException)17 IdentityMgtServiceException (org.wso2.carbon.identity.mgt.IdentityMgtServiceException)14 PrivilegedCarbonContext (org.wso2.carbon.context.PrivilegedCarbonContext)12 Test (org.testng.annotations.Test)10 UserDTO (org.wso2.carbon.identity.user.rename.core.dto.UserDTO)10 UserRecoveryDTO (org.wso2.carbon.identity.mgt.dto.UserRecoveryDTO)9 UserStoreManager (org.wso2.carbon.user.api.UserStoreManager)9 RealmService (org.wso2.carbon.user.core.service.RealmService)9 TenantManager (org.wso2.carbon.user.core.tenant.TenantManager)8 UserDTO (org.wso2.carbon.identity.test.integration.service.stub.UserDTO)7 UserDTO (org.wso2.carbon.identity.user.endpoint.dto.UserDTO)6 NotificationDataDTO (org.wso2.carbon.identity.mgt.dto.NotificationDataDTO)5 UserChallengesDTO (org.wso2.carbon.identity.mgt.dto.UserChallengesDTO)4 User (org.wso2.carbon.apimgt.core.models.User)3 User (org.wso2.carbon.identity.application.common.model.User)3 ChallengeQuestionProcessor (org.wso2.carbon.identity.mgt.ChallengeQuestionProcessor)3