Handle missing daemon keys better and fix subuser missing key errors

This commit is contained in:
Dane Everitt 2018-03-03 21:31:44 -06:00
parent 18e394eb14
commit a4f03f5d02
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
6 changed files with 74 additions and 49 deletions

View file

@ -11,7 +11,6 @@ namespace Pterodactyl\Http\Middleware\Server;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Contracts\Session\Session;
use Pterodactyl\Services\DaemonKeys\DaemonKeyProviderService;
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
@ -23,21 +22,14 @@ class AuthenticateAsSubuser
*/
private $keyProviderService;
/**
* @var \Illuminate\Contracts\Session\Session
*/
private $session;
/**
* SubuserAccessAuthenticate constructor.
*
* @param \Pterodactyl\Services\DaemonKeys\DaemonKeyProviderService $keyProviderService
* @param \Illuminate\Contracts\Session\Session $session
*/
public function __construct(DaemonKeyProviderService $keyProviderService, Session $session)
public function __construct(DaemonKeyProviderService $keyProviderService)
{
$this->keyProviderService = $keyProviderService;
$this->session = $session;
}
/**
@ -60,7 +52,6 @@ class AuthenticateAsSubuser
throw new AccessDeniedHttpException('This account does not have permission to access this server.');
}
$this->session->now('server_data.token', $token);
$request->attributes->set('server_token', $token);
return $next($request);

View file

@ -28,6 +28,7 @@ use Carbon\Carbon;
use Pterodactyl\Models\User;
use Pterodactyl\Models\Server;
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
use Pterodactyl\Contracts\Repository\SubuserRepositoryInterface;
use Pterodactyl\Contracts\Repository\DaemonKeyRepositoryInterface;
class DaemonKeyProviderService
@ -47,21 +48,29 @@ class DaemonKeyProviderService
*/
private $repository;
/**
* @var \Pterodactyl\Contracts\Repository\SubuserRepositoryInterface
*/
private $subuserRepository;
/**
* GetDaemonKeyService constructor.
*
* @param \Pterodactyl\Services\DaemonKeys\DaemonKeyCreationService $keyCreationService
* @param \Pterodactyl\Contracts\Repository\DaemonKeyRepositoryInterface $repository
* @param \Pterodactyl\Services\DaemonKeys\DaemonKeyUpdateService $keyUpdateService
* @param \Pterodactyl\Contracts\Repository\SubuserRepositoryInterface $subuserRepository
*/
public function __construct(
DaemonKeyCreationService $keyCreationService,
DaemonKeyRepositoryInterface $repository,
DaemonKeyUpdateService $keyUpdateService
DaemonKeyUpdateService $keyUpdateService,
SubuserRepositoryInterface $subuserRepository
) {
$this->keyCreationService = $keyCreationService;
$this->keyUpdateService = $keyUpdateService;
$this->repository = $repository;
$this->subuserRepository = $subuserRepository;
}
/**
@ -89,10 +98,18 @@ class DaemonKeyProviderService
return $this->keyCreationService->handle($server->id, $user->id);
}
// If they aren't the admin or owner of the server, they shouldn't get access.
// Subusers should always have an entry created when they are, so if there is
// no record, it should fail.
throw $exception;
// Check if user is a subuser for this server. Ideally they should always have
// a record associated with them in the database, but we should still handle
// that potentiality here.
//
// If no subuser is found, a RecordNotFoundException will be thrown, thus handling
// the parent error as well.
$subuser = $this->subuserRepository->findFirstWhere([
['user_id', '=', $user->id],
['server_id', '=', $server->id],
]);
return $this->keyCreationService->handle($subuser->server_id, $subuser->user_id);
}
if (! $updateIfExpired || Carbon::now()->diffInSeconds($key->expires_at, false) > 0) {

View file

@ -1,26 +1,4 @@
<?php
/*
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
namespace Pterodactyl\Transformers\Daemon;
@ -83,8 +61,8 @@ class ApiKeyTransformer extends TransformerAbstract
$daemonPermissions = ['s:console'];
foreach ($permissions as $permission) {
if (! is_null($mappings[$permission])) {
$daemonPermissions[] = $mappings[$permission];
if (! is_null(array_get($mappings, $permission))) {
$daemonPermissions[] = array_get($mappings, $permission);
}
}