You are here

function MoAuthCustomerSetup::mo_auth_validate_otp_submit in Google Authenticator / 2 Factor Authentication - 2FA 8.2

Same name and namespace in other branches
  1. 8 src/Form/MoAuthCustomerSetup.php \Drupal\miniorange_2fa\Form\MoAuthCustomerSetup::mo_auth_validate_otp_submit()

File

src/Form/MoAuthCustomerSetup.php, line 449
Contains form for customer setup.

Class

MoAuthCustomerSetup
Customer setup form().

Namespace

Drupal\miniorange_2fa\Form

Code

function mo_auth_validate_otp_submit(&$form, FormStateInterface $form_state) {
  $utilities = new MoAuthUtilities();
  $variables = array(
    'mo_auth_customer_admin_email',
    'mo_auth_customer_admin_phone',
    'mo_auth_tx_id',
    'mo_auth_customer_admin_password',
  );
  $mo_db_values = $utilities
    ->miniOrange_set_get_configurations($variables, 'GET');
  $user = User::load(\Drupal::currentUser()
    ->id());
  $user_id = $user
    ->id();
  $otp_token = $form_state
    ->getValue('mo_auth_customer_otp_token');
  if (empty($otp_token)) {
    \Drupal::messenger()
      ->addError(t('The <b>OTP</b> field is mandatory.'));
    return;
  }
  $username = $mo_db_values['mo_auth_customer_admin_email'] == '' ? NULL : $mo_db_values['mo_auth_customer_admin_email'];
  $phone = $mo_db_values['mo_auth_customer_admin_phone'] == '' ? NULL : $mo_db_values['mo_auth_customer_admin_phone'];
  $txId = $mo_db_values['mo_auth_tx_id'] == '' ? NULL : $mo_db_values['mo_auth_tx_id'];
  $customerSetup = new MiniorangeCustomerSetup($username, $phone, NULL, $otp_token);

  // Validate OTP.
  $validate_otp_response = $customerSetup
    ->validate_otp_token($txId, $otp_token, MoAuthConstants::$DEFAULT_CUSTOMER_ID, MoAuthConstants::$DEFAULT_CUSTOMER_API_KEY);
  if ($validate_otp_response->status == 'CURL_ERROR') {
    \Drupal::messenger()
      ->addError(t('cURL is not enabled. Please enable cURL'));
    return;
  }
  elseif ($validate_otp_response->status == 'SUCCESS') {

    // OTP Validated. Create customer.
    $password = $mo_db_values['mo_auth_customer_admin_password'];
    $customer_config = new MiniorangeCustomerSetup($username, $phone, $password, NULL);
    $create_customer_response = $customer_config
      ->createCustomer();
    if ($create_customer_response->status == 'CURL_ERROR') {
      \Drupal::messenger()
        ->addError(t('cURL is not enabled. Please enable cURL'));
      return;
    }
    elseif ($create_customer_response->status == 'SUCCESS') {

      // OTP Validated. Show Configuration page.
      $utilities
        ->miniOrange_set_get_configurations(array(
        'mo_auth_status' => 'PLUGIN_CONFIGURATION',
      ), 'SET');
      $utilities
        ->miniOrange_set_get_configurations(array(
        'mo_auth_tx_id',
      ), 'CLEAR');

      // Customer created.
      $this
        ->mo_auth_save_customer($user_id, $create_customer_response, $username, $phone);
      \Drupal::messenger()
        ->addStatus(t('Your account has been created successfully. Email Verification has been set as your default 2nd-factor method.'));
      return;
    }
    elseif ($create_customer_response->status == 'INVALID_EMAIL_QUICK_EMAIL') {
      \Drupal::messenger()
        ->addError(t('There was an error creating an account for you.<br> You may have entered an invalid Email-Id
                <strong>(We discourage the use of disposable emails) </strong>
                <br>Please try again with a valid email.'));
      return;
    }
    else {
      MoAuthUtilities::mo_add_loggers_for_failures($create_customer_response->message, 'error');
      \Drupal::messenger()
        ->addError(t('An error occurred while creating your account. Please try again or contact us at') . ' <a href="mailto:info@xecurify.com">info@xecurify.com</a>.');
      return;
    }
  }
  else {
    \Drupal::messenger()
      ->addError(t('The OTP you have entered is incorrect. Please try again.'));
    return;
  }
}