2017-10-25 04:35:25 +00:00
|
|
|
<?php
|
|
|
|
|
2018-01-20 01:58:57 +00:00
|
|
|
namespace Pterodactyl\Http\Controllers\Api\Remote;
|
2017-10-25 04:35:25 +00:00
|
|
|
|
|
|
|
use Illuminate\Http\Request;
|
2018-01-31 04:40:21 +00:00
|
|
|
use Illuminate\Http\Response;
|
2017-10-25 04:35:25 +00:00
|
|
|
use Illuminate\Http\JsonResponse;
|
|
|
|
use Pterodactyl\Http\Controllers\Controller;
|
|
|
|
use Illuminate\Foundation\Auth\ThrottlesLogins;
|
|
|
|
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
|
|
|
|
use Pterodactyl\Services\Sftp\AuthenticateUsingPasswordService;
|
2018-01-31 04:40:21 +00:00
|
|
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
2018-01-20 02:01:56 +00:00
|
|
|
use Pterodactyl\Http\Requests\Api\Remote\SftpAuthenticationFormRequest;
|
2017-10-25 04:35:25 +00:00
|
|
|
|
|
|
|
class SftpController extends Controller
|
|
|
|
{
|
|
|
|
use ThrottlesLogins;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Services\Sftp\AuthenticateUsingPasswordService
|
|
|
|
*/
|
|
|
|
private $authenticationService;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SftpController constructor.
|
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Services\Sftp\AuthenticateUsingPasswordService $authenticationService
|
|
|
|
*/
|
|
|
|
public function __construct(AuthenticateUsingPasswordService $authenticationService)
|
|
|
|
{
|
|
|
|
$this->authenticationService = $authenticationService;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Authenticate a set of credentials and return the associated server details
|
|
|
|
* for a SFTP connection on the daemon.
|
|
|
|
*
|
2018-01-20 02:01:56 +00:00
|
|
|
* @param \Pterodactyl\Http\Requests\Api\Remote\SftpAuthenticationFormRequest $request
|
2017-10-25 04:35:25 +00:00
|
|
|
* @return \Illuminate\Http\JsonResponse
|
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
|
|
|
*/
|
|
|
|
public function index(SftpAuthenticationFormRequest $request): JsonResponse
|
|
|
|
{
|
2018-02-11 22:39:50 +00:00
|
|
|
$parts = explode('.', strrev($request->input('username')), 2);
|
|
|
|
$connection = [
|
|
|
|
'username' => strrev(array_get($parts, 1)),
|
|
|
|
'server' => strrev(array_get($parts, 0)),
|
|
|
|
];
|
|
|
|
|
2017-10-25 04:35:25 +00:00
|
|
|
$this->incrementLoginAttempts($request);
|
|
|
|
|
|
|
|
if ($this->hasTooManyLoginAttempts($request)) {
|
|
|
|
return response()->json([
|
|
|
|
'error' => 'Logins throttled.',
|
2018-01-31 04:40:21 +00:00
|
|
|
], Response::HTTP_TOO_MANY_REQUESTS);
|
2017-10-25 04:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$data = $this->authenticationService->handle(
|
2018-02-11 22:39:50 +00:00
|
|
|
$connection['username'],
|
2017-10-25 04:35:25 +00:00
|
|
|
$request->input('password'),
|
|
|
|
object_get($request->attributes->get('node'), 'id', 0),
|
2018-02-11 22:39:50 +00:00
|
|
|
empty($connection['server']) ? null : $connection['server']
|
2017-10-25 04:35:25 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->clearLoginAttempts($request);
|
2018-01-31 04:40:21 +00:00
|
|
|
} catch (BadRequestHttpException $exception) {
|
2017-10-25 04:35:25 +00:00
|
|
|
return response()->json([
|
2018-01-31 04:40:21 +00:00
|
|
|
'error' => 'The server you are trying to access is not installed or is suspended.',
|
|
|
|
], Response::HTTP_BAD_REQUEST);
|
2017-10-25 04:35:25 +00:00
|
|
|
} catch (RecordNotFoundException $exception) {
|
|
|
|
return response()->json([
|
2018-01-31 04:40:21 +00:00
|
|
|
'error' => 'Unable to locate a resource using the username and password provided.',
|
|
|
|
], Response::HTTP_NOT_FOUND);
|
2017-10-25 04:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return response()->json($data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the throttle key for the given request.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function throttleKey(Request $request)
|
|
|
|
{
|
|
|
|
return strtolower(array_get(explode('.', $request->input('username')), 0) . '|' . $request->ip());
|
|
|
|
}
|
|
|
|
}
|