You are here

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

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

File

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

Class

MoAuthCustomerSetup
Customer setup form().

Namespace

Drupal\miniorange_2fa\Form

Code

function mo_auth_validate_otp_submit(&$form, $form_state) {
  $user = User::load(\Drupal::currentUser()
    ->id());
  $user_id = $user
    ->id();
  $otp_token = $form['mo_auth_customer_otp_token']['#value'];
  if (empty($otp_token)) {
    \Drupal::messenger()
      ->addMessage(t('The <b>OTP</b> field is mandatory.'), 'error');
    return;
  }
  $username = \Drupal::config('miniorange_2fa.settings')
    ->get('mo_auth_customer_admin_email') == '' ? NULL : \Drupal::config('miniorange_2fa.settings')
    ->get('mo_auth_customer_admin_email');
  $phone = \Drupal::config('miniorange_2fa.settings')
    ->get('mo_auth_customer_admin_phone') == '' ? NULL : \Drupal::config('miniorange_2fa.settings')
    ->get('mo_auth_customer_admin_phone');
  $txId = \Drupal::config('miniorange_2fa.settings')
    ->get('mo_auth_tx_id') == '' ? NULL : \Drupal::config('miniorange_2fa.settings')
    ->get('mo_auth_tx_id');
  $customerSetup = new MiniorangeCustomerSetup($username, $phone, NULL, $otp_token);

  // Validate OTP.
  $validate_otp_response = json_decode($customerSetup
    ->validate_otp_token($txId, $otp_token, MoAuthConstants::$DEFAULT_CUSTOMER_ID, MoAuthConstants::$DEFAULT_CUSTOMER_API_KEY));
  if ($validate_otp_response->status == 'SUCCESS') {

    // OTP Validated. Show Configuration page.
    \Drupal::configFactory()
      ->getEditable('miniorange_2fa.settings')
      ->set('mo_auth_status', 'PLUGIN_CONFIGURATION')
      ->save();
    \Drupal::configFactory()
      ->getEditable('miniorange_2fa.settings')
      ->clear('mo_auth_tx_id')
      ->save();

    // OTP Validated. Create customer.
    $password = \Drupal::config('miniorange_2fa.settings')
      ->get('mo_auth_customer_admin_password');
    $customer_config = new MiniorangeCustomerSetup($username, $phone, $password, NULL);
    $create_customer_response = $customer_config
      ->createCustomer();
    if ($create_customer_response->status == 'SUCCESS') {

      // Customer created.
      $this
        ->mo_auth_save_customer($user_id, $create_customer_response, $username, $phone);
      \Drupal::messenger()
        ->addMessage(t('Customer account created. Email Verification has been set as your default 2nd factor method.'));
    }
    else {
      if (trim($create_customer_response->message) == 'Email is not enterprise email.') {
        \Drupal::messenger()
          ->addMessage(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.'), 'error');
        return;
      }
      else {
        \Drupal::messenger()
          ->addMessage(t('An error occured while creating your account. Please try again or contact us at <a href="mailto:info@xecurify.com">info@xecurify.com</a>.'), 'error');
        return;
      }
    }
  }
  else {
    \Drupal::messenger()
      ->addMessage(t('The OTP you have entered is incorrect. Please try again.'), 'error');
    return;
  }
}