Path: blob/1.0-develop/app/Http/Middleware/RequireTwoFactorAuthentication.php
10279 views
<?php12namespace Pterodactyl\Http\Middleware;34use Illuminate\Support\Str;5use Illuminate\Http\Request;6use Prologue\Alerts\AlertsMessageBag;7use Pterodactyl\Exceptions\Http\TwoFactorAuthRequiredException;89class RequireTwoFactorAuthentication10{11public const LEVEL_NONE = 0;12public const LEVEL_ADMIN = 1;13public const LEVEL_ALL = 2;1415/**16* The route to redirect a user to enable 2FA.17*/18protected string $redirectRoute = '/account';1920/**21* RequireTwoFactorAuthentication constructor.22*/23public function __construct(private AlertsMessageBag $alert)24{25}2627/**28* Check the user state on the incoming request to determine if they should be allowed to29* proceed or not. This checks if the Panel is configured to require 2FA on an account in30* order to perform actions. If so, we check the level at which it is required (all users31* or just admins) and then check if the user has enabled it for their account.32*33* @throws TwoFactorAuthRequiredException34*/35public function handle(Request $request, \Closure $next): mixed36{37$user = $request->user();38$uri = rtrim($request->getRequestUri(), '/') . '/';39$current = $request->route()->getName();4041if (!$user || Str::startsWith($uri, ['/auth/']) || Str::startsWith($current, ['auth.', 'account.'])) {42return $next($request);43}4445$level = (int) config('pterodactyl.auth.2fa_required');46// If this setting is not configured, or the user is already using 2FA then we can just47// send them right through, nothing else needs to be checked.48//49// If the level is set as admin and the user is not an admin, pass them through as well.50if ($level === self::LEVEL_NONE || $user->use_totp) {51return $next($request);52} elseif ($level === self::LEVEL_ADMIN && !$user->root_admin) {53return $next($request);54}5556// For API calls return an exception which gets rendered nicely in the API response.57if ($request->isJson() || Str::startsWith($uri, '/api/')) {58throw new TwoFactorAuthRequiredException();59}6061$this->alert->danger(trans('auth.2fa_must_be_enabled'))->flash();6263return redirect()->to($this->redirectRoute);64}65}666768