mirror of
https://github.com/Bubka/2FAuth.git
synced 2024-12-11 09:40:43 +01:00
64 lines
1.3 KiB
PHP
64 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\TwoFAccount;
|
|
use Zxing\QrReader;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Illuminate\Support\Facades\Log;
|
|
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);
|
|
|
|
Log::info('data encoded to QR code');
|
|
|
|
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;
|
|
}
|
|
|
|
Log::info('QR code decoded');
|
|
|
|
return $data;
|
|
}
|
|
} |