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.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Contracts\Encryption\Encrypter $encrypter
|
|
|
|
* @param \Pterodactyl\Services\Api\KeyCreationService $keyCreationService
|
2020-03-23 02:10:49 +00:00
|
|
|
* @param \Pterodactyl\Repositories\Eloquent\ApiKeyRepository $repository
|
2020-03-23 01:15:38 +00:00
|
|
|
*/
|
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.
|
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Http\Requests\Api\Client\ClientApiRequest $request
|
|
|
|
* @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.
|
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Http\Requests\Api\Client\Account\StoreApiKeyRequest $request
|
|
|
|
* @return array
|
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\DisplayException
|
|
|
|
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
|
|
|
*/
|
|
|
|
public function store(StoreApiKeyRequest $request)
|
|
|
|
{
|
|
|
|
if ($request->user()->apiKeys->count() >= 5) {
|
|
|
|
throw new DisplayException(
|
|
|
|
'You have reached the account limit for number of API keys.'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$key = $this->keyCreationService->setKeyType(ApiKey::TYPE_ACCOUNT)->handle([
|
|
|
|
'user_id' => $request->user()->id,
|
|
|
|
'memo' => $request->input('description'),
|
|
|
|
'allowed_ips' => $request->input('allowed_ips') ?? [],
|
|
|
|
]);
|
|
|
|
|
|
|
|
return $this->fractal->item($key)
|
|
|
|
->transformWith($this->getTransformer(ApiKeyTransformer::class))
|
|
|
|
->addMeta([
|
|
|
|
'secret_token' => $this->encrypter->decrypt($key->token),
|
|
|
|
])
|
|
|
|
->toArray();
|
|
|
|
}
|
|
|
|
|
2020-03-23 02:10:49 +00:00
|
|
|
/**
|
|
|
|
* Deletes a given API key.
|
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Http\Requests\Api\Client\ClientApiRequest $request
|
|
|
|
* @param string $identifier
|
|
|
|
* @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([
|
|
|
|
'user_id' => $request->user()->id,
|
|
|
|
'identifier' => $identifier,
|
|
|
|
]);
|
|
|
|
|
|
|
|
if (! $response) {
|
|
|
|
throw new NotFoundHttpException;
|
|
|
|
}
|
|
|
|
|
|
|
|
return JsonResponse::create([], JsonResponse::HTTP_NO_CONTENT);
|
2020-03-23 01:15:38 +00:00
|
|
|
}
|
|
|
|
}
|