Files
pterodactyl-panel/app/Http/Middleware/RequireTwoFactorAuthentication.php
T

74 lines
2.5 KiB
PHP
Raw Normal View History

2017-09-25 21:58:16 -04:00
<?php
2026-10-05 18:10:18 -04:00
declare(strict_types=1);
2017-09-25 21:58:16 -04:00
namespace Pterodactyl\Http\Middleware;
2026-10-05 18:10:18 -04:00
use Closure;
2017-10-29 12:37:25 -05:00
use Illuminate\Http\Request;
2026-10-05 18:10:18 -04:00
use Illuminate\Support\Str;
use Pterodactyl\Exceptions\Http\TwoFactorAuthRequiredException;
2026-10-08 17:22:14 -04:00
use Pterodactyl\Facades\Alert;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Http\Concerns\ResolvesRequestContext;
use Symfony\Component\HttpFoundation\Response;
2017-09-25 21:58:16 -04:00
class RequireTwoFactorAuthentication
{
2026-10-05 18:10:18 -04:00
use ResolvesRequestContext;
public const int LEVEL_NONE = 0;
public const int LEVEL_ADMIN = 1;
public const int LEVEL_ALL = 2;
2017-09-25 21:58:16 -04:00
/**
2022-10-14 10:59:20 -06:00
* The route to redirect a user to enable 2FA.
2017-09-25 21:58:16 -04:00
*/
2022-10-14 10:59:20 -06:00
protected string $redirectRoute = '/account';
2017-10-29 12:37:25 -05:00
2017-09-25 21:58:16 -04:00
/**
* Check the user state on the incoming request to determine if they should be allowed to
* proceed or not. This checks if the Panel is configured to require 2FA on an account in
* order to perform actions. If so, we check the level at which it is required (all users
* or just admins) and then check if the user has enabled it for their account.
2017-09-25 21:58:16 -04:00
*
2024-10-21 19:18:20 -06:00
* @throws TwoFactorAuthRequiredException
2017-09-25 21:58:16 -04:00
*/
2026-10-05 18:10:18 -04:00
/**
* @param Closure(Request):Response $next
* @return Response
*/
public function handle(Request $request, Closure $next): mixed
2017-09-25 21:58:16 -04:00
{
$user = $request->user();
2026-10-05 18:10:18 -04:00
$uri = mb_rtrim($request->getRequestUri(), '/').'/';
$current = $this->resolvedRoute($request)->getName() ?? '';
2026-10-05 18:10:18 -04:00
if (! $user || Str::startsWith($uri, ['/auth/']) || Str::startsWith($current, ['auth.', 'account.'])) {
2017-09-25 21:58:16 -04:00
return $next($request);
}
2026-10-05 18:10:18 -04:00
$configuredLevel = filter_var(config('pterodactyl.auth.2fa_required'), FILTER_VALIDATE_INT);
$level = $configuredLevel === false ? self::LEVEL_NONE : $configuredLevel;
// If this setting is not configured, or the user is already using 2FA then we can just
// send them right through, nothing else needs to be checked.
//
// If the level is set as admin and the user is not an admin, pass them through as well.
if ($level === self::LEVEL_NONE || $user->use_totp) {
return $next($request);
2026-10-05 18:10:18 -04:00
}
if ($level === self::LEVEL_ADMIN && ! $user->root_admin) {
2017-09-25 21:58:16 -04:00
return $next($request);
}
// For API calls return an exception which gets rendered nicely in the API response.
2026-10-05 18:10:18 -04:00
throw_if($request->isJson() || Str::startsWith($uri, '/api/'), TwoFactorAuthRequiredException::class);
2017-09-25 21:58:16 -04:00
2026-10-08 17:22:14 -04:00
Alert::danger(trans('auth.2fa_must_be_enabled'))->flash();
2017-09-25 21:58:16 -04:00
return redirect()->to($this->redirectRoute);
2017-09-25 21:58:16 -04:00
}
}