You are here

public function test_otp_over_sms::submitForm in Google Authenticator / 2 Factor Authentication - 2FA 8

Form submit handler for otp over sms validation.

Overrides FormInterface::submitForm

File

src/Form/test_otp_over_sms.php, line 103
OTP Over SMS(test) functions.

Class

test_otp_over_sms
Menu callback for testing OTP Over SMS.

Namespace

Drupal\miniorange_2fa\Form

Code

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');
  }
}