2017-09-25 02:12:30 +00:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* Pterodactyl - Panel
|
|
|
|
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
* copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
* SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Pterodactyl\Services\DaemonKeys;
|
|
|
|
|
|
|
|
use Carbon\Carbon;
|
2017-11-05 18:38:39 +00:00
|
|
|
use Pterodactyl\Models\User;
|
|
|
|
use Pterodactyl\Models\Server;
|
2017-11-05 22:07:50 +00:00
|
|
|
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
|
2018-03-04 03:31:44 +00:00
|
|
|
use Pterodactyl\Contracts\Repository\SubuserRepositoryInterface;
|
2017-09-25 02:12:30 +00:00
|
|
|
use Pterodactyl\Contracts\Repository\DaemonKeyRepositoryInterface;
|
|
|
|
|
|
|
|
class DaemonKeyProviderService
|
|
|
|
{
|
2017-11-05 22:07:50 +00:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Services\DaemonKeys\DaemonKeyCreationService
|
|
|
|
*/
|
|
|
|
private $keyCreationService;
|
|
|
|
|
2017-09-25 02:12:30 +00:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Services\DaemonKeys\DaemonKeyUpdateService
|
|
|
|
*/
|
2017-11-05 18:38:39 +00:00
|
|
|
private $keyUpdateService;
|
2017-09-25 02:12:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\DaemonKeyRepositoryInterface
|
|
|
|
*/
|
2017-11-05 18:38:39 +00:00
|
|
|
private $repository;
|
2017-09-25 02:12:30 +00:00
|
|
|
|
2018-03-04 03:31:44 +00:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\SubuserRepositoryInterface
|
|
|
|
*/
|
|
|
|
private $subuserRepository;
|
|
|
|
|
2017-09-25 02:12:30 +00:00
|
|
|
/**
|
|
|
|
* GetDaemonKeyService constructor.
|
|
|
|
*
|
2019-09-06 04:32:57 +00:00
|
|
|
* @param \Pterodactyl\Services\DaemonKeys\DaemonKeyCreationService $keyCreationService
|
2017-09-25 02:12:30 +00:00
|
|
|
* @param \Pterodactyl\Contracts\Repository\DaemonKeyRepositoryInterface $repository
|
2019-09-06 04:32:57 +00:00
|
|
|
* @param \Pterodactyl\Services\DaemonKeys\DaemonKeyUpdateService $keyUpdateService
|
|
|
|
* @param \Pterodactyl\Contracts\Repository\SubuserRepositoryInterface $subuserRepository
|
2017-09-25 02:12:30 +00:00
|
|
|
*/
|
2017-11-05 22:07:50 +00:00
|
|
|
public function __construct(
|
|
|
|
DaemonKeyCreationService $keyCreationService,
|
|
|
|
DaemonKeyRepositoryInterface $repository,
|
2018-03-04 03:31:44 +00:00
|
|
|
DaemonKeyUpdateService $keyUpdateService,
|
|
|
|
SubuserRepositoryInterface $subuserRepository
|
2017-11-05 22:07:50 +00:00
|
|
|
) {
|
|
|
|
$this->keyCreationService = $keyCreationService;
|
2017-09-25 02:12:30 +00:00
|
|
|
$this->keyUpdateService = $keyUpdateService;
|
|
|
|
$this->repository = $repository;
|
2018-03-04 03:31:44 +00:00
|
|
|
$this->subuserRepository = $subuserRepository;
|
2017-09-25 02:12:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the access key for a user on a specific server.
|
|
|
|
*
|
2017-11-05 18:38:39 +00:00
|
|
|
* @param \Pterodactyl\Models\Server $server
|
2019-09-06 04:32:57 +00:00
|
|
|
* @param \Pterodactyl\Models\User $user
|
|
|
|
* @param bool $updateIfExpired
|
2017-09-25 02:12:30 +00:00
|
|
|
* @return string
|
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
|
|
|
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
|
|
|
*/
|
2017-11-05 18:38:39 +00:00
|
|
|
public function handle(Server $server, User $user, $updateIfExpired = true): string
|
2017-09-25 02:12:30 +00:00
|
|
|
{
|
2017-11-05 22:07:50 +00:00
|
|
|
try {
|
|
|
|
$key = $this->repository->findFirstWhere([
|
|
|
|
['user_id', '=', $user->id],
|
|
|
|
['server_id', '=', $server->id],
|
|
|
|
]);
|
|
|
|
} catch (RecordNotFoundException $exception) {
|
|
|
|
// If key doesn't exist but we are an admin or the server owner,
|
|
|
|
// create it.
|
|
|
|
if ($user->root_admin || $user->id === $server->owner_id) {
|
|
|
|
return $this->keyCreationService->handle($server->id, $user->id);
|
|
|
|
}
|
2017-09-25 02:12:30 +00:00
|
|
|
|
2018-03-04 03:31:44 +00:00
|
|
|
// Check if user is a subuser for this server. Ideally they should always have
|
|
|
|
// a record associated with them in the database, but we should still handle
|
|
|
|
// that potentiality here.
|
|
|
|
//
|
|
|
|
// If no subuser is found, a RecordNotFoundException will be thrown, thus handling
|
|
|
|
// the parent error as well.
|
|
|
|
$subuser = $this->subuserRepository->findFirstWhere([
|
|
|
|
['user_id', '=', $user->id],
|
|
|
|
['server_id', '=', $server->id],
|
|
|
|
]);
|
|
|
|
|
|
|
|
return $this->keyCreationService->handle($subuser->server_id, $subuser->user_id);
|
2017-11-05 22:07:50 +00:00
|
|
|
}
|
2017-09-25 02:12:30 +00:00
|
|
|
|
2017-11-05 18:38:39 +00:00
|
|
|
if (! $updateIfExpired || Carbon::now()->diffInSeconds($key->expires_at, false) > 0) {
|
2017-09-25 02:12:30 +00:00
|
|
|
return $key->secret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->keyUpdateService->handle($key->id);
|
|
|
|
}
|
|
|
|
}
|