Search in sources :

Example 31 with ChallengeQuestion

use of org.wso2.carbon.identity.recovery.model.ChallengeQuestion in project identity-governance by wso2-extensions.

the class ChallengeQuestionManager method getAllChallengeQuestions.

/**
 * Get registered challenge questions in tenant based on a locale.
 *
 * @param tenantDomain
 * @param locale
 * @return
 * @throws IdentityRecoveryException
 */
public List<ChallengeQuestion> getAllChallengeQuestions(String tenantDomain, String locale) throws IdentityRecoveryException {
    // check the value and set defaults if empty or null
    locale = validateLocale(locale);
    tenantDomain = validateTenantDomain(tenantDomain);
    List<ChallengeQuestion> questions = new ArrayList<>();
    try {
        Resource questionCollection = resourceMgtService.getIdentityResource(QUESTIONS_BASE_PATH, tenantDomain);
        // check whether the base challenge question directory exists
        if (questionCollection != null) {
            Collection questionSetCollection = (Collection) resourceMgtService.getIdentityResource(QUESTIONS_BASE_PATH, tenantDomain);
            for (String questionSetId : questionSetCollection.getChildren()) {
                Collection questionIdCollection = (Collection) resourceMgtService.getIdentityResource(questionSetId, tenantDomain);
                // iterate each question to find the one with correct locale
                for (String questionIdPath : questionIdCollection.getChildren()) {
                    Resource questionResource = resourceMgtService.getIdentityResource(questionIdPath, tenantDomain, locale);
                    if (questionResource != null) {
                        questions.add(createChallengeQuestion(questionResource));
                    }
                }
            }
        }
    } catch (RegistryException e) {
        throw Utils.handleServerException(IdentityRecoveryConstants.ErrorMessages.ERROR_CODE_REGISTRY_EXCEPTION_GET_CHALLENGE_QUESTIONS, null, e);
    }
    return questions;
}
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) ChallengeQuestion(org.wso2.carbon.identity.recovery.model.ChallengeQuestion)

Example 32 with ChallengeQuestion

use of org.wso2.carbon.identity.recovery.model.ChallengeQuestion in project identity-governance by wso2-extensions.

the class ChallengeQuestionManager method validateSecurityQuestionDuplicate.

/**
 * Validate whether two questions from the same question set have been answered (ie. we only allow a maximum of
 * one question from each set)
 *
 * @param userChallengeAnswers
 * @throws IdentityRecoveryException
 */
private void validateSecurityQuestionDuplicate(UserChallengeAnswer[] userChallengeAnswers) throws IdentityRecoveryException {
    Set<String> tmpMap = new HashSet<>();
    UserChallengeAnswer challengeAnswer;
    ChallengeQuestion challengeQuestion;
    for (UserChallengeAnswer userChallengeAnswer : userChallengeAnswers) {
        challengeAnswer = userChallengeAnswer;
        challengeQuestion = challengeAnswer.getQuestion();
        // if there's no challenge question details we throw a client exception
        if (challengeQuestion == null) {
            String errorMsg = "Challenge question details not provided with the challenge answers.";
            throw Utils.handleClientException(IdentityRecoveryConstants.ErrorMessages.ERROR_CODE_CHALLENGE_QUESTION_NOT_FOUND, errorMsg);
        }
        if (tmpMap.contains(challengeQuestion.getQuestionSetId())) {
            log.error(IdentityRecoveryConstants.ErrorMessages.ERROR_CODE_DUPLICATE_ANSWERS.getMessage());
            throw Utils.handleClientException(IdentityRecoveryConstants.ErrorMessages.ERROR_CODE_DUPLICATE_ANSWERS, null);
        }
        tmpMap.add(challengeQuestion.getQuestionSetId());
    }
}
Also used : UserChallengeAnswer(org.wso2.carbon.identity.recovery.model.UserChallengeAnswer) HashSet(java.util.HashSet) ChallengeQuestion(org.wso2.carbon.identity.recovery.model.ChallengeQuestion)

Example 33 with ChallengeQuestion

use of org.wso2.carbon.identity.recovery.model.ChallengeQuestion in project identity-governance by wso2-extensions.

the class ChallengeQuestionManager method deleteChallengeQuestions.

/**
 * Delete challenge questions from a tenant registry.
 *
 * @param challengeQuestions
 * @param tenantDomain
 * @throws IdentityRecoveryException
 */
public void deleteChallengeQuestions(ChallengeQuestion[] challengeQuestions, String tenantDomain) throws IdentityRecoveryException {
    try {
        tenantDomain = validateTenantDomain(tenantDomain);
        for (ChallengeQuestion question : challengeQuestions) {
            if (isChallengeQuestionExists(question, tenantDomain)) {
                String questionPath = getQuestionPath(question);
                if (StringUtils.isNotEmpty(question.getLocale())) {
                    String locale = question.getLocale();
                    resourceMgtService.deleteIdentityResource(questionPath, tenantDomain, locale);
                } else {
                    resourceMgtService.deleteIdentityResource(questionPath, tenantDomain);
                }
            }
        }
    } catch (IdentityRuntimeException e) {
        log.error("Error deleting challenge quesitons in " + tenantDomain);
        throw new IdentityRecoveryException("Error when deleting challenge questions.", e);
    }
}
Also used : IdentityRuntimeException(org.wso2.carbon.identity.base.IdentityRuntimeException) ChallengeQuestion(org.wso2.carbon.identity.recovery.model.ChallengeQuestion)

Example 34 with ChallengeQuestion

use of org.wso2.carbon.identity.recovery.model.ChallengeQuestion in project identity-governance by wso2-extensions.

the class ChallengeQuestionManager method createChallengeQuestion.

/**
 * Create a challenge question object from the registry resource
 *
 * @param resource
 * @return
 */
private ChallengeQuestion createChallengeQuestion(Resource resource) throws RegistryException {
    ChallengeQuestion challengeQuestion = null;
    byte[] resourceContent = (byte[]) resource.getContent();
    String questionText = new String(resourceContent, Charset.forName("UTF-8"));
    String questionSetId = resource.getProperty(IdentityRecoveryConstants.Questions.CHALLENGE_QUESTION_SET_ID);
    String questionId = resource.getProperty(IdentityRecoveryConstants.Questions.CHALLENGE_QUESTION_ID);
    String questionLocale = resource.getProperty(IdentityRecoveryConstants.Questions.CHALLENGE_QUESTION_LOCALE);
    if (questionSetId != null) {
        if (IdentityUtil.isBlank(questionLocale)) {
            questionLocale = LOCALE_EN_US;
        }
        challengeQuestion = new ChallengeQuestion(questionSetId, questionId, questionText, questionLocale);
    }
    return challengeQuestion;
}
Also used : ChallengeQuestion(org.wso2.carbon.identity.recovery.model.ChallengeQuestion)

Example 35 with ChallengeQuestion

use of org.wso2.carbon.identity.recovery.model.ChallengeQuestion in project identity-governance by wso2-extensions.

the class RecoveryUtil method getUserChallengeAnswers.

public static UserChallengeAnswer[] getUserChallengeAnswers(List<SecurityAnswerDTO> securityAnswerDTOs) {
    UserChallengeAnswer[] userChallengeAnswers = new UserChallengeAnswer[securityAnswerDTOs.size()];
    for (int i = 0; i < securityAnswerDTOs.size(); i++) {
        ChallengeQuestion challengeQuestion = new ChallengeQuestion(securityAnswerDTOs.get(i).getQuestionSetId(), null);
        UserChallengeAnswer userChallengeAnswer = new UserChallengeAnswer(challengeQuestion, securityAnswerDTOs.get(i).getAnswer());
        userChallengeAnswers[i] = userChallengeAnswer;
    }
    return userChallengeAnswers;
}
Also used : UserChallengeAnswer(org.wso2.carbon.identity.recovery.model.UserChallengeAnswer) ChallengeQuestion(org.wso2.carbon.identity.recovery.model.ChallengeQuestion)

Aggregations

ChallengeQuestion (org.wso2.carbon.identity.recovery.model.ChallengeQuestion)26 ArrayList (java.util.ArrayList)14 Test (org.testng.annotations.Test)8 IdentityRecoveryException (org.wso2.carbon.identity.recovery.IdentityRecoveryException)8 UserChallengeAnswer (org.wso2.carbon.identity.recovery.model.UserChallengeAnswer)7 ChallengeQuestion (org.wso2.carbon.identity.recovery.stub.model.ChallengeQuestion)6 UserStoreException (org.wso2.carbon.user.api.UserStoreException)6 HashMap (java.util.HashMap)5 HashSet (java.util.HashSet)5 IdentityRecoveryClientException (org.wso2.carbon.identity.recovery.IdentityRecoveryClientException)5 IdentityException (org.wso2.carbon.identity.base.IdentityException)3 ChallengeQuestionManager (org.wso2.carbon.identity.recovery.ChallengeQuestionManager)3 ChallengeQuestionResponse (org.wso2.carbon.identity.recovery.bean.ChallengeQuestionResponse)3 UserRecoveryData (org.wso2.carbon.identity.recovery.model.UserRecoveryData)3 UserRecoveryDataStore (org.wso2.carbon.identity.recovery.store.UserRecoveryDataStore)3 ChallengeQuestionDTO (org.wso2.carbon.identity.rest.api.server.challenge.v1.dto.ChallengeQuestionDTO)3 ISIntegrationTest (org.wso2.identity.integration.common.utils.ISIntegrationTest)3 Gson (com.google.gson.Gson)2 GsonBuilder (com.google.gson.GsonBuilder)2 Map (java.util.Map)2