You are here

public function authenticate_user::buildForm in Google Authenticator / 2 Factor Authentication - 2FA 8.2

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

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides FormInterface::buildForm

File

src/Form/authenticate_user.php, line 27
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 buildForm(array $form, FormStateInterface $form_state) {
  \Drupal::service('page_cache_kill_switch')
    ->trigger();
  global $base_url;
  $form['markup_library'] = array(
    '#attached' => array(
      'library' => array(
        "miniorange_2fa/miniorange_2fa.admin",
        "miniorange_2fa/miniorange_2fa.license",
      ),
    ),
  );
  $session = MoAuthUtilities::getSession();
  $moMfaSession = $session
    ->get("mo_auth", null);
  if (is_null($moMfaSession) || !isset($moMfaSession['status']) || !isset($moMfaSession['uid']) || $moMfaSession['status'] !== '1ST_FACTOR_AUTHENTICATED') {
    return $form;
  }
  $url_parts = MoAuthUtilities::mo_auth_get_url_parts();
  end($url_parts);
  $user_id = prev($url_parts);
  if ($moMfaSession['uid'] != $user_id) {
    return $form;
  }
  $custom_attribute = MoAuthUtilities::get_users_custom_attribute($user_id);
  $user_email = $custom_attribute[0]->miniorange_registered_email;
  if ($moMfaSession['status'] === '1ST_FACTOR_AUTHENTICATED' && $moMfaSession['challenged'] === 0) {
    $moMfaSession = $this
      ->mo_auth_challenge_user($form, $user_email, $user_id, $form_state);
  }
  $moMfaSession['challenged'] = 1;
  $session
    ->set('mo_auth', $moMfaSession);
  $session
    ->save();
  $moMfaSession = $session
    ->get("mo_auth", null);
  if (empty($moMfaSession['mo_challenge_response'])) {
    return 0;
  }
  if (isset($moMfaSession['status']) && $moMfaSession['status'] === '1ST_FACTOR_AUTHENTICATED') {
    $challenge_response = $moMfaSession['mo_challenge_response'];
    $authType = $challenge_response->authType;
    $form['actions'] = array(
      '#type' => 'actions',
    );
    if (!empty($authType)) {
      $form['authType'] = array(
        '#type' => 'hidden',
        '#value' => $authType,
      );
      $authType = AuthenticationType::getAuthType($authType);
      $form = self::mo_auth_build_form($form, $base_url, $authType, $challenge_response, TRUE, $custom_attribute[0]->activated_auth_methods);
      unset($form['mo_message']);
    }
    else {
      $form['actions']['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Save'),
        '#attributes' => array(
          'class' => array(
            'hidebutton',
          ),
        ),
      );
    }
    return $form;
  }
  $url = Url::fromRoute('user.login')
    ->toString();
  $response = new RedirectResponse($url);
  $response
    ->send();
  exit;
}