mirror of
https://github.com/Bubka/2FAuth.git
synced 2024-11-08 17:34:39 +01:00
Move QrCode controller logic to a business service
This commit is contained in:
parent
9af39a469c
commit
3036e534e7
@ -48,6 +48,10 @@ public function report(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);
|
||||
}
|
||||
}
|
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;
|
||||
|
||||
use Zxing\QrReader;
|
||||
use App\TwoFAccount;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use chillerlan\QRCode\{QRCode, QROptions};
|
||||
use App\Services\QrCodeService;
|
||||
use App\Http\Requests\QrCodeDecodeRequest;
|
||||
|
||||
|
||||
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
|
||||
* @return \Illuminate\Http\Response
|
||||
@ -19,41 +36,22 @@ class QrCodeController extends Controller
|
||||
public function show(TwoFAccount $twofaccount)
|
||||
{
|
||||
|
||||
$options = new QROptions([
|
||||
'quietzoneSize' => 2,
|
||||
'scale' => 8,
|
||||
]);
|
||||
|
||||
$qrcode = new QRCode($options);
|
||||
|
||||
return response()->json(['qrcode' => $qrcode->render($twofaccount->uri)], 200);
|
||||
return response()->json(['qrcode' => $this->qrcodeService->encode($twofaccount->uri)], 200);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Decode an uploaded QR Code image
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param \App\Http\Requests\QrCodeDecodeRequest $request
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function decode(Request $request)
|
||||
public function decode(QrCodeDecodeRequest $request)
|
||||
{
|
||||
|
||||
// input validation
|
||||
$this->validate($request, [
|
||||
'qrcode' => 'required|image',
|
||||
]);
|
||||
$file = $request->file('qrcode');
|
||||
|
||||
// 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);
|
||||
|
||||
return response()->json(['uri' => $uri], 200);
|
||||
return response()->json(['data' => $this->qrcodeService->decode($file)], 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…
Reference in New Issue
Block a user