2017-06-25 22:31:50 +02:00
|
|
|
<?php
|
|
|
|
|
2017-07-09 19:29:18 +02:00
|
|
|
namespace Pterodactyl\Services\Api;
|
2017-06-25 22:31:50 +02:00
|
|
|
|
2018-01-14 19:06:15 +01:00
|
|
|
use Pterodactyl\Models\ApiKey;
|
2018-01-13 23:06:19 +01:00
|
|
|
use Illuminate\Contracts\Encryption\Encrypter;
|
2017-07-08 21:07:51 +02:00
|
|
|
use Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface;
|
2017-06-25 22:31:50 +02:00
|
|
|
|
2017-08-31 04:11:14 +02:00
|
|
|
class KeyCreationService
|
2017-06-25 22:31:50 +02:00
|
|
|
{
|
|
|
|
/**
|
2018-01-13 23:06:19 +01:00
|
|
|
* @var \Illuminate\Contracts\Encryption\Encrypter
|
2017-06-25 22:31:50 +02:00
|
|
|
*/
|
2018-01-13 23:06:19 +01:00
|
|
|
private $encrypter;
|
2017-06-25 22:31:50 +02:00
|
|
|
|
2018-01-14 20:30:55 +01:00
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
private $keyType = ApiKey::TYPE_NONE;
|
|
|
|
|
2017-07-08 21:07:51 +02:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface
|
|
|
|
*/
|
2017-11-19 20:32:17 +01:00
|
|
|
private $repository;
|
2017-07-08 21:07:51 +02:00
|
|
|
|
2017-06-25 22:31:50 +02:00
|
|
|
/**
|
|
|
|
* ApiKeyService constructor.
|
|
|
|
*/
|
2018-01-13 23:23:43 +01:00
|
|
|
public function __construct(ApiKeyRepositoryInterface $repository, Encrypter $encrypter)
|
|
|
|
{
|
2018-01-13 23:06:19 +01:00
|
|
|
$this->encrypter = $encrypter;
|
2018-01-13 23:23:43 +01:00
|
|
|
$this->repository = $repository;
|
2017-06-25 22:31:50 +02:00
|
|
|
}
|
|
|
|
|
2018-01-14 20:30:55 +01:00
|
|
|
/**
|
|
|
|
* Set the type of key that should be created. By default an orphaned key will be
|
|
|
|
* created. These keys cannot be used for anything, and will not render in the UI.
|
|
|
|
*
|
|
|
|
* @return \Pterodactyl\Services\Api\KeyCreationService
|
|
|
|
*/
|
|
|
|
public function setKeyType(int $type)
|
|
|
|
{
|
|
|
|
$this->keyType = $type;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2017-06-25 22:31:50 +02:00
|
|
|
/**
|
2018-01-13 23:06:19 +01:00
|
|
|
* Create a new API key for the Panel using the permissions passed in the data request.
|
2018-05-13 16:50:56 +02:00
|
|
|
* This will automatically generate an identifier and an encrypted token that are
|
2018-01-13 23:06:19 +01:00
|
|
|
* stored in the database.
|
2017-06-25 22:31:50 +02:00
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
|
|
|
*/
|
2018-01-14 20:30:55 +01:00
|
|
|
public function handle(array $data, array $permissions = []): ApiKey
|
2017-06-25 22:31:50 +02:00
|
|
|
{
|
2018-01-13 23:06:19 +01:00
|
|
|
$data = array_merge($data, [
|
2018-01-14 20:30:55 +01:00
|
|
|
'key_type' => $this->keyType,
|
2018-01-14 19:06:15 +01:00
|
|
|
'identifier' => str_random(ApiKey::IDENTIFIER_LENGTH),
|
|
|
|
'token' => $this->encrypter->encrypt(str_random(ApiKey::KEY_LENGTH)),
|
2018-01-13 23:06:19 +01:00
|
|
|
]);
|
2017-06-25 22:31:50 +02:00
|
|
|
|
2018-01-14 20:30:55 +01:00
|
|
|
if ($this->keyType === ApiKey::TYPE_APPLICATION) {
|
|
|
|
$data = array_merge($data, $permissions);
|
|
|
|
}
|
|
|
|
|
2020-03-23 02:15:38 +01:00
|
|
|
return $this->repository->create($data, true, true);
|
2017-06-25 22:31:50 +02:00
|
|
|
}
|
|
|
|
}
|