2020-01-03 17:25:56 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
2020-01-09 16:33:32 +01:00
|
|
|
use Validator;
|
2020-01-09 17:32:27 +01:00
|
|
|
use Zxing\QrReader;
|
|
|
|
use App\Classes\TimedTOTP;
|
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-01-03 17:25:56 +01:00
|
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
|
|
|
|
class QrCodecontroller extends Controller
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Handle uploaded qr code image
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function decode(Request $request)
|
|
|
|
{
|
|
|
|
|
2020-01-09 16:33:32 +01:00
|
|
|
// input validation
|
|
|
|
$messages = [
|
|
|
|
'qrcode.image' => 'Supported format are jpeg, png, bmp, gif, svg, or webp'
|
|
|
|
];
|
2020-01-03 17:25:56 +01:00
|
|
|
|
2020-01-09 16:33:32 +01:00
|
|
|
$validator = Validator::make($request->all(), [
|
|
|
|
'qrcode' => 'required|image',
|
|
|
|
], $messages);
|
|
|
|
|
|
|
|
if ($validator->fails()) {
|
|
|
|
return response()->json(['error' => $validator->errors()], 400);
|
|
|
|
}
|
2020-01-03 17:25:56 +01:00
|
|
|
|
|
|
|
|
2020-01-09 16:33:32 +01:00
|
|
|
// qrcode analysis
|
|
|
|
$path = $request->file('qrcode')->store('qrcodes');
|
|
|
|
$qrcode = new QrReader(storage_path('app/' . $path));
|
|
|
|
$uri = urldecode($qrcode->text());
|
2020-01-03 17:25:56 +01:00
|
|
|
|
2020-01-09 17:32:27 +01:00
|
|
|
// delete uploaded file
|
2020-01-09 16:33:32 +01:00
|
|
|
Storage::delete($path);
|
2020-01-03 17:25:56 +01:00
|
|
|
|
2020-01-09 17:32:27 +01:00
|
|
|
// Check uri validity
|
|
|
|
if( !TimedTOTP::get($uri) ) {
|
|
|
|
|
|
|
|
return response()->json([
|
|
|
|
'error' => [
|
2020-01-09 20:33:31 +01:00
|
|
|
'qrcode' => 'No valid TOTP resource in this QR code'
|
2020-01-09 17:32:27 +01:00
|
|
|
]
|
2020-01-09 16:33:32 +01:00
|
|
|
], 400);
|
2020-01-03 17:25:56 +01:00
|
|
|
|
2020-01-09 16:33:32 +01:00
|
|
|
}
|
2020-01-03 17:25:56 +01:00
|
|
|
|
2020-01-09 16:33:32 +01:00
|
|
|
$uriChunks = explode('?', $uri);
|
|
|
|
|
|
|
|
foreach(explode('&', $uriChunks[1]) as $option) {
|
|
|
|
$option = explode('=', $option);
|
|
|
|
$options[$option[0]] = $option[1];
|
|
|
|
}
|
2020-01-03 17:25:56 +01:00
|
|
|
|
2020-01-09 16:33:32 +01:00
|
|
|
$account = $service = '';
|
2020-01-03 17:25:56 +01:00
|
|
|
|
2020-01-09 16:33:32 +01:00
|
|
|
$serviceChunks = explode(':', str_replace('otpauth://totp/', '', $uriChunks[0]));
|
2020-01-03 17:25:56 +01:00
|
|
|
|
2020-01-09 16:33:32 +01:00
|
|
|
if( count($serviceChunks) > 1 ) {
|
|
|
|
$account = $serviceChunks[1];
|
|
|
|
}
|
2020-01-03 17:25:56 +01:00
|
|
|
|
2020-01-09 16:33:32 +01:00
|
|
|
$service = $serviceChunks[0];
|
2020-01-03 17:25:56 +01:00
|
|
|
|
2020-01-09 16:33:32 +01:00
|
|
|
if( strstr( $service, '@') ) {
|
|
|
|
$account = $service;
|
|
|
|
$service = '';
|
2020-01-03 17:25:56 +01:00
|
|
|
}
|
2020-01-09 16:33:32 +01:00
|
|
|
|
|
|
|
if( empty($service) & !empty($options['issuer']) ) {
|
|
|
|
$service = $options['issuer'];
|
2020-01-03 17:25:56 +01:00
|
|
|
}
|
2020-01-09 16:33:32 +01:00
|
|
|
|
|
|
|
|
|
|
|
// returned object
|
|
|
|
$twofaccount = (object) array(
|
|
|
|
'service' => $service,
|
|
|
|
'account' => $account,
|
|
|
|
'uri' => $uri,
|
|
|
|
'icon' => '',
|
|
|
|
'options' => $options
|
|
|
|
);
|
|
|
|
|
|
|
|
return response()->json($twofaccount, 201);
|
2020-01-03 17:25:56 +01:00
|
|
|
}
|
2020-01-09 16:33:32 +01:00
|
|
|
|
2020-01-03 17:25:56 +01:00
|
|
|
}
|