Files
pterodactyl-panel/app/Http/Controllers/Auth/ResetPasswordController.php
T

122 lines
4.7 KiB
PHP
Raw Normal View History

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;
use Illuminate\Http\JsonResponse;
2026-10-05 18:10:18 -04:00
use Illuminate\Support\Facades\Auth;
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;
use Pterodactyl\Events\User\PasswordChanged;
use Pterodactyl\Exceptions\DisplayException;
2016-09-03 17:09:00 -04:00
use Pterodactyl\Http\Controllers\Controller;
use Pterodactyl\Http\Requests\Auth\ResetPasswordRequest;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Models\User;
use Pterodactyl\Support\JsonValueGuard;
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
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;
2017-03-19 19:36:50 -04: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
*/
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')]
#[ScribeResponse(self::RESET_COMPLETE_EXAMPLE, description: 'Password reset completed.')]
#[ScribeResponse(self::RESET_FAILED_ERROR, status: 400, description: 'The reset token is invalid or expired.')]
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 {
$this->resetPassword($user, $password);
}
);
if ($response === Password::PASSWORD_RESET) {
return $this->sendResetResponse();
}
2026-10-05 18:10:18 -04:00
throw new DisplayException(trans(JsonValueGuard::nullableString($response)));
}
/**
* 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
*/
2026-10-05 18:10:18 -04:00
protected function resetPassword($user, $password): void // @pest-ignore-type
{
2026-10-05 18:10:18 -04:00
$user->forceFill([
'password' => $password,
$user->getRememberTokenName() => Str::random(60),
2026-10-05 18:10:18 -04:00
])->save();
2026-10-05 18:10:18 -04:00
event(new PasswordReset($user));
event(new PasswordChanged($user));
// 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);
}
$this->hasTwoFactor = $user->use_totp;
}
/**
* Send a successful password reset response back to the callee.
2017-03-19 19:36:50 -04:00
*/
protected function sendResetResponse(): JsonResponse
2016-12-07 22:46:38 +00:00
{
return response()->json([
'success' => true,
'redirect_to' => $this->redirectTo,
2018-07-04 18:09:07 -07:00
'send_to_login' => $this->hasTwoFactor,
]);
}
2016-09-03 17:09:00 -04:00
}