mirror of
https://github.com/Bubka/2FAuth.git
synced 2025-03-27 14:56:04 +01:00
Get OTP data from OTPHP parser
This commit is contained in:
parent
f195de07d0
commit
ad09f233db
@ -4,7 +4,9 @@
|
||||
|
||||
use Validator;
|
||||
use Zxing\QrReader;
|
||||
use App\Classes\TimedTOTP;
|
||||
use OTPHP\TOTP;
|
||||
use OTPHP\Factory;
|
||||
use Assert\AssertionFailedException;
|
||||
use Illuminate\Http\File;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
@ -37,13 +39,35 @@ public function decode(Request $request)
|
||||
// qrcode analysis
|
||||
$path = $request->file('qrcode')->store('qrcodes');
|
||||
$qrcode = new QrReader(storage_path('app/' . $path));
|
||||
|
||||
$uri = urldecode($qrcode->text());
|
||||
|
||||
// delete uploaded file
|
||||
Storage::delete($path);
|
||||
|
||||
// Check uri validity
|
||||
if( !TimedTOTP::get($uri) ) {
|
||||
// return the OTP object
|
||||
try {
|
||||
|
||||
$otp = Factory::loadFromProvisioningUri($uri);
|
||||
|
||||
if(!$otp->getIssuer()) {
|
||||
$otp->setIssuer($otp->getLabel());
|
||||
$otp->setLabel('');
|
||||
}
|
||||
|
||||
// returned object
|
||||
$twofaccount = (object) array(
|
||||
'service' => $otp->getIssuer(),
|
||||
'account' => $otp->getLabel(),
|
||||
'uri' => $uri,
|
||||
'icon' => '',
|
||||
'options' => $otp->getParameters()
|
||||
);
|
||||
|
||||
return response()->json($twofaccount, 200);
|
||||
|
||||
}
|
||||
catch (AssertionFailedException $exception) {
|
||||
|
||||
return response()->json([
|
||||
'error' => [
|
||||
@ -52,44 +76,6 @@ public function decode(Request $request)
|
||||
], 400);
|
||||
|
||||
}
|
||||
|
||||
$uriChunks = explode('?', $uri);
|
||||
|
||||
foreach(explode('&', $uriChunks[1]) as $option) {
|
||||
$option = explode('=', $option);
|
||||
$options[$option[0]] = $option[1];
|
||||
}
|
||||
|
||||
$account = $service = '';
|
||||
|
||||
$serviceChunks = explode(':', str_replace('otpauth://totp/', '', $uriChunks[0]));
|
||||
|
||||
if( count($serviceChunks) > 1 ) {
|
||||
$account = $serviceChunks[1];
|
||||
}
|
||||
|
||||
$service = $serviceChunks[0];
|
||||
|
||||
if( strstr( $service, '@') ) {
|
||||
$account = $service;
|
||||
$service = '';
|
||||
}
|
||||
|
||||
if( empty($service) & !empty($options['issuer']) ) {
|
||||
$service = $options['issuer'];
|
||||
}
|
||||
|
||||
|
||||
// returned object
|
||||
$twofaccount = (object) array(
|
||||
'service' => $service,
|
||||
'account' => $account,
|
||||
'uri' => $uri,
|
||||
'icon' => '',
|
||||
'options' => $options
|
||||
);
|
||||
|
||||
return response()->json($twofaccount, 201);
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user