From 7576d0d08ecfbf5419386108d19b2451fbb6ea79 Mon Sep 17 00:00:00 2001 From: Mackenzie Molloy <48184419+MackenzieMolloy@users.noreply.github.com> Date: Thu, 12 Feb 2026 00:03:35 +0000 Subject: [PATCH] Added support for viewing and deleting of any Application API Keys (#5176) This PR closes Issue #5175. For context, I am aware that Application API Keys are deprecated in favour of Client API Keys however they are still operational within Pterodactyl and thus, not fully removed. Currently in Pterodactyl, as an Admin, you can only view your Application API Keys on the Admin Panel. In this PR, I made it so all Application Keys are visible and deletable. The key strings are obfuscated if the key does not belong to the user viewing. ![image](https://github.com/user-attachments/assets/9edb2533-d99c-4ec3-80cc-25630fd48594) The reason for adding this is primarily so other admin users can be aware of and delete another admin user's Application API keys from the UI. This functionality is useful in the event of a malicious user compromising an admin account, creating some API Keys to continue their attacks and the owner of the compromised admin account being unaware of Application API Keys. In this instance, even after a password reset, the attack could continue via the Application API without the admin realising it. I've tested the creation and deleting of keys along with using keys via the Application API to ensure no breakages have occurred. --------- Co-authored-by: DaneEveritt --- .../Repository/ApiKeyRepositoryInterface.php | 10 --------- app/Http/Controllers/Admin/ApiController.php | 9 ++++---- .../Admin/NodeAutoDeployController.php | 17 ++++----------- .../Eloquent/ApiKeyRepository.php | 21 ------------------- resources/views/admin/api/index.blade.php | 12 ++++++++++- 5 files changed, 20 insertions(+), 49 deletions(-) diff --git a/app/Contracts/Repository/ApiKeyRepositoryInterface.php b/app/Contracts/Repository/ApiKeyRepositoryInterface.php index bfebbddb6..0ed8b247c 100644 --- a/app/Contracts/Repository/ApiKeyRepositoryInterface.php +++ b/app/Contracts/Repository/ApiKeyRepositoryInterface.php @@ -12,18 +12,8 @@ interface ApiKeyRepositoryInterface extends RepositoryInterface */ public function getAccountKeys(User $user): Collection; - /** - * Get all the application API keys that exist for a specific user. - */ - public function getApplicationKeys(User $user): Collection; - /** * Delete an account API key from the panel for a specific user. */ public function deleteAccountKey(User $user, string $identifier): int; - - /** - * Delete an application API key from the panel for a specific user. - */ - public function deleteApplicationKey(User $user, string $identifier): int; } diff --git a/app/Http/Controllers/Admin/ApiController.php b/app/Http/Controllers/Admin/ApiController.php index 6aa7571e3..a8f87bf53 100644 --- a/app/Http/Controllers/Admin/ApiController.php +++ b/app/Http/Controllers/Admin/ApiController.php @@ -11,7 +11,6 @@ use Prologue\Alerts\AlertsMessageBag; use Pterodactyl\Services\Acl\Api\AdminAcl; use Pterodactyl\Http\Controllers\Controller; use Pterodactyl\Services\Api\KeyCreationService; -use Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface; use Pterodactyl\Http\Requests\Admin\Api\StoreApplicationApiKeyRequest; class ApiController extends Controller @@ -21,7 +20,6 @@ class ApiController extends Controller */ public function __construct( private AlertsMessageBag $alert, - private ApiKeyRepositoryInterface $repository, private KeyCreationService $keyCreationService, ) { } @@ -32,7 +30,7 @@ class ApiController extends Controller public function index(Request $request): View { return view('admin.api.index', [ - 'keys' => $this->repository->getApplicationKeys($request->user()), + 'keys' => ApiKey::query()->where('key_type', ApiKey::TYPE_APPLICATION)->get(), ]); } @@ -78,7 +76,10 @@ class ApiController extends Controller */ public function delete(Request $request, string $identifier): Response { - $this->repository->deleteApplicationKey($request->user(), $identifier); + ApiKey::query() + ->where('key_type', ApiKey::TYPE_APPLICATION) + ->where('identifier', $identifier) + ->delete(); return response('', 204); } diff --git a/app/Http/Controllers/Admin/NodeAutoDeployController.php b/app/Http/Controllers/Admin/NodeAutoDeployController.php index c53d8b9a4..2be1a9111 100644 --- a/app/Http/Controllers/Admin/NodeAutoDeployController.php +++ b/app/Http/Controllers/Admin/NodeAutoDeployController.php @@ -9,7 +9,6 @@ use Illuminate\Http\JsonResponse; use Pterodactyl\Http\Controllers\Controller; use Illuminate\Contracts\Encryption\Encrypter; use Pterodactyl\Services\Api\KeyCreationService; -use Pterodactyl\Repositories\Eloquent\ApiKeyRepository; class NodeAutoDeployController extends Controller { @@ -17,7 +16,6 @@ class NodeAutoDeployController extends Controller * NodeAutoDeployController constructor. */ public function __construct( - private ApiKeyRepository $repository, private Encrypter $encrypter, private KeyCreationService $keyCreationService, ) { @@ -31,17 +29,10 @@ class NodeAutoDeployController extends Controller */ public function __invoke(Request $request, Node $node): JsonResponse { - /** @var ApiKey|null $key */ - $key = $this->repository->getApplicationKeys($request->user()) - ->filter(function (ApiKey $key) { - foreach ($key->getAttributes() as $permission => $value) { - if ($permission === 'r_nodes' && $value === 1) { - return true; - } - } - - return false; - }) + $key = ApiKey::query() + ->where('user_id', $request->user()->id) + ->where('key_type', ApiKey::TYPE_APPLICATION) + ->where('r_nodes', 1) ->first(); // We couldn't find a key that exists for this user with only permission for diff --git a/app/Repositories/Eloquent/ApiKeyRepository.php b/app/Repositories/Eloquent/ApiKeyRepository.php index eb1a362ae..5a63514fe 100644 --- a/app/Repositories/Eloquent/ApiKeyRepository.php +++ b/app/Repositories/Eloquent/ApiKeyRepository.php @@ -27,16 +27,6 @@ class ApiKeyRepository extends EloquentRepository implements ApiKeyRepositoryInt ->get($this->getColumns()); } - /** - * Get all the application API keys that exist for a specific user. - */ - public function getApplicationKeys(User $user): Collection - { - return $this->getBuilder()->where('user_id', $user->id) - ->where('key_type', ApiKey::TYPE_APPLICATION) - ->get($this->getColumns()); - } - /** * Delete an account API key from the panel for a specific user. */ @@ -47,15 +37,4 @@ class ApiKeyRepository extends EloquentRepository implements ApiKeyRepositoryInt ->where('identifier', $identifier) ->delete(); } - - /** - * Delete an application API key from the panel for a specific user. - */ - public function deleteApplicationKey(User $user, string $identifier): int - { - return $this->getBuilder()->where('user_id', $user->id) - ->where('key_type', ApiKey::TYPE_APPLICATION) - ->where('identifier', $identifier) - ->delete(); - } } diff --git a/resources/views/admin/api/index.blade.php b/resources/views/admin/api/index.blade.php index d863c5779..4658ee9c8 100644 --- a/resources/views/admin/api/index.blade.php +++ b/resources/views/admin/api/index.blade.php @@ -29,11 +29,18 @@ Memo Last Used Created + Created by @foreach($keys as $key) - {{ $key->identifier }}{{ decrypt($key->token) }} + + @if (Auth::user()->is($key->user)) + {{ $key->identifier . decrypt($key->token) }} + @else + {{ $key->identifier . '****' }} + @endif + {{ $key->memo }} @if(!is_null($key->last_used_at)) @@ -43,6 +50,9 @@ @endif @datetimeHuman($key->created_at) + + {{ $key->user->username }} +