PHPGangsta_GoogleAuthenticator::verifyCode PHP Method

verifyCode() public method

Check if the code is correct. This will accept codes starting from $discrepancy*30sec ago to $discrepancy*30sec from now.
public verifyCode ( string $secret, string $code, integer $discrepancy = 1, integer | null $currentTimeSlice = null ) : boolean
$secret string
$code string
$discrepancy integer This is the allowed time drift in 30 second units (8 means 4 minutes before or after)
$currentTimeSlice integer | null time slice if we want use other that time()
return boolean
    public function verifyCode($secret, $code, $discrepancy = 1, $currentTimeSlice = null)
    {
        if ($currentTimeSlice === null) {
            $currentTimeSlice = floor(time() / 30);
        }
        if (strlen($code) != 6) {
            return false;
        }
        for ($i = -$discrepancy; $i <= $discrepancy; ++$i) {
            $calculatedCode = $this->getCode($secret, $currentTimeSlice + $i);
            if ($this->timingSafeEquals($calculatedCode, $code)) {
                return true;
            }
        }
        return false;
    }

Usage Example

Example #1
3
 /**
  * Melde den spezifizierte User mit dem angegebenen Benutername / Passwort an
  * @param string $username
  * @param string $password
  * @param string $googleAuthCode
  */
 public function loginPerson(string $username, string $password, string $googleAuthCode)
 {
     $user = $this->model->load($username);
     $passwordCorrect = password_verify($password, $user['password']);
     if ($passwordCorrect) {
         $secret = $user['secret'];
         //If Secret is set
         if ($secret) {
             $authenticator = new PHPGangsta_GoogleAuthenticator();
             $result = $authenticator->verifyCode($user['secret'], $googleAuthCode, 2);
             // 2 = 2*30sec clock tolerance
             //Entered Code correct
             if ($result) {
                 $this->saveUser($user);
                 return;
             }
             //Code wrong
             $this->loginError();
             return;
         }
         $this->saveUser($user);
         return;
     }
     //Password wrong
     $this->loginError();
 }
All Usage Examples Of PHPGangsta_GoogleAuthenticator::verifyCode