2017-09-24 01:45:25 +00:00
|
|
|
<?php
|
|
|
|
|
2018-01-13 22:06:19 +00:00
|
|
|
namespace Pterodactyl\Http\Middleware\Api\Daemon;
|
2017-09-24 01:45:25 +00:00
|
|
|
|
|
|
|
use Closure;
|
|
|
|
use Illuminate\Http\Request;
|
2020-04-10 22:15:38 +00:00
|
|
|
use Illuminate\Contracts\Encryption\Encrypter;
|
2017-09-24 01:45:25 +00:00
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpException;
|
|
|
|
use Pterodactyl\Contracts\Repository\NodeRepositoryInterface;
|
|
|
|
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
|
2020-04-10 22:53:19 +00:00
|
|
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
2017-11-05 18:38:39 +00:00
|
|
|
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
|
2017-09-24 01:45:25 +00:00
|
|
|
|
|
|
|
class DaemonAuthenticate
|
|
|
|
{
|
2017-10-30 02:40:34 +00:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\NodeRepositoryInterface
|
|
|
|
*/
|
|
|
|
private $repository;
|
|
|
|
|
2017-09-24 01:45:25 +00:00
|
|
|
/**
|
2017-10-29 17:37:25 +00:00
|
|
|
* Daemon routes that this middleware should be skipped on.
|
2017-10-30 02:40:34 +00:00
|
|
|
*
|
2017-09-24 01:45:25 +00:00
|
|
|
* @var array
|
|
|
|
*/
|
2017-10-29 17:37:25 +00:00
|
|
|
protected $except = [
|
|
|
|
'daemon.configuration',
|
|
|
|
];
|
2017-09-24 01:45:25 +00:00
|
|
|
|
2020-04-10 22:15:38 +00:00
|
|
|
/**
|
|
|
|
* @var \Illuminate\Contracts\Encryption\Encrypter
|
|
|
|
*/
|
|
|
|
private $encrypter;
|
|
|
|
|
2017-09-24 01:45:25 +00:00
|
|
|
/**
|
|
|
|
* DaemonAuthenticate constructor.
|
|
|
|
*
|
2020-04-10 22:15:38 +00:00
|
|
|
* @param \Illuminate\Contracts\Encryption\Encrypter $encrypter
|
2017-09-24 01:45:25 +00:00
|
|
|
* @param \Pterodactyl\Contracts\Repository\NodeRepositoryInterface $repository
|
|
|
|
*/
|
2020-04-10 22:15:38 +00:00
|
|
|
public function __construct(Encrypter $encrypter, NodeRepositoryInterface $repository)
|
2017-09-24 01:45:25 +00:00
|
|
|
{
|
|
|
|
$this->repository = $repository;
|
2020-04-10 22:15:38 +00:00
|
|
|
$this->encrypter = $encrypter;
|
2017-09-24 01:45:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if a request from the daemon can be properly attributed back to a single node instance.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\Request $request
|
2019-09-06 04:32:57 +00:00
|
|
|
* @param \Closure $next
|
2017-09-24 01:45:25 +00:00
|
|
|
* @return mixed
|
|
|
|
*
|
|
|
|
* @throws \Symfony\Component\HttpKernel\Exception\HttpException
|
|
|
|
*/
|
|
|
|
public function handle(Request $request, Closure $next)
|
|
|
|
{
|
2017-10-29 17:37:25 +00:00
|
|
|
if (in_array($request->route()->getName(), $this->except)) {
|
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
|
2020-04-10 22:15:38 +00:00
|
|
|
if (is_null($bearer = $request->bearerToken())) {
|
|
|
|
throw new HttpException(
|
|
|
|
401, 'Access this this endpoint must include an Authorization header.', null, ['WWW-Authenticate' => 'Bearer']
|
|
|
|
);
|
2017-09-24 01:45:25 +00:00
|
|
|
}
|
|
|
|
|
2020-04-10 22:53:19 +00:00
|
|
|
$parts = explode('.', $bearer);
|
|
|
|
// Ensure that all of the correct parts are provided in the header.
|
|
|
|
if (count($parts) !== 2 || empty($parts[0]) || empty($parts[1])) {
|
|
|
|
throw new BadRequestHttpException(
|
|
|
|
'The Authorization headed provided was not in a valid format.',
|
|
|
|
);
|
|
|
|
}
|
2020-04-10 22:15:38 +00:00
|
|
|
|
2017-09-24 01:45:25 +00:00
|
|
|
try {
|
2020-04-10 22:15:38 +00:00
|
|
|
/** @var \Pterodactyl\Models\Node $node */
|
|
|
|
$node = $this->repository->findFirstWhere([
|
2020-04-10 22:53:19 +00:00
|
|
|
'daemon_token_id' => $parts[0],
|
2020-04-10 22:15:38 +00:00
|
|
|
]);
|
|
|
|
|
2020-04-10 22:53:19 +00:00
|
|
|
if (hash_equals((string) $this->encrypter->decrypt($node->daemon_token), $parts[1])) {
|
2020-04-10 22:15:38 +00:00
|
|
|
$request->attributes->set('node', $node);
|
|
|
|
|
|
|
|
return $next($request);
|
|
|
|
}
|
2017-09-24 01:45:25 +00:00
|
|
|
} catch (RecordNotFoundException $exception) {
|
2020-04-10 22:15:38 +00:00
|
|
|
// Do nothing, we don't want to expose a node not existing at all.
|
2017-09-24 01:45:25 +00:00
|
|
|
}
|
|
|
|
|
2020-04-10 22:15:38 +00:00
|
|
|
throw new AccessDeniedHttpException(
|
|
|
|
'You are not authorized to access this resource.'
|
|
|
|
);
|
2017-09-24 01:45:25 +00:00
|
|
|
}
|
|
|
|
}
|