misc_pterodactyl-panel/app/Http/Controllers/Api/Client/TwoFactorController.php
2021-03-06 15:53:45 -07:00

102 lines
3 KiB
PHP

<?php
namespace Pterodactyl\Http\Controllers\Api\Client;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Http\JsonResponse;
use Illuminate\Contracts\Validation\Factory;
use Illuminate\Validation\ValidationException;
use Pterodactyl\Services\Users\TwoFactorSetupService;
use Pterodactyl\Services\Users\ToggleTwoFactorService;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
class TwoFactorController extends ClientApiController
{
private ToggleTwoFactorService $toggleTwoFactorService;
private TwoFactorSetupService $setupService;
private Factory $validation;
/**
* TwoFactorController constructor.
*/
public function __construct(
ToggleTwoFactorService $toggleTwoFactorService,
TwoFactorSetupService $setupService,
Factory $validation
) {
parent::__construct();
$this->toggleTwoFactorService = $toggleTwoFactorService;
$this->setupService = $setupService;
$this->validation = $validation;
}
/**
* 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
{
if ($request->user()->use_totp) {
throw new BadRequestHttpException('Two-factor authentication is already enabled on this account.');
}
return new JsonResponse([
'data' => [
'image_url_data' => $this->setupService->handle($request->user()),
],
]);
}
/**
* Updates a user's account to have two-factor enabled.
*
* @throws \Throwable
*/
public function store(Request $request): JsonResponse
{
$validator = $this->validation->make($request->all(), [
'code' => 'required|string',
]);
if ($validator->fails()) {
throw new ValidationException($validator);
}
$tokens = $this->toggleTwoFactorService->handle($request->user(), $request->input('code'), true);
return new JsonResponse([
'object' => 'recovery_tokens',
'attributes' => [
'tokens' => $tokens,
],
]);
}
/**
* Disables two-factor authentication on an account if the password provided
* is valid.
*/
public function delete(Request $request): Response
{
if (!password_verify($request->input('password') ?? '', $request->user()->password)) {
throw new BadRequestHttpException('The password provided was not valid.');
}
/** @var \Pterodactyl\Models\User $user */
$user = $request->user();
$user->update([
'totp_authenticated_at' => Carbon::now(),
'use_totp' => false,
]);
return $this->returnNoContent();
}
}