Path: blob/1.0-develop/app/Services/Api/KeyCreationService.php
10279 views
<?php12namespace Pterodactyl\Services\Api;34use Pterodactyl\Models\ApiKey;5use Illuminate\Contracts\Encryption\Encrypter;6use Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface;78class KeyCreationService9{10private int $keyType = ApiKey::TYPE_NONE;1112/**13* ApiKeyService constructor.14*/15public function __construct(private ApiKeyRepositoryInterface $repository, private Encrypter $encrypter)16{17}1819/**20* Set the type of key that should be created. By default an orphaned key will be21* created. These keys cannot be used for anything, and will not render in the UI.22*/23public function setKeyType(int $type): self24{25$this->keyType = $type;2627return $this;28}2930/**31* Create a new API key for the Panel using the permissions passed in the data request.32* This will automatically generate an identifier and an encrypted token that are33* stored in the database.34*35* @throws \Pterodactyl\Exceptions\Model\DataValidationException36*/37public function handle(array $data, array $permissions = []): ApiKey38{39$data = array_merge($data, [40'key_type' => $this->keyType,41'identifier' => ApiKey::generateTokenIdentifier($this->keyType),42'token' => $this->encrypter->encrypt(str_random(ApiKey::KEY_LENGTH)),43]);4445if ($this->keyType === ApiKey::TYPE_APPLICATION) {46$data = array_merge($data, $permissions);47}4849return $this->repository->create($data, true, true);50}51}525354