use of org.wso2.carbon.identity.recovery.IdentityRecoveryServerException in project identity-governance by wso2-extensions.
the class ChallengeAnswerValidationHandler method validateChallengeAnswerUniqueness.
/**
* Validate the uniqueness of a given answer.
*
* @param newChallengeAnswers Newly added challenge question answers.
* @param existingChallengeAnswers Existing challenge question answers.
* @throws IdentityRecoveryServerException Error while hashing the newly added answers.
* @throws IdentityRecoveryClientException Error while validating the answer uniqueness.
*/
private void validateChallengeAnswerUniqueness(List<UserChallengeAnswer> newChallengeAnswers, List<UserChallengeAnswer> existingChallengeAnswers) throws IdentityRecoveryServerException, IdentityRecoveryClientException {
Set<String> uniqueChallengeAnswerHashSet = new HashSet<>();
for (UserChallengeAnswer existingChallengeAnswer : existingChallengeAnswers) {
uniqueChallengeAnswerHashSet.add(existingChallengeAnswer.getAnswer().trim());
}
String hashedNewChallengeAnswer;
for (UserChallengeAnswer userChallengeAnswer : newChallengeAnswers) {
String challengeQuestion = userChallengeAnswer.getQuestion().getQuestion();
try {
hashedNewChallengeAnswer = Utils.doHash(userChallengeAnswer.getAnswer().trim().toLowerCase());
} catch (UserStoreException e) {
throw Utils.handleServerException(IdentityRecoveryConstants.ErrorMessages.ERROR_CODE_NO_HASHING_ALGO, null);
}
if (!uniqueChallengeAnswerHashSet.add(hashedNewChallengeAnswer)) {
if (log.isDebugEnabled()) {
log.debug(String.format("The challenge question answer is not unique. The given answer for " + "the challenge question '%s' has been used more than once.", challengeQuestion));
}
throw Utils.handleClientException(IdentityRecoveryConstants.ErrorMessages.ERROR_CODE_NOT_UNIQUE_ANSWER, challengeQuestion);
}
}
}
use of org.wso2.carbon.identity.recovery.IdentityRecoveryServerException in project identity-governance by wso2-extensions.
the class ChallengeAnswerValidationHandler method handleEvent.
@Override
public void handleEvent(Event event) throws IdentityEventException {
String eventName = event.getEventName();
Map<String, Object> eventProperties = event.getEventProperties();
UserStoreManager userStoreManager = (UserStoreManager) eventProperties.get(IdentityEventConstants.EventProperty.USER_STORE_MANAGER);
User user = (User) eventProperties.get(IdentityEventConstants.EventProperty.USER);
UserChallengeAnswer[] userChallengeAnswers = (UserChallengeAnswer[]) eventProperties.get(IdentityEventConstants.EventProperty.USER_CHALLENGE_ANSWERS);
Map<String, String> existingQuestionAndAnswers = (Map<String, String>) eventProperties.get(IdentityEventConstants.EventProperty.USER_OLD_CHALLENGE_ANSWERS);
user.setUserStoreDomain(userStoreManager.getRealmConfiguration().getUserStoreProperty(UserCoreConstants.RealmConfig.PROPERTY_DOMAIN_NAME));
if (IdentityEventConstants.Event.PRE_SET_CHALLENGE_QUESTION_ANSWERS.equals(eventName)) {
try {
validateChallengeAnswers(user, userChallengeAnswers, existingQuestionAndAnswers);
} catch (IdentityRecoveryClientException e) {
throw new IdentityEventClientException(e.getErrorCode(), e.getMessage(), e);
} catch (IdentityRecoveryServerException e) {
throw new IdentityEventServerException(e.getErrorCode(), e.getMessage(), e);
}
}
}
use of org.wso2.carbon.identity.recovery.IdentityRecoveryServerException in project identity-governance by wso2-extensions.
the class ChallengeQuestionManager method getAllChallengeQuestionSetsURIs.
/**
* Get all challenge questions set URIs registered for a tenant.
*
* @param tenantDomain
* @return
* @throws IdentityRecoveryServerException
*/
public List<String> getAllChallengeQuestionSetsURIs(String tenantDomain) throws IdentityRecoveryServerException {
tenantDomain = validateTenantDomain(tenantDomain);
List<String> challengeQuestions = new ArrayList<>();
try {
Resource questionCollection = resourceMgtService.getIdentityResource(QUESTIONS_BASE_PATH, tenantDomain);
if (questionCollection != null) {
Collection questionSetCollection = (Collection) resourceMgtService.getIdentityResource(QUESTIONS_BASE_PATH, tenantDomain);
for (String questionSetId : questionSetCollection.getChildren()) {
challengeQuestions.add(questionSetId.replace(QUESTIONS_BASE_PATH, IdentityRecoveryConstants.WSO2CARBON_CLAIM_DIALECT));
}
}
return challengeQuestions;
} catch (RegistryException e) {
throw Utils.handleServerException(IdentityRecoveryConstants.ErrorMessages.ERROR_CODE_REGISTRY_EXCEPTION_GET_CHALLENGE_QUESTIONS, null, e);
}
}
use of org.wso2.carbon.identity.recovery.IdentityRecoveryServerException in project identity-governance by wso2-extensions.
the class ChallengeQuestionManager method triggerChallengeAnswersValidation.
/**
* Trigger challenge question answers validation according to the given event name.
*
* @param user User
* @param userChallengeAnswers Array of challenge answers
* @param eventName Event name
* @throws IdentityRecoveryClientException Error while validating the challenge answers
* @throws IdentityRecoveryServerException Error while getting the user store manager or triggering the event.
*/
private void triggerChallengeAnswersValidation(User user, UserChallengeAnswer[] userChallengeAnswers, Map<String, String> existingQuestionAndAnswers, String eventName) throws IdentityRecoveryClientException, IdentityRecoveryServerException {
Map<String, Object> properties = new HashMap<>();
properties.put(IdentityEventConstants.EventProperty.USER, user);
properties.put(IdentityEventConstants.EventProperty.USER_CHALLENGE_ANSWERS, userChallengeAnswers);
properties.put(IdentityEventConstants.EventProperty.USER_OLD_CHALLENGE_ANSWERS, existingQuestionAndAnswers);
try {
UserStoreManager userStoreManager;
if (IdentityUtil.getPrimaryDomainName().equals(user.getUserStoreDomain())) {
userStoreManager = (UserStoreManager) CarbonContext.getThreadLocalCarbonContext().getUserRealm().getUserStoreManager();
} else {
userStoreManager = ((UserStoreManager) CarbonContext.getThreadLocalCarbonContext().getUserRealm().getUserStoreManager()).getSecondaryUserStoreManager(user.getUserStoreDomain());
}
properties.put(IdentityEventConstants.EventProperty.USER_STORE_MANAGER, userStoreManager);
} catch (UserStoreException e) {
throw Utils.handleServerException(IdentityRecoveryConstants.ErrorMessages.ERROR_CODE_FAILED_TO_LOAD_USER_STORE_MANAGER, null, e);
}
Event identityMgtEvent = new Event(eventName, properties);
try {
IdentityRecoveryServiceDataHolder.getInstance().getIdentityEventService().handleEvent(identityMgtEvent);
} catch (IdentityEventClientException e) {
throw new IdentityRecoveryClientException(e.getErrorCode(), e.getMessage(), e);
} catch (IdentityEventServerException e) {
throw new IdentityRecoveryServerException(e.getErrorCode(), e.getMessage(), e);
} catch (IdentityEventException e) {
throw Utils.handleServerException(IdentityRecoveryConstants.ErrorMessages.ERROR_CODE_PUBLISH_EVENT, eventName, e);
}
}
use of org.wso2.carbon.identity.recovery.IdentityRecoveryServerException in project identity-governance by wso2-extensions.
the class UserAccountRecoveryManager method addRecoveryDataObject.
/**
* Add the notification channel recovery data to the store.
*
* @param username Username
* @param tenantDomain Tenant domain
* @param secretKey RecoveryId
* @param scenario RecoveryScenario
* @param recoveryData Data to be stored as mata which are needed to evaluate the recovery data object
* @throws IdentityRecoveryServerException If an error occurred while storing recovery data.
*/
private void addRecoveryDataObject(String username, String tenantDomain, String secretKey, RecoveryScenarios scenario, String recoveryData) throws IdentityRecoveryServerException {
// Create a user object.
User user = Utils.buildUser(username, tenantDomain);
UserRecoveryData recoveryDataDO = new UserRecoveryData(user, secretKey, scenario, RecoverySteps.SEND_RECOVERY_INFORMATION);
// Store available channels in remaining setIDs.
recoveryDataDO.setRemainingSetIds(recoveryData);
try {
UserRecoveryDataStore userRecoveryDataStore = JDBCRecoveryDataStore.getInstance();
userRecoveryDataStore.invalidate(user);
userRecoveryDataStore.store(recoveryDataDO);
} catch (IdentityRecoveryException e) {
throw Utils.handleServerException(IdentityRecoveryConstants.ErrorMessages.ERROR_CODE_ERROR_STORING_RECOVERY_DATA, "Error Storing Recovery Data", e);
}
}
Aggregations