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

72 lines
1.8 KiB
PHP
Raw Normal View History

2020-01-03 17:25:56 +01:00
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Http\File;
use Illuminate\Support\Facades\Storage;
use Zxing\QrReader;
use App\TwoFAccount;
class QrCodecontroller extends Controller
{
/**
* Handle uploaded qr code image
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function decode(Request $request)
{
if($request->hasFile('qrcode')){
$path = $request->file('qrcode')->store('qrcodes');
$qrcode = new QrReader(storage_path('app/' . $path));
$uri = urldecode($qrcode->text());
$uriChunks = explode('?', $uri);
foreach(explode('&', $uriChunks[1]) as $option) {
$option = explode('=', $option);
$options[$option[0]] = $option[1];
}
$account = $service = '';
2020-01-03 17:25:56 +01:00
$serviceChunks = explode(':', str_replace('otpauth://totp/', '', $uriChunks[0]));
if( count($serviceChunks) > 1 ) {
$account = $serviceChunks[1];
2020-01-03 17:25:56 +01:00
}
$service = $serviceChunks[0];
if( strstr( $service, '@') ) {
$account = $service;
2020-01-03 17:25:56 +01:00
$service = '';
}
if( empty($service) & !empty($options['issuer']) ) {
$service = $options['issuer'];
}
$twofaccount = (object) array(
'service' => $service,
'account' => $account,
2020-01-03 17:25:56 +01:00
'uri' => $uri,
2020-01-05 23:21:28 +01:00
'icon' => '',
2020-01-03 17:25:56 +01:00
'options' => $options
);
Storage::delete($path);
return response()->json($twofaccount, 201);
}
else {
return response()->json('no file in $request', 204);
}
}
}