You are here

public function GoogleAuthenticator::authenticateUser in Google Authenticator login 7

Same name and namespace in other branches
  1. 6 ga4php.php \GoogleAuthenticator::authenticateUser()

Authenticate a user using a code.

1 method overrides GoogleAuthenticator::authenticateUser()
GALoginGA::authenticateUser in ./ga_login.class.php
Authenticate a user.

File

./ga4php.php, line 177
Abstract GoogleAuthenticator class.

Class

GoogleAuthenticator
@file Abstract GoogleAuthenticator class.

Code

public function authenticateUser($username, $code) {
  if (preg_match("/[0-9][0-9][0-9][0-9][0-9][0-9]/", $code) < 1) {
    $this->errorText = "6 digits please";
    return FALSE;
  }
  $tokendata = $this
    ->internalGetData($username);
  if ($tokendata["tokenkey"] == "") {
    $this->errorText = "No Assigned Token";
    return FALSE;
  }

  // @todo check return value.
  $ttype = $tokendata["tokentype"];
  $tlid = $tokendata["tokencounter"];
  $tkey = $tokendata["tokenkey"];
  switch ($ttype) {
    case "HOTP":
      $st = $tlid + 1;
      $en = $tlid + $this->hotpSkew;
      for ($i = $st; $i < $en; $i++) {
        $stest = $this
          ->oathHotp($tkey, $i);
        if ($code == $stest) {
          $tokendata["tokencounter"] = $i;
          $this
            ->internalPutData($username, $tokendata);
          return TRUE;
        }
      }
      return FALSE;
    case "TOTP":
      $t_now = time();
      $t_ear = $t_now - $this->totpSkew * $tokendata["tokentimer"];
      $t_lat = $t_now + $this->totpSkew * $tokendata["tokentimer"];
      $t_st = (int) ($t_ear / $tokendata["tokentimer"]);
      $t_en = (int) ($t_lat / $tokendata["tokentimer"]);
      for ($i = $t_st; $i <= $t_en; $i++) {
        $stest = $this
          ->oathHotp($tkey, $i);
        error_log("testing code: {$code}, {$stest}, {$tkey}\n");
        if ($code == $stest) {
          return TRUE;
        }
      }
      break;
    default:
      return FALSE;
  }
  return FALSE;
}