1111use Illuminate \Foundation \Auth \ThrottlesLogins ;
1212use Pterodactyl \Exceptions \Http \HttpForbiddenException ;
1313use Pterodactyl \Services \Servers \GetUserPermissionsService ;
14- use Symfony \Component \HttpKernel \Exception \NotFoundHttpException ;
14+ use Symfony \Component \HttpKernel \Exception \BadRequestHttpException ;
1515use Pterodactyl \Http \Requests \Api \Remote \SftpAuthenticationFormRequest ;
1616use Symfony \Component \HttpKernel \Exception \TooManyRequestsHttpException ;
1717
@@ -34,8 +34,15 @@ public function __construct(GetUserPermissionsService $permissions)
3434 public function __invoke (SftpAuthenticationFormRequest $ request ): JsonResponse
3535 {
3636 $ connection = $ this ->parseUsername ($ request ->input ('username ' ));
37+ if (empty ($ connection ['server ' ])) {
38+ throw new BadRequestHttpException ('No valid server identifier was included in the request. ' );
39+ }
3740
38- $ this ->validateRequestState ($ request );
41+ if ($ this ->hasTooManyLoginAttempts ($ request )) {
42+ $ seconds = $ this ->limiter ()->availableIn ($ this ->throttleKey ($ request ));
43+
44+ throw new TooManyRequestsHttpException ($ seconds , "Too many login attempts for this account, please try again in {$ seconds } seconds. " );
45+ }
3946
4047 $ user = $ this ->getUser ($ request , $ connection ['username ' ]);
4148 $ server = $ this ->getServer ($ request , $ connection ['server ' ]);
@@ -45,8 +52,8 @@ public function __invoke(SftpAuthenticationFormRequest $request): JsonResponse
4552 $ this ->reject ($ request );
4653 }
4754 } else {
48- if (!$ user ->sshKeys ()->where ('public_key ' , $ request ->input ('password ' ))->exists ()) {
49- $ this ->reject ($ request );
55+ if (!$ user ->sshKeys ()->where ('public_key ' , trim ( $ request ->input ('password ' ) ))->exists ()) {
56+ $ this ->reject ($ request, false );
5057 }
5158 }
5259
@@ -100,29 +107,14 @@ protected function parseUsername(string $value): array
100107 ];
101108 }
102109
103- /**
104- * Checks that the request should not be throttled yet, and that the server was
105- * provided in the username.
106- */
107- protected function validateRequestState (Request $ request ): void
108- {
109- if ($ this ->hasTooManyLoginAttempts ($ request )) {
110- $ seconds = $ this ->limiter ()->availableIn ($ this ->throttleKey ($ request ));
111-
112- throw new TooManyRequestsHttpException ($ seconds , "Too many login attempts for this account, please try again in {$ seconds } seconds. " );
113- }
114-
115- if (empty ($ connection ['server ' ])) {
116- throw new NotFoundHttpException ();
117- }
118- }
119-
120110 /**
121111 * Rejects the request and increments the login attempts.
122112 */
123- protected function reject (Request $ request ): void
113+ protected function reject (Request $ request, bool $ increment = true ): void
124114 {
125- $ this ->incrementLoginAttempts ($ request );
115+ if ($ increment ) {
116+ $ this ->incrementLoginAttempts ($ request );
117+ }
126118
127119 throw new HttpForbiddenException ('Authorization credentials were not correct, please try again. ' );
128120 }
0 commit comments