You are here

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

OTP Over SMS(test) functions.

File

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

namespace Drupal\miniorange_2fa\Form;


/**
 * @file
 * OTP Over SMS(test) functions.
 */
use Drupal\user\Entity\User;
use Drupal\Core\Form\FormBase;
use Drupal\miniorange_2fa\MiniorangeUser;
use Drupal\miniorange_2fa\MoAuthUtilities;
use Drupal\miniorange_2fa\AuthenticationType;
use Drupal\miniorange_2fa\AuthenticationAPIHandler;
use Drupal\miniorange_2fa\MiniorangeCustomerProfile;

/**
 * Menu callback for testing OTP Over SMS.
 */
class test_otp_over_sms extends FormBase {
  public function getFormId() {
    return 'miniorange_otp_over_sms';
  }
  public function buildForm(array $form, \Drupal\Core\Form\FormStateInterface $form_state) {
    $form['markup_top_2'] = array(
      '#markup' => '<div class="mo_saml_table_layout_1"><div class="mo_saml_table_layout mo_saml_container">',
    );
    $form['markup_library'] = array(
      '#attached' => array(
        'library' => 'miniorange_2fa/miniorange_2fa.admin',
        'library' => 'miniorange_2fa/miniorange_2fa.license',
      ),
    );
    global $base_url;
    $url = $base_url . '/admin/config/people/miniorange_2fa/setup_twofactor';
    $user = User::load(\Drupal::currentUser()
      ->id());
    $user_id = $user
      ->id();
    $custom_attribute = MoAuthUtilities::get_users_custom_attribute($user_id);
    $user_email = $custom_attribute[0]->miniorange_registered_email;
    $user_phone = \Drupal::config('miniorange_2fa.settings')
      ->get('mo_phone');
    $authTypeCode = AuthenticationType::$SMS['code'];
    $txId_Value = \Drupal::config('miniorange_2fa.settings')
      ->get('txId_Value');
    if ($txId_Value == 'EMPTY_VALUE') {
      $messageHeader = 'An OTP has been sent to <strong>' . $user_phone . '</strong>. Please enter it here to complete the test.';
      $customer = new MiniorangeCustomerProfile();
      $miniorange_user = new MiniorangeUser($customer
        ->getCustomerID(), NULL, $user_phone, $authTypeCode, NULL, $user_email);
      $auth_api_handler = new AuthenticationAPIHandler($customer
        ->getCustomerID(), $customer
        ->getAPIKey());
      $response = $auth_api_handler
        ->challenge($miniorange_user);
      if (isset($response->status) && $response->status != 'SUCCESS') {
        $message = 'An error occured while sending passcode. <em>(' . $response->message . ')</em>';
        MoAuthUtilities::show_error_or_success_message($message, 'error');
      }
      else {
        \Drupal::messenger()
          ->addMessage(t($messageHeader), 'status');
      }

      //Store txId
      \Drupal::configFactory()
        ->getEditable('miniorange_2fa.settings')
        ->set('txId_Value', $response->txId)
        ->save();
    }
    $form['header']['#markup'] = '<div class="mo2f-setup-header"><div class="mo2f-setup-header-top-left">Test OTP Over SMS</div></div>';
    $form['mo_auth_otpoversms_token'] = array(
      '#type' => 'textfield',
      '#maxlength' => 6,
      '#attributes' => array(
        'placeholder' => t('Enter passcode.'),
        'class' => array(
          'mo2f-textbox',
          'mo2f-textbox-otp',
        ),
        'style' => 'width:200px',
        'autofocus' => TRUE,
      ),
      '#title' => t('Passcode'),
    );
    $form['miniorange_2fa_form_submit'] = array(
      '#type' => 'submit',
      '#value' => t('Verify'),
    );
    $form['actions_form_cancel'] = array(
      '#markup' => '<span>&nbsp;&nbsp;</span><a href="' . $url . ' "> Cancel Test</a>',
    );
    $form['main_layout_div_end'] = array(
      '#markup' => '<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br></div>',
    );
    MoAuthUtilities::AddsupportTab($form, $form_state);
    return $form;
  }

  /**
   * Form submit handler for otp over sms validation.
   */
  public function submitForm(array &$form, \Drupal\Core\Form\FormStateInterface $form_state) {

    // Clear all the messages
    \Drupal::messenger()
      ->deleteAll();
    $txId = \Drupal::config('miniorange_2fa.settings')
      ->get('txId_Value');
    $token = $form['mo_auth_otpoversms_token']['#value'];
    if (empty($token)) {
      \Drupal::messenger()
        ->addMessage(t('Please enter Passcode first.'), 'error');
      return;
    }
    $user = User::load(\Drupal::currentUser()
      ->id());
    $user_id = $user
      ->id();
    $custom_attribute = MoAuthUtilities::get_users_custom_attribute($user_id);
    $user_email = $custom_attribute[0]->miniorange_registered_email;
    $authTypeCode = AuthenticationType::$SMS['code'];
    $user_phone = \Drupal::config('miniorange_2fa.settings')
      ->get('mo_phone');
    $customer = new MiniorangeCustomerProfile();
    $miniorange_user = new MiniorangeUser($customer
      ->getCustomerID(), NULL, $user_phone, $authTypeCode, $user_email);
    $auth_api_handler = new AuthenticationAPIHandler($customer
      ->getCustomerID(), $customer
      ->getAPIKey());
    $response = $auth_api_handler
      ->validate($miniorange_user, $txId, $token);

    // read API response
    if ($response->status == 'SUCCESS') {
      $message = 'You have successfully completed the test.';
      MoAuthUtilities::show_error_or_success_message($message, 'status');
    }
    elseif ($response->status == 'FAILED') {
      \Drupal::messenger()
        ->addMessage(t('The passcode you have entered is incorrect. Please try again.'), 'error');
      return;
    }
    else {
      $message = 'An error occured while processing your request. Please try again.';
      MoAuthUtilities::show_error_or_success_message($message, 'error');
    }
  }

}

Classes

Namesort descending Description
test_otp_over_sms Menu callback for testing OTP Over SMS.