/**
   * To get the challenge question for the user.
   *
   * @param userName
   * @param confirmation
   * @param questionId - Question id returned from the getUserChanllegneQuestionIds method.
   * @return Populated question bean with the question details and the key.
   * @throws IdentityMgtServiceException
   */
  public UserChallengesDTO getUserChallengeQuestion(
      String userName, String confirmation, String questionId) throws IdentityMgtServiceException {

    UserDTO userDTO = null;
    UserChallengesDTO userChallengesDTO = new UserChallengesDTO();

    if (log.isDebugEnabled()) {
      log.debug("User challenge question request received with username :"******"Error validating user : "******"Invalid confirmation code for user : "******"User challenge question retrieved successfully");
        }
      } else {
        if (log.isDebugEnabled()) {
          log.debug("Verification failed for user. Error : " + bean.getError());
        }
        userChallengesDTO.setError(
            VerificationBean.ERROR_CODE_INVALID_USER + " " + bean.getError());
      }
    } finally {
      if (IdentityMgtConfig.getInstance().isSaasEnabled()) {
        PrivilegedCarbonContext.endTenantFlow();
      }
    }

    return userChallengesDTO;
  }
  /**
   * This method is to verify the user supplied answer for the challenge question.
   *
   * @param userName
   * @param confirmation
   * @param questionId
   * @param answer
   * @return status and key details about the operation status.
   * @throws IdentityMgtServiceException
   */
  public VerificationBean verifyUserChallengeAnswer(
      String userName, String confirmation, String questionId, String answer)
      throws IdentityMgtServiceException {

    VerificationBean bean = new VerificationBean();
    bean.setVerified(false);

    if (log.isDebugEnabled()) {
      log.debug("User challenge answer request received with username :"******"No challenge question id provided for verification";
      bean.setError(error);
      if (log.isDebugEnabled()) {
        log.debug(error);
      }

      return bean;
    }

    UserDTO userDTO = null;
    try {
      userDTO = Utils.processUserId(userName);
    } catch (IdentityException e) {
      bean =
          handleError(
              VerificationBean.ERROR_CODE_INVALID_USER + " Error verifying user: "******" Error verifying confirmation code for user : "******"");
        bean.setUserId(userName);
        if (log.isDebugEnabled()) {
          log.debug("User answer verification successful for user: "******"Challenge answer verification failed for user : "******""); // clear the key to avoid returning to caller.
        log.error(bean.getError());
      }
    } finally {
      if (IdentityMgtConfig.getInstance().isSaasEnabled()) {
        PrivilegedCarbonContext.endTenantFlow();
      }
    }
    return bean;
  }
  public ChallengeQuestionIdsDTO getUserChallengeQuestionIds(String username, String confirmation)
      throws IdentityMgtServiceException {

    UserDTO userDTO = null;
    ChallengeQuestionIdsDTO idsDTO = new ChallengeQuestionIdsDTO();

    if (log.isDebugEnabled()) {
      log.debug("User challenge questions id request received with username: "******"Error while getting challenge question ids for user : "******"Error when validating code", e1);
        return idsDTO;
      }
      if (bean.isVerified()) {
        try {
          idsDTO =
              processor
                  .getQuestionProcessor()
                  .getUserChallengeQuestionIds(userDTO.getUserId(), userDTO.getTenantId());
          idsDTO.setKey(bean.getKey());
          if (log.isDebugEnabled()) {
            log.debug("User challenge question response successful for user: "******"Error when getting user challenge questions for user : "******"Verification failed for user. Error : " + bean.getError();
        log.error(msg);
        idsDTO.setError(VerificationBean.ERROR_CODE_UNEXPECTED + " " + msg);
        idsDTO.setKey("");
      }
    } finally {
      if (IdentityMgtConfig.getInstance().isSaasEnabled()) {
        PrivilegedCarbonContext.endTenantFlow();
      }
    }

    return idsDTO;
  }