e3df0738da
Previously, a single key was used to access the API, this has not changed in terms of what the user sees. However, API keys now use an identifier and token internally. The identifier is the first 16 characters of the key, and the token is the remaining 32. The token is stored encrypted at rest in the database and the identifier is used by the API middleware to grab that record and make a timing attack safe comparison.
65 lines
1.8 KiB
PHP
65 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace Pterodactyl\Services\Api;
|
|
|
|
use Pterodactyl\Models\APIKey;
|
|
use Illuminate\Database\ConnectionInterface;
|
|
use Illuminate\Contracts\Encryption\Encrypter;
|
|
use Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface;
|
|
|
|
class KeyCreationService
|
|
{
|
|
/**
|
|
* @var \Illuminate\Database\ConnectionInterface
|
|
*/
|
|
private $connection;
|
|
|
|
/**
|
|
* @var \Illuminate\Contracts\Encryption\Encrypter
|
|
*/
|
|
private $encrypter;
|
|
|
|
/**
|
|
* @var \Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface
|
|
*/
|
|
private $repository;
|
|
|
|
/**
|
|
* ApiKeyService constructor.
|
|
*
|
|
* @param \Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface $repository
|
|
* @param \Illuminate\Database\ConnectionInterface $connection
|
|
* @param \Illuminate\Contracts\Encryption\Encrypter $encrypter
|
|
*/
|
|
public function __construct(
|
|
ApiKeyRepositoryInterface $repository,
|
|
ConnectionInterface $connection,
|
|
Encrypter $encrypter
|
|
) {
|
|
$this->repository = $repository;
|
|
$this->connection = $connection;
|
|
$this->encrypter = $encrypter;
|
|
}
|
|
|
|
/**
|
|
* 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.
|
|
*
|
|
* @param array $data
|
|
* @return \Pterodactyl\Models\APIKey
|
|
*
|
|
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
|
*/
|
|
public function handle(array $data): APIKey
|
|
{
|
|
$data = array_merge($data, [
|
|
'identifer' => str_random(APIKey::IDENTIFIER_LENGTH),
|
|
'token' => $this->encrypter->encrypt(str_random(APIKey::KEY_LENGTH)),
|
|
]);
|
|
|
|
$instance = $this->repository->create($data, true, true);
|
|
|
|
return $instance;
|
|
}
|
|
}
|