mirror of
https://github.com/Bubka/2FAuth.git
synced 2025-04-11 19:18:26 +02:00
Get OTP data from OTPHP parser
This commit is contained in:
parent
f195de07d0
commit
ad09f233db
@ -4,7 +4,9 @@
|
|||||||
|
|
||||||
use Validator;
|
use Validator;
|
||||||
use Zxing\QrReader;
|
use Zxing\QrReader;
|
||||||
use App\Classes\TimedTOTP;
|
use OTPHP\TOTP;
|
||||||
|
use OTPHP\Factory;
|
||||||
|
use Assert\AssertionFailedException;
|
||||||
use Illuminate\Http\File;
|
use Illuminate\Http\File;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Support\Facades\Storage;
|
use Illuminate\Support\Facades\Storage;
|
||||||
@ -37,13 +39,35 @@ public function decode(Request $request)
|
|||||||
// qrcode analysis
|
// qrcode analysis
|
||||||
$path = $request->file('qrcode')->store('qrcodes');
|
$path = $request->file('qrcode')->store('qrcodes');
|
||||||
$qrcode = new QrReader(storage_path('app/' . $path));
|
$qrcode = new QrReader(storage_path('app/' . $path));
|
||||||
|
|
||||||
$uri = urldecode($qrcode->text());
|
$uri = urldecode($qrcode->text());
|
||||||
|
|
||||||
// delete uploaded file
|
// delete uploaded file
|
||||||
Storage::delete($path);
|
Storage::delete($path);
|
||||||
|
|
||||||
// Check uri validity
|
// return the OTP object
|
||||||
if( !TimedTOTP::get($uri) ) {
|
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([
|
return response()->json([
|
||||||
'error' => [
|
'error' => [
|
||||||
@ -52,44 +76,6 @@ public function decode(Request $request)
|
|||||||
], 400);
|
], 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