misc_pterodactyl-panel/app/Services/Api/KeyCreationService.php

55 lines
1.5 KiB
PHP
Raw Normal View History

<?php
2017-07-09 17:29:18 +00:00
namespace Pterodactyl\Services\Api;
use Pterodactyl\Models\APIKey;
use Illuminate\Contracts\Encryption\Encrypter;
use Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface;
class KeyCreationService
{
/**
* @var \Illuminate\Contracts\Encryption\Encrypter
*/
private $encrypter;
/**
* @var \Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface
*/
private $repository;
/**
* ApiKeyService constructor.
*
* @param \Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface $repository
* @param \Illuminate\Contracts\Encryption\Encrypter $encrypter
*/
2018-01-13 22:23:43 +00:00
public function __construct(ApiKeyRepositoryInterface $repository, Encrypter $encrypter)
{
$this->encrypter = $encrypter;
2018-01-13 22:23:43 +00:00
$this->repository = $repository;
}
/**
* 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-08-22 03:10:48 +00:00
* @param array $data
* @return \Pterodactyl\Models\APIKey
*
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
*/
public function handle(array $data): APIKey
{
$data = array_merge($data, [
2018-01-13 22:23:43 +00:00
'identifier' => str_random(APIKey::IDENTIFIER_LENGTH),
'token' => $this->encrypter->encrypt(str_random(APIKey::KEY_LENGTH)),
]);
$instance = $this->repository->create($data, true, true);
return $instance;
}
}