Path: blob/1.0-develop/app/Http/Controllers/Api/Client/TwoFactorController.php
10279 views
<?php12namespace Pterodactyl\Http\Controllers\Api\Client;34use Carbon\Carbon;5use Illuminate\Http\Request;6use Illuminate\Http\Response;7use Illuminate\Http\JsonResponse;8use Pterodactyl\Facades\Activity;9use Pterodactyl\Services\Users\TwoFactorSetupService;10use Pterodactyl\Services\Users\ToggleTwoFactorService;11use Illuminate\Contracts\Validation\Factory as ValidationFactory;12use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;1314class TwoFactorController extends ClientApiController15{16/**17* TwoFactorController constructor.18*/19public function __construct(20private ToggleTwoFactorService $toggleTwoFactorService,21private TwoFactorSetupService $setupService,22private ValidationFactory $validation,23) {24parent::__construct();25}2627/**28* Returns two-factor token credentials that allow a user to configure29* it on their account. If two-factor is already enabled this endpoint30* will return a 400 error.31*32* @throws \Pterodactyl\Exceptions\Model\DataValidationException33* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException34*/35public function index(Request $request): JsonResponse36{37if ($request->user()->use_totp) {38throw new BadRequestHttpException('Two-factor authentication is already enabled on this account.');39}4041return new JsonResponse([42'data' => $this->setupService->handle($request->user()),43]);44}4546/**47* Updates a user's account to have two-factor enabled.48*49* @throws \Throwable50* @throws \Illuminate\Validation\ValidationException51*/52public function store(Request $request): JsonResponse53{54$validator = $this->validation->make($request->all(), [55'code' => ['required', 'string', 'size:6'],56'password' => ['required', 'string'],57]);5859$data = $validator->validate();60if (!password_verify($data['password'], $request->user()->password)) {61throw new BadRequestHttpException('The password provided was not valid.');62}6364$tokens = $this->toggleTwoFactorService->handle($request->user(), $data['code'], true);6566Activity::event('user:two-factor.create')->log();6768return new JsonResponse([69'object' => 'recovery_tokens',70'attributes' => [71'tokens' => $tokens,72],73]);74}7576/**77* Disables two-factor authentication on an account if the password provided78* is valid.79*80* @throws \Throwable81*/82public function delete(Request $request): JsonResponse83{84if (!password_verify($request->input('password') ?? '', $request->user()->password)) {85throw new BadRequestHttpException('The password provided was not valid.');86}8788/** @var \Pterodactyl\Models\User $user */89$user = $request->user();9091$user->update([92'totp_authenticated_at' => Carbon::now(),93'use_totp' => false,94]);9596Activity::event('user:two-factor.delete')->log();9798return new JsonResponse([], Response::HTTP_NO_CONTENT);99}100}101102103