Path: blob/1.0-develop/app/Http/Middleware/Api/Daemon/DaemonAuthenticate.php
10277 views
<?php12namespace Pterodactyl\Http\Middleware\Api\Daemon;34use Illuminate\Http\Request;5use Illuminate\Contracts\Encryption\Encrypter;6use Pterodactyl\Repositories\Eloquent\NodeRepository;7use Symfony\Component\HttpKernel\Exception\HttpException;8use Pterodactyl\Exceptions\Repository\RecordNotFoundException;9use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;10use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;1112class DaemonAuthenticate13{14/**15* Daemon routes that this middleware should be skipped on.16*/17protected array $except = [18'daemon.configuration',19];2021/**22* DaemonAuthenticate constructor.23*/24public function __construct(private Encrypter $encrypter, private NodeRepository $repository)25{26}2728/**29* Check if a request from the daemon can be properly attributed back to a single node instance.30*31* @throws HttpException32*/33public function handle(Request $request, \Closure $next): mixed34{35if (in_array($request->route()->getName(), $this->except)) {36return $next($request);37}3839if (is_null($bearer = $request->bearerToken())) {40throw new HttpException(401, 'Access to this endpoint must include an Authorization header.', null, ['WWW-Authenticate' => 'Bearer']);41}4243$parts = explode('.', $bearer);44// Ensure that all of the correct parts are provided in the header.45if (count($parts) !== 2 || empty($parts[0]) || empty($parts[1])) {46throw new BadRequestHttpException('The Authorization header provided was not in a valid format.');47}4849try {50/** @var \Pterodactyl\Models\Node $node */51$node = $this->repository->findFirstWhere([52'daemon_token_id' => $parts[0],53]);5455if (hash_equals((string) $this->encrypter->decrypt($node->daemon_token), $parts[1])) {56$request->attributes->set('node', $node);5758return $next($request);59}60} catch (RecordNotFoundException $exception) {61// Do nothing, we don't want to expose a node not existing at all.62}6364throw new AccessDeniedHttpException('You are not authorized to access this resource.');65}66}676869