Full support of HOTP

This commit is contained in:
Bubka
2020-01-24 22:37:48 +01:00
parent a4a780b14f
commit 24e643ff87
5 changed files with 99 additions and 63 deletions

66
app/Classes/OTP.php Normal file
View File

@@ -0,0 +1,66 @@
<?php
namespace App\Classes;
use OTPHP\TOTP;
use OTPHP\Factory;
use Assert\AssertionFailedException;
class OTP
{
/**
* Generate a TOTP
*
* @param \App\TwoFAccount $twofaccount
* @return an array that represent the totp code
*/
public static function get($uri)
{
try {
$otp = Factory::loadFromProvisioningUri($uri);
}
catch (AssertionFailedException $exception) {
return false;
}
if( get_class($otp) === 'OTPHP\TOTP' ) {
$currentPosition = time();
$PeriodCount = floor($currentPosition / $otp->getPeriod()); //nombre de période de x s depuis T0 (x=30 par défaut)
$currentPeriodStartAt = $PeriodCount * $otp->getPeriod();
$positionInCurrentPeriod = $currentPosition - $currentPeriodStartAt;
// For memo :
// $nextOtpAt = ($PeriodCount+1)*$period
// $remainingTime = $nextOtpAt - time()
return $totp = [
'otp' => $otp->now(),
'position' => $positionInCurrentPeriod
];
}
else {
// It's a HOTP
$hotp = [
'otp' => $otp->at($otp->getCounter()),
'counter' => $otp->getCounter(),
];
// now we update the counter for next code
$otp->setParameter( 'counter', $otp->getcounter() + 1 );
$twofaccount = \App\TwoFAccount::where('uri', $uri)->first();
$twofaccount->uri = $otp->getProvisioningUri();
$twofaccount->save();
return $hotp;
}
}
}

View File

@@ -1,47 +0,0 @@
<?php
namespace App\Classes;
use OTPHP\TOTP;
use OTPHP\Factory;
use Assert\AssertionFailedException;
class TimedTOTP
{
/**
* Generate a TOTP
*
* @param \App\TwoFAccount $twofaccount
* @return an array that represent the totp code
*/
public static function get($uri)
{
try {
$otp = Factory::loadFromProvisioningUri($uri);
}
catch (AssertionFailedException $exception) {
return false;
}
$currentPosition = time();
$PeriodCount = floor($currentPosition / $otp->getPeriod()); //nombre de période de x s depuis T0 (x=30 par défaut)
$currentPeriodStartAt = $PeriodCount * $otp->getPeriod();
$positionInCurrentPeriod = $currentPosition - $currentPeriodStartAt;
// for memo :
// $nextOtpAt = ($PeriodCount+1)*$period
// $remainingTime = $nextOtpAt - time()
$totp = [
'totp' => $otp->now(),
'position' => $positionInCurrentPeriod
];
return $totp;
}
}