2016-10-14 21:15:36 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Pterodactyl - Panel
|
2017-01-24 22:57:08 +00:00
|
|
|
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>
|
2016-12-07 22:46:38 +00:00
|
|
|
* Some Modifications (c) 2015 Dylan Seidt <dylan.seidt@gmail.com>.
|
2016-10-14 21:15:36 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
* copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
* SOFTWARE.
|
|
|
|
*/
|
2016-12-07 22:46:38 +00:00
|
|
|
|
2016-10-14 21:15:36 +00:00
|
|
|
namespace Pterodactyl\Http\Controllers\Base;
|
|
|
|
|
2017-08-31 02:11:14 +00:00
|
|
|
use Illuminate\Contracts\Config\Repository as ConfigRepository;
|
|
|
|
use Illuminate\Contracts\Session\Session;
|
2016-12-07 22:46:38 +00:00
|
|
|
use Illuminate\Http\Request;
|
2017-08-31 02:11:14 +00:00
|
|
|
use Prologue\Alerts\AlertsMessageBag;
|
|
|
|
use Pterodactyl\Contracts\Repository\SessionRepositoryInterface;
|
|
|
|
use Pterodactyl\Exceptions\Service\User\TwoFactorAuthenticationTokenInvalid;
|
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\ToggleTwoFactorService;
|
|
|
|
use Pterodactyl\Services\Users\TwoFactorSetupService;
|
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 \Illuminate\Contracts\Session\Session
|
|
|
|
*/
|
|
|
|
protected $session;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Services\Users\ToggleTwoFactorService
|
|
|
|
*/
|
|
|
|
protected $toggleTwoFactorService;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Services\Users\TwoFactorSetupService
|
|
|
|
*/
|
|
|
|
protected $twoFactorSetupService;
|
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
AlertsMessageBag $alert,
|
|
|
|
ConfigRepository $config,
|
|
|
|
Session $session,
|
|
|
|
SessionRepositoryInterface $repository,
|
|
|
|
ToggleTwoFactorService $toggleTwoFactorService,
|
|
|
|
TwoFactorSetupService $twoFactorSetupService
|
|
|
|
) {
|
|
|
|
$this->alert = $alert;
|
|
|
|
$this->config = $config;
|
|
|
|
$this->repository = $repository;
|
|
|
|
$this->session = $session;
|
|
|
|
$this->toggleTwoFactorService = $toggleTwoFactorService;
|
|
|
|
$this->twoFactorSetupService = $twoFactorSetupService;
|
|
|
|
}
|
|
|
|
|
2016-10-14 21:15:36 +00:00
|
|
|
/**
|
|
|
|
* Returns Security Management Page.
|
|
|
|
*
|
2017-08-22 03:10:48 +00:00
|
|
|
* @param \Illuminate\Http\Request $request
|
2017-03-19 23:36:50 +00:00
|
|
|
* @return \Illuminate\View\View
|
2016-10-14 21:15:36 +00:00
|
|
|
*/
|
|
|
|
public function index(Request $request)
|
|
|
|
{
|
2017-08-31 02:11:14 +00:00
|
|
|
if ($this->config->get('session.driver') === 'database') {
|
|
|
|
$activeSessions = $this->repository->getUserSessions($request->user()->id);
|
|
|
|
}
|
|
|
|
|
2016-10-14 21:15:36 +00:00
|
|
|
return view('base.security', [
|
2017-08-31 02:11:14 +00:00
|
|
|
'sessions' => $activeSessions ?? null,
|
2016-10-14 21:15:36 +00:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates TOTP Secret and returns popup data for user to verify
|
|
|
|
* that they can generate a valid response.
|
|
|
|
*
|
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
|
|
|
*/
|
|
|
|
public function generateTotp(Request $request)
|
|
|
|
{
|
2017-08-31 02:11:14 +00:00
|
|
|
return response()->json($this->twoFactorSetupService->handle($request->user()));
|
2016-10-14 21:15:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Verifies that 2FA token recieved is valid and will work on the account.
|
|
|
|
*
|
2017-08-22 03:10:48 +00:00
|
|
|
* @param \Illuminate\Http\Request $request
|
2016-10-14 21:15:36 +00:00
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function setTotp(Request $request)
|
|
|
|
{
|
2017-08-31 02:11:14 +00:00
|
|
|
try {
|
|
|
|
$this->toggleTwoFactorService->handle($request->user(), $request->input('token'));
|
2016-10-14 21:15:36 +00:00
|
|
|
|
|
|
|
return response('true');
|
2017-08-31 02:11:14 +00:00
|
|
|
} catch (TwoFactorAuthenticationTokenInvalid $exception) {
|
|
|
|
return response('false');
|
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
|
2017-03-19 23:36:50 +00:00
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
2016-10-14 21:15:36 +00:00
|
|
|
*/
|
|
|
|
public function disableTotp(Request $request)
|
|
|
|
{
|
2017-08-31 02:11:14 +00:00
|
|
|
try {
|
|
|
|
$this->toggleTwoFactorService->handle($request->user(), $request->input('token'), false);
|
|
|
|
} catch (TwoFactorAuthenticationTokenInvalid $exception) {
|
|
|
|
$this->alert->danger(trans('base.security.2fa_disable_error'))->flash();
|
2016-10-14 21:15:36 +00:00
|
|
|
}
|
|
|
|
|
2016-12-07 22:46:38 +00:00
|
|
|
return redirect()->route('account.security');
|
2016-10-14 21:15:36 +00:00
|
|
|
}
|
|
|
|
|
2017-03-19 23:36:50 +00:00
|
|
|
/**
|
|
|
|
* Revokes a user session.
|
|
|
|
*
|
2017-08-22 03:10:48 +00:00
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @param int $id
|
2017-03-19 23:36:50 +00:00
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
*/
|
2016-10-14 21:15:36 +00:00
|
|
|
public function revoke(Request $request, $id)
|
|
|
|
{
|
2017-08-31 02:11:14 +00:00
|
|
|
$this->repository->deleteUserSession($request->user()->id, $id);
|
2016-12-07 22:46:38 +00:00
|
|
|
|
2016-10-14 21:15:36 +00:00
|
|
|
return redirect()->route('account.security');
|
|
|
|
}
|
|
|
|
}
|