2020-03-23 01:15:38 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Http\Controllers\Api\Client;
|
|
|
|
|
2021-03-05 17:03:12 +00:00
|
|
|
use Illuminate\Http\Response;
|
2020-03-23 01:15:38 +00:00
|
|
|
use Pterodactyl\Models\ApiKey;
|
|
|
|
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;
|
2021-07-28 04:23:11 +00:00
|
|
|
use Pterodactyl\Transformers\Api\Client\PersonalAccessTokenTransformer;
|
2020-03-23 01:15:38 +00:00
|
|
|
|
|
|
|
class ApiKeyController extends ClientApiController
|
|
|
|
{
|
2021-03-05 17:03:12 +00:00
|
|
|
private Encrypter $encrypter;
|
2021-07-28 04:23:11 +00:00
|
|
|
|
2021-03-05 17:03:12 +00:00
|
|
|
private ApiKeyRepository $repository;
|
2021-07-28 04:23:11 +00:00
|
|
|
|
2021-03-05 17:03:12 +00:00
|
|
|
private KeyCreationService $keyCreationService;
|
2020-03-23 02:10:49 +00:00
|
|
|
|
2020-03-23 01:15:38 +00:00
|
|
|
/**
|
|
|
|
* ApiKeyController constructor.
|
|
|
|
*/
|
2020-03-23 02:10:49 +00:00
|
|
|
public function __construct(
|
|
|
|
Encrypter $encrypter,
|
2021-03-05 17:03:12 +00:00
|
|
|
ApiKeyRepository $repository,
|
|
|
|
KeyCreationService $keyCreationService
|
2020-03-23 02:10:49 +00:00
|
|
|
) {
|
2020-03-23 01:15:38 +00:00
|
|
|
parent::__construct();
|
|
|
|
|
|
|
|
$this->encrypter = $encrypter;
|
2020-03-23 02:10:49 +00:00
|
|
|
$this->repository = $repository;
|
2021-03-05 17:03:12 +00:00
|
|
|
$this->keyCreationService = $keyCreationService;
|
2020-03-23 01:15:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns all of the API keys that exist for the given client.
|
|
|
|
*
|
2021-03-05 17:03:12 +00:00
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException
|
2020-03-23 01:15:38 +00:00
|
|
|
*/
|
2021-03-05 17:03:12 +00:00
|
|
|
public function index(ClientApiRequest $request): array
|
2020-03-23 01:15:38 +00:00
|
|
|
{
|
2021-07-28 04:23:11 +00:00
|
|
|
return $this->fractal->collection($request->user()->tokens)
|
|
|
|
->transformWith($this->getTransformer(PersonalAccessTokenTransformer::class))
|
2020-03-23 01:15:38 +00:00
|
|
|
->toArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store a new API key for a user's account.
|
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\DisplayException
|
2021-03-05 17:03:12 +00:00
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException
|
2020-03-23 01:15:38 +00:00
|
|
|
*/
|
2021-03-05 17:03:12 +00:00
|
|
|
public function store(StoreApiKeyRequest $request): array
|
2020-03-23 01:15:38 +00:00
|
|
|
{
|
2021-07-28 04:23:11 +00:00
|
|
|
if ($request->user()->tokens->count() >= 10) {
|
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
|
|
|
}
|
|
|
|
|
2021-07-28 04:23:11 +00:00
|
|
|
// TODO: this should accept an array of different scopes to apply as permissions
|
|
|
|
// for the token. Right now it allows any account level permission.
|
2021-07-29 03:53:54 +00:00
|
|
|
[$token, $plaintext] = $request->user()->createToken($request->input('description'));
|
2020-03-23 01:15:38 +00:00
|
|
|
|
2021-07-29 03:53:54 +00:00
|
|
|
return $this->fractal->item($token)
|
2021-07-28 04:23:11 +00:00
|
|
|
->transformWith($this->getTransformer(PersonalAccessTokenTransformer::class))
|
2020-03-23 01:15:38 +00:00
|
|
|
->addMeta([
|
2021-07-29 03:53:54 +00:00
|
|
|
'secret_token' => $plaintext,
|
2020-03-23 01:15:38 +00:00
|
|
|
])
|
|
|
|
->toArray();
|
|
|
|
}
|
|
|
|
|
2020-03-23 02:10:49 +00:00
|
|
|
/**
|
|
|
|
* Deletes a given API key.
|
|
|
|
*/
|
2021-07-29 03:53:54 +00:00
|
|
|
public function delete(ClientApiRequest $request, string $id): Response
|
2020-03-23 01:15:38 +00:00
|
|
|
{
|
2021-07-29 03:53:54 +00:00
|
|
|
$request->user()->tokens()->where('token_id', $id)->delete();
|
2020-03-23 02:10:49 +00:00
|
|
|
|
2021-03-05 17:03:12 +00:00
|
|
|
return $this->returnNoContent();
|
2020-03-23 01:15:38 +00:00
|
|
|
}
|
|
|
|
}
|