2015-12-06 13:58:49 -05:00
|
|
|
<?php
|
|
|
|
|
2016-12-07 22:46:38 +00:00
|
|
|
namespace Pterodactyl\Http\Controllers\Auth;
|
2015-12-06 13:58:49 -05:00
|
|
|
|
2019-09-04 20:24:46 -07:00
|
|
|
use Cake\Chronos\Chronos;
|
2019-06-22 13:33:11 -07:00
|
|
|
use Illuminate\Support\Str;
|
2015-12-06 13:58:49 -05:00
|
|
|
use Illuminate\Http\Request;
|
2019-06-22 13:33:11 -07:00
|
|
|
use Illuminate\Auth\AuthManager;
|
2018-04-01 17:46:16 -05:00
|
|
|
use Illuminate\Http\JsonResponse;
|
2018-04-08 15:46:32 -05:00
|
|
|
use Illuminate\Contracts\View\View;
|
2019-06-22 13:33:11 -07:00
|
|
|
use Illuminate\Contracts\Config\Repository;
|
|
|
|
use Illuminate\Contracts\View\Factory as ViewFactory;
|
|
|
|
use Illuminate\Contracts\Cache\Repository as CacheRepository;
|
|
|
|
use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
|
2017-11-18 15:09:58 -06:00
|
|
|
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
|
2016-09-03 17:09:00 -04:00
|
|
|
|
2018-04-07 12:35:15 -05:00
|
|
|
class LoginController extends AbstractLoginController
|
2015-12-06 13:58:49 -05:00
|
|
|
{
|
2019-06-22 13:33:11 -07:00
|
|
|
/**
|
|
|
|
* @var \Illuminate\Contracts\View\Factory
|
|
|
|
*/
|
|
|
|
private $view;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Illuminate\Contracts\Cache\Repository
|
|
|
|
*/
|
|
|
|
private $cache;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\UserRepositoryInterface
|
|
|
|
*/
|
|
|
|
private $repository;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* LoginController constructor.
|
|
|
|
*
|
2019-09-05 21:32:57 -07:00
|
|
|
* @param \Illuminate\Auth\AuthManager $auth
|
|
|
|
* @param \Illuminate\Contracts\Config\Repository $config
|
|
|
|
* @param \Illuminate\Contracts\Cache\Repository $cache
|
2019-06-22 13:33:11 -07:00
|
|
|
* @param \Pterodactyl\Contracts\Repository\UserRepositoryInterface $repository
|
2019-09-05 21:32:57 -07:00
|
|
|
* @param \Illuminate\Contracts\View\Factory $view
|
2019-06-22 13:33:11 -07:00
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
AuthManager $auth,
|
|
|
|
Repository $config,
|
|
|
|
CacheRepository $cache,
|
|
|
|
UserRepositoryInterface $repository,
|
|
|
|
ViewFactory $view
|
|
|
|
) {
|
|
|
|
parent::__construct($auth, $config);
|
|
|
|
|
|
|
|
$this->view = $view;
|
|
|
|
$this->cache = $cache;
|
|
|
|
$this->repository = $repository;
|
|
|
|
}
|
|
|
|
|
2018-04-08 15:46:32 -05:00
|
|
|
/**
|
|
|
|
* Handle all incoming requests for the authentication routes and render the
|
|
|
|
* base authentication view component. Vuejs will take over at this point and
|
|
|
|
* turn the login area into a SPA.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Contracts\View\View
|
|
|
|
*/
|
|
|
|
public function index(): View
|
|
|
|
{
|
2019-06-22 13:33:11 -07:00
|
|
|
return $this->view->make('templates/auth.core');
|
2018-04-08 15:46:32 -05:00
|
|
|
}
|
|
|
|
|
2015-12-10 21:58:17 -05:00
|
|
|
/**
|
|
|
|
* Handle a login request to the application.
|
|
|
|
*
|
2017-08-21 22:10:48 -05:00
|
|
|
* @param \Illuminate\Http\Request $request
|
2020-04-25 13:01:16 -07:00
|
|
|
* @return \Illuminate\Http\JsonResponse|void
|
2017-12-17 13:07:38 -06:00
|
|
|
*
|
2018-04-01 17:46:16 -05:00
|
|
|
* @throws \Pterodactyl\Exceptions\DisplayException
|
2017-12-17 13:07:38 -06:00
|
|
|
* @throws \Illuminate\Validation\ValidationException
|
2015-12-10 21:58:17 -05:00
|
|
|
*/
|
2018-04-01 17:46:16 -05:00
|
|
|
public function login(Request $request): JsonResponse
|
2015-12-10 21:58:17 -05:00
|
|
|
{
|
2018-04-07 12:35:15 -05:00
|
|
|
$username = $request->input('user');
|
2017-11-18 15:09:58 -06:00
|
|
|
$useColumn = $this->getField($username);
|
2015-12-10 21:58:17 -05:00
|
|
|
|
2017-04-14 14:33:15 -04:00
|
|
|
if ($this->hasTooManyLoginAttempts($request)) {
|
2016-09-03 17:09:00 -04:00
|
|
|
$this->fireLockoutEvent($request);
|
2017-12-17 13:07:38 -06:00
|
|
|
$this->sendLockoutResponse($request);
|
2015-12-10 21:58:17 -05:00
|
|
|
}
|
|
|
|
|
2017-11-18 15:09:58 -06:00
|
|
|
try {
|
|
|
|
$user = $this->repository->findFirstWhere([[$useColumn, '=', $username]]);
|
|
|
|
} catch (RecordNotFoundException $exception) {
|
2016-09-03 17:09:00 -04:00
|
|
|
return $this->sendFailedLoginResponse($request);
|
2015-12-13 21:30:57 -05:00
|
|
|
}
|
|
|
|
|
2018-04-07 16:17:51 -05:00
|
|
|
// Ensure that the account is using a valid username and password before trying to
|
|
|
|
// continue. Previously this was handled in the 2FA checkpoint, however that has
|
|
|
|
// a flaw in which you can discover if an account exists simply by seeing if you
|
|
|
|
// can proceede to the next step in the login process.
|
|
|
|
if (! password_verify($request->input('password'), $user->password)) {
|
|
|
|
return $this->sendFailedLoginResponse($request, $user);
|
|
|
|
}
|
|
|
|
|
2017-04-14 14:33:15 -04:00
|
|
|
if ($user->use_totp) {
|
2019-06-22 13:33:11 -07:00
|
|
|
$token = Str::random(64);
|
2019-09-04 20:24:46 -07:00
|
|
|
$this->cache->put($token, $user->id, Chronos::now()->addMinutes(5));
|
2019-06-22 13:33:11 -07:00
|
|
|
|
2020-07-02 23:01:02 -07:00
|
|
|
return new JsonResponse([
|
2019-06-22 13:33:11 -07:00
|
|
|
'data' => [
|
|
|
|
'complete' => false,
|
|
|
|
'confirmation_token' => $token,
|
|
|
|
],
|
|
|
|
]);
|
2017-04-14 14:33:15 -04:00
|
|
|
}
|
2015-12-10 21:58:17 -05:00
|
|
|
|
2019-06-21 21:39:24 -07:00
|
|
|
$this->auth->guard()->login($user, true);
|
2017-04-14 14:33:15 -04:00
|
|
|
|
2019-06-22 12:28:44 -07:00
|
|
|
return $this->sendLoginResponse($user, $request);
|
2017-02-01 22:58:48 -05:00
|
|
|
}
|
2015-12-06 13:58:49 -05:00
|
|
|
}
|