mirror of
https://github.com/pterodactyl/panel.git
synced 2026-12-05 13:55:44 -05:00
Start 2.0 development
Co-authored-by: Anthony <anthony@infraly.co> Co-authored-by: Sky Mulley <daniel.mulley@outlook.com>
This commit is contained in:
co-authored by
Anthony
Sky Mulley
parent
98079a0166
commit
6c0413e39b
@@ -1,68 +1,107 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Pterodactyl\Http\Controllers\Api\Client;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Response;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
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\Services\Users\TwoFactorSetupService;
|
||||
use Pterodactyl\Services\Users\ToggleTwoFactorService;
|
||||
use Illuminate\Contracts\Validation\Factory as ValidationFactory;
|
||||
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
|
||||
{
|
||||
/**
|
||||
* TwoFactorController constructor.
|
||||
*/
|
||||
public function __construct(
|
||||
private ToggleTwoFactorService $toggleTwoFactorService,
|
||||
private TwoFactorSetupService $setupService,
|
||||
private ValidationFactory $validation,
|
||||
) {
|
||||
parent::__construct();
|
||||
}
|
||||
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.
|
||||
*
|
||||
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
||||
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
||||
*/
|
||||
public function index(Request $request): JsonResponse
|
||||
#[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
|
||||
{
|
||||
if ($request->user()->use_totp) {
|
||||
throw new BadRequestHttpException('Two-factor authentication is already enabled on this account.');
|
||||
}
|
||||
$user = $this->authenticatedUser($request);
|
||||
throw_if($user->use_totp, BadRequestHttpException::class, 'Two-factor authentication is already enabled on this account.');
|
||||
|
||||
return new JsonResponse([
|
||||
'data' => $this->setupService->handle($request->user()),
|
||||
'data' => $twoFactor->setup($user),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates a user's account to have two-factor enabled.
|
||||
*
|
||||
* @throws \Throwable
|
||||
* @throws \Illuminate\Validation\ValidationException
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function store(Request $request): JsonResponse
|
||||
#[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
|
||||
{
|
||||
$validator = $this->validation->make($request->all(), [
|
||||
'code' => ['required', 'string', 'size:6'],
|
||||
'password' => ['required', 'string'],
|
||||
]);
|
||||
$user = $this->authenticatedUser($request);
|
||||
$password = JsonValueGuard::string($request->validated('password'));
|
||||
throw_unless(Hash::check($password, $user->password), InvalidPasswordProvidedException::class, trans('validation.internal.invalid_password'));
|
||||
|
||||
$data = $validator->validate();
|
||||
if (!password_verify($data['password'], $request->user()->password)) {
|
||||
throw new BadRequestHttpException('The password provided was not valid.');
|
||||
}
|
||||
$code = JsonValueGuard::string($request->validated('code'));
|
||||
|
||||
$tokens = $this->toggleTwoFactorService->handle($request->user(), $data['code'], true);
|
||||
$tokens = $twoFactor->toggle($user, $code, true);
|
||||
|
||||
Activity::event('user:two-factor.create')->log();
|
||||
|
||||
@@ -78,21 +117,19 @@ class TwoFactorController extends ClientApiController
|
||||
* Disables two-factor authentication on an account if the password provided
|
||||
* is valid.
|
||||
*
|
||||
* @throws \Throwable
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function delete(Request $request): JsonResponse
|
||||
#[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
|
||||
{
|
||||
if (!password_verify($request->input('password') ?? '', $request->user()->password)) {
|
||||
throw new BadRequestHttpException('The password provided was not valid.');
|
||||
}
|
||||
$user = $this->authenticatedUser($request);
|
||||
$password = JsonValueGuard::string($request->validated('password'));
|
||||
throw_unless(Hash::check($password, $user->password), InvalidPasswordProvidedException::class, trans('validation.internal.invalid_password'));
|
||||
|
||||
/** @var \Pterodactyl\Models\User $user */
|
||||
$user = $request->user();
|
||||
|
||||
$user->update([
|
||||
'totp_authenticated_at' => Carbon::now(),
|
||||
'use_totp' => false,
|
||||
]);
|
||||
$twoFactor->disable($user);
|
||||
|
||||
Activity::event('user:two-factor.delete')->log();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user