use of org.wso2.carbon.identity.mgt.IdentityMgtServiceException 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;
}
use of org.wso2.carbon.identity.mgt.IdentityMgtServiceException 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;
}
use of org.wso2.carbon.identity.mgt.IdentityMgtServiceException 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;
}
use of org.wso2.carbon.identity.mgt.IdentityMgtServiceException in project carbon-identity-framework by wso2.
the class UserInformationRecoveryService method getAllChallengeQuestions.
/**
* Get all challenge questions
*
* @return array of questions
* @throws IdentityMgtServiceException if fails
*/
public ChallengeQuestionDTO[] getAllChallengeQuestions() throws IdentityMgtServiceException {
ChallengeQuestionProcessor processor = IdentityMgtServiceComponent.getRecoveryProcessor().getQuestionProcessor();
List<ChallengeQuestionDTO> questionDTOs = null;
try {
questionDTOs = processor.getAllChallengeQuestions();
} catch (IdentityException e) {
log.error("Error while loading user challenges", e);
throw new IdentityMgtServiceException("Error while loading user challenges");
}
return questionDTOs.toArray(new ChallengeQuestionDTO[questionDTOs.size()]);
}
use of org.wso2.carbon.identity.mgt.IdentityMgtServiceException in project carbon-identity-framework by wso2.
the class UserIdentityManagementAdminService method deleteUser.
// --------Operations require Admin permissions ---------//
/**
* Admin deletes a user from the system. This is an irreversible operation.
*
* @param userName
* @throws IdentityMgtServiceException
*/
public void deleteUser(String userName) throws IdentityMgtServiceException {
try {
UserStoreManager userStoreManager = IdentityMgtServiceComponent.getRealmService().getTenantUserRealm(CarbonContext.getThreadLocalCarbonContext().getTenantId()).getUserStoreManager();
userStoreManager.deleteUser(userName);
log.info("Deleted user: " + userName);
} catch (UserStoreException e) {
String errorMessage = "Error occured while deleting user : " + userName;
log.error(errorMessage, e);
throw new IdentityMgtServiceException(errorMessage);
}
}
Aggregations