2020-01-03 17:25:56 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
2020-01-10 22:52:47 +01:00
|
|
|
use OTPHP\TOTP;
|
|
|
|
use OTPHP\Factory;
|
2020-11-02 13:39:43 +01:00
|
|
|
use Zxing\QrReader;
|
|
|
|
use App\TwoFAccount;
|
|
|
|
use chillerlan\QRCode\{QRCode, QROptions};
|
2020-04-24 09:03:00 +02:00
|
|
|
use App\Classes\Options;
|
2020-01-03 17:25:56 +01:00
|
|
|
use Illuminate\Http\File;
|
2020-01-09 17:32:27 +01:00
|
|
|
use Illuminate\Http\Request;
|
2020-11-02 13:39:43 +01:00
|
|
|
use Assert\AssertionFailedException;
|
2020-01-03 17:25:56 +01:00
|
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
|
2020-10-11 19:14:56 +02:00
|
|
|
class QrCodeController extends Controller
|
2020-01-03 17:25:56 +01:00
|
|
|
{
|
2020-11-02 13:39:43 +01:00
|
|
|
/**
|
|
|
|
* Return a QR code image
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-01-03 17:25:56 +01:00
|
|
|
/**
|
|
|
|
* Handle uploaded qr code image
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function decode(Request $request)
|
|
|
|
{
|
|
|
|
|
2020-11-02 21:51:53 +01:00
|
|
|
if( Options::get('useBasicQrcodeReader') || $request->inputFormat === 'fileUpload') {
|
2020-01-09 16:33:32 +01:00
|
|
|
|
2020-04-24 09:03:00 +02:00
|
|
|
// input validation
|
|
|
|
$this->validate($request, [
|
|
|
|
'qrcode' => 'required|image',
|
|
|
|
]);
|
|
|
|
|
|
|
|
// qrcode analysis
|
|
|
|
$path = $request->file('qrcode')->store('qrcodes');
|
|
|
|
$qrcode = new QrReader(storage_path('app/' . $path));
|
2020-01-10 22:52:47 +01:00
|
|
|
|
2020-04-24 09:03:00 +02:00
|
|
|
$uri = urldecode($qrcode->text());
|
|
|
|
|
|
|
|
// delete uploaded file
|
|
|
|
Storage::delete($path);
|
|
|
|
}
|
|
|
|
else {
|
2020-01-03 17:25:56 +01:00
|
|
|
|
2020-04-24 09:03:00 +02:00
|
|
|
$this->validate($request, [
|
|
|
|
'uri' => 'required|string',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$uri = $request->uri;
|
|
|
|
}
|
2020-01-03 17:25:56 +01:00
|
|
|
|
2020-01-10 22:52:47 +01:00
|
|
|
// return the OTP object
|
|
|
|
try {
|
2020-01-03 17:25:56 +01:00
|
|
|
|
2020-01-10 22:52:47 +01:00
|
|
|
$otp = Factory::loadFromProvisioningUri($uri);
|
2020-01-09 16:33:32 +01:00
|
|
|
|
2020-01-10 22:52:47 +01:00
|
|
|
if(!$otp->getIssuer()) {
|
|
|
|
$otp->setIssuer($otp->getLabel());
|
|
|
|
$otp->setLabel('');
|
|
|
|
}
|
2020-01-03 17:25:56 +01:00
|
|
|
|
2020-01-10 22:52:47 +01:00
|
|
|
// returned object
|
|
|
|
$twofaccount = (object) array(
|
|
|
|
'service' => $otp->getIssuer(),
|
|
|
|
'account' => $otp->getLabel(),
|
|
|
|
'uri' => $uri,
|
|
|
|
'icon' => '',
|
|
|
|
'options' => $otp->getParameters()
|
|
|
|
);
|
2020-01-03 17:25:56 +01:00
|
|
|
|
2020-01-10 22:52:47 +01:00
|
|
|
return response()->json($twofaccount, 200);
|
2020-01-03 17:25:56 +01:00
|
|
|
|
2020-01-09 16:33:32 +01:00
|
|
|
}
|
2020-01-10 22:52:47 +01:00
|
|
|
catch (AssertionFailedException $exception) {
|
2020-01-03 17:25:56 +01:00
|
|
|
|
2020-01-19 23:02:20 +01:00
|
|
|
$error = \Illuminate\Validation\ValidationException::withMessages([
|
2020-02-05 17:17:25 +01:00
|
|
|
'qrcode' => __('errors.response.no_valid_otp')
|
2020-01-19 23:02:20 +01:00
|
|
|
]);
|
2020-01-09 16:33:32 +01:00
|
|
|
|
2020-01-19 23:02:20 +01:00
|
|
|
throw $error;
|
|
|
|
|
2020-01-03 17:25:56 +01:00
|
|
|
}
|
|
|
|
}
|
2020-01-09 16:33:32 +01:00
|
|
|
|
2020-01-03 17:25:56 +01:00
|
|
|
}
|