2017-09-03 02:35:33 +00:00
|
|
|
<?php
|
2017-09-26 02:43:01 +00:00
|
|
|
/**
|
2017-09-03 02:35:33 +00:00
|
|
|
* Pterodactyl - Panel
|
|
|
|
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
|
|
|
|
*
|
2017-09-26 02:43:01 +00:00
|
|
|
* This software is licensed under the terms of the MIT license.
|
|
|
|
* https://opensource.org/licenses/MIT
|
2017-09-03 02:35:33 +00:00
|
|
|
*/
|
|
|
|
|
2017-10-29 17:37:25 +00:00
|
|
|
namespace Pterodactyl\Http\Middleware\Server;
|
2017-09-03 02:35:33 +00:00
|
|
|
|
|
|
|
use Closure;
|
|
|
|
use Illuminate\Http\Request;
|
2017-09-03 21:41:03 +00:00
|
|
|
use Illuminate\Contracts\Session\Session;
|
2017-09-25 02:12:30 +00:00
|
|
|
use Pterodactyl\Services\DaemonKeys\DaemonKeyProviderService;
|
|
|
|
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
|
2017-11-05 18:38:39 +00:00
|
|
|
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
|
2017-09-03 02:35:33 +00:00
|
|
|
|
2017-10-29 17:37:25 +00:00
|
|
|
class AuthenticateAsSubuser
|
2017-09-03 02:35:33 +00:00
|
|
|
{
|
|
|
|
/**
|
2017-09-25 02:12:30 +00:00
|
|
|
* @var \Pterodactyl\Services\DaemonKeys\DaemonKeyProviderService
|
2017-09-03 02:35:33 +00:00
|
|
|
*/
|
2017-10-29 17:37:25 +00:00
|
|
|
private $keyProviderService;
|
2017-09-03 02:35:33 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Illuminate\Contracts\Session\Session
|
|
|
|
*/
|
2017-10-29 17:37:25 +00:00
|
|
|
private $session;
|
2017-09-03 02:35:33 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* SubuserAccessAuthenticate constructor.
|
|
|
|
*
|
2017-09-25 02:12:30 +00:00
|
|
|
* @param \Pterodactyl\Services\DaemonKeys\DaemonKeyProviderService $keyProviderService
|
|
|
|
* @param \Illuminate\Contracts\Session\Session $session
|
2017-09-03 02:35:33 +00:00
|
|
|
*/
|
2017-10-29 17:37:25 +00:00
|
|
|
public function __construct(DaemonKeyProviderService $keyProviderService, Session $session)
|
|
|
|
{
|
2017-09-25 02:12:30 +00:00
|
|
|
$this->keyProviderService = $keyProviderService;
|
2017-09-03 02:35:33 +00:00
|
|
|
$this->session = $session;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-09-25 02:12:30 +00:00
|
|
|
* Determine if a subuser has permissions to access a server, if so set thier access token.
|
|
|
|
*
|
2017-09-03 02:35:33 +00:00
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @param \Closure $next
|
|
|
|
* @return mixed
|
|
|
|
*
|
2017-09-25 02:12:30 +00:00
|
|
|
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
2017-12-03 20:00:47 +00:00
|
|
|
* @throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException
|
2017-09-03 02:35:33 +00:00
|
|
|
*/
|
|
|
|
public function handle(Request $request, Closure $next)
|
|
|
|
{
|
2017-10-29 17:37:25 +00:00
|
|
|
$server = $request->attributes->get('server');
|
2017-09-03 02:35:33 +00:00
|
|
|
|
|
|
|
try {
|
2017-11-05 18:38:39 +00:00
|
|
|
$token = $this->keyProviderService->handle($server, $request->user());
|
2017-09-25 02:12:30 +00:00
|
|
|
} catch (RecordNotFoundException $exception) {
|
2017-11-05 18:38:39 +00:00
|
|
|
throw new AccessDeniedHttpException('This account does not have permission to access this server.');
|
2017-09-03 02:35:33 +00:00
|
|
|
}
|
|
|
|
|
2017-10-29 17:37:25 +00:00
|
|
|
$this->session->now('server_data.token', $token);
|
|
|
|
$request->attributes->set('server_token', $token);
|
|
|
|
|
2017-09-03 02:35:33 +00:00
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
}
|