public function test_it_gets_the_good_code_at_given_times() { $o = new \OTPHP\TOTP('JDDK4U6G3BJLEZ7Y'); $this->assertEquals(855783, $o->at(0)); $this->assertEquals(762124, $o->at(319690800)); $this->assertEquals(139664, $o->at(1301012137)); }
private function verifyTOTP($provided, $is_test = false) { /*** * Check the TOTP code provided by the user * * @param int $provided Provided OTP passcode * @param bool $is_test if it's a test run, check the temporary rather than real column. * @return bool ***/ self::doLoadOTP(); $secret = $this->getSecret($is_test); if ($secret === false) { return false; } try { $totp = new OTPHP\TOTP($secret); $totp->setDigest($this->getDigest()); if ($totp->verify($provided)) { return true; } if (!is_numeric($this->totpSteps)) { throw new Exception('Bad TOTP step count'); } $i = 1; while ($i <= $this->totpSteps) { $test = array(); $test[] = $totp->now(); $test[] = $totp->at(time() + 30 * $i); $test[] = $totp->at(time() - 30 * $i); ++$i; # Check on every iteration. It'll usually be faster. if (in_array($provided, $test)) { return true; } } return false; } catch (Exception $e) { throw new Exception('Bad parameters provided to verifyOTP :: ' . $e->getMessage()); } }