2FAuth/app/Http/Controllers/QrCodeController.php

78 lines
1.9 KiB
PHP
Raw Normal View History

2020-01-03 17:25:56 +01:00
<?php
namespace App\Http\Controllers;
use Zxing\QrReader;
use App\TwoFAccount;
use App\Classes\Options;
use Illuminate\Http\Request;
2020-01-03 17:25:56 +01:00
use Illuminate\Support\Facades\Storage;
2020-11-05 22:47:59 +01:00
use chillerlan\QRCode\{QRCode, QROptions};
2020-01-03 17:25:56 +01:00
class QrCodeController extends Controller
2020-01-03 17:25:56 +01:00
{
/**
* Return a QR code image
*
2020-11-05 22:47:59 +01:00
* @param App\TwoFAccount $twofaccount
* @return \Illuminate\Http\Response
*/
public function show(TwoFAccount $twofaccount)
{
2020-11-05 22:47:59 +01:00
$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
/**
* Decode an uploaded QR Code image
2020-01-03 17:25:56 +01:00
*
* @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') {
// The frontend send an image resource of the QR code
// 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
$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;
}
2020-01-03 17:25:56 +01:00
2020-01-10 22:52:47 +01:00
// return the OTP object
$twofaccount = new TwoFAccount;
$twofaccount->populateFromUri($uri);
2020-01-03 17:25:56 +01:00
return response()->json($twofaccount->makeVisible(['uri', 'secret', 'algorithm']), 200);
2020-01-03 17:25:56 +01:00
}
2020-01-03 17:25:56 +01:00
}