2017-09-23 20:45:25 -05:00
<? php
2026-10-05 18:10:18 -04:00
declare ( strict_types = 1 );
2018-01-13 16:06:19 -06:00
namespace Pterodactyl\Http\Middleware\Api\Daemon ;
2017-09-23 20:45:25 -05:00
2026-10-05 18:10:18 -04:00
use Closure ;
use Illuminate\Http\Request ;
2026-10-08 17:22:14 -04:00
use Illuminate\Support\Facades\Crypt ;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Http\Concerns\ResolvesRequestContext ;
use Pterodactyl\Models\Node ;
use Symfony\Component\HttpFoundation\Response ;
2017-11-05 12:38:39 -06:00
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException ;
2026-10-05 18:10:18 -04:00
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException ;
use Symfony\Component\HttpKernel\Exception\HttpException ;
2017-09-23 20:45:25 -05:00
class DaemonAuthenticate
{
2026-10-05 18:10:18 -04:00
use ResolvesRequestContext ;
2017-09-23 20:45:25 -05:00
/**
2017-10-29 12:37:25 -05:00
* Daemon routes that this middleware should be skipped on.
2026-10-05 18:10:18 -04:00
*
* @var list<string>
2017-09-23 20:45:25 -05:00
*/
2022-10-14 10:59:20 -06:00
protected array $except = [
2017-10-29 12:37:25 -05:00
'daemon.configuration' ,
];
2017-09-23 20:45:25 -05:00
/**
* Check if a request from the daemon can be properly attributed back to a single node instance.
*
2024-10-21 19:18:20 -06:00
* @throws HttpException
2017-09-23 20:45:25 -05: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-23 20:45:25 -05:00
{
2026-10-05 18:10:18 -04:00
if ( in_array ( $this -> resolvedRoute ( $request ) -> getName (), $this -> except , true )) {
2017-10-29 12:37:25 -05:00
return $next ( $request );
}
2026-10-05 18:10:18 -04:00
throw_if (( $bearer = $request -> bearerToken ()) === null , HttpException :: class , 401 , 'Access to this endpoint must include an Authorization header.' , null , [ 'WWW-Authenticate' => 'Bearer' ]);
2017-09-23 20:45:25 -05:00
2020-04-10 15:53:19 -07:00
$parts = explode ( '.' , $bearer );
// Ensure that all of the correct parts are provided in the header.
2026-10-05 18:10:18 -04:00
throw_if ( count ( $parts ) !== 2 || empty ( $parts [ 0 ]) || empty ( $parts [ 1 ]), BadRequestHttpException :: class , 'The Authorization header provided was not in a valid format.' );
2020-04-10 15:15:38 -07:00
2026-10-05 18:10:18 -04:00
// Unknown token ids and wrong tokens fail identically.
$node = Node :: query () -> where ( 'daemon_token_id' , $parts [ 0 ]) -> first ();
if ( $node !== null ) {
2026-10-08 17:22:14 -04:00
$token = Crypt :: decrypt ( $node -> daemon_token );
2026-10-05 18:10:18 -04:00
throw_unless ( is_string ( $token ), AccessDeniedHttpException :: class , 'The authentication token provided is not valid.' );
2020-04-10 15:15:38 -07:00
2026-10-05 18:10:18 -04:00
if ( hash_equals ( $token , $parts [ 1 ])) {
2020-04-10 15:15:38 -07:00
$request -> attributes -> set ( 'node' , $node );
return $next ( $request );
}
2017-09-23 20:45:25 -05:00
}
2021-01-23 12:33:34 -08:00
throw new AccessDeniedHttpException ( 'You are not authorized to access this resource.' );
2017-09-23 20:45:25 -05:00
}
}