Skip to content

Commit db4df2b

Browse files
committed
Push basis of new API key policy
Will need to revisit this another day when I’m fresh to figure out the best method to do this.
1 parent 51204b8 commit db4df2b

File tree

6 files changed

+104
-19
lines changed

6 files changed

+104
-19
lines changed

app/Http/Controllers/API/Admin/ServerController.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,17 @@ public function index(Request $request)
6060
public function view(Request $request, $id)
6161
{
6262
$server = Server::findOrFail($id);
63-
6463
$fractal = Fractal::create()->item($server);
6564

65+
// dd($request->user()->can('view-node', $request->apiKey()));
66+
67+
// Have the api key model return a list of includes that would be allowed
68+
// given the permissions they have aleady been granted?
69+
//
70+
// If someone has 'view-node' they would then be able to use ->parseIncludes(['*.node.*']);
71+
// How that logic will work is beyond me currently, but should keep things
72+
// fairly clean?
73+
6674
if ($request->input('include')) {
6775
$fractal->parseIncludes(explode(',', $request->input('include')));
6876
}

app/Http/Middleware/HMACAuthorization.php

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ public function handle(Request $request, Closure $next)
9292

9393
$this->checkBearer();
9494
$this->validateRequest();
95+
$this->validateIPAccess();
9596
$this->validateContents();
9697

9798
Auth::loginUsingId($this->key()->user_id);
@@ -137,23 +138,6 @@ protected function validateRequest()
137138
if (! $this->key) {
138139
throw new AccessDeniedHttpException('Unable to identify requester. Authorization token is invalid.');
139140
}
140-
141-
if (empty($this->request()->route()->getName())) {
142-
throw new \Exception('Attempting to access un-named route. This should not be possible.');
143-
}
144-
145-
$this->validateIPAccess();
146-
147-
$query = APIPermission::where('key_id', $this->key()->id)
148-
->where('permission', $this->request()->route()->getName());
149-
150-
if (starts_with($this->request()->route()->getName(), 'api.user')) {
151-
$query->orWhere('permission', 'api.user.*');
152-
}
153-
154-
if (! $query->first()) {
155-
throw new AccessDeniedHttpException('You do not have permission to access this resource on the API.');
156-
}
157141
}
158142

159143
/**

app/Policies/APIKeyPolicy.php

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
/**
3+
* Pterodactyl - Panel
4+
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
namespace Pterodactyl\Policies;
26+
27+
use Cache;
28+
use Carbon;
29+
use Pterodactyl\Models\User;
30+
use Pterodactyl\Models\APIKey as Key;
31+
use Pterodactyl\Models\APIPermission as Permission;
32+
33+
class APIKeyPolicy
34+
{
35+
/**
36+
* Checks if the API key has permission to perform an action.
37+
*
38+
* @param \Pterodactyl\Models\User $user
39+
* @param \Pterodactyl\Models\APIKey $key
40+
* @param string $permission
41+
* @return bool
42+
*/
43+
private function checkPermission(User $user, Key $key, $permission)
44+
{
45+
$permissions = Cache::remember('APIKeyPolicy.' . $user->uuid . $key->public, Carbon::now()->addSeconds(5), function () use ($key) {
46+
return $key->permissions()->get()->transform(function ($item) {
47+
return $item->permission;
48+
})->values();
49+
});
50+
51+
return $permissions->search($permission, true) !== false;
52+
}
53+
54+
/**
55+
* Determine if API key is allowed to view all nodes.
56+
*
57+
* @param \Pterodactyl\Models\User $user
58+
* @param \Pterodactyl\Models\APIKey $key
59+
* @return bool
60+
*/
61+
public function listNodes(User $user, Key $key)
62+
{
63+
return $this->checkPermission($user, $key, 'list-nodes');
64+
}
65+
66+
/**
67+
* Determine if API key is allowed to view a specific node.
68+
*
69+
* @param \Pterodactyl\Models\User $user
70+
* @param \Pterodactyl\Models\APIKey $key
71+
* @return bool
72+
*/
73+
public function viewNode(User $user, Key $key)
74+
{
75+
return $this->checkPermission($user, $key, 'view-node');
76+
}
77+
}

app/Policies/ServerPolicy.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ private function checkPermission(User $user, Server $server, $permission)
4545
return true;
4646
}
4747

48-
$permissions = Cache::remember('ServerPolicy.' . $user->uuid . $server->uuid, Carbon::now()->addSeconds(10), function () use ($user, $server) {
48+
$permissions = Cache::remember('ServerPolicy.' . $user->uuid . $server->uuid, Carbon::now()->addSeconds(5), function () use ($user, $server) {
4949
return $user->permissions()->server($server)->get()->transform(function ($item) {
5050
return $item->permission;
5151
})->values();

app/Providers/AuthServiceProvider.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class AuthServiceProvider extends ServiceProvider
1313
*/
1414
protected $policies = [
1515
'Pterodactyl\Models\Server' => 'Pterodactyl\Policies\ServerPolicy',
16+
'Pterodactyl\Models\APIKey' => 'Pterodactyl\Policies\APIKeyPolicy',
1617
];
1718

1819
/**

app/Providers/MacroServiceProvider.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
namespace Pterodactyl\Providers;
2626

2727
use File;
28+
use Request;
2829
use Illuminate\Support\ServiceProvider;
2930

3031
class MacroServiceProvider extends ServiceProvider
@@ -48,5 +49,19 @@ public function boot()
4849

4950
return round($size, ($i < 2) ? 0 : $precision) . ' ' . $units[$i];
5051
});
52+
53+
Request::macro('apiKey', function () {
54+
if (! Request::bearerToken()) {
55+
return false;
56+
}
57+
58+
$parts = explode('.', Request::bearerToken());
59+
60+
if (count($parts) === 2) {
61+
return \Pterodactyl\Models\APIKey::where('public', $parts[0])->first();
62+
}
63+
64+
return false;
65+
});
5166
}
5267
}

0 commit comments

Comments
 (0)