2019-12-23 01:03:44 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Http\Controllers\Api\Client;
|
|
|
|
|
2019-12-23 04:41:25 +00:00
|
|
|
use Carbon\Carbon;
|
2019-12-23 01:03:44 +00:00
|
|
|
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
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Services\Users\TwoFactorSetupService
|
|
|
|
*/
|
|
|
|
private $setupService;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Illuminate\Contracts\Validation\Factory
|
|
|
|
*/
|
|
|
|
private $validation;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Services\Users\ToggleTwoFactorService
|
|
|
|
*/
|
|
|
|
private $toggleTwoFactorService;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* TwoFactorController constructor.
|
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
ToggleTwoFactorService $toggleTwoFactorService,
|
|
|
|
TwoFactorSetupService $setupService,
|
|
|
|
Factory $validation
|
|
|
|
) {
|
|
|
|
parent::__construct();
|
|
|
|
|
|
|
|
$this->setupService = $setupService;
|
|
|
|
$this->validation = $validation;
|
|
|
|
$this->toggleTwoFactorService = $toggleTwoFactorService;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\JsonResponse
|
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
|
|
|
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
|
|
|
*/
|
|
|
|
public function index(Request $request)
|
|
|
|
{
|
2020-06-27 18:06:35 +00:00
|
|
|
if ($request->user()->use_totp) {
|
2019-12-23 01:03:44 +00:00
|
|
|
throw new BadRequestHttpException('Two-factor authentication is already enabled on this account.');
|
|
|
|
}
|
|
|
|
|
2020-06-27 18:06:35 +00:00
|
|
|
return new JsonResponse([
|
2019-12-23 01:03:44 +00:00
|
|
|
'data' => [
|
|
|
|
'image_url_data' => $this->setupService->handle($request->user()),
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates a user's account to have two-factor enabled.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\JsonResponse
|
|
|
|
*
|
|
|
|
* @throws \Illuminate\Validation\ValidationException
|
|
|
|
* @throws \PragmaRX\Google2FA\Exceptions\IncompatibleWithGoogleAuthenticatorException
|
|
|
|
* @throws \PragmaRX\Google2FA\Exceptions\InvalidCharactersException
|
|
|
|
* @throws \PragmaRX\Google2FA\Exceptions\SecretKeyTooShortException
|
|
|
|
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
|
|
|
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
|
|
|
* @throws \Pterodactyl\Exceptions\Service\User\TwoFactorAuthenticationTokenInvalid
|
|
|
|
*/
|
|
|
|
public function store(Request $request)
|
|
|
|
{
|
|
|
|
$validator = $this->validation->make($request->all(), [
|
|
|
|
'code' => 'required|string',
|
|
|
|
]);
|
|
|
|
|
|
|
|
if ($validator->fails()) {
|
|
|
|
throw new ValidationException($validator);
|
|
|
|
}
|
|
|
|
|
2020-07-03 04:55:25 +00:00
|
|
|
$tokens = $this->toggleTwoFactorService->handle($request->user(), $request->input('code'), true);
|
2019-12-23 01:03:44 +00:00
|
|
|
|
2020-07-03 04:55:25 +00:00
|
|
|
return new JsonResponse([
|
|
|
|
'object' => 'recovery_tokens',
|
|
|
|
'attributes' => [
|
|
|
|
'tokens' => $tokens,
|
|
|
|
],
|
|
|
|
]);
|
2019-12-23 01:03:44 +00:00
|
|
|
}
|
|
|
|
|
2019-12-23 04:41:25 +00:00
|
|
|
/**
|
|
|
|
* Disables two-factor authentication on an account if the password provided
|
|
|
|
* is valid.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\JsonResponse
|
|
|
|
*/
|
|
|
|
public function delete(Request $request)
|
2019-12-23 01:03:44 +00:00
|
|
|
{
|
2021-01-23 20:33:34 +00:00
|
|
|
if (!password_verify($request->input('password') ?? '', $request->user()->password)) {
|
|
|
|
throw new BadRequestHttpException('The password provided was not valid.');
|
2019-12-23 04:41:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @var \Pterodactyl\Models\User $user */
|
|
|
|
$user = $request->user();
|
|
|
|
|
|
|
|
$user->update([
|
|
|
|
'totp_authenticated_at' => Carbon::now(),
|
|
|
|
'use_totp' => false,
|
|
|
|
]);
|
|
|
|
|
2020-06-27 18:06:35 +00:00
|
|
|
return new JsonResponse([], Response::HTTP_NO_CONTENT);
|
2019-12-23 01:03:44 +00:00
|
|
|
}
|
|
|
|
}
|