2017-09-26 01:58:16 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Pterodactyl - Panel
|
|
|
|
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
|
|
|
|
*
|
2017-09-26 02:43:01 +00:00
|
|
|
* This software is licensed under the terms of the MIT license.
|
|
|
|
* https://opensource.org/licenses/MIT
|
2017-09-26 01:58:16 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Pterodactyl\Http\Middleware;
|
|
|
|
|
|
|
|
use Closure;
|
2018-07-05 02:38:23 +00:00
|
|
|
use Illuminate\Support\Str;
|
2017-10-29 17:37:25 +00:00
|
|
|
use Illuminate\Http\Request;
|
2017-09-26 01:58:16 +00:00
|
|
|
use Prologue\Alerts\AlertsMessageBag;
|
|
|
|
|
|
|
|
class RequireTwoFactorAuthentication
|
|
|
|
{
|
|
|
|
const LEVEL_NONE = 0;
|
|
|
|
const LEVEL_ADMIN = 1;
|
|
|
|
const LEVEL_ALL = 2;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Prologue\Alerts\AlertsMessageBag
|
|
|
|
*/
|
2017-10-29 17:37:25 +00:00
|
|
|
private $alert;
|
2017-09-26 01:58:16 +00:00
|
|
|
|
2017-10-29 17:37:25 +00:00
|
|
|
/**
|
|
|
|
* The route to redirect a user to to enable 2FA.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2018-07-05 02:38:23 +00:00
|
|
|
protected $redirectRoute = 'account';
|
2017-10-29 17:37:25 +00:00
|
|
|
|
2017-09-26 01:58:16 +00:00
|
|
|
/**
|
|
|
|
* RequireTwoFactorAuthentication constructor.
|
|
|
|
*
|
|
|
|
* @param \Prologue\Alerts\AlertsMessageBag $alert
|
|
|
|
*/
|
2017-12-15 03:05:26 +00:00
|
|
|
public function __construct(AlertsMessageBag $alert)
|
2017-09-26 01:58:16 +00:00
|
|
|
{
|
|
|
|
$this->alert = $alert;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle an incoming request.
|
|
|
|
*
|
2017-09-26 02:43:01 +00:00
|
|
|
* @param \Illuminate\Http\Request $request
|
2019-09-06 04:32:57 +00:00
|
|
|
* @param \Closure $next
|
2017-09-26 01:58:16 +00:00
|
|
|
* @return mixed
|
|
|
|
*/
|
2017-10-29 17:37:25 +00:00
|
|
|
public function handle(Request $request, Closure $next)
|
2017-09-26 01:58:16 +00:00
|
|
|
{
|
|
|
|
if (! $request->user()) {
|
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
|
2018-07-05 02:38:23 +00:00
|
|
|
$current = $request->route()->getName();
|
|
|
|
if (in_array($current, ['auth', 'account']) || Str::startsWith($current, ['auth.', 'account.'])) {
|
2017-09-26 01:58:16 +00:00
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
|
2017-12-15 03:05:26 +00:00
|
|
|
switch ((int) config('pterodactyl.auth.2fa_required')) {
|
2017-09-26 01:58:16 +00:00
|
|
|
case self::LEVEL_ADMIN:
|
2017-11-03 23:16:49 +00:00
|
|
|
if (! $request->user()->root_admin || $request->user()->use_totp) {
|
2017-09-26 01:58:16 +00:00
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case self::LEVEL_ALL:
|
|
|
|
if ($request->user()->use_totp) {
|
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
break;
|
2017-12-15 03:05:26 +00:00
|
|
|
case self::LEVEL_NONE:
|
|
|
|
default:
|
|
|
|
return $next($request);
|
2017-09-26 01:58:16 +00:00
|
|
|
}
|
|
|
|
|
2017-10-29 17:37:25 +00:00
|
|
|
$this->alert->danger(trans('auth.2fa_must_be_enabled'))->flash();
|
2017-09-26 01:58:16 +00:00
|
|
|
|
2017-10-29 17:37:25 +00:00
|
|
|
return redirect()->route($this->redirectRoute);
|
2017-09-26 01:58:16 +00:00
|
|
|
}
|
|
|
|
}
|