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;
|
2017-10-29 17:37:25 +00:00
|
|
|
use Illuminate\Http\Request;
|
2017-09-26 01:58:16 +00:00
|
|
|
use Krucas\Settings\Settings;
|
|
|
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Krucas\Settings\Settings
|
|
|
|
*/
|
2017-10-29 17:37:25 +00:00
|
|
|
private $settings;
|
2017-09-26 01:58:16 +00:00
|
|
|
|
|
|
|
/**
|
2017-10-29 17:37:25 +00:00
|
|
|
* The names of routes that should be accessable without 2FA enabled.
|
2017-09-26 01:58:16 +00:00
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2017-10-29 17:37:25 +00:00
|
|
|
protected $except = [
|
|
|
|
'account.security',
|
|
|
|
'account.security.revoke',
|
|
|
|
'account.security.totp',
|
|
|
|
'account.security.totp.set',
|
|
|
|
'account.security.totp.disable',
|
|
|
|
'auth.totp',
|
|
|
|
'auth.logout',
|
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
|
|
|
|
*/
|
|
|
|
protected $redirectRoute = 'account.security';
|
|
|
|
|
2017-09-26 01:58:16 +00:00
|
|
|
/**
|
|
|
|
* RequireTwoFactorAuthentication constructor.
|
|
|
|
*
|
|
|
|
* @param \Prologue\Alerts\AlertsMessageBag $alert
|
|
|
|
* @param \Krucas\Settings\Settings $settings
|
|
|
|
*/
|
|
|
|
public function __construct(AlertsMessageBag $alert, Settings $settings)
|
|
|
|
{
|
|
|
|
$this->alert = $alert;
|
|
|
|
$this->settings = $settings;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle an incoming request.
|
|
|
|
*
|
2017-09-26 02:43:01 +00:00
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @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);
|
|
|
|
}
|
|
|
|
|
2017-10-29 17:37:25 +00:00
|
|
|
if (in_array($request->route()->getName(), $this->except)) {
|
2017-09-26 01:58:16 +00:00
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ((int) $this->settings->get('2fa', 0)) {
|
|
|
|
case self::LEVEL_NONE:
|
|
|
|
return $next($request);
|
2017-11-03 23:16:49 +00:00
|
|
|
break;
|
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-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
|
|
|
}
|
|
|
|
}
|