misc_pterodactyl-panel/app/Http/Middleware/RequireTwoFactorAuthentication.php

97 lines
2.3 KiB
PHP
Raw Normal View History

<?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
*/
namespace Pterodactyl\Http\Middleware;
use Closure;
2017-10-29 17:37:25 +00:00
use Illuminate\Http\Request;
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-10-29 17:37:25 +00:00
* The names of routes that should be accessable without 2FA enabled.
*
* @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-10-29 17:37:25 +00:00
/**
* The route to redirect a user to to enable 2FA.
*
* @var string
*/
protected $redirectRoute = 'account.security';
/**
* RequireTwoFactorAuthentication constructor.
*
* @param \Prologue\Alerts\AlertsMessageBag $alert
*/
public function __construct(AlertsMessageBag $alert)
{
$this->alert = $alert;
}
/**
* Handle an incoming request.
*
2017-09-26 02:43:01 +00:00
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
2017-10-29 17:37:25 +00:00
public function handle(Request $request, Closure $next)
{
if (! $request->user()) {
return $next($request);
}
2017-10-29 17:37:25 +00:00
if (in_array($request->route()->getName(), $this->except)) {
return $next($request);
}
switch ((int) config('pterodactyl.auth.2fa_required')) {
case self::LEVEL_ADMIN:
2017-11-03 23:16:49 +00:00
if (! $request->user()->root_admin || $request->user()->use_totp) {
return $next($request);
}
break;
case self::LEVEL_ALL:
if ($request->user()->use_totp) {
return $next($request);
}
break;
case self::LEVEL_NONE:
default:
return $next($request);
}
2017-10-29 17:37:25 +00:00
$this->alert->danger(trans('auth.2fa_must_be_enabled'))->flash();
2017-10-29 17:37:25 +00:00
return redirect()->route($this->redirectRoute);
}
}