2018-01-19 03:36:15 +00:00
|
|
|
<?php
|
|
|
|
|
2018-02-25 21:30:56 +00:00
|
|
|
namespace Pterodactyl\Http\Middleware\Api\Client;
|
2018-01-19 03:36:15 +00:00
|
|
|
|
|
|
|
use Closure;
|
|
|
|
use Illuminate\Http\Request;
|
2018-11-10 23:27:50 +00:00
|
|
|
use Pterodactyl\Services\DaemonKeys\DaemonKeyProviderService;
|
|
|
|
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
|
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
2018-01-19 03:36:15 +00:00
|
|
|
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
|
|
|
|
|
2018-02-25 21:30:56 +00:00
|
|
|
class AuthenticateClientAccess
|
2018-01-19 03:36:15 +00:00
|
|
|
{
|
2018-11-10 23:27:50 +00:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Services\DaemonKeys\DaemonKeyProviderService
|
|
|
|
*/
|
|
|
|
private $keyProviderService;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* AuthenticateClientAccess constructor.
|
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Services\DaemonKeys\DaemonKeyProviderService $keyProviderService
|
|
|
|
*/
|
|
|
|
public function __construct(DaemonKeyProviderService $keyProviderService)
|
|
|
|
{
|
|
|
|
$this->keyProviderService = $keyProviderService;
|
|
|
|
}
|
|
|
|
|
2018-01-19 03:36:15 +00:00
|
|
|
/**
|
2018-02-25 21:30:56 +00:00
|
|
|
* Authenticate that the currently authenticated user has permission
|
2018-11-10 23:27:50 +00:00
|
|
|
* to access the specified server. This only checks that the user is an
|
|
|
|
* admin, owner, or a subuser. You'll need to do more specific checks in
|
|
|
|
* the API calls to determine if they can perform different actions.
|
2018-01-19 03:36:15 +00:00
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @param \Closure $next
|
|
|
|
* @return mixed
|
2018-11-10 23:27:50 +00:00
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
2018-01-19 03:36:15 +00:00
|
|
|
*/
|
|
|
|
public function handle(Request $request, Closure $next)
|
|
|
|
{
|
2018-02-25 21:30:56 +00:00
|
|
|
if (is_null($request->user())) {
|
2018-11-10 23:27:50 +00:00
|
|
|
throw new AccessDeniedHttpException('A request must be made using an authenticated client.');
|
2018-01-19 03:36:15 +00:00
|
|
|
}
|
|
|
|
|
2018-11-10 23:27:50 +00:00
|
|
|
/** @var \Pterodactyl\Models\Server $server */
|
|
|
|
$server = $request->route()->parameter('server');
|
|
|
|
|
|
|
|
try {
|
|
|
|
$token = $this->keyProviderService->handle($server, $request->user());
|
|
|
|
} catch (RecordNotFoundException $exception) {
|
|
|
|
throw new NotFoundHttpException('The requested server could not be located.');
|
|
|
|
}
|
|
|
|
|
|
|
|
$request->attributes->set('server_token', $token);
|
|
|
|
|
2018-01-19 03:36:15 +00:00
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
}
|