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;
|
2020-03-23 01:15:38 +00:00
|
|
|
use Pterodactyl\Exceptions\DisplayException;
|
|
|
|
use Illuminate\Contracts\Encryption\Encrypter;
|
|
|
|
use Pterodactyl\Services\Api\KeyCreationService;
|
2020-03-23 02:10:49 +00:00
|
|
|
use Pterodactyl\Repositories\Eloquent\ApiKeyRepository;
|
2020-03-23 01:15:38 +00:00
|
|
|
use Pterodactyl\Http\Requests\Api\Client\ClientApiRequest;
|
|
|
|
use Pterodactyl\Transformers\Api\Client\ApiKeyTransformer;
|
2020-03-23 02:10:49 +00:00
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
2020-03-23 01:15:38 +00:00
|
|
|
use Pterodactyl\Http\Requests\Api\Client\Account\StoreApiKeyRequest;
|
|
|
|
|
|
|
|
class ApiKeyController extends ClientApiController
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Services\Api\KeyCreationService
|
|
|
|
*/
|
|
|
|
private $keyCreationService;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Illuminate\Contracts\Encryption\Encrypter
|
|
|
|
*/
|
|
|
|
private $encrypter;
|
|
|
|
|
2020-03-23 02:10:49 +00:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Repositories\Eloquent\ApiKeyRepository
|
|
|
|
*/
|
|
|
|
private $repository;
|
|
|
|
|
2020-03-23 01:15:38 +00:00
|
|
|
/**
|
|
|
|
* ApiKeyController constructor.
|
|
|
|
*/
|
2020-03-23 02:10:49 +00:00
|
|
|
public function __construct(
|
|
|
|
Encrypter $encrypter,
|
|
|
|
KeyCreationService $keyCreationService,
|
|
|
|
ApiKeyRepository $repository
|
|
|
|
) {
|
2020-03-23 01:15:38 +00:00
|
|
|
parent::__construct();
|
|
|
|
|
|
|
|
$this->encrypter = $encrypter;
|
|
|
|
$this->keyCreationService = $keyCreationService;
|
2020-03-23 02:10:49 +00:00
|
|
|
$this->repository = $repository;
|
2020-03-23 01:15:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns all of the API keys that exist for the given client.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function index(ClientApiRequest $request)
|
|
|
|
{
|
|
|
|
return $this->fractal->collection($request->user()->apiKeys)
|
|
|
|
->transformWith($this->getTransformer(ApiKeyTransformer::class))
|
|
|
|
->toArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store a new API key for a user's account.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\DisplayException
|
|
|
|
*/
|
|
|
|
public function store(StoreApiKeyRequest $request)
|
|
|
|
{
|
|
|
|
if ($request->user()->apiKeys->count() >= 5) {
|
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-22 23:03:51 +00:00
|
|
|
return $this->fractal->item($token->accessToken)
|
2020-03-23 01:15:38 +00:00
|
|
|
->transformWith($this->getTransformer(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.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\JsonResponse
|
|
|
|
*/
|
|
|
|
public function delete(ClientApiRequest $request, string $identifier)
|
2020-03-23 01:15:38 +00:00
|
|
|
{
|
2020-03-23 02:10:49 +00:00
|
|
|
$response = $this->repository->deleteWhere([
|
2020-06-26 05:36:58 +00:00
|
|
|
'key_type' => ApiKey::TYPE_ACCOUNT,
|
2020-03-23 02:10:49 +00:00
|
|
|
'user_id' => $request->user()->id,
|
|
|
|
'identifier' => $identifier,
|
|
|
|
]);
|
|
|
|
|
2021-01-23 20:33:34 +00:00
|
|
|
if (!$response) {
|
|
|
|
throw new NotFoundHttpException();
|
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
|
|
|
}
|
|
|
|
}
|