2016-10-14 21:15:36 +00:00
|
|
|
<?php
|
2016-12-07 22:46:38 +00:00
|
|
|
|
2016-10-14 21:15:36 +00:00
|
|
|
namespace Pterodactyl\Http\Controllers\Base;
|
|
|
|
|
2016-12-07 22:46:38 +00:00
|
|
|
use Illuminate\Http\Request;
|
2018-06-21 06:05:35 +00:00
|
|
|
use Illuminate\Http\JsonResponse;
|
2017-08-31 02:11:14 +00:00
|
|
|
use Prologue\Alerts\AlertsMessageBag;
|
2016-10-14 21:15:36 +00:00
|
|
|
use Pterodactyl\Http\Controllers\Controller;
|
2017-08-31 02:11:14 +00:00
|
|
|
use Pterodactyl\Services\Users\TwoFactorSetupService;
|
2017-08-31 02:14:20 +00:00
|
|
|
use Pterodactyl\Services\Users\ToggleTwoFactorService;
|
|
|
|
use Illuminate\Contracts\Config\Repository as ConfigRepository;
|
|
|
|
use Pterodactyl\Contracts\Repository\SessionRepositoryInterface;
|
|
|
|
use Pterodactyl\Exceptions\Service\User\TwoFactorAuthenticationTokenInvalid;
|
2016-10-14 21:15:36 +00:00
|
|
|
|
|
|
|
class SecurityController extends Controller
|
|
|
|
{
|
2017-08-31 02:11:14 +00:00
|
|
|
/**
|
|
|
|
* @var \Prologue\Alerts\AlertsMessageBag
|
|
|
|
*/
|
|
|
|
protected $alert;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Illuminate\Contracts\Config\Repository
|
|
|
|
*/
|
|
|
|
protected $config;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\SessionRepositoryInterface
|
|
|
|
*/
|
|
|
|
protected $repository;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Services\Users\ToggleTwoFactorService
|
|
|
|
*/
|
|
|
|
protected $toggleTwoFactorService;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Services\Users\TwoFactorSetupService
|
|
|
|
*/
|
|
|
|
protected $twoFactorSetupService;
|
|
|
|
|
2017-09-02 23:56:15 +00:00
|
|
|
/**
|
|
|
|
* SecurityController constructor.
|
|
|
|
*
|
2019-09-06 04:32:57 +00:00
|
|
|
* @param \Prologue\Alerts\AlertsMessageBag $alert
|
|
|
|
* @param \Illuminate\Contracts\Config\Repository $config
|
2017-09-02 23:56:15 +00:00
|
|
|
* @param \Pterodactyl\Contracts\Repository\SessionRepositoryInterface $repository
|
2019-09-06 04:32:57 +00:00
|
|
|
* @param \Pterodactyl\Services\Users\ToggleTwoFactorService $toggleTwoFactorService
|
|
|
|
* @param \Pterodactyl\Services\Users\TwoFactorSetupService $twoFactorSetupService
|
2017-09-02 23:56:15 +00:00
|
|
|
*/
|
2017-08-31 02:11:14 +00:00
|
|
|
public function __construct(
|
|
|
|
AlertsMessageBag $alert,
|
|
|
|
ConfigRepository $config,
|
|
|
|
SessionRepositoryInterface $repository,
|
|
|
|
ToggleTwoFactorService $toggleTwoFactorService,
|
|
|
|
TwoFactorSetupService $twoFactorSetupService
|
|
|
|
) {
|
|
|
|
$this->alert = $alert;
|
|
|
|
$this->config = $config;
|
|
|
|
$this->repository = $repository;
|
|
|
|
$this->toggleTwoFactorService = $toggleTwoFactorService;
|
|
|
|
$this->twoFactorSetupService = $twoFactorSetupService;
|
|
|
|
}
|
|
|
|
|
2016-10-14 21:15:36 +00:00
|
|
|
/**
|
2018-06-21 06:05:35 +00:00
|
|
|
* Return information about the user's two-factor authentication status. If not enabled setup their
|
|
|
|
* secret and return information to allow the user to proceede with setup.
|
2016-10-14 21:15:36 +00:00
|
|
|
*
|
2017-08-22 03:10:48 +00:00
|
|
|
* @param \Illuminate\Http\Request $request
|
2017-03-19 23:36:50 +00:00
|
|
|
* @return \Illuminate\Http\JsonResponse
|
2017-08-31 02:11:14 +00:00
|
|
|
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
|
|
|
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
2016-10-14 21:15:36 +00:00
|
|
|
*/
|
2018-06-21 06:05:35 +00:00
|
|
|
public function index(Request $request): JsonResponse
|
2016-10-14 21:15:36 +00:00
|
|
|
{
|
2018-06-21 06:05:35 +00:00
|
|
|
if ($request->user()->use_totp) {
|
|
|
|
return JsonResponse::create([
|
|
|
|
'enabled' => true,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
$response = $this->twoFactorSetupService->handle($request->user());
|
|
|
|
|
|
|
|
return JsonResponse::create([
|
|
|
|
'enabled' => false,
|
2019-06-22 19:28:44 +00:00
|
|
|
'qr_image' => $response,
|
|
|
|
'secret' => '',
|
2017-11-18 18:35:33 +00:00
|
|
|
]);
|
2016-10-14 21:15:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-05-13 14:50:56 +00:00
|
|
|
* Verifies that 2FA token received is valid and will work on the account.
|
2016-10-14 21:15:36 +00:00
|
|
|
*
|
2017-08-22 03:10:48 +00:00
|
|
|
* @param \Illuminate\Http\Request $request
|
2018-06-21 06:05:35 +00:00
|
|
|
* @return \Illuminate\Http\JsonResponse
|
2017-09-02 23:56:15 +00:00
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
|
|
|
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
2016-10-14 21:15:36 +00:00
|
|
|
*/
|
2018-06-21 06:05:35 +00:00
|
|
|
public function store(Request $request): JsonResponse
|
2016-10-14 21:15:36 +00:00
|
|
|
{
|
2017-08-31 02:11:14 +00:00
|
|
|
try {
|
2018-02-18 19:15:10 +00:00
|
|
|
$this->toggleTwoFactorService->handle($request->user(), $request->input('token') ?? '');
|
2017-08-31 02:11:14 +00:00
|
|
|
} catch (TwoFactorAuthenticationTokenInvalid $exception) {
|
2018-06-21 06:05:35 +00:00
|
|
|
$error = true;
|
2016-10-14 21:15:36 +00:00
|
|
|
}
|
2018-06-21 06:05:35 +00:00
|
|
|
|
|
|
|
return JsonResponse::create([
|
|
|
|
'success' => ! isset($error),
|
|
|
|
]);
|
2016-10-14 21:15:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Disables TOTP on an account.
|
|
|
|
*
|
2017-08-22 03:10:48 +00:00
|
|
|
* @param \Illuminate\Http\Request $request
|
2018-06-21 06:05:35 +00:00
|
|
|
* @return \Illuminate\Http\JsonResponse
|
2017-09-02 23:56:15 +00:00
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
|
|
|
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
2016-10-14 21:15:36 +00:00
|
|
|
*/
|
2018-06-21 06:05:35 +00:00
|
|
|
public function delete(Request $request): JsonResponse
|
2016-10-14 21:15:36 +00:00
|
|
|
{
|
2017-08-31 02:11:14 +00:00
|
|
|
try {
|
2018-02-18 19:15:10 +00:00
|
|
|
$this->toggleTwoFactorService->handle($request->user(), $request->input('token') ?? '', false);
|
2017-08-31 02:11:14 +00:00
|
|
|
} catch (TwoFactorAuthenticationTokenInvalid $exception) {
|
2018-06-21 06:05:35 +00:00
|
|
|
$error = true;
|
2016-10-14 21:15:36 +00:00
|
|
|
}
|
|
|
|
|
2018-06-21 06:05:35 +00:00
|
|
|
return JsonResponse::create([
|
|
|
|
'success' => ! isset($error),
|
|
|
|
]);
|
2016-10-14 21:15:36 +00:00
|
|
|
}
|
|
|
|
}
|