Files
pterodactyl-panel/app/Http/Controllers/Admin/NodeAutoDeployController.php
T

57 lines
1.9 KiB
PHP
Raw Normal View History

2020-04-12 17:20:09 -07:00
<?php
namespace Pterodactyl\Http\Controllers\Admin;
use Illuminate\Http\Request;
use Pterodactyl\Models\Node;
use Pterodactyl\Models\ApiKey;
use Illuminate\Http\JsonResponse;
2026-06-23 16:50:14 -04:00
use Pterodactyl\Services\Acl\Api\AdminAcl;
2020-04-12 17:20:09 -07:00
use Pterodactyl\Http\Controllers\Controller;
use Illuminate\Contracts\Encryption\Encrypter;
use Pterodactyl\Services\Api\KeyCreationService;
class NodeAutoDeployController extends Controller
{
/**
* NodeAutoDeployController constructor.
*/
public function __construct(
2022-10-14 10:59:20 -06:00
private Encrypter $encrypter,
2024-10-21 19:18:20 -06:00
private KeyCreationService $keyCreationService,
2020-04-12 17:20:09 -07:00
) {
}
/**
2022-10-14 10:59:20 -06:00
* Generates a new API key for the logged-in user with only permission to read
2026-06-23 16:50:14 -04:00
* node configurations, and returns that as the deployment key for a node.
2020-04-12 17:20:09 -07:00
*
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
*/
2022-10-14 10:59:20 -06:00
public function __invoke(Request $request, Node $node): JsonResponse
2020-04-12 17:20:09 -07:00
{
$key = ApiKey::query()
->where('user_id', $request->user()->id)
2026-06-23 16:50:14 -04:00
->where('node_id', $node->id)
->where('key_type', ApiKey::TYPE_APPLICATION)
2026-06-23 16:50:14 -04:00
->where('r_nodes', AdminAcl::READ_CONFIGURATION)
2020-04-12 17:20:09 -07:00
->first();
// We couldn't find a key that exists for this user with only permission for
2026-06-23 16:50:14 -04:00
// reading node configurations. Go ahead and create it now.
2021-01-23 12:33:34 -08:00
if (!$key) {
2020-04-12 17:20:09 -07:00
$key = $this->keyCreationService->setKeyType(ApiKey::TYPE_APPLICATION)->handle([
'user_id' => $request->user()->id,
2026-06-23 16:50:14 -04:00
'node_id' => $node->id,
2020-04-12 17:20:09 -07:00
'memo' => 'Automatically generated node deployment key.',
'allowed_ips' => [],
2026-06-23 16:50:14 -04:00
], ['r_nodes' => AdminAcl::READ_CONFIGURATION]);
2020-04-12 17:20:09 -07:00
}
return new JsonResponse([
2020-04-12 17:20:09 -07:00
'node' => $node->id,
'token' => $key->identifier . $this->encrypter->decrypt($key->token),
]);
}
}