Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/app/Http/Controllers/Admin/NodeAutoDeployController.php
14044 views
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
13
class NodeAutoDeployController extends Controller
14
{
15
/**
16
* NodeAutoDeployController constructor.
17
*/
18
public function __construct(
19
private Encrypter $encrypter,
20
private KeyCreationService $keyCreationService,
21
) {
22
}
23
24
/**
25
* Generates a new API key for the logged-in user with only permission to read
26
* nodes, and returns that as the deployment key for a node.
27
*
28
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
29
*/
30
public function __invoke(Request $request, Node $node): JsonResponse
31
{
32
$key = ApiKey::query()
33
->where('user_id', $request->user()->id)
34
->where('key_type', ApiKey::TYPE_APPLICATION)
35
->where('r_nodes', 1)
36
->first();
37
38
// We couldn't find a key that exists for this user with only permission for
39
// reading nodes. Go ahead and create it now.
40
if (!$key) {
41
$key = $this->keyCreationService->setKeyType(ApiKey::TYPE_APPLICATION)->handle([
42
'user_id' => $request->user()->id,
43
'memo' => 'Automatically generated node deployment key.',
44
'allowed_ips' => [],
45
], ['r_nodes' => 1]);
46
}
47
48
return new JsonResponse([
49
'node' => $node->id,
50
'token' => $key->identifier . $this->encrypter->decrypt($key->token),
51
]);
52
}
53
}
54
55