/**
   * 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;
  }
  /**
   * This method used to confirm the self registered user account and unlock it.
   *
   * @param username
   * @param code
   * @param captcha
   * @param tenantDomain
   * @return
   * @throws IdentityMgtServiceException
   */
  public VerificationBean confirmUserSelfRegistration(
      String username, String code, CaptchaInfoBean captcha, String tenantDomain)
      throws IdentityMgtServiceException {

    VerificationBean bean = new VerificationBean();

    if (log.isDebugEnabled()) {
      log.debug("User registration verification request received with username :"******" Error while validating captcha for user : "******"Trying to confirm users in unauthorized tenant space";
        log.error(msg);
      }
      if (tenantDomain == null || tenantDomain.isEmpty()) {
        tenantDomain = loggedInTenant;
      }
    }

    UserDTO userDTO = null;
    try {
      userDTO = Utils.processUserId(username + "@" + tenantDomain);

    } catch (IdentityException e) {
      bean =
          handleError(
              VerificationBean.ERROR_CODE_INVALID_USER
                  + " Error verifying user account for user : "******"Error retrieving the user store manager for the tenant : " + tenantDomain, e);
        return bean;
      }

      try {
        bean = processor.verifyConfirmationCode(1, username, code);
        if (bean.isVerified()) {
          UserIdentityManagementUtil.unlockUserAccount(username, userStoreManager);
          bean.setVerified(true);

        } else {
          bean.setVerified(false);
          bean.setKey("");
          log.error("User verification failed against the given confirmation code");
        }
      } catch (IdentityException e) {
        bean = handleError("Error while validating confirmation code for user : " + username, e);
        return bean;
      }
    } finally {
      if (IdentityMgtConfig.getInstance().isSaasEnabled()) {
        PrivilegedCarbonContext.endTenantFlow();
      }
    }
    return bean;
  }
  /**
   * This method is used to verify the confirmation code sent to user is correct and validates.
   * Before calling this method it needs to supply a Captcha and should call getCaptcha().
   *
   * @param username - username of whom the password needs to be recovered.
   * @param code - confirmation code sent to user by notification.
   * @param captcha - generated captcha with answer for this communication.
   * @return - VerificationBean with new code to be used in updatePassword().
   * @throws IdentityMgtServiceException
   */
  public VerificationBean verifyConfirmationCode(
      String username, String code, CaptchaInfoBean captcha) throws IdentityMgtServiceException {

    UserDTO userDTO;
    VerificationBean bean = new VerificationBean();

    if (log.isDebugEnabled()) {
      log.debug("User confirmation code verification request received with username :"******" Error while validating captcha for user : "******" invalid user : "******"User confirmation code verification successful for user: "******"");
        log.error(bean.getError());
      }
    } catch (IdentityException e) {
      bean =
          handleError(
              VerificationBean.ERROR_CODE_INVALID_CODE
                  + " Error verifying confirmation code for user : "
                  + username,
              e);
      return bean;
    } finally {
      if (IdentityMgtConfig.getInstance().isSaasEnabled()) {
        PrivilegedCarbonContext.endTenantFlow();
      }
    }

    return bean;
  }