2022-05-22 19:03:51 -04:00
<? php
2026-10-05 18:10:18 -04:00
declare ( strict_types = 1 );
2022-05-22 19:03:51 -04:00
namespace Pterodactyl\Http\Middleware\Api\Client ;
2026-10-05 18:10:18 -04:00
use Closure ;
2022-05-22 19:03:51 -04:00
use Illuminate\Http\Request ;
2026-10-05 18:10:18 -04:00
use Laravel\Sanctum\Contracts\HasAbilities ;
use Laravel\Sanctum\TransientToken ;
use Pterodactyl\Http\Concerns\ResolvesRequestContext ;
2022-05-22 19:03:51 -04:00
use Pterodactyl\Models\ApiKey ;
2026-10-05 18:10:18 -04:00
use Symfony\Component\HttpFoundation\Response ;
2022-05-22 19:03:51 -04:00
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException ;
class RequireClientApiKey
{
2026-10-05 18:10:18 -04:00
use ResolvesRequestContext ;
2022-05-22 19:03:51 -04:00
/**
* Blocks a request to the Client API endpoints if the user is providing an API token
* that was created for the application API.
*/
2026-10-05 18:10:18 -04:00
/**
* @param Closure(Request):Response $next
* @return Response
*/
public function handle ( Request $request , Closure $next ) : mixed
2022-05-22 19:03:51 -04:00
{
2026-10-05 18:10:18 -04:00
$token = $this -> authenticatedUser ( $request ) -> currentAccessToken ();
if ( ! $token instanceof HasAbilities || $token instanceof TransientToken ) {
return $next ( $request );
2022-05-22 19:03:51 -04:00
}
2026-10-05 18:10:18 -04:00
throw_if ( $token -> key_type === ApiKey :: TYPE_APPLICATION , AccessDeniedHttpException :: class , 'You are attempting to use an application API key on an endpoint that requires a client API key.' );
2022-05-22 19:03:51 -04:00
return $next ( $request );
}
}