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 <dane@daneeveritt.com>
This commit is contained in:
Mackenzie Molloy
2026-02-11 16:03:35 -08:00
committed by GitHub
co-authored by DaneEveritt
parent d648a63e13
commit 7576d0d08e
5 changed files with 20 additions and 49 deletions
@@ -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;
}
+5 -4
View File
@@ -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);
}
@@ -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
@@ -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();
}
}
+11 -1
View File
@@ -29,11 +29,18 @@
<th>Memo</th>
<th>Last Used</th>
<th>Created</th>
<th>Created by</th>
<th></th>
</tr>
@foreach($keys as $key)
<tr>
<td><code>{{ $key->identifier }}{{ decrypt($key->token) }}</code></td>
<td><code>
@if (Auth::user()->is($key->user))
{{ $key->identifier . decrypt($key->token) }}
@else
{{ $key->identifier . '****' }}
@endif
</code></td>
<td>{{ $key->memo }}</td>
<td>
@if(!is_null($key->last_used_at))
@@ -43,6 +50,9 @@
@endif
</td>
<td>@datetimeHuman($key->created_at)</td>
<td>
<a href="{{ route('admin.users.view', $key->user->id) }}">{{ $key->user->username }}</a>
</td>
<td>
<a href="#" data-action="revoke-key" data-attr="{{ $key->identifier }}">
<i class="fa fa-trash-o text-danger"></i>