2017-06-25 22:31:50 +02:00
|
|
|
<?php
|
2017-09-26 04:43:01 +02:00
|
|
|
/**
|
2017-06-25 22:31:50 +02:00
|
|
|
* Pterodactyl - Panel
|
|
|
|
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
|
|
|
|
*
|
2017-09-26 04:43:01 +02:00
|
|
|
* This software is licensed under the terms of the MIT license.
|
|
|
|
* https://opensource.org/licenses/MIT
|
2017-06-25 22:31:50 +02:00
|
|
|
*/
|
|
|
|
|
2017-07-09 19:29:18 +02:00
|
|
|
namespace Pterodactyl\Services\Api;
|
2017-06-25 22:31:50 +02:00
|
|
|
|
2017-07-08 21:07:51 +02:00
|
|
|
use Illuminate\Database\ConnectionInterface;
|
2017-06-25 22:31:50 +02: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
|
|
|
{
|
2017-09-14 06:07:02 +02:00
|
|
|
const PUB_CRYPTO_LENGTH = 16;
|
|
|
|
const PRIV_CRYPTO_LENGTH = 64;
|
2017-06-25 22:31:50 +02:00
|
|
|
|
|
|
|
/**
|
2017-07-08 21:07:51 +02:00
|
|
|
* @var \Illuminate\Database\ConnectionInterface
|
2017-06-25 22:31:50 +02:00
|
|
|
*/
|
2017-08-31 04:11:14 +02:00
|
|
|
protected $connection;
|
2017-06-25 22:31:50 +02:00
|
|
|
|
|
|
|
/**
|
2017-07-08 21:07:51 +02:00
|
|
|
* @var \Illuminate\Contracts\Encryption\Encrypter
|
2017-06-25 22:31:50 +02:00
|
|
|
*/
|
2017-07-08 21:07:51 +02:00
|
|
|
protected $encrypter;
|
2017-06-25 22:31:50 +02:00
|
|
|
|
|
|
|
/**
|
2017-07-09 19:29:18 +02:00
|
|
|
* @var \Pterodactyl\Services\Api\PermissionService
|
2017-06-25 22:31:50 +02:00
|
|
|
*/
|
|
|
|
protected $permissionService;
|
|
|
|
|
2017-07-08 21:07:51 +02:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface
|
|
|
|
*/
|
|
|
|
protected $repository;
|
|
|
|
|
2017-06-25 22:31:50 +02:00
|
|
|
/**
|
|
|
|
* ApiKeyService constructor.
|
|
|
|
*
|
2017-07-08 21:07:51 +02:00
|
|
|
* @param \Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface $repository
|
2017-08-31 04:11:14 +02:00
|
|
|
* @param \Illuminate\Database\ConnectionInterface $connection
|
2017-07-08 21:07:51 +02:00
|
|
|
* @param \Illuminate\Contracts\Encryption\Encrypter $encrypter
|
2017-07-09 19:29:18 +02:00
|
|
|
* @param \Pterodactyl\Services\Api\PermissionService $permissionService
|
2017-06-25 22:31:50 +02:00
|
|
|
*/
|
|
|
|
public function __construct(
|
2017-07-08 21:07:51 +02:00
|
|
|
ApiKeyRepositoryInterface $repository,
|
2017-08-31 04:11:14 +02:00
|
|
|
ConnectionInterface $connection,
|
2017-06-25 22:31:50 +02:00
|
|
|
Encrypter $encrypter,
|
2017-07-09 19:29:18 +02:00
|
|
|
PermissionService $permissionService
|
2017-06-25 22:31:50 +02:00
|
|
|
) {
|
2017-07-08 21:07:51 +02:00
|
|
|
$this->repository = $repository;
|
2017-08-31 04:11:14 +02:00
|
|
|
$this->connection = $connection;
|
2017-06-25 22:31:50 +02:00
|
|
|
$this->encrypter = $encrypter;
|
|
|
|
$this->permissionService = $permissionService;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new API Key on the system with the given permissions.
|
|
|
|
*
|
2017-08-22 05:10:48 +02:00
|
|
|
* @param array $data
|
|
|
|
* @param array $permissions
|
|
|
|
* @param array $administrative
|
2017-06-25 22:31:50 +02:00
|
|
|
* @return string
|
|
|
|
*
|
|
|
|
* @throws \Exception
|
|
|
|
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
|
|
|
*/
|
2017-08-31 04:11:14 +02:00
|
|
|
public function handle(array $data, array $permissions, array $administrative = [])
|
2017-06-25 22:31:50 +02:00
|
|
|
{
|
2017-09-14 06:07:02 +02:00
|
|
|
$publicKey = str_random(self::PUB_CRYPTO_LENGTH);
|
|
|
|
$secretKey = str_random(self::PRIV_CRYPTO_LENGTH);
|
2017-06-25 22:31:50 +02:00
|
|
|
|
|
|
|
// Start a Transaction
|
2017-08-31 04:11:14 +02:00
|
|
|
$this->connection->beginTransaction();
|
2017-06-25 22:31:50 +02:00
|
|
|
|
2017-07-08 21:07:51 +02:00
|
|
|
$data = array_merge($data, [
|
|
|
|
'public' => $publicKey,
|
|
|
|
'secret' => $this->encrypter->encrypt($secretKey),
|
|
|
|
]);
|
2017-06-25 22:31:50 +02:00
|
|
|
|
2017-07-08 21:07:51 +02:00
|
|
|
$instance = $this->repository->create($data, true, true);
|
2017-06-25 22:31:50 +02:00
|
|
|
$nodes = $this->permissionService->getPermissions();
|
|
|
|
|
|
|
|
foreach ($permissions as $permission) {
|
2017-11-05 20:42:57 +01:00
|
|
|
@list($block, $search) = explode('-', $permission, 2);
|
2017-06-25 22:31:50 +02:00
|
|
|
|
|
|
|
if (
|
|
|
|
(empty($block) || empty($search)) ||
|
|
|
|
! array_key_exists($block, $nodes['_user']) ||
|
|
|
|
! in_array($search, $nodes['_user'][$block])
|
|
|
|
) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-07-08 21:07:51 +02:00
|
|
|
$this->permissionService->create($instance->id, sprintf('user.%s', $permission));
|
2017-06-25 22:31:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($administrative as $permission) {
|
2017-11-05 20:42:57 +01:00
|
|
|
@list($block, $search) = explode('-', $permission, 2);
|
2017-06-25 22:31:50 +02:00
|
|
|
|
|
|
|
if (
|
|
|
|
(empty($block) || empty($search)) ||
|
|
|
|
! array_key_exists($block, $nodes) ||
|
|
|
|
! in_array($search, $nodes[$block])
|
|
|
|
) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-07-08 21:07:51 +02:00
|
|
|
$this->permissionService->create($instance->id, $permission);
|
2017-06-25 22:31:50 +02:00
|
|
|
}
|
|
|
|
|
2017-08-31 04:11:14 +02:00
|
|
|
$this->connection->commit();
|
2017-06-25 22:31:50 +02:00
|
|
|
|
|
|
|
return $secretKey;
|
|
|
|
}
|
|
|
|
}
|