Files
pterodactyl-panel/app/Http/Controllers/Api/Client/SSHKeyController.php
T

88 lines
3.3 KiB
PHP
Raw Normal View History

<?php
2026-10-05 18:10:18 -04:00
declare(strict_types=1);
namespace Pterodactyl\Http\Controllers\Api\Client;
use Illuminate\Http\JsonResponse;
2026-10-05 18:10:18 -04:00
use Knuckles\Scribe\Attributes\Endpoint;
use Knuckles\Scribe\Attributes\Group;
use Knuckles\Scribe\Attributes\Response as ScribeResponse;
use Knuckles\Scribe\Attributes\Subgroup;
use Pterodactyl\Extensions\Scribe\Attributes\ResponseFromTransformer;
2022-05-29 18:48:35 -04:00
use Pterodactyl\Facades\Activity;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Facades\Fractal;
use Pterodactyl\Http\Requests\Api\Client\Account\DeleteSSHKeyRequest;
use Pterodactyl\Http\Requests\Api\Client\Account\StoreSSHKeyRequest;
use Pterodactyl\Http\Requests\Api\Client\ClientApiRequest;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Models\UserSSHKey;
2022-05-14 18:08:48 -04:00
use Pterodactyl\Transformers\Api\Client\UserSSHKeyTransformer;
2026-10-05 18:10:18 -04:00
#[Group('Client API', 'Endpoints authenticated as a panel user using a client API token.')]
#[Subgroup('Account SSH Keys', 'Manage SSH public keys for the authenticated user.')]
class SSHKeyController extends ClientApiController
{
/**
2022-10-14 10:59:20 -06:00
* Returns all the SSH keys that have been configured for the logged-in
* user account.
2026-10-05 18:10:18 -04:00
*
* @return ApiPayload
*/
2026-10-05 18:10:18 -04:00
#[Endpoint('List SSH keys', 'Returns all SSH public keys configured for the authenticated user.')]
#[ResponseFromTransformer(UserSSHKeyTransformer::class, UserSSHKey::class, description: 'SSH keys returned.', collection: true, resourceKey: 'ssh_key')]
public function index(ClientApiRequest $request): array
{
2026-10-05 18:10:18 -04:00
return Fractal::collection($request->user()->sshKeys)
2022-05-14 18:08:48 -04:00
->transformWith($this->getTransformer(UserSSHKeyTransformer::class))
2026-10-05 18:10:18 -04:00
->toResponseArray();
}
/**
* Stores a new SSH key for the authenticated user's account.
2026-10-05 18:10:18 -04:00
*
* @return ApiPayload
*/
2026-10-05 18:10:18 -04:00
#[Endpoint('Create SSH key', 'Adds an SSH public key to the authenticated user account.')]
#[ResponseFromTransformer(UserSSHKeyTransformer::class, UserSSHKey::class, description: 'SSH key created.', resourceKey: 'ssh_key')]
public function store(StoreSSHKeyRequest $request): array
{
$model = $request->user()->sshKeys()->create([
2026-10-05 18:10:18 -04:00
'name' => $request->validated('name'),
'public_key' => $request->getPublicKey(),
'fingerprint' => $request->getKeyFingerprint(),
]);
2022-05-29 18:48:35 -04:00
Activity::event('user:ssh-key.create')
->subject($model)
->property('fingerprint', $request->getKeyFingerprint())
->log();
2026-10-05 18:10:18 -04:00
return Fractal::item($model)
2022-05-14 18:08:48 -04:00
->transformWith($this->getTransformer(UserSSHKeyTransformer::class))
2026-10-05 18:10:18 -04:00
->toResponseArray();
}
/**
* Deletes an SSH key from the user's account.
*/
2026-10-05 18:10:18 -04:00
#[Endpoint('Delete SSH key', 'Deletes an SSH key from the authenticated user account by fingerprint. Missing keys are treated as already deleted.')]
#[ScribeResponse(status: 204, description: 'SSH key deleted or already absent.')]
public function delete(DeleteSSHKeyRequest $request): JsonResponse
{
$key = $request->user()->sshKeys()
2026-10-05 18:10:18 -04:00
->where('fingerprint', $request->validated('fingerprint'))
->first();
2022-05-29 18:48:35 -04:00
2026-10-05 18:10:18 -04:00
if (($key) !== null) {
$key->delete();
Activity::event('user:ssh-key.delete')
->subject($key)
->property('fingerprint', $key->fingerprint)
->log();
}
return new JsonResponse([], JsonResponse::HTTP_NO_CONTENT);
}
}