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