|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Pterodactyl\Http\Controllers\API\Remote; |
| 4 | + |
| 5 | +use Illuminate\Http\Request; |
| 6 | +use Illuminate\Http\JsonResponse; |
| 7 | +use Illuminate\Auth\AuthenticationException; |
| 8 | +use Pterodactyl\Http\Controllers\Controller; |
| 9 | +use Illuminate\Foundation\Auth\ThrottlesLogins; |
| 10 | +use Pterodactyl\Exceptions\Repository\RecordNotFoundException; |
| 11 | +use Pterodactyl\Services\Sftp\AuthenticateUsingPasswordService; |
| 12 | +use Pterodactyl\Http\Requests\API\Remote\SftpAuthenticationFormRequest; |
| 13 | + |
| 14 | +class SftpController extends Controller |
| 15 | +{ |
| 16 | + use ThrottlesLogins; |
| 17 | + |
| 18 | + /** |
| 19 | + * @var \Pterodactyl\Services\Sftp\AuthenticateUsingPasswordService |
| 20 | + */ |
| 21 | + private $authenticationService; |
| 22 | + |
| 23 | + /** |
| 24 | + * SftpController constructor. |
| 25 | + * |
| 26 | + * @param \Pterodactyl\Services\Sftp\AuthenticateUsingPasswordService $authenticationService |
| 27 | + */ |
| 28 | + public function __construct(AuthenticateUsingPasswordService $authenticationService) |
| 29 | + { |
| 30 | + $this->authenticationService = $authenticationService; |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Authenticate a set of credentials and return the associated server details |
| 35 | + * for a SFTP connection on the daemon. |
| 36 | + * |
| 37 | + * @param \Pterodactyl\Http\Requests\API\Remote\SftpAuthenticationFormRequest $request |
| 38 | + * @return \Illuminate\Http\JsonResponse |
| 39 | + * |
| 40 | + * @throws \Pterodactyl\Exceptions\Model\DataValidationException |
| 41 | + */ |
| 42 | + public function index(SftpAuthenticationFormRequest $request): JsonResponse |
| 43 | + { |
| 44 | + $connection = explode('.', $request->input('username')); |
| 45 | + $this->incrementLoginAttempts($request); |
| 46 | + |
| 47 | + if ($this->hasTooManyLoginAttempts($request)) { |
| 48 | + return response()->json([ |
| 49 | + 'error' => 'Logins throttled.', |
| 50 | + ], 429); |
| 51 | + } |
| 52 | + |
| 53 | + try { |
| 54 | + $data = $this->authenticationService->handle( |
| 55 | + array_get($connection, 0), |
| 56 | + $request->input('password'), |
| 57 | + object_get($request->attributes->get('node'), 'id', 0), |
| 58 | + array_get($connection, 1) |
| 59 | + ); |
| 60 | + |
| 61 | + $this->clearLoginAttempts($request); |
| 62 | + } catch (AuthenticationException $exception) { |
| 63 | + return response()->json([ |
| 64 | + 'error' => 'Invalid credentials.', |
| 65 | + ], 403); |
| 66 | + } catch (RecordNotFoundException $exception) { |
| 67 | + return response()->json([ |
| 68 | + 'error' => 'Invalid server.', |
| 69 | + ], 404); |
| 70 | + } |
| 71 | + |
| 72 | + return response()->json($data); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Get the throttle key for the given request. |
| 77 | + * |
| 78 | + * @param \Illuminate\Http\Request $request |
| 79 | + * @return string |
| 80 | + */ |
| 81 | + protected function throttleKey(Request $request) |
| 82 | + { |
| 83 | + return strtolower(array_get(explode('.', $request->input('username')), 0) . '|' . $request->ip()); |
| 84 | + } |
| 85 | +} |
0 commit comments