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