2018-09-11 07:51:35 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Middleware;
|
|
|
|
|
|
|
|
use App\Models\UserPref;
|
|
|
|
use Closure;
|
2023-05-24 22:21:54 +02:00
|
|
|
use Illuminate\Http\Request;
|
2020-04-17 17:37:56 -05:00
|
|
|
use Illuminate\Support\Str;
|
2018-09-11 07:51:35 -05:00
|
|
|
use LibreNMS\Config;
|
2023-05-24 22:21:54 +02:00
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
2018-09-11 07:51:35 -05:00
|
|
|
|
|
|
|
class VerifyTwoFactor
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Handle an incoming request.
|
|
|
|
*
|
2021-09-08 23:35:56 +02:00
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @param \Closure $next
|
2018-09-11 07:51:35 -05:00
|
|
|
*/
|
2023-05-24 22:21:54 +02:00
|
|
|
public function handle(Request $request, Closure $next): Response
|
2018-09-11 07:51:35 -05:00
|
|
|
{
|
|
|
|
// check twofactor
|
|
|
|
if (Config::get('twofactor') === true) {
|
|
|
|
// don't apply on 2fa checking routes
|
2022-09-07 02:06:24 -05:00
|
|
|
$route_name = $request->route()->getName();
|
|
|
|
if ($route_name && Str::startsWith($route_name, '2fa.')) {
|
2018-09-11 07:51:35 -05:00
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
|
|
|
|
$twofactor = $request->session()->get('twofactoradd', UserPref::getPref($request->user(), 'twofactor'));
|
|
|
|
|
|
|
|
if (! empty($twofactor)) {
|
|
|
|
// user has 2fa enabled
|
|
|
|
if (! $request->session()->get('twofactor')) {
|
|
|
|
// verification is needed
|
|
|
|
return redirect('/2fa');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
}
|