private UserChallengesDTO handleChallengesError(String error, Exception e) {

    UserChallengesDTO bean = new UserChallengesDTO();

    if (error != null) {
      bean.setError(error);
      log.error(error, e);
    } else {
      bean.setError(e.getMessage());
      log.error(e.getMessage(), e);
    }

    return bean;
  }
  /**
   * 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;
  }
  /**
   * 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;
  }