Example #1
0
 public function test_it_verify_the_code()
 {
     $o = new \OTPHP\TOTP('JDDK4U6G3BJLEZ7Y');
     $this->assertTrue($o->verify(855783, 0));
     $this->assertTrue($o->verify(762124, 319690800));
     $this->assertTrue($o->verify(139664, 1301012137));
 }
Example #2
0
 public function tfaEnableAction()
 {
     if (!$this->session2FA->secretCode) {
         $this->session2FA->secretCode = Base32::encode(random_bytes(256));
     }
     $totp = new \OTPHP\TOTP('Zource', $this->session2FA->secretCode);
     if ($this->getRequest()->isPost()) {
         $code = $this->getRequest()->getPost('code');
         var_dump($totp->verify($code));
     }
     return new ViewModel(['secretCode' => $this->session2FA->secretCode]);
 }
 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());
     }
 }