You are here

miniorange_2fa_inline_registration.php in Google Authenticator / 2 Factor Authentication - 2FA 8

Same filename and directory in other branches
  1. 8.2 src/Form/miniorange_2fa_inline_registration.php

File

src/Form/miniorange_2fa_inline_registration.php
View source
<?php

namespace Drupal\miniorange_2fa\form;

use http\Client\Curl\User;
use Drupal\Core\Form\FormBase;
use Drupal\miniorange_2fa\MiniorangeUser;
use Drupal\miniorange_2fa\UsersAPIHandler;
use Drupal\miniorange_2fa\MoAuthUtilities;
use Drupal\miniorange_2fa\MoAuthConstants;
use Drupal\miniorange_2fa\AuthenticationType;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\miniorange_2fa\AuthenticationAPIHandler;
use Drupal\miniorange_2fa\MiniorangeCustomerProfile;
use Symfony\Component\HttpFoundation\RedirectResponse;

/*
 * @file
 * Page 1: Select Email address.
 * Page 2: Verify OTP.
 * Page 3: Select Auth Method.
 * Page 4: Configure Auth Method.
 * Page 5: Configure KBA.
 */
class miniorange_2fa_inline_registration extends FormBase {
  public function getFormId() {
    return 'mo_auth_inline_registration';
  }
  public function buildForm(array $form, \Drupal\Core\Form\FormStateInterface $form_state) {
    $form['markup_library'] = array(
      '#attached' => array(
        'library' => array(
          "miniorange_2fa/miniorange_2fa.admin",
          "miniorange_2fa/miniorange_2fa.license",
        ),
      ),
    );
    if (!isset($_SESSION['success_status'])) {
      $_SESSION['success_status'] = TRUE;
    }
    $query_param = \Drupal::service('path.current')
      ->getPath();
    $url_parts = explode('/', $query_param);
    end($url_parts);
    $user_id = prev($url_parts);
    $form_state->uid = $user_id;
    $utilities = new MoAuthUtilities();
    $custom_attribute = $utilities::get_users_custom_attribute($user_id);
    $account = \Drupal\user\Entity\User::load($user_id);
    $storage = $form_state
      ->getStorage();
    if (empty($custom_attribute[0]->miniorange_registered_email)) {

      // Display page 5 if $storage['page_five'] is set
      if (isset($storage['page_five'])) {
        return $this
          ->mo_auth_inline_registration_page_five($form, $form_state);
      }
      elseif (isset($storage['page_otp_validate'])) {
        return $this
          ->mo_auth_inline_registration_page_four_otp_validate($form, $form_state, $_SESSION['success_status'], $_SESSION['message']);
      }
      elseif (isset($storage['page_four'])) {
        return $this
          ->mo_auth_inline_registration_page_four($form, $form_state, $_SESSION['success_status'], $_SESSION['message']);
      }
      elseif (isset($storage['page_three'])) {
        return $this
          ->mo_auth_inline_registration_page_three($form, $form_state);
      }
      elseif (isset($storage['page_two'])) {
        return $this
          ->mo_auth_inline_registration_page_two($form, $form_state, $_SESSION['success_status']);
      }
      else {
        return $this
          ->mo_auth_inline_registration_page_one($form, $form_state, $_SESSION['success_status'], $account
          ->getEmail());
      }
    }
  }
  function mo_auth_inline_registration_page_four_otp_validate(array $form, \Drupal\Core\Form\FormStateInterface $form_state, $success_status, $message) {
    return $this
      ->mo_auth_get_otp_over_sms_validate_form($form, $form_state, $success_status, $message);
  }
  function mo_auth_inline_registration_page_one($form, $form_state, $success_status, $user_email) {
    $prefix = '<div class="mo2f-modal">
              <div class="mo2f-modal-content">
              <div class="mo2f-modal-container mo2f-modal-header">Register (Step 1/5)</div>
              <div class="mo2f-modal-container">';
    if ($success_status === FALSE) {
      if (isset($_SESSION['message'])) {
        $message = $_SESSION['message'];
        $prefix .= '<div class="mo2f-message mo2f-message-error">' . $message . '</div>';
      }
      else {
        $message = 'This email is already in use. Please try another email.';
        $prefix .= '<div class="mo2f-message mo2f-message-error">' . $message . '</div>';
      }
      unset($_SESSION['message']);
      $_SESSION['success_status'] = TRUE;
    }
    $prefix .= '<div class="mo2f-info mo2f-text-center">
  		A new security system has been enabled to better protect your account. Please configure
  		your Two-Factor Authentication method by setting up your account</div>
  		<div class="mo2f-text-center">';
    $sufix = '</div></div><div class="mo2f-modal-container mo2f-modal-footer">';
    $form['mo_auth_user_email'] = array(
      '#type' => 'textfield',
      '#default_value' => $user_email,
      '#attributes' => array(
        'placeholder' => 'person@example.com',
        'class' => array(
          'mo2f_email_textbox',
        ),
      ),
      '#prefix' => $prefix,
      '#suffix' => $sufix,
    );
    $form['actions'] = array(
      '#type' => 'actions',
    );
    $form['actions']['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Get Started'),
      '#submit' => array(
        '::handle_page_one_submit',
      ),
    );
    $form['actions']['cancel'] = array(
      '#type' => 'submit',
      '#value' => t('Cancel'),
      '#submit' => array(
        '::handle_page_cancel',
      ),
    );
    return $form;
  }
  function handle_page_one_submit(array $form, \Drupal\Core\Form\FormStateInterface $form_state) {
    $email = $form['mo_auth_user_email']['#value'];
    if (!\Drupal::service('email.validator')
      ->isValid($email)) {

      // Send Status as this to show error message
      $_SESSION['success_status'] = FALSE;
      $_SESSION['message'] = 'The email address <b><u>' . $email . '</u></b> is not valid.';
      $form_state
        ->setRebuild();
      return $form;
    }
    $connection = \Drupal::database();
    $query = $connection
      ->query("SELECT * FROM {UserAuthenticationType} where miniorange_registered_email = '{$email}'");
    $result = $query
      ->fetchAll();
    $email_used = FALSE;
    if (!empty($result)) {
      $email_used = TRUE;
    }
    if ($email_used) {

      // Send Status as this to show error message
      $_SESSION['success_status'] = FALSE;
      $form_state
        ->setRebuild();
      return $form;
    }
    $customer = new MiniorangeCustomerProfile();
    $miniorange_user = new MiniorangeUser($customer
      ->getCustomerID(), $email, NULL, NULL, NULL);
    $user_api_handler = new UsersAPIHandler($customer
      ->getCustomerID(), $customer
      ->getAPIKey());
    $response = $user_api_handler
      ->search($miniorange_user);
    if ($response->status == 'USER_FOUND' || $response->status == 'USER_NOT_FOUND') {
      $challenge_response = $this
        ->send_otp_email_to_user($email);
      if ($challenge_response->status == 'SUCCESS') {
        $page = [
          'page_two' => TRUE,
          'page_one_values' => $form_state
            ->getValues(),
          [
            'user_search_response' => $response,
            'user_challenge_response' => $challenge_response,
          ],
        ];
        $form_state
          ->setStorage($page);
        $form_state
          ->setRebuild();
      }
      else {
        \Drupal::messenger()
          ->addMessage('An error occured while registering. Please contact your administrator.', 'error');
      }
    }
    elseif ($response->status == 'USER_FOUND_UNDER_DIFFERENT_CUSTOMER') {
      $_SESSION['success_status'] = FALSE;
      $form_state
        ->setRebuild();
      return $form;
    }
    else {
      global $base_url;
      unset($_SESSION['success_status']);
      \Drupal::messenger()
        ->addMessage(t("'An error occurred. Please contact your administrator.' "), 'error', TRUE);
      $response = new RedirectResponse($base_url . '/user/login');
      $response
        ->send();
    }
  }
  function mo_auth_inline_registration_page_two(array $form, \Drupal\Core\Form\FormStateInterface $form_state, $success_status) {
    $storage = $form_state
      ->getStorage();
    $email = $storage['page_one_values']['mo_auth_user_email'];
    if ($success_status === FALSE) {
      $message = 'The OTP you have entered is incorrect.';
      $_SESSION['success_status'] = TRUE;
    }
    else {
      $message = 'We have sent an OTP to ' . $email . '. Enter the OTP to verify your email.';
    }
    $message_div_class = $success_status === TRUE ? 'mo2f-message-status' : 'mo2f-message-error';
    $prefix = '<div class="mo2f-modal">
              <div class="mo2f-modal-content">
                <div class="mo2f-modal-container mo2f-modal-header">Verify Email (Step 2/5)</div>
                <div class="mo2f-modal-container">
				  <div class="mo2f-message ' . $message_div_class . '">' . $message . '</div>
  		          <div class="mo2f-info">Enter the passcode:</div>
				<div>';
    $suffix = '</div></div><div class="mo2f-modal-container mo2f-modal-footer">';
    $form['mo_auth_verify_token'] = array(
      '#type' => 'textfield',
      '#attributes' => array(
        'placeholder' => 'Enter the OTP',
        'class' => array(
          'mo2f-textbox',
          'mo2f-textbox-otp',
        ),
        'autofocus' => 'true',
      ),
      //'#value' => '',
      '#maxlength' => 6,
      '#prefix' => $prefix,
      '#suffix' => $suffix,
    );
    $form['actions'] = array(
      '#type' => 'actions',
    );
    $form['actions']['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Verify'),
      '#submit' => array(
        '::handle_page_two_submit',
      ),
    );
    $form['actions']['cancel'] = array(
      '#type' => 'submit',
      '#value' => t('Cancel'),
      '#submit' => array(
        '::handle_page_cancel',
      ),
    );
    return $form;
  }
  function handle_page_two_submit(array $form, \Drupal\Core\Form\FormStateInterface $form_state) {
    global $base_url;
    $storage = $form_state
      ->getStorage();
    $token = $form['mo_auth_verify_token']['#value'];
    $challenge_response = $storage[0]['user_challenge_response'];
    $validate_response = $this
      ->validate_otp_for_user($challenge_response->txId, $token);
    if ($validate_response->status == 'FAILED') {
      $page = [
        'page_two' => TRUE,
        'page_one_values' => $storage['page_one_values'],
        [
          'user_search_response' => $storage[0]['user_search_response'],
          'user_challenge_response' => $storage[0]['user_challenge_response'],
        ],
      ];
      $form_state
        ->setStorage($page);
      $_SESSION['success_status'] = FALSE;
      $form_state
        ->setRebuild();
      return $form;
    }
    elseif ($validate_response->status != 'SUCCESS') {
      unset($_SESSION['success_status']);
      $form_state
        ->setRebuild();
      \Drupal::messenger()
        ->addMessage(t('An error occured while registering the user.'), 'error', TRUE);
      $response = new RedirectResponse($base_url . '/user/login');
      $response
        ->send();
    }
    $form_state
      ->setRebuild();
    $email = $storage['page_one_values']['mo_auth_user_email'];
    $user_search_response = $storage[0]['user_search_response'];
    $customer = new MiniorangeCustomerProfile();
    $miniorange_user = new MiniorangeUser($customer
      ->getCustomerID(), $email, NULL, NULL, AuthenticationType::$EMAIL_VERIFICATION['code']);
    $user_api_handler = new UsersAPIHandler($customer
      ->getCustomerID(), $customer
      ->getAPIKey());
    if ($user_search_response->status == 'USER_NOT_FOUND') {
      $create_response = $user_api_handler
        ->create($miniorange_user);
    }

    /* Check whether user creation limit is exceeded or not */
    if ($create_response->status == 'ERROR' && $create_response->message == 'Your user creation limit has been completed. Please upgrade your license to add more users.') {
      \Drupal::configFactory()
        ->getEditable('miniorange_2fa.settings')
        ->set('mo_user_limit_exceed', TRUE)
        ->save();
    }
    else {
      \Drupal::configFactory()
        ->getEditable('miniorange_2fa.settings')
        ->clear('mo_user_limit_exceed')
        ->save();
    }
    if (isset($create_response) && isset($create_response->status) && $create_response->status == 'ERROR') {
      unset($_SESSION['success_status']);
      $form_state
        ->setRebuild();
      \Drupal::messenger()
        ->addMessage(t('An error occurred while creating the user. Please contact your administrator.'), 'error');
      $response = new RedirectResponse($base_url . '/user/login');
      $response
        ->send();
      exit;
    }

    // Update User Auth method to OUT OF BAND EMAIL
    $user_update_response = $user_api_handler
      ->update($miniorange_user);
    if ($user_update_response->status == 'SUCCESS') {
      $page = [
        'page_three' => TRUE,
        'page_two_values' => $form_state
          ->getValues(),
        'page_one_values' => $storage['page_one_values'],
      ];
      $form_state
        ->setStorage($page);
      return;
    }

    // Handle error. return to login.
  }
  function mo_auth_inline_registration_page_three(array $form, \Drupal\Core\Form\FormStateInterface $form_state) {
    $prefix = '<div class="mo2f-modal">
            <div class="mo2f-modal-content">
              <div class="mo2f-modal-container mo2f-modal-header">Select
  		      Authentication method (Step 3/5)</div>
              <div class="mo2f-modal-container">
		<div class="mo2f-info">Select your authentication method:</div><div>';
    $suffix = '</div></div><div class="mo2f-modal-container mo2f-modal-footer">';
    $options = array(
      AuthenticationType::$EMAIL_VERIFICATION['code'] => AuthenticationType::$EMAIL_VERIFICATION['name'],
      AuthenticationType::$GOOGLE_AUTHENTICATOR['code'] => AuthenticationType::$GOOGLE_AUTHENTICATOR['name'],
      AuthenticationType::$QR_CODE['code'] => AuthenticationType::$QR_CODE['name'],
      AuthenticationType::$KBA['code'] => AuthenticationType::$KBA['name'],
      AuthenticationType::$SOFT_TOKEN['code'] => AuthenticationType::$SOFT_TOKEN['name'],
      AuthenticationType::$PUSH_NOTIFICATIONS['code'] => AuthenticationType::$PUSH_NOTIFICATIONS['name'],
      AuthenticationType::$SMS['code'] => AuthenticationType::$SMS['name'],
      AuthenticationType::$SMS_AND_EMAIL['code'] => AuthenticationType::$SMS_AND_EMAIL['name'],
      AuthenticationType::$EMAIL['code'] => AuthenticationType::$EMAIL['name'],
      AuthenticationType::$OTP_OVER_PHONE['code'] => AuthenticationType::$OTP_OVER_PHONE['name'],
    );
    $form['mo_auth_method'] = array(
      '#type' => 'radios',
      '#default_value' => AuthenticationType::$EMAIL_VERIFICATION['code'],
      '#options' => $options,
      '#required' => TRUE,
      '#prefix' => $prefix,
      '#suffix' => $suffix,
    );
    $form['actions'] = array(
      '#type' => 'actions',
    );
    $form['actions']['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Next'),
      '#submit' => array(
        '::handle_page_three_submit',
      ),
    );
    $form['actions']['cancel'] = array(
      '#type' => 'submit',
      '#value' => t('Cancel'),
      '#submit' => array(
        '::handle_page_cancel',
      ),
    );
    return $form;
  }
  function handle_page_three_submit(array $form, \Drupal\Core\Form\FormStateInterface &$form_state) {
    $storage = $form_state
      ->getStorage();
    $form_state
      ->setRebuild();
    $method = $form['mo_auth_method']['#value'];
    if ($method == AuthenticationType::$EMAIL_VERIFICATION['code']) {

      // Go to Step 5 directly
      $page = [
        'page_five' => TRUE,
        'page_three_values' => $form_state
          ->getValues(),
        'page_two_values' => $storage['page_two_values'],
        'page_one_values' => $storage['page_one_values'],
      ];
      $form_state
        ->setStorage($page);
    }
    else {
      $page = [
        'page_four' => TRUE,
        'page_three_values' => $form_state
          ->getValues(),
        'page_two_values' => $storage['page_two_values'],
        'page_one_values' => $storage['page_one_values'],
      ];
      $_SESSION['message'] = '';
      $_SESSION['success_status'] = TRUE;
      $form_state
        ->setStorage($page);
    }
  }
  function mo_auth_inline_registration_page_four(array $form, \Drupal\Core\Form\FormStateInterface $form_state, $success_status, $message) {
    $storage = $form_state
      ->getStorage();
    $method = $storage['page_three_values']['mo_auth_method'];
    if ($method == AuthenticationType::$GOOGLE_AUTHENTICATOR['code']) {
      return $this
        ->mo_auth_get_google_authentication_form($form, $form_state, $success_status);
    }
    elseif ($method == AuthenticationType::$KBA['code']) {
      return $this
        ->mo_auth_get_kba_authentication_form($form, $form_state);
    }
    elseif ($method == AuthenticationType::$QR_CODE['code'] || $method == AuthenticationType::$PUSH_NOTIFICATIONS['code'] || $method == AuthenticationType::$SOFT_TOKEN['code']) {
      return $this
        ->mo_auth_get_qrcode_authentication_form($form, $form_state, $success_status);
    }
    elseif ($method == AuthenticationType::$SMS['code'] || $method == AuthenticationType::$SMS_AND_EMAIL['code'] || $method == AuthenticationType::$EMAIL['code'] || $method == AuthenticationType::$OTP_OVER_PHONE['code']) {
      return $this
        ->mo_auth_get_otp_over_sms_authentication_form($form, $form_state, $success_status, $message);
    }
  }
  function mo_auth_inline_registration_page_five($form, $form_state) {
    $prefixQuestion1 = '<div class="mo2f-modal">
            <div class="mo2f-modal-content">
        <div class="mo2f-modal-container mo2f-modal-header">Configure Backup method(Step 5/5)</div>
      <div class="mo2f-modal-container">
	    <div class="mo2f-info">Please choose your backup security questions:</div>
  		  <div>
  		    <div class="mo2f-kba-header mo2f-kba-row">
  			  <div class="mo2f-kba-srno">Sr. No.</div>
  			  <div class="mo2f-kba-question">Questions</div>
			  <div class="mo2f-kba-answer">Answers</div>
  			</div>
  			<div class="mo2f-kba-row">
  			  <div class="mo2f-kba-srno">1.</div>
  			  <div class="mo2f-kba-question">';
    $suffixQuestion1 = '</div>';
    $prefixAnswer1 = '<div class="mo2f-kba-answer">';
    $suffixAnswer1 = '</div></div>';
    $prefixQuestion2 = '<div class="mo2f-kba-row">
  			<div class="mo2f-kba-srno">2.</div>
  			<div class="mo2f-kba-question">';
    $suffixQuestion2 = '</div>';
    $prefixAnswer2 = '<div class="mo2f-kba-answer">';
    $suffixAnswer2 = '</div></div>';
    $prefixQuestion3 = '<div class="mo2f-kba-row">
  			<div class="mo2f-kba-srno">3.</div>
  			<div class="mo2f-kba-question">';
    $suffixQuestion3 = '</div>';
    $prefixAnswer3 = '<div class="mo2f-kba-answer">';
    $suffixAnswer3 = '</div></div></div></div><div class="mo2f-modal-container mo2f-modal-footer">';
    $options_one = array(
      'What is your first company name?' => 'What is your first company name?',
      'What was your childhood nickname?' => 'What was your childhood nickname?',
      'In what city did you meet your spouse/significant other?' => 'In what city did you meet your spouse/significant other?',
      'What is the name of your favorite childhood friend?' => 'What is the name of your favorite childhood friend?',
      'What school did you attend for sixth grade?' => 'What school did you attend for sixth grade?',
    );
    $options_two = array(
      'In what city or town was your first job?' => 'In what city or town was your first job?',
      'What is your favourite sport?' => 'What is your favourite sport?',
      'Who is your favourite sports player?' => 'Who is your favourite sports player?',
      'What is your grandmothers maiden name?' => 'What is your grandmothers maiden name?',
      'What was your first vehicles registration number?' => 'What was your first vehicles registration number?',
    );
    $form['mo_auth_question1'] = array(
      '#type' => 'select',
      '#options' => $options_one,
      '#prefix' => $prefixQuestion1,
      '#suffix' => $suffixQuestion1,
      '#required' => TRUE,
    );
    $form['mo_auth_answer1'] = array(
      '#type' => 'textfield',
      '#prefix' => $prefixAnswer1,
      '#suffix' => $suffixAnswer1,
      '#size' => '20',
      '#attributes' => array(
        'placeholder' => 'Enter your answer',
      ),
      '#required' => TRUE,
    );
    $form['mo_auth_question2'] = array(
      '#type' => 'select',
      '#options' => $options_two,
      '#prefix' => $prefixQuestion2,
      '#suffix' => $suffixQuestion2,
      '#required' => TRUE,
    );
    $form['mo_auth_answer2'] = array(
      '#type' => 'textfield',
      '#prefix' => $prefixAnswer2,
      '#suffix' => $suffixAnswer2,
      '#size' => '20',
      '#attributes' => array(
        'placeholder' => 'Enter your answer',
      ),
      '#required' => TRUE,
    );
    $form['mo_auth_question3'] = array(
      '#type' => 'textfield',
      '#prefix' => $prefixQuestion3,
      '#suffix' => $suffixQuestion3,
      '#attributes' => array(
        'placeholder' => 'Enter your custom question here',
      ),
      '#required' => TRUE,
      '#size' => 44,
    );
    $form['mo_auth_answer3'] = array(
      '#type' => 'textfield',
      '#prefix' => $prefixAnswer3,
      '#suffix' => $suffixAnswer3,
      '#size' => '20',
      '#attributes' => array(
        'placeholder' => 'Enter your answer',
      ),
      '#required' => TRUE,
    );
    $form['actions'] = array(
      '#type' => 'actions',
    );
    $form['actions']['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Register'),
      '#submit' => array(
        '::handle_page_five_submit',
      ),
    );
    return $form;
  }
  function handle_page_four_submit(array $form, \Drupal\Core\Form\FormStateInterface $form_state) {
    global $base_url;
    $storage = $form_state
      ->getStorage();
    $method = $storage['page_three_values']['mo_auth_method'];
    $form_state
      ->setRebuild();
    if ($method == AuthenticationType::$GOOGLE_AUTHENTICATOR['code']) {
      $email = $storage['page_one_values']['mo_auth_user_email'];
      $google_auth_response = $storage['page_four_values']['google_auth_response'];
      $token = $form['mo_auth_googleauth_token']['#value'];
      $secret = $google_auth_response->secret;
      $customer = new MiniorangeCustomerProfile();
      $miniorange_user = new MiniorangeUser($customer
        ->getCustomerID(), $email, NULL, NULL, AuthenticationType::$GOOGLE_AUTHENTICATOR['code']);
      $auth_api_handler = new AuthenticationAPIHandler($customer
        ->getCustomerID(), $customer
        ->getAPIKey());
      $response = $auth_api_handler
        ->register($miniorange_user, AuthenticationType::$GOOGLE_AUTHENTICATOR['code'], $secret, $token, NULL);
      if ($response->status == 'SUCCESS') {
        $page = [
          'page_five' => TRUE,
          'page_four_values' => $form_state
            ->getValues(),
          'page_three_values' => $storage['page_three_values'],
          'page_two_values' => $storage['page_two_values'],
          'page_one_values' => $storage['page_one_values'],
        ];
        $_SESSION['success_status'] = TRUE;
        $form_state
          ->setStorage($page);
        return;
      }
      elseif ($response->status == 'FAILED') {

        // Passcode incorrect. Try again - Show error form
        $page = [
          'page_four' => TRUE,
          'page_four_values' => [
            'google_auth_response' => $storage['page_four_values']['google_auth_response'],
          ],
          'page_three_values' => $storage['page_three_values'],
          'page_two_values' => $storage['page_two_values'],
          'page_one_values' => $storage['page_one_values'],
        ];
        $form_state
          ->setStorage($page);
        $_SESSION['success_status'] = FALSE;
        $form_state
          ->setRebuild();
        return;
      }
      elseif ($response->status != 'SUCCESS') {
        $form_state
          ->setRebuild();
        unset($_SESSION['success_status']);
        \Drupal::messenger()
          ->addMessage(t('An error occured while registering the user.'), 'error', TRUE);
        $response = new RedirectResponse($base_url . '/user/login');
        $response
          ->send();
      }
    }
    elseif ($method == AuthenticationType::$QR_CODE['code'] || $method == AuthenticationType::$SOFT_TOKEN['code'] || $method == AuthenticationType::$PUSH_NOTIFICATIONS['code']) {
      $qrcode_response = $storage['page_four_values']['qrcode_response'];
      $customer = new MiniorangeCustomerProfile();
      $auth_api_handler = new AuthenticationAPIHandler($customer
        ->getCustomerID(), $customer
        ->getAPIKey());
      $response = $auth_api_handler
        ->getRegistrationStatus($qrcode_response->txId);
      if ($response->status == 'SUCCESS') {
        $page = [
          'page_five' => TRUE,
          'page_four_values' => $form_state
            ->getValues(),
          'page_three_values' => $storage['page_three_values'],
          'page_two_values' => $storage['page_two_values'],
          'page_one_values' => $storage['page_one_values'],
        ];
        $form_state
          ->setStorage($page);
        return;
      }
    }
    elseif ($method == AuthenticationType::$SMS['code'] || $method == AuthenticationType::$SMS_AND_EMAIL['code'] || $method == AuthenticationType::$EMAIL['code'] || $method == AuthenticationType::$OTP_OVER_PHONE['code']) {
      $input = $form_state
        ->getUserInput();
      $phone = isset($input['mo_auth_otpoversms_phone']) ? $input['mo_auth_otpoversms_phone'] : '';
      $email = $storage['page_one_values']['mo_auth_user_email'];
      $customer = new MiniorangeCustomerProfile();
      if ($method == AuthenticationType::$SMS_AND_EMAIL['code']) {
        $miniorange_user = new MiniorangeUser($customer
          ->getCustomerID(), NULL, $phone, NULL, $method, $email);
      }
      elseif ($method == AuthenticationType::$SMS['code']) {
        $miniorange_user = new MiniorangeUser($customer
          ->getCustomerID(), NULL, $phone, NULL, $method, NULL);
      }
      elseif ($method == AuthenticationType::$EMAIL['code']) {
        $miniorange_user = new MiniorangeUser($customer
          ->getCustomerID(), NULL, NULL, NULL, $method, $email);
      }
      elseif ($method == AuthenticationType::$OTP_OVER_PHONE['code']) {
        $miniorange_user = new MiniorangeUser($customer
          ->getCustomerID(), NULL, $phone, NULL, $method, NULL);
      }
      $auth_api_handler = new AuthenticationAPIHandler($customer
        ->getCustomerID(), $customer
        ->getAPIKey());
      $response = $auth_api_handler
        ->challenge($miniorange_user);
      if ($response->status == 'SUCCESS') {
        $page = [
          'page_otp_validate' => TRUE,
          'page_four_values' => $form_state
            ->getValues(),
          [
            'user_challenge_response' => $response,
          ],
          'page_three_values' => $storage['page_three_values'],
          'page_two_values' => $storage['page_two_values'],
          'page_one_values' => $storage['page_one_values'],
        ];
        $_SESSION['message'] = 'INVALID OTP';
        $_SESSION['success_status'] = TRUE;
        $form_state
          ->setStorage($page);
        return;
      }
      elseif ($response->status == 'FAILED') {

        //$error = $response->message;
        $page = [
          'Page_four' => TRUE,
          'page_three_values' => $storage['page_three_values'],
          'page_two_values' => $storage['page_two_values'],
          'page_one_values' => $storage['page_one_values'],
        ];
        $_SESSION['message'] = 'Error during sending OTP. You may choose other methods.';
        $_SESSION['success_status'] = FALSE;
        $form_state
          ->setStorage($page);
        $form_state
          ->setRebuild();
        return;
      }
    }
    elseif ($method == AuthenticationType::$KBA['code']) {
      $this
        ->handle_page_five_submit($form, $form_state);
      return;
    }

    // Handle all error
  }
  function handle_page_five_submit(array $form, \Drupal\Core\Form\FormStateInterface $form_state) {
    global $base_url;
    $form_state
      ->setRebuild();
    $storage = $form_state
      ->getStorage();
    $user_email = $storage['page_one_values']['mo_auth_user_email'];
    $user_phone = isset($storage['page_four_values']['mo_auth_otpoversms_phone']) ? $storage['page_four_values']['mo_auth_otpoversms_phone'] : NULL;
    $question1 = $form['mo_auth_question1']['#value'];
    $answer1 = $form['mo_auth_answer1']['#value'];
    $question2 = $form['mo_auth_question2']['#value'];
    $answer2 = $form['mo_auth_answer2']['#value'];
    $question3 = $form['mo_auth_question3']['#value'];
    $answer3 = $form['mo_auth_answer3']['#value'];
    $qa1 = array(
      "question" => $question1,
      "answer" => $answer1,
    );
    $qa2 = array(
      "question" => $question2,
      "answer" => $answer2,
    );
    $qa3 = array(
      "question" => $question3,
      "answer" => $answer3,
    );
    $kba = array(
      $qa1,
      $qa2,
      $qa3,
    );
    $method = $storage['page_three_values']['mo_auth_method'];
    $query_param = \Drupal::service('path.current')
      ->getPath();
    $url_parts = explode('/', $query_param);
    end($url_parts);
    $user_id = prev($url_parts);
    $customer = new MiniorangeCustomerProfile();
    $miniorange_user = new MiniorangeUser($customer
      ->getCustomerID(), $user_email, $user_phone, NULL, $method);
    $auth_api_handler = new AuthenticationAPIHandler($customer
      ->getCustomerID(), $customer
      ->getAPIKey());
    $response = $auth_api_handler
      ->register($miniorange_user, AuthenticationType::$KBA['code'], NULL, NULL, $kba);
    if ($response->status == 'SUCCESS') {
      $user_api_handler = new UsersAPIHandler($customer
        ->getCustomerID(), $customer
        ->getAPIKey());
      $user_update_response = $user_api_handler
        ->update($miniorange_user);
      if ($user_update_response->status == 'SUCCESS') {
        $database = \Drupal::database();
        $fields = array(
          'uid' => $user_id,
          'configured_auth_methods' => AuthenticationType::$EMAIL_VERIFICATION['code'],
          'miniorange_registered_email' => $user_email,
          'activated_auth_methods' => $method,
        );
        $database
          ->insert('UserAuthenticationType')
          ->fields($fields)
          ->execute();
        $configured_methods = MoAuthUtilities::mo_auth_get_configured_methods($user_id);
        $available = MoAuthUtilities::check_for_userID($user_id);
        if (!in_array($method, $configured_methods)) {
          array_push($configured_methods, $method);
        }
        if ($method != AuthenticationType::$KBA['code']) {
          array_push($configured_methods, AuthenticationType::$KBA['code']);
        }
        $config_methods = implode(', ', $configured_methods);
        if ($available == TRUE) {
          $database
            ->update('UserAuthenticationType')
            ->fields([
            'configured_auth_methods' => $config_methods,
          ])
            ->condition('uid', $user_id, '=')
            ->execute();
        }
        else {
          echo "error while updating authentication method.";
          exit;
        }
        $user = \Drupal\user\Entity\User::load($user_id);
        user_login_finalize($user);
        $response = new RedirectResponse($base_url . '/user/login');
        $response
          ->send();
      }
    }
    elseif ($response->status != 'SUCCESS') {

      // Error out. Send to login.
      \Drupal::messenger()
        ->addMessage(t('Unable to setup second factor. Please contact your administrator.'), 'error', TRUE);
      $response = new RedirectResponse($base_url . '/user/login');
      $response
        ->send();
    }
  }
  function send_otp_email_to_user($username) {
    $customer = new MiniorangeCustomerProfile();
    $miniorange_user = new MiniorangeUser($customer
      ->getCustomerID(), NULL, NULL, NULL, AuthenticationType::$EMAIL['code'], $username);
    $auth_api_handler = new AuthenticationAPIHandler($customer
      ->getCustomerID(), $customer
      ->getAPIKey());
    $response = $auth_api_handler
      ->challenge($miniorange_user);
    return $response;
  }
  function validate_otp_for_user($txId, $token) {
    $customer = new MiniorangeCustomerProfile();
    $miniorange_user = new MiniorangeUser($customer
      ->getCustomerID(), NULL, NULL, NULL, NULL);
    $auth_api_handler = new AuthenticationAPIHandler($customer
      ->getCustomerID(), $customer
      ->getAPIKey());
    $response = $auth_api_handler
      ->validate($miniorange_user, $txId, $token);
    return $response;
  }
  function mo_auth_get_google_authentication_form(array $form, \Drupal\Core\Form\FormStateInterface $form_state, $success_status) {
    global $base_url;
    $storage = $form_state
      ->getStorage();
    $email = $storage['page_one_values']['mo_auth_user_email'];
    if (isset($storage['page_four_values']['google_auth_response'])) {
      $google_auth_response = $storage['page_four_values']['google_auth_response'];
      $qrCode = $google_auth_response->qrCodeData;
      $image = new FormattableMarkup('<img src="data:image/jpg;base64, ' . $qrCode . '"/>', [
        ':src' => $qrCode,
      ]);
    }
    else {
      $customer = new MiniorangeCustomerProfile();
      $auth_api_handler = new AuthenticationAPIHandler($customer
        ->getCustomerID(), $customer
        ->getAPIKey());
      $miniorange_user = new MiniorangeUser($customer
        ->getCustomerID(), $email, NULL, NULL, NULL);
      $response = $auth_api_handler
        ->getGoogleAuthSecret($miniorange_user);
      if ($response->status == 'SUCCESS') {
        $page = [
          'page_four_values' => [
            'google_auth_response' => $response,
          ],
          'page_three_values' => $storage['page_three_values'],
          'page_two_values' => $storage['page_two_values'],
          'page_one_values' => $storage['page_one_values'],
        ];
        $form_state
          ->setStorage($page);
        $qrCode = $response->qrCodeData;
        $image = new FormattableMarkup('<img src="data:image/jpg;base64, ' . $qrCode . '"/>', [
          ':src' => $qrCode,
        ]);
      }
    }
    $iPhoneAppLink = file_create_url($base_url . '/' . drupal_get_path('module', 'miniorange_2fa') . '/includes/images/iphone-google-authenticator-app-link.png');
    $androidAppLink = file_create_url($base_url . '/' . drupal_get_path('module', 'miniorange_2fa') . '/includes/images/android-google-authenticator-app-link.png');
    $form['actions_21'] = array(
      '#markup' => '<div class="mo2f-modal">
              <div class="mo2f-modal-content">
                <div class="mo2f-modal-container mo2f-modal-header">Configure Google Authenticator (Step 4/5)</div>
                <div class="mo2f-modal-container">',
    );
    if ($success_status === FALSE) {
      $message = 'The passcode you have entered is incorrect.';
      $form['actions_22'] = array(
        '#markup' => '
                    <div class="mo2f-message mo2f-message-error">' . $message . '</div>',
      );
      $_SESSION['success_status'] = TRUE;
    }
    $form['actions_23'] = array(
      '#markup' => '<div><div class="mo2f-info"><b>Step 1:</b> Download and install the Google Authenticator app</div>
			<div class="mo2f-text-center" style="display:inline-block;width:60%;text-align:center">
			  <a target="_blank" href="https://itunes.apple.com/in/app/google-authenticator/id388497605?mt=8"><img src="' . $iPhoneAppLink . '"></a>
				</div>
				<div class="mo2f-text-center" style="display:inline-block">
				  <a target="_blank" href="https://play.google.com/store/apps/details?id=com.google.android.apps.authenticator2"><img src="' . $androidAppLink . '"></a>
				</div>
				</div>',
    );
    $form['actions_24'] = array(
      '#markup' => '<div><div class="mo2f-info"><b>Step 2:</b> Scan the QR Code from Google Authenticator app</div>',
    );
    $form['actions_qrcode_google_authenticator'] = array(
      '#markup' => $image,
    );
    $form['actions_25'] = array(
      '#markup' => '<div class="mo2f-info mo2f-text-center">
               </div><div><div class="mo2f-info"><b>Step 3:</b> Enter the passcode generated by Google Authenticator app</div>',
    );
    $sufix = '</div></div><div class="mo2f-modal-container mo2f-modal-footer">';
    $form['mo_auth_googleauth_token'] = array(
      '#type' => 'textfield',
      '#attributes' => array(
        'placeholder' => 'Enter the passcode',
        'class' => array(
          'mo2f-textbox',
          'mo2f-textbox-otp',
        ),
        'autofocus' => 'true',
      ),
      '#maxlength' => 6,
      '#suffix' => $sufix,
    );
    $form['actions'] = array(
      '#type' => 'actions',
    );
    $form['actions']['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Verify and Save'),
      '#submit' => array(
        '::handle_page_four_submit',
      ),
    );
    $form['actions']['cancel'] = array(
      '#type' => 'submit',
      '#value' => t('Cancel'),
      '#submit' => array(
        '::handle_page_cancel',
      ),
    );
    return $form;
  }
  function mo_auth_get_qrcode_authentication_form(array $form, \Drupal\Core\Form\FormStateInterface $form_state, $success_form) {
    global $base_url;
    $storage = $form_state
      ->getStorage();
    $email = $storage['page_one_values']['mo_auth_user_email'];
    $method = $storage['page_three_values']['mo_auth_method'];
    $configureMessage = 'Configure QR Code Authentication';
    if ($method == AuthenticationType::$SOFT_TOKEN['code']) {
      $configureMessage = 'Configure Soft Token';
    }
    elseif ($method == AuthenticationType::$PUSH_NOTIFICATIONS['code']) {
      $configureMessage = 'Configure Push Notification';
    }
    $customer = new MiniorangeCustomerProfile();
    $auth_api_handler = new AuthenticationAPIHandler($customer
      ->getCustomerID(), $customer
      ->getAPIKey());
    $miniorange_user = new MiniorangeUser($customer
      ->getCustomerID(), $email, NULL, NULL, NULL);
    $response = $auth_api_handler
      ->register($miniorange_user, AuthenticationType::$QR_CODE['code'], NULL, NULL, NULL);
    if ($response->status == 'IN_PROGRESS') {
      $page = [
        'page_four_values' => [
          'qrcode_response' => $response,
        ],
        'page_three_values' => $storage['page_three_values'],
        'page_two_values' => $storage['page_two_values'],
        'page_one_values' => $storage['page_one_values'],
      ];
      $form_state
        ->setStorage($page);
      $qrCode = $response->qrCode;
      $image = new FormattableMarkup('<img src="data:image/jpg;base64, ' . $qrCode . '"/>', [
        ':src' => $qrCode,
      ]);
      $iPhoneAppLink = file_create_url($base_url . '/' . drupal_get_path('module', 'miniorange_2fa') . '/includes/images/iphone-google-authenticator-app-link.png');
      $androidAppLink = file_create_url($base_url . '/' . drupal_get_path('module', 'miniorange_2fa') . '/includes/images/android-google-authenticator-app-link.png');
      $form['actions_31'] = array(
        '#markup' => '<div class="mo2f-modal">
              <div class="mo2f-modal-content">
                <div class="mo2f-modal-container mo2f-modal-header">' . $configureMessage . ' (Step 4/5)</div>
                <div class="mo2f-modal-container">',
      );
      $form['actions_32'] = array(
        '#markup' => '<div><div class="mo2f-info"><b>Step 1:</b> Download and install the miniOrange Authenticator app</div>
				<div class="mo2f-text-center" style="display:inline-block;width:60%;text-align:center">
				  <a target="_blank" href="https://itunes.apple.com/us/app/miniorange-authenticator/id796303566?ls=1"><img src="' . $iPhoneAppLink . '"></a>
				</div>
				<div class="mo2f-text-center" style="display:inline-block">
				  <a target="_blank" href="https://play.google.com/store/apps/details?id=com.miniorange.authbeta"><img src="' . $androidAppLink . '"></a>
				</div>
				</div>',
      );
      $form['actions_33'] = array(
        '#markup' => '<div><div class="mo2f-info"><b>Step 2:</b> Scan the QR Code from miniOrange Authenticator app</div>
                                  <div class="mo2f-info"><b>Note:</b> Once you scan the QR code, page will get automatically submitted (Dont refresh the page).</div>
  			                      <div class="mo2f-info mo2f-text-center">',
      );
      $form['actions_qrcode_miniOrange'] = array(
        '#markup' => $image,
      );
      $form['actions_34'] = array(
        '#markup' => '</div></div>',
      );
      $sufix = '</div><div class="mo2f-modal-container mo2f-modal-footer">';
      $form['txId'] = array(
        '#type' => 'hidden',
        '#value' => $response->txId,
        '#suffix' => $sufix,
      );
      $form['url'] = array(
        '#type' => 'hidden',
        '#value' => MoAuthConstants::$AUTH_REGISTRATION_STATUS_API,
      );
      $form['actions'] = array(
        '#type' => 'actions',
      );
      $form['actions']['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Save'),
        '#attributes' => array(
          'class' => array(
            'hidebutton',
          ),
        ),
        '#submit' => array(
          '::handle_page_four_submit',
        ),
      );
      global $base_url;
      $url = $base_url . '/user/login';
      $form['cancel_button'] = array(
        '#markup' => '<a href="' . $url . '">Cancel</a>',
      );
    }
    return $form;
  }
  function mo_auth_get_kba_authentication_form(array $form, \Drupal\Core\Form\FormStateInterface $form_state) {
    $prefixQuestion1 = '<div class="mo2f-modal">
            <div class="mo2f-modal-content">
            <div class="mo2f-modal-container mo2f-modal-header">Configure Security Questions (Step 5/5)</div>
            <div class="mo2f-modal-container">';
    $prefixQuestion1 .= '<div class="mo2f-info">Please choose your backup security questions:</div><div>
            <div class="mo2f-kba-header mo2f-kba-row">
  			    <div class="mo2f-kba-srno">Sr. No.</div>
  			    <div class="mo2f-kba-question">Questions</div>
			    <div class="mo2f-kba-answer">Answers</div>
  			</div>
  			<div class="mo2f-kba-row">
  			<div class="mo2f-kba-srno">1.</div>
  			<div class="mo2f-kba-question">';
    $suffixQuestion1 = '</div>';
    $prefixAnswer1 = '<div class="mo2f-kba-answer">';
    $suffixAnswer1 = '</div></div>';
    $prefixQuestion2 = '<div class="mo2f-kba-row">
  			<div class="mo2f-kba-srno">2.</div>
  			<div class="mo2f-kba-question">';
    $suffixQuestion2 = '</div>';
    $prefixAnswer2 = '<div class="mo2f-kba-answer">';
    $suffixAnswer2 = '</div></div>';
    $prefixQuestion3 = '<div class="mo2f-kba-row">
  			<div class="mo2f-kba-srno">3.</div>
  			<div class="mo2f-kba-question">';
    $suffixQuestion3 = '</div>';
    $prefixAnswer3 = '<div class="mo2f-kba-answer">';
    $suffixAnswer3 = '</div></div></div></div><div class="mo2f-modal-container mo2f-modal-footer">';
    $options_one = array(
      'What is your first company name?' => 'What is your first company name?',
      'What was your childhood nickname?' => 'What was your childhood nickname?',
      'In what city did you meet your spouse/significant other?' => 'In what city did you meet your spouse/significant other?',
      'What is the name of your favorite childhood friend?' => 'What is the name of your favorite childhood friend?',
      'What school did you attend for sixth grade?' => 'What school did you attend for sixth grade?',
    );
    $options_two = array(
      'In what city or town was your first job?' => 'In what city or town was your first job?',
      'What is your favourite sport?' => 'What is your favourite sport?',
      'Who is your favourite sports player?' => 'Who is your favourite sports player?',
      'What is your grandmothers maiden name?' => 'What is your grandmothers maiden name?',
      'What was your first vehicles registration number?' => 'What was your first vehicles registration number?',
    );
    $form['mo_auth_question1'] = array(
      '#type' => 'select',
      '#options' => $options_one,
      '#prefix' => $prefixQuestion1,
      '#suffix' => $suffixQuestion1,
      '#required' => TRUE,
    );
    $form['mo_auth_answer1'] = array(
      '#type' => 'textfield',
      '#prefix' => $prefixAnswer1,
      '#suffix' => $suffixAnswer1,
      '#size' => '20',
      '#attributes' => array(
        'placeholder' => 'Enter your answer',
      ),
      '#required' => TRUE,
    );
    $form['mo_auth_question2'] = array(
      '#type' => 'select',
      '#options' => $options_two,
      '#prefix' => $prefixQuestion2,
      '#suffix' => $suffixQuestion2,
      '#required' => TRUE,
    );
    $form['mo_auth_answer2'] = array(
      '#type' => 'textfield',
      '#prefix' => $prefixAnswer2,
      '#suffix' => $suffixAnswer2,
      '#size' => '20',
      '#attributes' => array(
        'placeholder' => 'Enter your answer',
      ),
      '#required' => TRUE,
    );
    $form['mo_auth_question3'] = array(
      '#type' => 'textfield',
      '#prefix' => $prefixQuestion3,
      '#suffix' => $suffixQuestion3,
      '#attributes' => array(
        'placeholder' => 'Enter your custom question here',
      ),
      '#required' => TRUE,
      '#size' => 39,
    );
    $form['mo_auth_answer3'] = array(
      '#type' => 'textfield',
      '#prefix' => $prefixAnswer3,
      '#suffix' => $suffixAnswer3,
      '#size' => '20',
      '#attributes' => array(
        'placeholder' => 'Enter your answer',
      ),
      '#required' => TRUE,
    );
    $form['actions'] = array(
      '#type' => 'actions',
    );
    $form['actions']['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Register'),
      '#submit' => array(
        '::handle_page_four_submit',
      ),
    );
    return $form;
  }
  function mo_auth_get_otp_over_sms_authentication_form(array $form, \Drupal\Core\Form\FormStateInterface $form_state, $success_status, $message) {
    $storage = $form_state
      ->getStorage();
    $method = $storage['page_three_values']['mo_auth_method'];
    $email = $storage['page_one_values']['mo_auth_user_email'];
    $otp_over_sms = AuthenticationType::$SMS['code'];
    $otp_over_email = AuthenticationType::$EMAIL['code'];
    $otp_over_sms_and_email = AuthenticationType::$SMS_AND_EMAIL['code'];
    $otp_over_phone = AuthenticationType::$OTP_OVER_PHONE['code'];
    if ($method == $otp_over_sms) {
      $authmethod = 'OTP Over SMS';
    }
    elseif ($method == $otp_over_sms_and_email) {
      $authmethod = 'OTP Over SMS And Email';
    }
    elseif ($method == $otp_over_email) {
      $authmethod = 'OTP Over Email';
    }
    elseif ($method == $otp_over_phone) {
      $authmethod = 'OTP Over Phone';
    }
    $form['markup_library'] = array(
      '#attached' => array(
        'library' => array(
          "miniorange_2fa/miniorange_2fa.admin",
          "miniorange_2fa/miniorange_2fa.license",
        ),
      ),
    );
    $prefix = '<div class="mo2f-modal">
              <div class="mo2f-modal-content">
                <div class="mo2f-modal-container mo2f-modal-header">Configure ' . $authmethod . ' (Step 4/5)</div><div class="mo2f-modal-container">';
    if ($success_status === FALSE) {
      $prefix .= '<div class="mo2f-message mo2f-message-error">' . $message . '</div>';
    }
    if ($method == $otp_over_sms || $method == $otp_over_phone) {
      $step1 = ' Verify your phone number.';
    }
    elseif ($method == $otp_over_sms_and_email) {
      $step1 = ' Verify your phone number and email.';
    }
    elseif ($method == $otp_over_email) {
      $step1 = 'Verify your email.';
    }
    $prefix .= '<div><div class="mo2f-info">' . $step1;
    if ($method == $otp_over_sms_and_email || $method == $otp_over_email) {
      $prefix .= '<br><input type="text" class="mo2f-textbox mo2f-textbox-otp" value="' . $email . '" disabled><br>';
    }
    $prefix .= '</div></div>';
    $sufix = '</div><div class="mo2f-modal-container mo2f-modal-footer">';
    if ($method != $otp_over_email) {
      $form['mo_auth_otpoversms_phone'] = array(
        '#type' => 'textfield',
        '#id' => 'query_phone',
        '#description' => t('Enter number with country code Eg. +00xxxxxxxxxx'),
        '#attributes' => array(
          'placeholder' => 'Phone Number',
          'pattern' => '[\\+]?[0-9]{1,4}\\s?[0-9]{7,12}',
          'class' => array(
            'query_phone',
            'mo2f-textbox',
            'mo2f-textbox-otp',
          ),
          'autofocus' => 'true',
        ),
        '#required' => TRUE,
        '#prefix' => $prefix,
        '#suffix' => $sufix,
      );
    }
    else {
      $form['mo_auth_otpoversms_phone'] = array(
        '#type' => 'textfield',
        '#default_value' => $email,
        '#disabled' => 'true',
        '#description' => 'Your Email Id to which an OTP will be sent: ',
        '#attributes' => array(
          'class' => array(
            'mo2f-textbox',
            'mo2f-textbox-otp',
          ),
          'autofocus' => 'true',
        ),
        '#prefix' => $prefix,
        '#suffix' => $sufix,
      );
    }
    $form['actions'] = array(
      '#type' => 'actions',
    );
    $form['actions']['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Send OTP'),
      '#submit' => array(
        '::handle_page_four_submit',
      ),
    );
    return $form;
  }
  function mo_auth_get_otp_over_sms_validate_form(array $form, \Drupal\Core\Form\FormStateInterface $form_state, $success_status, $message) {
    $storage = $form_state
      ->getStorage();
    $method = $storage['page_three_values']['mo_auth_method'];
    $otp_over_sms = AuthenticationType::$SMS['code'];
    $otp_over_sms_and_email = AuthenticationType::$SMS_AND_EMAIL['code'];
    $otp_over_email = AuthenticationType::$EMAIL['code'];
    $otp_over_phone = AuthenticationType::$OTP_OVER_PHONE['code'];
    if ($method == $otp_over_sms) {
      $authmethod = 'OTP Over SMS';
    }
    elseif ($method == $otp_over_sms_and_email) {
      $authmethod = 'OTP Over SMS And Email';
    }
    elseif ($method == $otp_over_email) {
      $authmethod = 'OTP Over Email';
    }
    elseif ($method == $otp_over_phone) {
      $authmethod = 'OTP Over Phone';
    }
    $email = $storage['page_one_values']['mo_auth_user_email'];
    $phone = str_replace(' ', '', $storage['page_four_values']['mo_auth_otpoversms_phone']);
    if ($success_status === TRUE) {
      if ($method == $otp_over_sms) {
        $message = 'We have sent an OTP to ' . $phone . '. Enter the OTP received to verify your phone.';
      }
      elseif ($method == $otp_over_sms_and_email) {
        $message = 'We have sent an OTP to ' . $phone . ' and ' . $email . '. Enter the OTP received to verify your phone and email.';
      }
      elseif ($method == $otp_over_email) {
        $message = 'We have sent an OTP to ' . $email . '. Enter the OTP received to verify your email.';
      }
      elseif ($method == $otp_over_phone) {
        $message = 'You will get a call on ' . $phone . ' shortly, which prompts OTP. Please enter the OTP to verify your phone number.';
      }
    }
    $message_div_class = $success_status === TRUE ? 'mo2f-message-status' : 'mo2f-message-error';
    $prefix = '<div class="mo2f-modal">
              <div class="mo2f-modal-content">
                <div class="mo2f-modal-container mo2f-modal-header">Configure ' . $authmethod . ' (Step 4/5)</div>
                <div class="mo2f-modal-container"><div class="mo2f-message ' . $message_div_class . '">' . $message . '</div>
  		          <div class="mo2f-info">Enter the passcode:</div>
				<div>';
    $suffix = '</div><div class="mo2f-modal-container mo2f-modal-footer">';
    $form['mo_auth_otpoversms_code'] = array(
      '#type' => 'textfield',
      '#attributes' => array(
        'placeholder' => 'Enter the passcode',
        'class' => array(
          'mo2f-textbox',
          'mo2f-textbox-otp',
        ),
        'autofocus' => 'true',
      ),
      '#maxlength' => 6,
      '#prefix' => $prefix,
      '#suffix' => $suffix,
    );
    $form['actions'] = array(
      '#type' => 'actions',
    );
    $form['actions']['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Validate OTP'),
      '#submit' => array(
        '::handle_page_otp_validate_submit',
      ),
    );
    $form['actions']['cancel'] = array(
      '#type' => 'submit',
      '#value' => t('Cancel'),
      '#submit' => array(
        '::handle_page_cancel',
      ),
    );
    return $form;
  }
  function handle_page_otp_validate_submit(array $form, \Drupal\Core\Form\FormStateInterface $form_state) {
    $storage = $form_state
      ->getStorage();
    $input = $form_state
      ->getUserInput();
    $otp_code = $input['mo_auth_otpoversms_code'];
    $form_state
      ->setRebuild();
    $challenge_response = $storage[0]['user_challenge_response'];
    $validate_response = $this
      ->validate_otp_for_user($challenge_response->txId, $otp_code);
    if ($validate_response->status == 'SUCCESS') {
      $page = [
        'page_five' => TRUE,
        'page_otp_validate_values' => $form_state
          ->getValues(),
        'page_four_values' => $storage['page_four_values'],
        'page_three_values' => $storage['page_three_values'],
        'page_two_values' => $storage['page_two_values'],
        'page_one_values' => $storage['page_one_values'],
      ];
      $_SESSION['success_status'] = TRUE;
      $form_state
        ->setStorage($page);
      return;
    }
    elseif ($validate_response->status == 'FAILED') {

      //Retain All the values
      $page = [
        'page_otp_validate' => TRUE,
        'page_four_values' => $storage['page_four_values'],
        [
          'user_challenge_response' => $storage[0]['user_challenge_response'],
        ],
        'page_three_values' => $storage['page_three_values'],
        'page_two_values' => $storage['page_two_values'],
        'page_one_values' => $storage['page_one_values'],
      ];
      $_SESSION['message'] = 'INVALID OTP';
      $_SESSION['success_status'] = FALSE;
      $form_state
        ->setStorage($page);
      $form_state
        ->setRebuild();
      return;
    }
  }
  function handle_page_cancel() {
    global $base_url;
    unset($_SESSION['message']);
    $_SESSION['success_status'] = TRUE;
    $response = new RedirectResponse($base_url . '/user/login');
    $response
      ->send();
  }
  public function submitForm(array &$form, \Drupal\Core\Form\FormStateInterface $form_state) {
  }

}