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

75 lines
2.3 KiB
PHP
Raw Normal View History

<?php
namespace Pterodactyl\Http\Controllers\Api\Client;
use Pterodactyl\Models\ApiKey;
2020-03-22 19:10:49 -07:00
use Illuminate\Http\JsonResponse;
2022-05-29 18:48:35 -04:00
use Pterodactyl\Facades\Activity;
use Illuminate\Support\Facades\DB;
use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Http\Requests\Api\Client\ClientApiRequest;
use Pterodactyl\Transformers\Api\Client\ApiKeyTransformer;
use Pterodactyl\Http\Requests\Api\Client\Account\StoreApiKeyRequest;
class ApiKeyController extends ClientApiController
{
/**
2022-10-14 10:59:20 -06:00
* Returns all the API keys that exist for the given client.
*/
2022-10-14 10:59:20 -06:00
public function index(ClientApiRequest $request): array
{
return $this->fractal->collection($request->user()->apiKeys)
->transformWith($this->getTransformer(ApiKeyTransformer::class))
->toArray();
}
/**
* Store a new API key for a user's account.
2022-10-14 10:59:20 -06:00
*
2024-10-21 19:18:20 -06:00
* @throws DisplayException
*/
2022-10-08 14:14:03 -07:00
public function store(StoreApiKeyRequest $request): array
{
$token = DB::transaction(function () use ($request) {
if ($request->user()->apiKeys()->lockForUpdate()->count() >= 25) {
throw new DisplayException('You have reached the account limit for number of API keys.');
}
return $request->user()->createToken(
$request->input('description'),
$request->input('allowed_ips')
);
});
2022-05-29 18:48:35 -04:00
Activity::event('user:api-key.create')
->subject($token->accessToken)
->property('identifier', $token->accessToken->identifier)
->log();
return $this->fractal->item($token->accessToken)
->transformWith($this->getTransformer(ApiKeyTransformer::class))
->addMeta(['secret_token' => $token->plainTextToken])
->toArray();
}
2020-03-22 19:10:49 -07:00
/**
* Deletes a given API key.
*/
2022-10-14 10:59:20 -06:00
public function delete(ClientApiRequest $request, string $identifier): JsonResponse
{
2024-10-21 19:18:20 -06:00
/** @var ApiKey $key */
2022-05-29 18:48:35 -04:00
$key = $request->user()->apiKeys()
->where('key_type', ApiKey::TYPE_ACCOUNT)
->where('identifier', $identifier)
2022-05-29 20:39:51 -04:00
->firstOrFail();
2020-03-22 19:10:49 -07:00
2022-05-29 18:48:35 -04:00
Activity::event('user:api-key.delete')
2022-06-18 12:16:54 -04:00
->property('identifier', $key->identifier)
2022-05-29 18:48:35 -04:00
->log();
$key->delete();
2020-03-22 19:10:49 -07:00
return new JsonResponse([], JsonResponse::HTTP_NO_CONTENT);
}
}