You are here

public function PHPGangsta_GoogleAuthenticator::verifyCode in TFA Basic plugins 7

Check if the code is correct. This will accept codes starting from $discrepancy*30sec ago to $discrepancy*30sec from now.

Parameters

string $secret:

string $code:

int $discrepancy This is the allowed time drift in 30 second units (8 means 4 minutes before or after):

int|null $currentTimeSlice time slice if we want use other that time():

Return value

bool

File

includes/googleauthenticator/GoogleAuthenticator.php, line 125

Class

PHPGangsta_GoogleAuthenticator
PHP Class for handling Google Authenticator 2-factor authentication.

Code

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;
}