|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Pterodactyl\Http\Controllers\Api\Client; |
| 4 | + |
| 5 | +use Pterodactyl\Models\ApiKey; |
| 6 | +use Pterodactyl\Exceptions\DisplayException; |
| 7 | +use Illuminate\Contracts\Encryption\Encrypter; |
| 8 | +use Pterodactyl\Services\Api\KeyCreationService; |
| 9 | +use Pterodactyl\Http\Requests\Api\Client\ClientApiRequest; |
| 10 | +use Pterodactyl\Transformers\Api\Client\ApiKeyTransformer; |
| 11 | +use Pterodactyl\Http\Requests\Api\Client\Account\StoreApiKeyRequest; |
| 12 | + |
| 13 | +class ApiKeyController extends ClientApiController |
| 14 | +{ |
| 15 | + /** |
| 16 | + * @var \Pterodactyl\Services\Api\KeyCreationService |
| 17 | + */ |
| 18 | + private $keyCreationService; |
| 19 | + |
| 20 | + /** |
| 21 | + * @var \Illuminate\Contracts\Encryption\Encrypter |
| 22 | + */ |
| 23 | + private $encrypter; |
| 24 | + |
| 25 | + /** |
| 26 | + * ApiKeyController constructor. |
| 27 | + * |
| 28 | + * @param \Illuminate\Contracts\Encryption\Encrypter $encrypter |
| 29 | + * @param \Pterodactyl\Services\Api\KeyCreationService $keyCreationService |
| 30 | + */ |
| 31 | + public function __construct(Encrypter $encrypter, KeyCreationService $keyCreationService) |
| 32 | + { |
| 33 | + parent::__construct(); |
| 34 | + |
| 35 | + $this->encrypter = $encrypter; |
| 36 | + $this->keyCreationService = $keyCreationService; |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Returns all of the API keys that exist for the given client. |
| 41 | + * |
| 42 | + * @param \Pterodactyl\Http\Requests\Api\Client\ClientApiRequest $request |
| 43 | + * @return array |
| 44 | + */ |
| 45 | + public function index(ClientApiRequest $request) |
| 46 | + { |
| 47 | + return $this->fractal->collection($request->user()->apiKeys) |
| 48 | + ->transformWith($this->getTransformer(ApiKeyTransformer::class)) |
| 49 | + ->toArray(); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Store a new API key for a user's account. |
| 54 | + * |
| 55 | + * @param \Pterodactyl\Http\Requests\Api\Client\Account\StoreApiKeyRequest $request |
| 56 | + * @return array |
| 57 | + * |
| 58 | + * @throws \Pterodactyl\Exceptions\DisplayException |
| 59 | + * @throws \Pterodactyl\Exceptions\Model\DataValidationException |
| 60 | + */ |
| 61 | + public function store(StoreApiKeyRequest $request) |
| 62 | + { |
| 63 | + if ($request->user()->apiKeys->count() >= 5) { |
| 64 | + throw new DisplayException( |
| 65 | + 'You have reached the account limit for number of API keys.' |
| 66 | + ); |
| 67 | + } |
| 68 | + |
| 69 | + $key = $this->keyCreationService->setKeyType(ApiKey::TYPE_ACCOUNT)->handle([ |
| 70 | + 'user_id' => $request->user()->id, |
| 71 | + 'memo' => $request->input('description'), |
| 72 | + 'allowed_ips' => $request->input('allowed_ips') ?? [], |
| 73 | + ]); |
| 74 | + |
| 75 | + return $this->fractal->item($key) |
| 76 | + ->transformWith($this->getTransformer(ApiKeyTransformer::class)) |
| 77 | + ->addMeta([ |
| 78 | + 'secret_token' => $this->encrypter->decrypt($key->token), |
| 79 | + ]) |
| 80 | + ->toArray(); |
| 81 | + } |
| 82 | + |
| 83 | + public function delete() |
| 84 | + { |
| 85 | + } |
| 86 | +} |
0 commit comments