You are here

public function authenticate_user::mo_auth_challenge_user in Google Authenticator / 2 Factor Authentication - 2FA 8

Same name and namespace in other branches
  1. 8.2 src/Form/authenticate_user.php \Drupal\miniorange_2fa\form\authenticate_user::mo_auth_challenge_user()
1 call to authenticate_user::mo_auth_challenge_user()
authenticate_user::buildForm in src/Form/authenticate_user.php
Form constructor.

File

src/Form/authenticate_user.php, line 84
This is used to authenticate user during login.

Class

authenticate_user
@file This is used to authenticate user during login.

Namespace

Drupal\miniorange_2fa\form

Code

public function mo_auth_challenge_user($form, $user_email, $user_id, \Drupal\Core\Form\FormStateInterface $form_state) {
  global $base_url;
  $customer = new MiniorangeCustomerProfile();
  $connection = \Drupal::database();
  $query = $connection
    ->query("SELECT * FROM {UserAuthenticationType} where miniorange_registered_email = '{$user_email}'");
  $result = $query
    ->fetchAll();
  $miniorange_user = new MiniorangeUser($customer
    ->getCustomerID(), $user_email, NULL, NULL, NULL);
  $auth_api_handler = new AuthenticationAPIHandler($customer
    ->getCustomerID(), $customer
    ->getAPIKey());
  $response = $auth_api_handler
    ->challenge($miniorange_user);

  /*redirected so already 1st_factor_authenticated*/
  $_SESSION['mo_auth']['status'] = '1ST_FACTOR_AUTHENTICATED';
  if ($response->status == 'SUCCESS') {
    $session_data = array(
      'status' => $_SESSION['mo_auth']['status'],
      'mo_challenge_response' => $response,
    );
    if (session_status() == PHP_SESSION_NONE) {
      session_start();
    }
    $_SESSION['mo_auth'] = $session_data;
  }
  else {
    global $base_url;
    \Drupal::messenger()
      ->addMessage(t('An error occured while processing your request. Please contact administrator.'), 'error', TRUE);
    $response = new RedirectResponse($base_url);
    $response
      ->send();
    exit;
  }
}