mirror of
https://github.com/pterodactyl/panel.git
synced 2026-11-30 11:25:52 -05:00
Co-authored-by: Anthony <anthony@infraly.co> Co-authored-by: Sky Mulley <daniel.mulley@outlook.com>
139 lines
5.8 KiB
PHP
139 lines
5.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Pterodactyl\Http\Controllers\Api\Client;
|
|
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\Response;
|
|
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;
|
|
use Pterodactyl\Facades\Activity;
|
|
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;
|
|
use Throwable;
|
|
|
|
#[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
|
|
{
|
|
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.
|
|
*/
|
|
#[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
|
|
{
|
|
$user = $this->authenticatedUser($request);
|
|
throw_if($user->use_totp, BadRequestHttpException::class, 'Two-factor authentication is already enabled on this account.');
|
|
|
|
return new JsonResponse([
|
|
'data' => $twoFactor->setup($user),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Updates a user's account to have two-factor enabled.
|
|
*
|
|
* @throws Throwable
|
|
*/
|
|
#[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
|
|
{
|
|
$user = $this->authenticatedUser($request);
|
|
$password = JsonValueGuard::string($request->validated('password'));
|
|
throw_unless(Hash::check($password, $user->password), InvalidPasswordProvidedException::class, trans('validation.internal.invalid_password'));
|
|
|
|
$code = JsonValueGuard::string($request->validated('code'));
|
|
|
|
$tokens = $twoFactor->toggle($user, $code, true);
|
|
|
|
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.
|
|
*
|
|
* @throws Throwable
|
|
*/
|
|
#[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
|
|
{
|
|
$user = $this->authenticatedUser($request);
|
|
$password = JsonValueGuard::string($request->validated('password'));
|
|
throw_unless(Hash::check($password, $user->password), InvalidPasswordProvidedException::class, trans('validation.internal.invalid_password'));
|
|
|
|
$twoFactor->disable($user);
|
|
|
|
Activity::event('user:two-factor.delete')->log();
|
|
|
|
return new JsonResponse([], Response::HTTP_NO_CONTENT);
|
|
}
|
|
}
|