2015-12-06 13:58:49 -05:00
<? php
2026-10-05 18:10:18 -04:00
declare ( strict_types = 1 );
2016-12-07 22:46:38 +00:00
namespace Pterodactyl\Http\Controllers\Auth ;
2015-12-06 13:58:49 -05:00
2026-10-05 18:10:18 -04:00
use Illuminate\Contracts\View\View ;
use Illuminate\Http\JsonResponse ;
use Illuminate\Http\RedirectResponse ;
2015-12-06 13:58:49 -05:00
use Illuminate\Http\Request ;
2026-10-05 18:10:18 -04:00
use Illuminate\Support\Facades\Auth ;
use Illuminate\Support\Facades\Hash ;
use Illuminate\Validation\ValidationException ;
use Knuckles\Scribe\Attributes\BodyParam ;
use Knuckles\Scribe\Attributes\Endpoint ;
use Knuckles\Scribe\Attributes\Group ;
use Knuckles\Scribe\Attributes\Response as ScribeResponse ;
use Knuckles\Scribe\Attributes\Unauthenticated ;
use Pterodactyl\Contracts\Users\CompletesLogins ;
use Pterodactyl\Exceptions\DisplayException ;
2026-10-06 12:53:25 +01:00
use Pterodactyl\Http\Requests\Auth\LoginRequest ;
2021-09-21 21:30:08 -07:00
use Pterodactyl\Models\User ;
2016-09-03 17:09:00 -04:00
2026-10-05 18:10:18 -04:00
#[ Group ( 'Authentication' , 'Browser session authentication endpoints used by the panel frontend.' , authenticated : false )]
2018-04-07 12:35:15 -05:00
class LoginController extends AbstractLoginController
2015-12-06 13:58:49 -05:00
{
2026-10-05 18:10:18 -04:00
private const array LOGIN_COMPLETE_EXAMPLE = [
'data' => [
'complete' => true ,
'intended' => '/' ,
'user' => [
'uuid' => '9a8b7c6d-5e4f-4321-9876-123456789abc' ,
'username' => 'admin' ,
'email' => 'admin@example.com' ,
'name_first' => 'Admin' ,
'name_last' => 'User' ,
'language' => 'en' ,
'root_admin' => true ,
'use_totp' => false ,
'created_at' => '2026-06-29T12:00:00+00:00' ,
'updated_at' => '2026-06-29T12:00:00+00:00' ,
'identifier' => 'usr_1a2b3c4d' ,
],
],
];
private const array LOGIN_CHECKPOINT_EXAMPLE = [
'data' => [
'complete' => false ,
'confirmation_token' => 'b6e22f85d63c4d9db9739f9ab0a27a48f51f7ad38ef8c12fd157c4c6f4b2e51d' ,
],
];
private const array LOGIN_FAILED_ERROR = [
'errors' => [
[
'code' => 'DisplayException' ,
'status' => '400' ,
'detail' => 'These credentials do not match our records.' ,
],
],
];
private const array CAPTCHA_FAILED_ERROR = [
'errors' => [
[
'code' => 'HttpException' ,
'status' => '400' ,
'detail' => 'Failed to validate reCAPTCHA data.' ,
],
],
];
2018-04-08 15:46:32 -05:00
/**
* Handle all incoming requests for the authentication routes and render the
2022-10-14 10:59:20 -06:00
* base authentication view component. React will take over at this point and
* turn the login area into an SPA.
2018-04-08 15:46:32 -05:00
*/
public function index () : View
{
2025-12-20 15:55:13 -08:00
return view ( 'templates/auth.core' );
2018-04-08 15:46:32 -05:00
}
2026-10-05 18:10:18 -04:00
#[ Endpoint ( 'Logout' , 'Destroys the current browser session and regenerates the CSRF token.' )]
#[ ScribeResponse ( status : 204 , description : 'The browser session was destroyed.' )]
public function logout ( Request $request ) : JsonResponse | RedirectResponse
{
Auth :: guard () -> logout ();
$request -> session () -> invalidate ();
$request -> session () -> regenerateToken ();
return $request -> wantsJson ()
? new JsonResponse ([], JsonResponse :: HTTP_NO_CONTENT )
: new RedirectResponse ( '/' );
}
2015-12-10 21:58:17 -05:00
/**
* Handle a login request to the application.
*
2026-10-05 18:10:18 -04:00
* @throws DisplayException
* @throws ValidationException
2015-12-10 21:58:17 -05:00
*/
2026-10-05 18:10:18 -04:00
#[ Unauthenticated ]
#[ Endpoint ( 'Login' , 'Authenticates a browser session. Returns a complete login payload, or a two-factor checkpoint token when the account requires a second factor.' )]
#[ BodyParam ( 'user' , 'string' , 'The account username or email address.' , required : true , example : 'admin' )]
#[ BodyParam ( 'password' , 'string' , 'The account password.' , required : true , example : 'correct-horse-battery-staple' )]
#[ BodyParam ( 'g-recaptcha-response' , 'string' , 'The reCAPTCHA token when reCAPTCHA is enabled.' , required : false , example : '03AFcWeA...' , nullable : true )]
#[ ScribeResponse ( self :: LOGIN_COMPLETE_EXAMPLE , description : 'Login completed and a browser session was created.' )]
#[ ScribeResponse ( self :: LOGIN_CHECKPOINT_EXAMPLE , description : 'Two-factor checkpoint is required before login can complete.' )]
#[ ScribeResponse ( self :: LOGIN_FAILED_ERROR , status : 400 , description : 'The credentials are invalid.' )]
#[ ScribeResponse ( self :: CAPTCHA_FAILED_ERROR , status : 400 , description : 'The reCAPTCHA token is invalid or missing.' )]
2026-10-06 12:53:25 +01:00
public function login ( LoginRequest $request , CompletesLogins $logins ) : JsonResponse
2015-12-10 21:58:17 -05:00
{
2017-04-14 14:33:15 -04:00
if ( $this -> hasTooManyLoginAttempts ( $request )) {
2016-09-03 17:09:00 -04:00
$this -> fireLockoutEvent ( $request );
2017-12-17 13:07:38 -06:00
$this -> sendLockoutResponse ( $request );
2015-12-10 21:58:17 -05:00
}
2026-10-11 16:17:55 -04:00
$username = $request -> string ( 'user' ) -> toString ();
$password = $request -> string ( 'password' ) -> toString ();
2021-09-21 21:30:08 -07:00
2026-10-05 18:10:18 -04:00
$user = User :: query () -> where ( $this -> getField ( $username ), $username ) -> first ();
2026-10-06 12:53:25 +01:00
if ( $user === null ) {
Hash :: make ( $password );
2021-09-21 21:30:08 -07:00
$this -> sendFailedLoginResponse ( $request );
2015-12-13 21:30:57 -05:00
}
2018-04-07 16:17:51 -05:00
// Ensure that the account is using a valid username and password before trying to
// continue. Previously this was handled in the 2FA checkpoint, however that has
// a flaw in which you can discover if an account exists simply by seeing if you
2022-10-14 10:59:20 -06:00
// can proceed to the next step in the login process.
2026-10-06 12:53:25 +01:00
if ( ! Hash :: check ( $password , $user -> password )) {
2021-09-21 21:30:08 -07:00
$this -> sendFailedLoginResponse ( $request , $user );
2018-04-07 16:17:51 -05:00
}
2026-10-05 18:10:18 -04:00
return $this -> sendLoginResponse ( $logins -> complete ( $user ), $request );
2017-02-01 22:58:48 -05:00
}
2015-12-06 13:58:49 -05:00
}