mirror of
https://github.com/Bubka/2FAuth.git
synced 2025-04-10 18:48:17 +02: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 App\TwoFAccount;
|
||||||
use OTPHP\TOTP;
|
use OTPHP\TOTP;
|
||||||
use OTPHP\Factory;
|
use OTPHP\Factory;
|
||||||
|
use App\Classes\TimedTOTP;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use ParagonIE\ConstantTime\Base32;
|
use ParagonIE\ConstantTime\Base32;
|
||||||
use Illuminate\Support\Facades\Storage;
|
use Illuminate\Support\Facades\Storage;
|
||||||
@ -78,25 +79,8 @@ public function show(TwoFAccount $twofaccount)
|
|||||||
*/
|
*/
|
||||||
public function generateTOTP(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();
|
return response()->json(TimedTOTP::get($twofaccount->uri), 200);
|
||||||
$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);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user