mirror of
https://github.com/Bubka/2FAuth.git
synced 2025-08-19 09:51:35 +02:00
Full support of HOTP
This commit is contained in:
66
app/Classes/OTP.php
Normal file
66
app/Classes/OTP.php
Normal 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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
@@ -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;
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
Reference in New Issue
Block a user