You are here

function authenticate_user::mo_auth_authenticate_user_submit 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_authenticate_user_submit()

File

src/Form/authenticate_user.php, line 117
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

function mo_auth_authenticate_user_submit(array $form, \Drupal\Core\Form\FormStateInterface $form_state) {
  global $base_url;
  $input = $form_state
    ->getUserInput();
  $challenge_response = $_SESSION['mo_auth']['mo_challenge_response'];
  $form_state
    ->setRebuild();
  $query_param = \Drupal::service('path.current')
    ->getPath();
  $url_parts = explode('/', $query_param);
  end($url_parts);
  $user_id = prev($url_parts);
  $custom_attribute = MoAuthUtilities::get_users_custom_attribute($user_id);
  $user_email = $custom_attribute[0]->miniorange_registered_email;
  $authType = AuthenticationType::getAuthType($challenge_response->authType);
  if ($authType['oob'] === FALSE) {
    $token = '';
    if (array_key_exists('token', $input)) {
      $token = $input['token'];
    }
    $txId = '';
    $kba = array();
    if ($authType['challenge'] === TRUE) {
      $txId = $challenge_response->txId;
      if ($challenge_response->authType == AuthenticationType::$KBA['code']) {
        $count = count($challenge_response->questions);
        for ($i = 1; $i <= $count; $i++) {
          $ques = $input['mo2f_kbaquestion' . $i];
          $ans = $input['mo2f_kbaanswer' . $i];
          $qa = array(
            "question" => $ques,
            "answer" => $ans,
          );
          array_push($kba, $qa);
        }
      }
    }
    $customer = new MiniorangeCustomerProfile();
    $miniorange_user = new MiniorangeUser($customer
      ->getCustomerID(), $user_email, NULL, NULL, NULL);
    $auth_api_handler = new AuthenticationAPIHandler($customer
      ->getCustomerID(), $customer
      ->getAPIKey());
    $response = $auth_api_handler
      ->validate($miniorange_user, $txId, $token, $kba);
  }
  else {
    $txId = $input['txId'];
    $customer = new MiniorangeCustomerProfile();
    $auth_api_handler = new AuthenticationAPIHandler($customer
      ->getCustomerID(), $customer
      ->getAPIKey());
    $response = $auth_api_handler
      ->getAuthStatus($txId);
  }

  // read API response
  if ($response->status == 'SUCCESS') {
    $user = User::load($user_id);
    user_login_finalize($user);
    unset($_SESSION['mo_auth']);
    $response = new RedirectResponse($base_url . '/user/login');
    $response
      ->send();
    exit;
  }
  elseif ($response->status == 'DENIED') {
    unset($_SESSION['mo_auth']);
    \Drupal::messenger()
      ->addMessage(t('Authentication denied.'), 'error', TRUE);
    $response = new RedirectResponse($base_url);
    $response
      ->send();
    exit;
  }
  elseif ($response->status == 'FAILED') {
    unset($_SESSION['mo_auth']);
    \Drupal::messenger()
      ->addMessage(t("Authentication failed try again."), 'error', TRUE);
    $response = new RedirectResponse($base_url);
    $response
      ->send();
    exit;
  }
  else {
    unset($_SESSION['mo_auth']);
    \Drupal::messenger()
      ->addMessage(t('An error occured while processing your request. Please try again.'), 'error', TRUE);
    $response = new RedirectResponse($base_url);
    $response
      ->send();
    exit;
  }
}