Inheritance: extends OTP, implements otphp\TOTPInterface
Example #1
0
 /**
  * Runs the command.
  *
  * @param InputInterface $input
  * @param OutputInterface $output
  * @return int
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $config = OTPGen::LoadConfig();
     $keyArgument = $input->getArgument('name');
     if (!key_exists($keyArgument, $config) && !is_numeric($keyArgument)) {
         $output->writeln('The entry ' . $keyArgument . ' does not exist in your library. Add it by using the add command.');
         return 0;
     }
     if (is_numeric($keyArgument)) {
         if (intval($keyArgument) > count($config) - 1) {
             $output->writeln('There is no entry in your library with the id: ' . $keyArgument . '.');
             return 0;
         }
         $count = 0;
         foreach ($config as $key => $WhyBother) {
             if ($count == intval($keyArgument)) {
                 $keyArgument = $key;
                 continue;
             }
             $count++;
         }
     }
     $totp = new TOTP();
     $totp->setDigits($config[$keyArgument]['size'])->setDigest($config[$keyArgument]['digest'])->setInterval($config[$keyArgument]['interval'])->setSecret($config[$keyArgument]['token']);
     $output->writeln('Your token is: <options=bold>' . $totp->now() . '</>');
     return 0;
 }
 /**
  * @dataProvider testProvisioningURIData
  */
 public function testProvisioningURI($secret, $label, $issuer, $expectedResult)
 {
     $totp = new TOTP($secret);
     $totp->setLabel($label);
     $totp->setIssuer($issuer);
     $this->assertEquals($expectedResult, $totp->getProvisioningUri());
 }
 /**
  * Verify one time code using the otp instance
  * @param $one_time_code
  * @param TOTP|null $totp
  * @return bool
  */
 public function verifyCode($one_time_code, TOTP $totp = null)
 {
     if ($totp !== null) {
         return $totp->verify($one_time_code);
     } else {
         return $this->totp->verify($one_time_code);
     }
 }
Example #4
0
 public function onValidateOneTimePassword($value)
 {
     switch ($this->oneTimePasswordType) {
         case TwoFactorAuthentication::OTP_HOTP:
             $oneTimePassword = new HOTP('', $this->getOneTimePasswordCode());
             break;
         case TwoFactorAuthentication::OTP_TOTP:
             $oneTimePassword = new TOTP('', $this->getOneTimePasswordCode());
             break;
         default:
             throw new RuntimeException('No valid OTP type set: ' . $this->oneTimePasswordType);
     }
     return $oneTimePassword->verify($value);
 }
 public function renderQrCodeAction()
 {
     /** @var AccountInterface $account */
     $account = $this->zourceAccount();
     $oneTimePassword = new TOTP($account->getContact()->getFullName(), $this->tfaService->getSecret());
     $oneTimePassword->setIssuer('Zource');
     $oneTimePassword->setIssuerIncludedAsParameter(true);
     $oneTimePassword->setParameter('image', $this->url()->fromRoute('settings/security/tfa-image', [], ['force_canonical' => true]));
     $filePath = tempnam('data/tmp/', '2fa-qr-');
     $renderer = new Png();
     $renderer->setHeight(256);
     $renderer->setWidth(256);
     $writer = new Writer($renderer);
     $writer->writeFile($oneTimePassword->getProvisioningUri(true), $filePath);
     $response = new Stream();
     $response->setCleanup(true);
     $response->setStream(fopen($filePath, 'rb'));
     $response->setStreamName($filePath);
     $headers = $response->getHeaders();
     $headers->addHeaderLine('Content-Length', filesize($filePath));
     $headers->addHeaderLine('Content-Type', 'image/png');
     return $response;
 }
Example #6
0
 /**
  * @param string $lable
  * @param int $digits
  * @param string $digets
  * @param int $interval
  * @return TOTP
  */
 public static function getTotp($lable = '', $digits = 6, $digets = 'sha1', $interval = 30)
 {
     $totp = new TOTP();
     $totp->setLabel($lable)->setDigits($digits)->setDigest($digets)->setInterval($interval);
     return $totp;
 }
Example #7
0
 private function createTOTP($digits, $digest, $interval, $secret = 'JDDK4U6G3BJLEZ7Y', $label = '*****@*****.**', $issuer = 'My Project')
 {
     $otp = new TOTP();
     $otp->setLabel($label)->setDigest($digest)->setDigits($digits)->setSecret($secret)->setIssuer($issuer)->setIssuerIncludedAsParameter(false)->setInterval($interval);
     return $otp;
 }