mirror of
https://github.com/Bubka/2FAuth.git
synced 2025-03-27 14:56:04 +01:00
Refactore generateTOTP() as a dedicated class
This commit is contained in:
parent
e966b06a2f
commit
93f7c4a709
42
app/Classes/TimedTOTP.php
Normal file
42
app/Classes/TimedTOTP.php
Normal file
@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace App\Classes;
|
||||
|
||||
use OTPHP\TOTP;
|
||||
use OTPHP\Factory;
|
||||
|
||||
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 (InvalidArgumentException $exception) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$currentPosition = time();
|
||||
$PeriodCount = floor($currentPosition / 30); //nombre de période de 30s depuis T0
|
||||
$currentPeriodStartAt = $PeriodCount * 30;
|
||||
$currentPeriodendAt = $currentPeriodStartAt + 30;
|
||||
$positionInCurrentPeriod = $currentPosition - $currentPeriodStartAt;
|
||||
|
||||
$totp = [
|
||||
'totp' => $otp->now(),
|
||||
'position' => $positionInCurrentPeriod
|
||||
];
|
||||
|
||||
return $totp;
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -6,6 +6,7 @@
|
||||
use App\TwoFAccount;
|
||||
use OTPHP\TOTP;
|
||||
use OTPHP\Factory;
|
||||
use App\Classes\TimedTOTP;
|
||||
use Illuminate\Http\Request;
|
||||
use ParagonIE\ConstantTime\Base32;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
@ -78,25 +79,8 @@ public function show(TwoFAccount $twofaccount)
|
||||
*/
|
||||
public function generateTOTP(TwoFAccount $twofaccount)
|
||||
{
|
||||
try {
|
||||
$otp = Factory::loadFromProvisioningUri($twofaccount->uri);
|
||||
} catch (InvalidArgumentException $exception) {
|
||||
return response()->json([
|
||||
'message' => 'Error generating TOTP',
|
||||
], 500);
|
||||
}
|
||||
|
||||
$currentPosition = time();
|
||||
$PeriodCount = floor($currentPosition / 30); //nombre de période de 30s depuis T0
|
||||
$currentPeriodStartAt = $PeriodCount * 30;
|
||||
$currentPeriodendAt = $currentPeriodStartAt + 30;
|
||||
$positionInCurrentPeriod = $currentPosition - $currentPeriodStartAt;
|
||||
|
||||
|
||||
return response()->json([
|
||||
'totp' => $otp->now(),
|
||||
'position' => $positionInCurrentPeriod
|
||||
], 200);
|
||||
return response()->json(TimedTOTP::get($twofaccount->uri), 200);
|
||||
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user