Get OTP data from OTPHP parser

This commit is contained in:
Bubka 2020-01-10 22:52:47 +01:00
parent f195de07d0
commit ad09f233db

View File

@ -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);
}
}