2017-10-25 04:35:25 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Services\Sftp;
|
|
|
|
|
|
|
|
use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
|
|
|
|
use Pterodactyl\Services\DaemonKeys\DaemonKeyProviderService;
|
|
|
|
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
|
|
|
|
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
|
2018-01-31 04:40:21 +00:00
|
|
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
2017-10-25 04:35:25 +00:00
|
|
|
|
|
|
|
class AuthenticateUsingPasswordService
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Services\DaemonKeys\DaemonKeyProviderService
|
|
|
|
*/
|
|
|
|
private $keyProviderService;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface
|
|
|
|
*/
|
|
|
|
private $repository;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\UserRepositoryInterface
|
|
|
|
*/
|
|
|
|
private $userRepository;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* AuthenticateUsingPasswordService constructor.
|
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Services\DaemonKeys\DaemonKeyProviderService $keyProviderService
|
|
|
|
* @param \Pterodactyl\Contracts\Repository\ServerRepositoryInterface $repository
|
|
|
|
* @param \Pterodactyl\Contracts\Repository\UserRepositoryInterface $userRepository
|
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
DaemonKeyProviderService $keyProviderService,
|
|
|
|
ServerRepositoryInterface $repository,
|
|
|
|
UserRepositoryInterface $userRepository
|
|
|
|
) {
|
|
|
|
$this->keyProviderService = $keyProviderService;
|
|
|
|
$this->repository = $repository;
|
|
|
|
$this->userRepository = $userRepository;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Attempt to authenticate a provded username and password and determine if they
|
|
|
|
* have permission to access a given server. This function does not account for
|
|
|
|
* subusers currently. Only administrators and server owners can login to access
|
|
|
|
* their files at this time.
|
|
|
|
*
|
|
|
|
* Server must exist on the node that the API call is being made from in order for a
|
|
|
|
* valid response to be provided.
|
|
|
|
*
|
|
|
|
* @param string $username
|
|
|
|
* @param string $password
|
|
|
|
* @param int $node
|
2018-01-31 04:40:21 +00:00
|
|
|
* @param string|null $server
|
2017-10-25 04:35:25 +00:00
|
|
|
* @return array
|
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
|
|
|
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
2018-01-31 04:40:21 +00:00
|
|
|
* @throws \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
|
2017-10-25 04:35:25 +00:00
|
|
|
*/
|
|
|
|
public function handle(string $username, string $password, int $node, string $server = null): array
|
|
|
|
{
|
|
|
|
if (is_null($server)) {
|
|
|
|
throw new RecordNotFoundException;
|
|
|
|
}
|
|
|
|
|
2018-01-31 04:40:21 +00:00
|
|
|
$user = $this->userRepository->setColumns(['id', 'root_admin', 'password'])->findFirstWhere([['username', '=', $username]]);
|
|
|
|
if (! password_verify($password, $user->password)) {
|
|
|
|
throw new RecordNotFoundException;
|
2017-10-25 04:35:25 +00:00
|
|
|
}
|
|
|
|
|
2018-01-31 04:40:21 +00:00
|
|
|
$server = $this->repository->setColumns(['id', 'node_id', 'owner_id', 'uuid', 'installed', 'suspended'])->getByUuid($server);
|
2017-10-25 04:35:25 +00:00
|
|
|
if ($server->node_id !== $node || (! $user->root_admin && $server->owner_id !== $user->id)) {
|
|
|
|
throw new RecordNotFoundException;
|
|
|
|
}
|
|
|
|
|
2018-01-31 04:40:21 +00:00
|
|
|
if ($server->installed !== 1 || $server->suspended) {
|
|
|
|
throw new BadRequestHttpException;
|
|
|
|
}
|
|
|
|
|
2017-10-25 04:35:25 +00:00
|
|
|
return [
|
|
|
|
'server' => $server->uuid,
|
2017-11-05 18:38:39 +00:00
|
|
|
'token' => $this->keyProviderService->handle($server, $user),
|
2017-10-25 04:35:25 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|