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
|
|
|
|
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-13 22:06:19 +00:00
|
|
|
* Create a new API key for the Panel using the permissions passed in the data request.
|
|
|
|
* This will automatically generate an identifer and an encrypted token that are
|
|
|
|
* 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 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 18:06:15 +00:00
|
|
|
public function handle(array $data): ApiKey
|
2017-06-25 20:31:50 +00:00
|
|
|
{
|
2018-01-13 22:06:19 +00:00
|
|
|
$data = array_merge($data, [
|
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
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|