Files
pterodactyl-panel/app/Http/Controllers/Api/Client/TwoFactorController.php
T

139 lines
5.8 KiB
PHP
Raw Normal View History

<?php
2026-10-05 18:10:18 -04:00
declare(strict_types=1);
namespace Pterodactyl\Http\Controllers\Api\Client;
2026-10-05 18:10:18 -04:00
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
2026-10-05 18:10:18 -04:00
use Illuminate\Support\Facades\Hash;
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\Subgroup;
use Pterodactyl\Contracts\Users\DisablesTwoFactor;
use Pterodactyl\Contracts\Users\SetsUpTwoFactor;
use Pterodactyl\Contracts\Users\TogglesTwoFactor;
use Pterodactyl\Exceptions\Http\Base\InvalidPasswordProvidedException;
2022-05-29 18:48:35 -04:00
use Pterodactyl\Facades\Activity;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Http\Requests\Api\Client\Account\DisableTwoFactorRequest;
use Pterodactyl\Http\Requests\Api\Client\Account\EnableTwoFactorRequest;
use Pterodactyl\Support\JsonValueGuard;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
2026-10-05 18:10:18 -04:00
use Throwable;
2026-10-05 18:10:18 -04:00
#[Group('Client API', 'Endpoints authenticated as a panel user using a client API token.')]
#[Subgroup('Two-Factor Authentication', 'Configure two-factor authentication for the authenticated user.')]
class TwoFactorController extends ClientApiController
{
2026-10-05 18:10:18 -04:00
private const array TWO_FACTOR_SETUP_EXAMPLE = [
'data' => [
'image_url_data' => 'otpauth://totp/Pterodactyl:user@example.com?secret=EXAMPLESECRET&issuer=Pterodactyl',
'secret' => 'EXAMPLESECRET',
],
];
private const array RECOVERY_TOKENS_EXAMPLE = [
'object' => 'recovery_tokens',
'attributes' => [
'tokens' => [
'r1a2b3c4d5',
'r6e7f8g9h0',
],
],
];
private const array BAD_REQUEST_ERROR = [
'errors' => [
[
'code' => 'InvalidPasswordProvidedException',
'status' => '400',
'detail' => 'The password provided was invalid for this account.',
],
],
];
private const array ALREADY_ENABLED_ERROR = [
'errors' => [
[
'code' => 'BadRequestHttpException',
'status' => '400',
'detail' => 'Two-factor authentication is already enabled on this account.',
],
],
];
/**
* Returns two-factor token credentials that allow a user to configure
* it on their account. If two-factor is already enabled this endpoint
* will return a 400 error.
*/
2026-10-05 18:10:18 -04:00
#[Endpoint('Get two-factor setup', 'Creates or returns the current two-factor setup payload for the authenticated user.')]
#[ScribeResponse(self::TWO_FACTOR_SETUP_EXAMPLE, description: 'Two-factor setup data returned.')]
#[ScribeResponse(self::ALREADY_ENABLED_ERROR, status: 400, description: 'Two-factor authentication is already enabled.')]
public function index(Request $request, SetsUpTwoFactor $twoFactor): JsonResponse
{
2026-10-05 18:10:18 -04:00
$user = $this->authenticatedUser($request);
throw_if($user->use_totp, BadRequestHttpException::class, 'Two-factor authentication is already enabled on this account.');
2020-06-27 11:06:35 -07:00
return new JsonResponse([
2026-10-05 18:10:18 -04:00
'data' => $twoFactor->setup($user),
]);
}
/**
* Updates a user's account to have two-factor enabled.
*
2026-10-05 18:10:18 -04:00
* @throws Throwable
*/
2026-10-05 18:10:18 -04:00
#[Endpoint('Enable two-factor authentication', 'Enables two-factor authentication for the authenticated user and returns one-time recovery tokens.')]
#[BodyParam('code', 'string', 'The six-digit TOTP code generated by the authenticator app.', required: true, example: '123456')]
#[BodyParam('password', 'string', 'The current account password.', required: true, example: 'current-password')]
#[ScribeResponse(self::RECOVERY_TOKENS_EXAMPLE, description: 'Two-factor authentication enabled. Store these recovery tokens now; they are not shown again.')]
#[ScribeResponse(self::BAD_REQUEST_ERROR, status: 400, description: 'The password or TOTP code is invalid.')]
public function store(EnableTwoFactorRequest $request, TogglesTwoFactor $twoFactor): JsonResponse
{
2026-10-05 18:10:18 -04:00
$user = $this->authenticatedUser($request);
$password = JsonValueGuard::string($request->validated('password'));
throw_unless(Hash::check($password, $user->password), InvalidPasswordProvidedException::class, trans('validation.internal.invalid_password'));
2026-10-05 18:10:18 -04:00
$code = JsonValueGuard::string($request->validated('code'));
2026-10-05 18:10:18 -04:00
$tokens = $twoFactor->toggle($user, $code, true);
2022-05-29 18:48:35 -04:00
Activity::event('user:two-factor.create')->log();
return new JsonResponse([
'object' => 'recovery_tokens',
'attributes' => [
'tokens' => $tokens,
],
]);
}
/**
* Disables two-factor authentication on an account if the password provided
* is valid.
*
2026-10-05 18:10:18 -04:00
* @throws Throwable
*/
2026-10-05 18:10:18 -04:00
#[Endpoint('Disable two-factor authentication', 'Disables two-factor authentication for the authenticated user after verifying their password.')]
#[BodyParam('password', 'string', 'The current account password.', required: true, example: 'current-password')]
#[ScribeResponse(status: 204, description: 'Two-factor authentication disabled.')]
#[ScribeResponse(self::BAD_REQUEST_ERROR, status: 400, description: 'The password is invalid.')]
public function delete(DisableTwoFactorRequest $request, DisablesTwoFactor $twoFactor): JsonResponse
{
2026-10-05 18:10:18 -04:00
$user = $this->authenticatedUser($request);
$password = JsonValueGuard::string($request->validated('password'));
throw_unless(Hash::check($password, $user->password), InvalidPasswordProvidedException::class, trans('validation.internal.invalid_password'));
2026-10-05 18:10:18 -04:00
$twoFactor->disable($user);
2022-05-29 18:48:35 -04:00
Activity::event('user:two-factor.delete')->log();
2020-06-27 11:06:35 -07:00
return new JsonResponse([], Response::HTTP_NO_CONTENT);
}
}