Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/app/Services/Api/KeyCreationService.php
10279 views
1
<?php
2
3
namespace Pterodactyl\Services\Api;
4
5
use Pterodactyl\Models\ApiKey;
6
use Illuminate\Contracts\Encryption\Encrypter;
7
use Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface;
8
9
class KeyCreationService
10
{
11
private int $keyType = ApiKey::TYPE_NONE;
12
13
/**
14
* ApiKeyService constructor.
15
*/
16
public function __construct(private ApiKeyRepositoryInterface $repository, private Encrypter $encrypter)
17
{
18
}
19
20
/**
21
* Set the type of key that should be created. By default an orphaned key will be
22
* created. These keys cannot be used for anything, and will not render in the UI.
23
*/
24
public function setKeyType(int $type): self
25
{
26
$this->keyType = $type;
27
28
return $this;
29
}
30
31
/**
32
* Create a new API key for the Panel using the permissions passed in the data request.
33
* This will automatically generate an identifier and an encrypted token that are
34
* stored in the database.
35
*
36
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
37
*/
38
public function handle(array $data, array $permissions = []): ApiKey
39
{
40
$data = array_merge($data, [
41
'key_type' => $this->keyType,
42
'identifier' => ApiKey::generateTokenIdentifier($this->keyType),
43
'token' => $this->encrypter->encrypt(str_random(ApiKey::KEY_LENGTH)),
44
]);
45
46
if ($this->keyType === ApiKey::TYPE_APPLICATION) {
47
$data = array_merge($data, $permissions);
48
}
49
50
return $this->repository->create($data, true, true);
51
}
52
}
53
54