|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Pterodactyl\Http\Controllers\Admin; |
| 4 | + |
| 5 | +use Illuminate\Http\Request; |
| 6 | +use Pterodactyl\Models\Node; |
| 7 | +use Pterodactyl\Models\ApiKey; |
| 8 | +use Illuminate\Http\JsonResponse; |
| 9 | +use Pterodactyl\Http\Controllers\Controller; |
| 10 | +use Illuminate\Contracts\Encryption\Encrypter; |
| 11 | +use Pterodactyl\Services\Api\KeyCreationService; |
| 12 | +use Pterodactyl\Repositories\Eloquent\ApiKeyRepository; |
| 13 | + |
| 14 | +class NodeAutoDeployController extends Controller |
| 15 | +{ |
| 16 | + /** |
| 17 | + * @var \Pterodactyl\Services\Api\KeyCreationService |
| 18 | + */ |
| 19 | + private $keyCreationService; |
| 20 | + |
| 21 | + /** |
| 22 | + * @var \Pterodactyl\Repositories\Eloquent\ApiKeyRepository |
| 23 | + */ |
| 24 | + private $repository; |
| 25 | + |
| 26 | + /** |
| 27 | + * @var \Illuminate\Contracts\Encryption\Encrypter |
| 28 | + */ |
| 29 | + private $encrypter; |
| 30 | + |
| 31 | + /** |
| 32 | + * NodeAutoDeployController constructor. |
| 33 | + * |
| 34 | + * @param \Pterodactyl\Repositories\Eloquent\ApiKeyRepository $repository |
| 35 | + * @param \Illuminate\Contracts\Encryption\Encrypter $encrypter |
| 36 | + * @param \Pterodactyl\Services\Api\KeyCreationService $keyCreationService |
| 37 | + */ |
| 38 | + public function __construct( |
| 39 | + ApiKeyRepository $repository, |
| 40 | + Encrypter $encrypter, |
| 41 | + KeyCreationService $keyCreationService |
| 42 | + ) { |
| 43 | + $this->keyCreationService = $keyCreationService; |
| 44 | + $this->repository = $repository; |
| 45 | + $this->encrypter = $encrypter; |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Generates a new API key for the logged in user with only permission to read |
| 50 | + * nodes, and returns that as the deployment key for a node. |
| 51 | + * |
| 52 | + * @param \Illuminate\Http\Request $request |
| 53 | + * @param \Pterodactyl\Models\Node $node |
| 54 | + * @return \Illuminate\Http\JsonResponse |
| 55 | + * |
| 56 | + * @throws \Pterodactyl\Exceptions\Model\DataValidationException |
| 57 | + */ |
| 58 | + public function __invoke(Request $request, Node $node) |
| 59 | + { |
| 60 | + /** @var \Pterodactyl\Models\ApiKey|null $key */ |
| 61 | + $key = $this->repository->getApplicationKeys($request->user()) |
| 62 | + ->filter(function (ApiKey $key) { |
| 63 | + foreach ($key->getAttributes() as $permission => $value) { |
| 64 | + if ($permission === 'r_nodes' && $value === 1) { |
| 65 | + return true; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + return false; |
| 70 | + }) |
| 71 | + ->first(); |
| 72 | + |
| 73 | + // We couldn't find a key that exists for this user with only permission for |
| 74 | + // reading nodes. Go ahead and create it now. |
| 75 | + if (! $key) { |
| 76 | + $key = $this->keyCreationService->setKeyType(ApiKey::TYPE_APPLICATION)->handle([ |
| 77 | + 'user_id' => $request->user()->id, |
| 78 | + 'memo' => 'Automatically generated node deployment key.', |
| 79 | + 'allowed_ips' => [], |
| 80 | + ], ['r_nodes' => 1]); |
| 81 | + } |
| 82 | + |
| 83 | + return JsonResponse::create([ |
| 84 | + 'node' => $node->id, |
| 85 | + 'token' => $key->identifier . $this->encrypter->decrypt($key->token), |
| 86 | + ]); |
| 87 | + } |
| 88 | +} |
0 commit comments