2017-11-19 14:05:13 -06:00
|
|
|
<?php
|
|
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2018-02-25 15:30:56 -06:00
|
|
|
namespace Pterodactyl\Http\Middleware\Api;
|
2017-11-19 14:05:13 -06:00
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
use Closure;
|
|
|
|
|
use Illuminate\Http\Request;
|
2017-11-19 14:05:13 -06:00
|
|
|
use IPTools\IP;
|
|
|
|
|
use IPTools\Range;
|
2022-05-22 14:57:06 -04:00
|
|
|
use Laravel\Sanctum\TransientToken;
|
2026-10-05 18:10:18 -04:00
|
|
|
use Pterodactyl\Facades\Activity;
|
|
|
|
|
use Pterodactyl\Http\Concerns\ResolvesRequestContext;
|
|
|
|
|
use Pterodactyl\Models\ApiKey;
|
|
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
2017-11-19 14:05:13 -06:00
|
|
|
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
|
|
|
|
|
|
|
|
|
|
class AuthenticateIPAccess
|
|
|
|
|
{
|
2026-10-05 18:10:18 -04:00
|
|
|
use ResolvesRequestContext;
|
|
|
|
|
|
2017-11-19 14:05:13 -06:00
|
|
|
/**
|
|
|
|
|
* Determine if a request IP has permission to access the API.
|
|
|
|
|
*
|
2026-10-05 18:10:18 -04:00
|
|
|
* @throws Exception
|
2024-10-21 19:18:20 -06:00
|
|
|
* @throws AccessDeniedHttpException
|
2017-11-19 14:05:13 -06: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-11-19 14:05:13 -06:00
|
|
|
{
|
2026-10-05 18:10:18 -04:00
|
|
|
$user = $this->authenticatedUser($request);
|
|
|
|
|
// SAFETY: Sanctum may supply a TransientToken for stateful requests; authenticated API requests use the configured ApiKey model.
|
|
|
|
|
/** @var TransientToken|ApiKey|null $token */
|
|
|
|
|
$token = $user->currentAccessToken();
|
2017-11-19 14:05:13 -06:00
|
|
|
|
2022-05-22 14:57:06 -04:00
|
|
|
// If this is a stateful request just push the request through to the next
|
|
|
|
|
// middleware in the stack, there is nothing we need to explicitly check. If
|
|
|
|
|
// this is a valid API Key, but there is no allowed IP restriction, also pass
|
|
|
|
|
// the request through.
|
2026-10-05 18:10:18 -04:00
|
|
|
if ($token === null || $token instanceof TransientToken || empty($token->allowed_ips)) {
|
2017-11-19 14:05:13 -06:00
|
|
|
return $next($request);
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
$requestIp = $request->ip();
|
|
|
|
|
throw_if($requestIp === null, AccessDeniedHttpException::class, 'The request did not contain a valid IP address.');
|
|
|
|
|
|
|
|
|
|
$find = new IP($requestIp);
|
2022-05-22 14:57:06 -04:00
|
|
|
foreach ($token->allowed_ips as $ip) {
|
2017-11-26 13:26:38 -06:00
|
|
|
if (Range::parse($ip)->contains($find)) {
|
2017-11-19 14:05:13 -06:00
|
|
|
return $next($request);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-18 12:04:51 -04:00
|
|
|
Activity::event('auth:ip-blocked')
|
2026-10-05 18:10:18 -04:00
|
|
|
->actor($user)
|
|
|
|
|
->subject($user, $token)
|
2022-06-18 12:04:51 -04:00
|
|
|
->property('identifier', $token->identifier)
|
|
|
|
|
->log();
|
|
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
throw new AccessDeniedHttpException('This IP address ('.$request->ip().') does not have permission to access the API using these credentials.');
|
2017-11-19 14:05:13 -06:00
|
|
|
}
|
|
|
|
|
}
|