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;
}
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());
}
}
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);
}
}
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;
}
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;
}
Aggregations