mirror of
https://github.com/Bubka/2FAuth.git
synced 2025-08-09 21:57:47 +02:00
Refactor QRcode handling using the brand new Start view
This commit is contained in:
@ -4,8 +4,6 @@ namespace App\Http\Controllers;
|
||||
|
||||
use Zxing\QrReader;
|
||||
use App\TwoFAccount;
|
||||
use App\Classes\Options;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use chillerlan\QRCode\{QRCode, QROptions};
|
||||
@ -40,62 +38,22 @@ class QrCodeController extends Controller
|
||||
*/
|
||||
public function decode(Request $request)
|
||||
{
|
||||
|
||||
// input validation
|
||||
$this->validate($request, [
|
||||
'qrcode' => 'required|image',
|
||||
]);
|
||||
|
||||
if( Options::get('useBasicQrcodeReader') || $request->inputFormat === 'fileUpload') {
|
||||
// qrcode analysis
|
||||
$path = $request->file('qrcode')->store('qrcodes');
|
||||
$qrcode = new QrReader(storage_path('app/' . $path));
|
||||
|
||||
// The frontend send an image resource of the QR code
|
||||
$uri = urldecode($qrcode->text());
|
||||
|
||||
// input validation
|
||||
$this->validate($request, [
|
||||
'qrcode' => 'required|image',
|
||||
]);
|
||||
// delete uploaded file
|
||||
Storage::delete($path);
|
||||
|
||||
// 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);
|
||||
}
|
||||
else {
|
||||
// The QR code has been flashed and the URI is already decoded
|
||||
$this->validate($request, [
|
||||
'uri' => 'required|string',
|
||||
]);
|
||||
|
||||
$uri = $request->uri;
|
||||
}
|
||||
|
||||
// return the OTP object
|
||||
$twofaccount = new TwoFAccount;
|
||||
$twofaccount->uri = $uri;
|
||||
|
||||
// When present, use the imageLink parameter to prefill the icon field
|
||||
if( $twofaccount->imageLink ) {
|
||||
|
||||
$chunks = explode('.', $twofaccount->imageLink);
|
||||
$hashFilename = Str::random(40) . '.' . end($chunks);
|
||||
|
||||
try {
|
||||
|
||||
Storage::disk('local')->put('imagesLink/' . $hashFilename, file_get_contents($twofaccount->imageLink));
|
||||
|
||||
if( in_array(Storage::mimeType('imagesLink/' . $hashFilename), ['image/png', 'image/jpeg', 'image/webp', 'image/bmp']) ) {
|
||||
if( getimagesize(storage_path() . '/app/imagesLink/' . $hashFilename) ) {
|
||||
|
||||
Storage::move('imagesLink/' . $hashFilename, 'public/icons/' . $hashFilename);
|
||||
$twofaccount->icon = $hashFilename;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch( Exception $e ) {
|
||||
$twofaccount->imageLink = null;
|
||||
}
|
||||
}
|
||||
|
||||
return response()->json($twofaccount->makeVisible(['uri', 'secret', 'algorithm']), 200);
|
||||
return response()->json(['uri' => $uri], 200);
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user