Search in sources :

Example 1 with IdentityRecoveryServerException

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);
        }
    }
}
Also used : UserStoreException(org.wso2.carbon.user.api.UserStoreException) UserChallengeAnswer(org.wso2.carbon.identity.recovery.model.UserChallengeAnswer) HashSet(java.util.HashSet)

Example 2 with IdentityRecoveryServerException

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);
        }
    }
}
Also used : IdentityEventServerException(org.wso2.carbon.identity.event.IdentityEventServerException) User(org.wso2.carbon.identity.application.common.model.User) IdentityRecoveryServerException(org.wso2.carbon.identity.recovery.IdentityRecoveryServerException) IdentityEventClientException(org.wso2.carbon.identity.event.IdentityEventClientException) UserStoreManager(org.wso2.carbon.user.core.UserStoreManager) UserChallengeAnswer(org.wso2.carbon.identity.recovery.model.UserChallengeAnswer) HashMap(java.util.HashMap) Map(java.util.Map) IdentityRecoveryClientException(org.wso2.carbon.identity.recovery.IdentityRecoveryClientException)

Example 3 with IdentityRecoveryServerException

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);
    }
}
Also used : ArrayList(java.util.ArrayList) Resource(org.wso2.carbon.registry.core.Resource) Collection(org.wso2.carbon.registry.core.Collection) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException)

Example 4 with IdentityRecoveryServerException

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);
    }
}
Also used : IdentityEventServerException(org.wso2.carbon.identity.event.IdentityEventServerException) HashMap(java.util.HashMap) IdentityEventException(org.wso2.carbon.identity.event.IdentityEventException) UserStoreException(org.wso2.carbon.user.api.UserStoreException) Event(org.wso2.carbon.identity.event.event.Event) IdentityEventClientException(org.wso2.carbon.identity.event.IdentityEventClientException) UserStoreManager(org.wso2.carbon.user.core.UserStoreManager)

Example 5 with IdentityRecoveryServerException

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);
    }
}
Also used : User(org.wso2.carbon.identity.application.common.model.User) UserRecoveryData(org.wso2.carbon.identity.recovery.model.UserRecoveryData) UserRecoveryDataStore(org.wso2.carbon.identity.recovery.store.UserRecoveryDataStore) IdentityRecoveryException(org.wso2.carbon.identity.recovery.IdentityRecoveryException)

Aggregations

UserStoreException (org.wso2.carbon.user.api.UserStoreException)12 IdentityRecoveryServerException (org.wso2.carbon.identity.recovery.IdentityRecoveryServerException)11 HashMap (java.util.HashMap)6 IdentityRecoveryException (org.wso2.carbon.identity.recovery.IdentityRecoveryException)6 RealmService (org.wso2.carbon.user.core.service.RealmService)6 IdentityEventException (org.wso2.carbon.identity.event.IdentityEventException)5 UserStoreManager (org.wso2.carbon.user.core.UserStoreManager)5 Event (org.wso2.carbon.identity.event.event.Event)4 IdentityRecoveryClientException (org.wso2.carbon.identity.recovery.IdentityRecoveryClientException)4 UserRecoveryData (org.wso2.carbon.identity.recovery.model.UserRecoveryData)3 UserRecoveryDataStore (org.wso2.carbon.identity.recovery.store.UserRecoveryDataStore)3 UserFunctionalityManager (org.wso2.carbon.identity.user.functionality.mgt.UserFunctionalityManager)3 UserFunctionalityManagementException (org.wso2.carbon.identity.user.functionality.mgt.exception.UserFunctionalityManagementException)3 UserStoreManager (org.wso2.carbon.user.api.UserStoreManager)3 Gson (com.google.gson.Gson)2 SimpleDateFormat (java.text.SimpleDateFormat)2 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 JSONObject (org.json.JSONObject)2 ConsentManager (org.wso2.carbon.consent.mgt.core.ConsentManager)2