Improved logic for handling permissions on API routes.

Still only partially implemented, however this method will allow the
inclusion of data that is granted with servers (such as viewing more
about the node, node location, allocations, etc) while still limiting
someone from doing `?include=node.servers` and listing all servers when
they don’t have list-servers as a permission.
This commit is contained in:
Dane Everitt 2017-04-08 12:05:29 -04:00
parent db4df2bfa1
commit 4479d3bf19
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
16 changed files with 296 additions and 29 deletions

View file

@ -45,7 +45,7 @@ class ServerController extends Controller
return Fractal::create()
->collection($servers)
->transformWith(new ServerTransformer)
->transformWith(new ServerTransformer($request))
->paginateWith(new IlluminatePaginatorAdapter($servers))
->withResourceName('server')
->toArray();
@ -62,20 +62,11 @@ class ServerController extends Controller
$server = Server::findOrFail($id);
$fractal = Fractal::create()->item($server);
// dd($request->user()->can('view-node', $request->apiKey()));
// Have the api key model return a list of includes that would be allowed
// given the permissions they have aleady been granted?
//
// If someone has 'view-node' they would then be able to use ->parseIncludes(['*.node.*']);
// How that logic will work is beyond me currently, but should keep things
// fairly clean?
if ($request->input('include')) {
$fractal->parseIncludes(explode(',', $request->input('include')));
}
return $fractal->transformWith(new ServerTransformer)
return $fractal->transformWith(new ServerTransformer($request))
->withResourceName('server')
->toArray();
}

View file

@ -28,6 +28,13 @@ use Illuminate\Database\Eloquent\Model;
class APIKey extends Model
{
/**
* Public key defined length used in verification methods.
*
* @var int
*/
const PUBLIC_KEY_LEN = 16;
/**
* The table associated with the model.
*

View file

@ -42,7 +42,9 @@ class APIKeyPolicy
*/
private function checkPermission(User $user, Key $key, $permission)
{
$permissions = Cache::remember('APIKeyPolicy.' . $user->uuid . $key->public, Carbon::now()->addSeconds(5), function () use ($key) {
// We don't tag this cache key with the user uuid because the key is already unique,
// and multiple users are not defiend for a single key.
$permissions = Cache::remember('APIKeyPolicy.' . $key->public, Carbon::now()->addSeconds(5), function () use ($key) {
return $key->permissions()->get()->transform(function ($item) {
return $item->permission;
})->values();

View file

@ -25,7 +25,10 @@
namespace Pterodactyl\Providers;
use File;
use Cache;
use Carbon;
use Request;
use Pterodactyl\Models\APIKey;
use Illuminate\Support\ServiceProvider;
class MacroServiceProvider extends ServiceProvider
@ -57,11 +60,27 @@ class MacroServiceProvider extends ServiceProvider
$parts = explode('.', Request::bearerToken());
if (count($parts) === 2) {
return \Pterodactyl\Models\APIKey::where('public', $parts[0])->first();
if (count($parts) === 2 && strlen($parts[0]) === APIKey::PUBLIC_KEY_LEN) {
// Because the key itself isn't changing frequently, we simply cache this for
// 15 minutes to speed up the API and keep requests flowing.
return Cache::tags([
'ApiKeyMacro',
'ApiKeyMacro:Key:' . $parts[0],
])->remember('ApiKeyMacro.' . $parts[0], Carbon::now()->addMinutes(15), function() use ($parts) {
return APIKey::where('public', $parts[0])->first();
});
}
return false;
});
Request::macro('apiKeyHasPermission', function($permission) {
$key = Request::apiKey();
if (! $key) {
return false;
}
return Request::user()->can($permission, $key);
});
}
}

View file

@ -24,6 +24,7 @@
namespace Pterodactyl\Transformers\Admin;
use Illuminate\Http\Request;
use Pterodactyl\Models\Allocation;
use League\Fractal\TransformerAbstract;
@ -37,13 +38,26 @@ class AllocationTransformer extends TransformerAbstract
protected $filter;
/**
* Transformer constructor.
* The Illuminate Request object if provided.
*
* @param bool|string $filter
* @var \Illuminate\Http\Request|bool
*/
protected $request;
/**
* Setup request object for transformer.
*
* @param \Illuminate\Http\Request|bool $request
* @param bool $filter
* @return void
*/
public function __construct($filter = false)
public function __construct($request = false, $filter = false)
{
if (! $request instanceof Request && $request !== false) {
throw new DisplayException('Request passed to constructor must be of type Request or false.');
}
$this->request = $request;
$this->filter = $filter;
}

View file

@ -24,6 +24,7 @@
namespace Pterodactyl\Transformers\Admin;
use Illuminate\Http\Request;
use Pterodactyl\Models\Location;
use League\Fractal\TransformerAbstract;
@ -39,6 +40,28 @@ class LocationTransformer extends TransformerAbstract
'servers',
];
/**
* The Illuminate Request object if provided.
*
* @var \Illuminate\Http\Request|bool
*/
protected $request;
/**
* Setup request object for transformer.
*
* @param \Illuminate\Http\Request|bool $request
* @return void
*/
public function __construct($request = false)
{
if (! $request instanceof Request && $request !== false) {
throw new DisplayException('Request passed to constructor must be of type Request or false.');
}
$this->request = $request;
}
/**
* Return a generic transformed pack array.
*

View file

@ -24,6 +24,7 @@
namespace Pterodactyl\Transformers\Admin;
use Illuminate\Http\Request;
use Pterodactyl\Models\Node;
use League\Fractal\TransformerAbstract;
@ -40,6 +41,28 @@ class NodeTransformer extends TransformerAbstract
'servers',
];
/**
* The Illuminate Request object if provided.
*
* @var \Illuminate\Http\Request|bool
*/
protected $request;
/**
* Setup request object for transformer.
*
* @param \Illuminate\Http\Request|bool $request
* @return void
*/
public function __construct($request = false)
{
if (! $request instanceof Request && $request !== false) {
throw new DisplayException('Request passed to constructor must be of type Request or false.');
}
$this->request = $request;
}
/**
* Return a generic transformed pack array.
*
@ -77,6 +100,10 @@ class NodeTransformer extends TransformerAbstract
*/
public function includeServers(Node $node)
{
if ($this->request && ! $this->request->apiKeyHasPermission('list-servers')) {
return;
}
return $this->collection($node->servers, new ServerTransformer, 'server');
}
}

View file

@ -24,6 +24,7 @@
namespace Pterodactyl\Transformers\Admin;
use Illuminate\Http\Request;
use Pterodactyl\Models\ServiceOption;
use League\Fractal\TransformerAbstract;
@ -41,6 +42,28 @@ class OptionTransformer extends TransformerAbstract
'variables',
];
/**
* The Illuminate Request object if provided.
*
* @var \Illuminate\Http\Request|bool
*/
protected $request;
/**
* Setup request object for transformer.
*
* @param \Illuminate\Http\Request|bool $request
* @return void
*/
public function __construct($request = false)
{
if (! $request instanceof Request && $request !== false) {
throw new DisplayException('Request passed to constructor must be of type Request or false.');
}
$this->request = $request;
}
/**
* Return a generic transformed service option array.
*

View file

@ -24,6 +24,7 @@
namespace Pterodactyl\Transformers\Admin;
use Illuminate\Http\Request;
use Pterodactyl\Models\Pack;
use League\Fractal\TransformerAbstract;
@ -39,6 +40,28 @@ class PackTransformer extends TransformerAbstract
'servers',
];
/**
* The Illuminate Request object if provided.
*
* @var \Illuminate\Http\Request|bool
*/
protected $request;
/**
* Setup request object for transformer.
*
* @param \Illuminate\Http\Request|bool $request
* @return void
*/
public function __construct($request = false)
{
if (! $request instanceof Request && $request !== false) {
throw new DisplayException('Request passed to constructor must be of type Request or false.');
}
$this->request = $request;
}
/**
* Return a generic transformed pack array.
*

View file

@ -24,6 +24,7 @@
namespace Pterodactyl\Transformers\Admin;
use Illuminate\Http\Request;
use Pterodactyl\Models\Server;
use League\Fractal\TransformerAbstract;
@ -46,6 +47,28 @@ class ServerTransformer extends TransformerAbstract
'node',
];
/**
* The Illuminate Request object if provided.
*
* @var \Illuminate\Http\Request|bool
*/
protected $request;
/**
* Setup request object for transformer.
*
* @param \Illuminate\Http\Request|bool $request
* @return void
*/
public function __construct($request = false)
{
if (! $request instanceof Request && $request !== false) {
throw new DisplayException('Request passed to constructor must be of type Request or false.');
}
$this->request = $request;
}
/**
* Return a generic transformed server array.
*
@ -63,7 +86,7 @@ class ServerTransformer extends TransformerAbstract
*/
public function includeAllocations(Server $server)
{
return $this->collection($server->allocations, new AllocationTransformer('server'), 'allocation');
return $this->collection($server->allocations, new AllocationTransformer($this->request, 'server'), 'allocation');
}
/**
@ -73,7 +96,7 @@ class ServerTransformer extends TransformerAbstract
*/
public function includeSubusers(Server $server)
{
return $this->collection($server->subusers, new SubuserTransformer, 'subuser');
return $this->collection($server->subusers, new SubuserTransformer($this->request), 'subuser');
}
/**
@ -83,7 +106,7 @@ class ServerTransformer extends TransformerAbstract
*/
public function includeUser(Server $server)
{
return $this->item($server->user, new UserTransformer, 'user');
return $this->item($server->user, new UserTransformer($this->request), 'user');
}
/**
@ -93,7 +116,7 @@ class ServerTransformer extends TransformerAbstract
*/
public function includePack(Server $server)
{
return $this->item($server->pack, new PackTransformer, 'pack');
return $this->item($server->pack, new PackTransformer($this->request), 'pack');
}
/**
@ -103,7 +126,7 @@ class ServerTransformer extends TransformerAbstract
*/
public function includeService(Server $server)
{
return $this->item($server->service, new ServiceTransformer, 'service');
return $this->item($server->service, new ServiceTransformer($this->request), 'service');
}
/**
@ -113,7 +136,7 @@ class ServerTransformer extends TransformerAbstract
*/
public function includeOption(Server $server)
{
return $this->item($server->option, new OptionTransformer, 'option');
return $this->item($server->option, new OptionTransformer($this->request), 'option');
}
/**
@ -123,7 +146,7 @@ class ServerTransformer extends TransformerAbstract
*/
public function includeVariables(Server $server)
{
return $this->collection($server->variables, new ServerVariableTransformer, 'server_variable');
return $this->collection($server->variables, new ServerVariableTransformer($this->request), 'server_variable');
}
/**
@ -133,16 +156,16 @@ class ServerTransformer extends TransformerAbstract
*/
public function includeLocation(Server $server)
{
return $this->item($server->location, new LocationTransformer, 'location');
return $this->item($server->location, new LocationTransformer($this->request), 'location');
}
/**
* Return a generic array with pack information for this server.
*
* @return \Leauge\Fractal\Resource\Item
* @return \Leauge\Fractal\Resource\Item|void
*/
public function includeNode(Server $server)
{
return $this->item($server->node, new NodeTransformer, 'node');
return $this->item($server->node, new NodeTransformer($this->request), 'node');
}
}

View file

@ -24,6 +24,7 @@
namespace Pterodactyl\Transformers\Admin;
use Illuminate\Http\Request;
use Pterodactyl\Models\ServerVariable;
use League\Fractal\TransformerAbstract;
@ -36,6 +37,28 @@ class ServerVariableTransformer extends TransformerAbstract
*/
protected $availableIncludes = ['parent'];
/**
* The Illuminate Request object if provided.
*
* @var \Illuminate\Http\Request|bool
*/
protected $request;
/**
* Setup request object for transformer.
*
* @param \Illuminate\Http\Request|bool $request
* @return void
*/
public function __construct($request = false)
{
if (! $request instanceof Request && $request !== false) {
throw new DisplayException('Request passed to constructor must be of type Request or false.');
}
$this->request = $request;
}
/**
* Return a generic transformed server variable array.
*

View file

@ -24,6 +24,7 @@
namespace Pterodactyl\Transformers\Admin;
use Illuminate\Http\Request;
use Pterodactyl\Models\Service;
use League\Fractal\TransformerAbstract;
@ -40,6 +41,28 @@ class ServiceTransformer extends TransformerAbstract
'packs',
];
/**
* The Illuminate Request object if provided.
*
* @var \Illuminate\Http\Request|bool
*/
protected $request;
/**
* Setup request object for transformer.
*
* @param \Illuminate\Http\Request|bool $request
* @return void
*/
public function __construct($request = false)
{
if (! $request instanceof Request && $request !== false) {
throw new DisplayException('Request passed to constructor must be of type Request or false.');
}
$this->request = $request;
}
/**
* Return a generic transformed service array.
*

View file

@ -24,6 +24,7 @@
namespace Pterodactyl\Transformers\Admin;
use Illuminate\Http\Request;
use Pterodactyl\Models\ServiceVariable;
use League\Fractal\TransformerAbstract;
@ -36,6 +37,28 @@ class ServiceVariableTransformer extends TransformerAbstract
*/
protected $availableIncludes = ['variables'];
/**
* The Illuminate Request object if provided.
*
* @var \Illuminate\Http\Request|bool
*/
protected $request;
/**
* Setup request object for transformer.
*
* @param \Illuminate\Http\Request|bool $request
* @return void
*/
public function __construct($request = false)
{
if (! $request instanceof Request && $request !== false) {
throw new DisplayException('Request passed to constructor must be of type Request or false.');
}
$this->request = $request;
}
/**
* Return a generic transformed server variable array.
*

View file

@ -24,12 +24,35 @@
namespace Pterodactyl\Transformers\Admin;
use Illuminate\Http\Request;
use Pterodactyl\Models\Subuser;
use Pterodactyl\Models\Permission;
use League\Fractal\TransformerAbstract;
class SubuserTransformer extends TransformerAbstract
{
/**
* The Illuminate Request object if provided.
*
* @var \Illuminate\Http\Request|bool
*/
protected $request;
/**
* Setup request object for transformer.
*
* @param \Illuminate\Http\Request|bool $request
* @return void
*/
public function __construct($request = false)
{
if (! $request instanceof Request && $request !== false) {
throw new DisplayException('Request passed to constructor must be of type Request or false.');
}
$this->request = $request;
}
/**
* Return a generic transformed subuser array.
*

View file

@ -24,11 +24,34 @@
namespace Pterodactyl\Transformers\Admin;
use Illuminate\Http\Request;
use Pterodactyl\Models\User;
use League\Fractal\TransformerAbstract;
class UserTransformer extends TransformerAbstract
{
/**
* The Illuminate Request object if provided.
*
* @var \Illuminate\Http\Request|bool
*/
protected $request;
/**
* Setup request object for transformer.
*
* @param \Illuminate\Http\Request|bool $request
* @return void
*/
public function __construct($request = false)
{
if (! $request instanceof Request && $request !== false) {
throw new DisplayException('Request passed to constructor must be of type Request or false.');
}
$this->request = $request;
}
/**
* Return a generic transformed subuser array.
*

View file

@ -28,7 +28,7 @@ return [
*/
'storage' => [
'enabled' => true,
'driver' => 'file', // redis, file, pdo
'driver' => env('DEBUGBAR_DRIVER', 'file'), // redis, file, pdo
'path' => storage_path() . '/debugbar', // For file driver
'connection' => null, // Leave null for default connection (Redis/PDO)
],
@ -125,7 +125,7 @@ return [
'enabled' => false,
'types' => ['SELECT', 'INSERT', 'UPDATE', 'DELETE'], // array('SELECT', 'INSERT', 'UPDATE', 'DELETE'); for MySQL 5.6.3+
],
'hints' => true, // Show hints for common mistakes
'hints' => false, // Show hints for common mistakes
],
'mail' => [
'full_log' => false,