mirror of
https://github.com/Bubka/2FAuth.git
synced 2025-06-19 19:28:08 +02:00
Move QrCode controller logic to a business service
This commit is contained in:
parent
9af39a469c
commit
3036e534e7
@ -48,6 +48,10 @@ class Handler extends ExceptionHandler
|
|||||||
*/
|
*/
|
||||||
public function render($request, Throwable $exception)
|
public function render($request, Throwable $exception)
|
||||||
{
|
{
|
||||||
|
if ($exception instanceof InvalidQrCodeException) {
|
||||||
|
return response()->json([
|
||||||
|
'message' => 'not a valid QR code'], 400);
|
||||||
|
}
|
||||||
return parent::render($request, $exception);
|
return parent::render($request, $exception);
|
||||||
}
|
}
|
||||||
}
|
}
|
14
app/Exceptions/InvalidQrCodeException.php
Normal file
14
app/Exceptions/InvalidQrCodeException.php
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Exceptions;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class UnsupportedOtpType.
|
||||||
|
*
|
||||||
|
* @codeCoverageIgnore
|
||||||
|
*/
|
||||||
|
class InvalidQrCodeException extends Exception
|
||||||
|
{
|
||||||
|
}
|
@ -2,16 +2,33 @@
|
|||||||
|
|
||||||
namespace App\Http\Controllers;
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
use Zxing\QrReader;
|
|
||||||
use App\TwoFAccount;
|
use App\TwoFAccount;
|
||||||
use Illuminate\Http\Request;
|
use App\Services\QrCodeService;
|
||||||
use Illuminate\Support\Facades\Storage;
|
use App\Http\Requests\QrCodeDecodeRequest;
|
||||||
use chillerlan\QRCode\{QRCode, QROptions};
|
|
||||||
|
|
||||||
class QrCodeController extends Controller
|
class QrCodeController extends Controller
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Return a QR code image
|
* The TwoFAccount Service instance.
|
||||||
|
*/
|
||||||
|
protected $qrcodeService;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new controller instance.
|
||||||
|
*
|
||||||
|
* @param QrCodeService $qrcodeService
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function __construct(QrCodeService $qrcodeService)
|
||||||
|
{
|
||||||
|
$this->qrcodeService = $qrcodeService;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Show a QR code image
|
||||||
*
|
*
|
||||||
* @param App\TwoFAccount $twofaccount
|
* @param App\TwoFAccount $twofaccount
|
||||||
* @return \Illuminate\Http\Response
|
* @return \Illuminate\Http\Response
|
||||||
@ -19,41 +36,22 @@ class QrCodeController extends Controller
|
|||||||
public function show(TwoFAccount $twofaccount)
|
public function show(TwoFAccount $twofaccount)
|
||||||
{
|
{
|
||||||
|
|
||||||
$options = new QROptions([
|
return response()->json(['qrcode' => $this->qrcodeService->encode($twofaccount->uri)], 200);
|
||||||
'quietzoneSize' => 2,
|
|
||||||
'scale' => 8,
|
|
||||||
]);
|
|
||||||
|
|
||||||
$qrcode = new QRCode($options);
|
|
||||||
|
|
||||||
return response()->json(['qrcode' => $qrcode->render($twofaccount->uri)], 200);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Decode an uploaded QR Code image
|
* Decode an uploaded QR Code image
|
||||||
*
|
*
|
||||||
* @param \Illuminate\Http\Request $request
|
* @param \App\Http\Requests\QrCodeDecodeRequest $request
|
||||||
* @return \Illuminate\Http\Response
|
* @return \Illuminate\Http\Response
|
||||||
*/
|
*/
|
||||||
public function decode(Request $request)
|
public function decode(QrCodeDecodeRequest $request)
|
||||||
{
|
{
|
||||||
|
|
||||||
// input validation
|
$file = $request->file('qrcode');
|
||||||
$this->validate($request, [
|
|
||||||
'qrcode' => 'required|image',
|
|
||||||
]);
|
|
||||||
|
|
||||||
// qrcode analysis
|
return response()->json(['data' => $this->qrcodeService->decode($file)], 200);
|
||||||
$path = $request->file('qrcode')->store('qrcodes');
|
|
||||||
$qrcode = new QrReader(storage_path('app/' . $path));
|
|
||||||
|
|
||||||
$uri = urldecode($qrcode->text());
|
|
||||||
|
|
||||||
// delete uploaded file
|
|
||||||
Storage::delete($path);
|
|
||||||
|
|
||||||
return response()->json(['uri' => $uri], 200);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
30
app/Http/Requests/QrCodeDecodeRequest.php
Normal file
30
app/Http/Requests/QrCodeDecodeRequest.php
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Requests;
|
||||||
|
|
||||||
|
use Illuminate\Foundation\Http\FormRequest;
|
||||||
|
|
||||||
|
class QrCodeDecodeRequest extends FormRequest
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Determine if the user is authorized to make this request.
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function authorize()
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the validation rules that apply to the request.
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function rules()
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'qrcode' => 'required|image',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
59
app/Services/QrCodeService.php
Normal file
59
app/Services/QrCodeService.php
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Services;
|
||||||
|
|
||||||
|
use App\TwoFAccount;
|
||||||
|
use Zxing\QrReader;
|
||||||
|
use Illuminate\Support\Facades\Storage;
|
||||||
|
use chillerlan\QRCode\{QRCode, QROptions};
|
||||||
|
|
||||||
|
class QrCodeService
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
//private $token;
|
||||||
|
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
//$this->token = $otpType === TOTP::create($secret) : HOTP::create($secret);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Encode a string into a QR code image
|
||||||
|
*
|
||||||
|
* @param string $data The string to encode
|
||||||
|
*
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function encode(string $data)
|
||||||
|
{
|
||||||
|
$options = new QROptions([
|
||||||
|
'quietzoneSize' => 2,
|
||||||
|
'scale' => 8,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$qrcode = new QRCode($options);
|
||||||
|
|
||||||
|
return $qrcode->render($data);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Decode an uploaded QR code image
|
||||||
|
*
|
||||||
|
* @param \Illuminate\Http\UploadedFile $file
|
||||||
|
*/
|
||||||
|
public function decode(\Illuminate\Http\UploadedFile $file)
|
||||||
|
{
|
||||||
|
$qrcode = new QrReader($file->get(), QrReader::SOURCE_TYPE_BLOB);
|
||||||
|
$data = urldecode($qrcode->text());
|
||||||
|
|
||||||
|
if(!$data) {
|
||||||
|
throw new \App\Exceptions\InvalidQrCodeException;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user