You are here

function configure_otp_over_sms_and_email::miniorange_saml_validate_otp_submit in Google Authenticator / 2 Factor Authentication - 2FA 8

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

File

src/Form/configure_otp_over_sms_and_email.php, line 186

Class

configure_otp_over_sms_and_email

Namespace

Drupal\miniorange_2fa\form

Code

function miniorange_saml_validate_otp_submit(array &$form, \Drupal\Core\Form\FormStateInterface $form_state) {
  $customer = new MiniorangeCustomerProfile();
  $cKey = $customer
    ->getCustomerID();
  $customerApiKey = $customer
    ->getAPIKey();
  $otpToken = $form['miniorange_OTP']['#value'];
  $user = User::load(\Drupal::currentUser()
    ->id());
  $user_id = $user
    ->id();
  $utilities = new MoAuthUtilities();
  $custom_attribute = $utilities::get_users_custom_attribute($user_id);
  $user_email = $custom_attribute[0]->miniorange_registered_email;
  $phone_number = str_replace(' ', '', $form['miniorange_phone']['#value']);
  if (empty($phone_number)) {
    \Drupal::messenger()
      ->addMessage(t('Please enter phone number first.'), 'error');
    return;
  }
  if (empty($otpToken)) {
    \Drupal::messenger()
      ->addMessage(t('Please enter OTP first.'), 'error');
    return;
  }
  \Drupal::configFactory()
    ->getEditable('miniorange_2fa.settings')
    ->set('mo_phone', $phone_number)
    ->save();
  $transactionId = \Drupal::config('miniorange_2fa.settings')
    ->get('mo_auth_tx_id');
  $customer_config = new MiniorangeCustomerSetup($user_email, $phone_number, NULL, NULL);
  $response = $customer_config
    ->validate_otp_token($transactionId, $otpToken, $cKey, $customerApiKey);
  $otp_validation = json_decode($response);
  $txId = $otp_validation->txId;
  if ($otp_validation->status == 'FAILED') {
    \Drupal::messenger()
      ->addMessage(t("Validation Failed. Please enter the correct OTP."), 'error');
    return;
  }
  elseif ($otp_validation->status == 'SUCCESS') {
    $form_state
      ->setRebuild();
    $authTypeCode = AuthenticationType::$SMS_AND_EMAIL['code'];
    $user_email = $custom_attribute[0]->miniorange_registered_email;
    $customer = new MiniorangeCustomerProfile();
    $miniorange_user = new MiniorangeUser($customer
      ->getCustomerID(), $user_email, NULL, NULL, AuthenticationType::$SMS_AND_EMAIL['code']);
    $auth_api_handler = new AuthenticationAPIHandler($customer
      ->getCustomerID(), $customer
      ->getAPIKey());
    $configured_methods = MoAuthUtilities::mo_auth_get_configured_methods($user_id);
    if (!in_array(AuthenticationType::$SMS_AND_EMAIL['code'], $configured_methods)) {
      array_push($configured_methods, AuthenticationType::$SMS_AND_EMAIL['code']);
    }
    $config_methods = implode(', ', $configured_methods);
    $user_api_handler = new UsersAPIHandler($customer
      ->getCustomerID(), $customer
      ->getAPIKey());

    // Updating the authentication method for the user
    $miniorange_user
      ->setAuthType($authTypeCode);
    $response = $user_api_handler
      ->update($miniorange_user);
    if ($response->status == 'SUCCESS') {

      // Save User
      $user_id = $user
        ->id();
      $utilities = new MoAuthUtilities();
      $available = $utilities::check_for_userID($user_id);
      $database = \Drupal::database();
      if ($available == TRUE) {
        $database
          ->update('UserAuthenticationType')
          ->fields([
          'activated_auth_methods' => AuthenticationType::$SMS_AND_EMAIL['code'],
        ])
          ->condition('uid', $user_id, '=')
          ->execute();
        $database
          ->update('UserAuthenticationType')
          ->fields([
          'configured_auth_methods' => $config_methods,
        ])
          ->condition('uid', $user_id, '=')
          ->execute();
      }
      else {
        echo "error while saving authentication method.";
        exit;
      }
      if ($authTypeCode == AuthenticationType::$SMS_AND_EMAIL['code']) {
        $message = 'OTP Over SMS and Email has been configured successfully.';
        MoAuthUtilities::show_error_or_success_message($message, 'status');
      }
    }
    return;
  }
  $message = 'An error occured while processing your request. Please try again.';
  MoAuthUtilities::show_error_or_success_message($message, 'error');
}