You are here

function mo_auth_authenticate_user_submit in Google Authenticator / 2 Factor Authentication - 2FA 7

File

./authenticate_user.inc, line 81
This is used to authenticate user during login.

Code

function mo_auth_authenticate_user_submit($form, &$form_state) {
  global $base_url;
  $form_state['rebuild'] = TRUE;
  $uid = $form_state['uid'];
  $user = user_load($uid);
  $input = isset($form_state['input']) && is_array($form_state['input']) ? $form_state['input'] : array();
  if (array_key_exists('mo_auth_remember_device', $input) && !is_null($input['mo_auth_remember_device'])) {
    if ($input['mo_auth_remember_device'] == 1) {
      if (isset($_SESSION['mo2f_rba_status'])) {
        $rba_register_status = $_SESSION['mo2f_rba_status']['status'];
        $rba_session_uuid = $_SESSION['mo2f_rba_status']['sessionUuid'];
        if ($rba_register_status == 'WAIT_FOR_INPUT') {
          $mo_auth_rba = new MoAuthRBA();
          $mo_auth_rba
            ->mo2f_register_rba_profile($user->miniorange_registered_email[LANGUAGE_NONE][0]['value'], $rba_session_uuid);
        }
      }
    }
  }
  $user_email = $user->miniorange_registered_email[LANGUAGE_NONE][0]['value'];

  // $user_api_response = $_SESSION['mo_auth']['user'];
  $challenge_response = $_SESSION['mo_auth']['mo_challenge_response'];
  $authType = AuthenticationType::getAuthType($challenge_response->authType);
  if ($authType['oob'] === FALSE) {
    $token = '';
    if (array_key_exists('token', $form_state['input'])) {
      $token = $form_state['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 = $form_state['input']['mo2f_kbaquestion' . $i];
          $ans = $form_state['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 = $form_state['input']['txId'];
    $customer = new MiniorangeCustomerProfile();
    $auth_api_handler = new AuthenticationAPIHandler($customer
      ->getCustomerID(), $customer
      ->getAPIKey());
    $response = $auth_api_handler
      ->getAuthStatus($txId);
  }

  // read API response
  if (isset($response)) {
    if ($response->status == 'SUCCESS') {
      $change2FARequest = array_key_exists('mo_auth_change_2fa', $input) && !is_null($input['mo_auth_change_2fa']) ? TRUE : FALSE;
      user_login_submit(array(), $form_state);
      unset($_SESSION['mo_auth']);
      $mo_redirect_after_login = variable_get('mo_auth_redirect_after_login', '');
      $currentpath = isset($mo_redirect_after_login) && !empty($mo_redirect_after_login) ? $mo_redirect_after_login : (isset($_SESSION['moDestination']) ? $base_url . '/' . $_SESSION['moDestination'] : $base_url);
      unset($_SESSION['moDestination']);

      // if change of 2fa method requested then set $mo_redirect_url to inline url
      // mark that user has requested to change his method
      if ($change2FARequest) {
        $uid = $form_state['uid'];
        $form_state['redirect'] = 'login/user/' . $uid . '/register';
        $url = $form_state['redirect'];
        drupal_goto($url, array(
          'query' => array(
            'mo2faresetrequest' => 'reset',
          ),
        ));
      }
      drupal_goto($currentpath);
    }
    elseif ($response->status == 'DENIED') {
      form_set_error('form', t('Authentication denied.'));
      unset($_SESSION['mo_auth']);
      drupal_goto('user');
    }
    elseif ($response->status == 'FAILED') {
      $new_form = mo_auth_build_form_with_error_message($form_state);
      form_set_error('form', t($new_form['mo_message']));
      unset($new_form['mo_message']);
    }
  }
  else {
    form_set_error('form', t('An error occured while processing your request. Please try again.'));
    unset($_SESSION['mo_auth']);
    drupal_goto('user');
  }
}