2016-09-03 17:09:00 -04:00
<? php
2026-10-05 18:10:18 -04:00
declare ( strict_types = 1 );
2016-09-03 17:09:00 -04:00
namespace Pterodactyl\Http\Controllers\Auth ;
2026-10-05 18:10:18 -04:00
use Illuminate\Auth\Events\PasswordReset ;
use Illuminate\Contracts\Auth\CanResetPassword ;
2018-04-08 15:18:13 -05:00
use Illuminate\Http\JsonResponse ;
2026-10-05 18:10:18 -04:00
use Illuminate\Support\Facades\Auth ;
2018-04-08 15:18:13 -05:00
use Illuminate\Support\Facades\Password ;
2026-10-05 18:10:18 -04:00
use Illuminate\Support\Str ;
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 ;
2026-02-14 10:51:26 -08:00
use Pterodactyl\Events\User\PasswordChanged ;
2018-04-08 15:18:13 -05:00
use Pterodactyl\Exceptions\DisplayException ;
2016-09-03 17:09:00 -04:00
use Pterodactyl\Http\Controllers\Controller ;
2018-04-08 15:18:13 -05:00
use Pterodactyl\Http\Requests\Auth\ResetPasswordRequest ;
2026-10-05 18:10:18 -04: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 )]
2016-09-03 17:09:00 -04:00
class ResetPasswordController extends Controller
{
2026-10-05 18:10:18 -04:00
private const array RESET_COMPLETE_EXAMPLE = [
'success' => true ,
'redirect_to' => '/' ,
'send_to_login' => false ,
];
private const array RESET_FAILED_ERROR = [
'errors' => [
[
'code' => 'DisplayException' ,
'status' => '400' ,
'detail' => 'This password reset token is invalid.' ,
],
],
];
2016-09-03 17:09:00 -04:00
2026-10-06 13:13:39 +01:00
private const array CAPTCHA_FAILED_ERROR = [
'errors' => [
[
'code' => 'HttpException' ,
'status' => '400' ,
'detail' => 'Failed to validate reCAPTCHA data.' ,
],
],
];
2017-03-19 19:36:50 -04:00
/**
* The URL to redirect users to after password reset.
*/
2022-10-14 10:59:20 -06:00
public string $redirectTo = '/' ;
2016-09-04 19:08:46 -04:00
2022-10-14 10:59:20 -06:00
protected bool $hasTwoFactor = false ;
2018-07-04 11:41:56 -07:00
2017-03-19 19:36:50 -04:00
/**
2018-04-08 15:18:13 -05:00
* Reset the given user's password.
2017-03-19 19:36:50 -04:00
*
2024-10-21 19:18:20 -06:00
* @throws DisplayException
2018-04-08 15:18:13 -05:00
*/
2026-10-05 18:10:18 -04:00
#[ Unauthenticated ]
#[ Endpoint ( 'Reset password' , 'Resets an account password using a password reset token. The response tells the frontend whether the user must return to the login form.' )]
#[ BodyParam ( 'email' , 'string' , 'The account email address.' , required : true , example : 'admin@example.com' )]
#[ BodyParam ( 'token' , 'string' , 'The password reset token.' , required : true , example : 'b6e22f85d63c4d9db9739f9ab0a27a48f51f7ad38ef8c12fd157c4c6f4b2e51d' )]
#[ BodyParam ( 'password' , 'string' , 'The new account password. Must be at least eight characters.' , required : true , example : 'correct-horse-battery-staple' )]
#[ BodyParam ( 'password_confirmation' , 'string' , 'Confirmation matching the new account password.' , required : true , example : 'correct-horse-battery-staple' )]
2026-10-06 13:13:39 +01:00
#[ BodyParam ( 'g-recaptcha-response' , 'string' , 'The reCAPTCHA token when reCAPTCHA is enabled.' , required : false , example : '03AFcWeA...' , nullable : true )]
2026-10-05 18:10:18 -04:00
#[ ScribeResponse ( self :: RESET_COMPLETE_EXAMPLE , description : 'Password reset completed.' )]
#[ ScribeResponse ( self :: RESET_FAILED_ERROR , status : 400 , description : 'The reset token is invalid or expired.' )]
2026-10-06 13:13:39 +01:00
#[ ScribeResponse ( self :: CAPTCHA_FAILED_ERROR , status : 400 , description : 'The reCAPTCHA token is invalid or missing.' )]
2018-04-08 15:18:13 -05:00
public function __invoke ( ResetPasswordRequest $request ) : JsonResponse
{
// Here we will attempt to reset the user's password. If it is successful we
// will update the password on an actual user model and persist it to the
2022-10-14 10:59:20 -06:00
// database. Otherwise, we will parse the error and return the response.
2026-10-05 18:10:18 -04:00
$response = Password :: broker () -> reset (
$request -> only ( 'email' , 'password' , 'password_confirmation' , 'token' ),
function ( User $user , string $password ) : void {
2018-04-08 15:18:13 -05:00
$this -> resetPassword ( $user , $password );
}
);
if ( $response === Password :: PASSWORD_RESET ) {
return $this -> sendResetResponse ();
}
2026-10-06 13:13:39 +01:00
throw new DisplayException ( trans ( 'passwords.token' ));
2018-04-08 15:18:13 -05:00
}
2018-07-04 11:41:56 -07:00
/**
* Reset the given user's password. If the user has two-factor authentication enabled on their
* account do not automatically log them in. In those cases, send the user back to the login
* form with a note telling them their password was changed and to log back in.
*
2026-10-05 18:10:18 -04:00
* @param CanResetPassword&User $user
* @param string $password
2018-07-04 11:41:56 -07:00
*/
2026-10-05 18:10:18 -04:00
protected function resetPassword ( $user , $password ) : void // @pest-ignore-type
2018-07-04 11:41:56 -07:00
{
2026-10-05 18:10:18 -04:00
$user -> forceFill ([
'password' => $password ,
2018-07-04 11:41:56 -07:00
$user -> getRememberTokenName () => Str :: random ( 60 ),
2026-10-05 18:10:18 -04:00
]) -> save ();
2018-07-04 11:41:56 -07:00
2026-10-05 18:10:18 -04:00
event ( new PasswordReset ( $user ));
event ( new PasswordChanged ( $user ));
2018-07-04 11:41:56 -07:00
// If the user is not using 2FA log them in, otherwise skip this step and force a
// fresh login where they'll be prompted to enter a token.
2026-10-05 18:10:18 -04:00
if ( ! $user -> use_totp ) {
Auth :: guard () -> login ( $user );
2026-10-07 22:24:12 +05:45
2026-10-08 09:54:19 +01:00
Auth :: guard () -> logoutOtherDevices ( $password );
2018-07-04 11:41:56 -07:00
}
$this -> hasTwoFactor = $user -> use_totp ;
}
2018-04-08 15:18:13 -05:00
/**
* Send a successful password reset response back to the callee.
2017-03-19 19:36:50 -04:00
*/
2018-04-08 15:18:13 -05:00
protected function sendResetResponse () : JsonResponse
2016-12-07 22:46:38 +00:00
{
2018-04-08 15:18:13 -05:00
return response () -> json ([
'success' => true ,
'redirect_to' => $this -> redirectTo ,
2018-07-04 18:09:07 -07:00
'send_to_login' => $this -> hasTwoFactor ,
2018-04-08 15:18:13 -05:00
]);
2016-10-30 21:02:39 +01:00
}
2016-09-03 17:09:00 -04:00
}