2015-12-06 18:58:49 +00:00
|
|
|
<?php
|
|
|
|
|
2016-12-07 22:46:38 +00:00
|
|
|
namespace Pterodactyl\Http\Controllers\Auth;
|
2015-12-06 18:58:49 +00:00
|
|
|
|
2021-01-23 20:09:16 +00:00
|
|
|
use Carbon\CarbonImmutable;
|
2019-06-22 20:33:11 +00:00
|
|
|
use Illuminate\Support\Str;
|
2015-12-06 18:58:49 +00:00
|
|
|
use Illuminate\Http\Request;
|
2019-06-22 20:33:11 +00:00
|
|
|
use Illuminate\Auth\AuthManager;
|
2018-04-01 22:46:16 +00:00
|
|
|
use Illuminate\Http\JsonResponse;
|
2018-04-08 20:46:32 +00:00
|
|
|
use Illuminate\Contracts\View\View;
|
2021-07-17 17:47:07 +00:00
|
|
|
use LaravelWebauthn\Facades\Webauthn;
|
2019-06-22 20:33:11 +00: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 21:09:58 +00:00
|
|
|
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
|
2016-09-03 21:09:00 +00:00
|
|
|
|
2018-04-07 17:35:15 +00:00
|
|
|
class LoginController extends AbstractLoginController
|
2015-12-06 18:58:49 +00:00
|
|
|
{
|
2019-06-22 20:33:11 +00:00
|
|
|
/**
|
2021-07-17 17:47:07 +00:00
|
|
|
* @var string
|
2019-06-22 20:33:11 +00:00
|
|
|
*/
|
2021-07-17 17:47:07 +00:00
|
|
|
private const SESSION_PUBLICKEY_REQUEST = 'webauthn.publicKeyRequest';
|
2019-06-22 20:33:11 +00:00
|
|
|
|
2021-07-17 17:47:07 +00:00
|
|
|
private CacheRepository $cache;
|
|
|
|
private UserRepositoryInterface $repository;
|
|
|
|
private ViewFactory $view;
|
2019-06-22 20:33:11 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* LoginController constructor.
|
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
AuthManager $auth,
|
|
|
|
Repository $config,
|
|
|
|
CacheRepository $cache,
|
|
|
|
UserRepositoryInterface $repository,
|
|
|
|
ViewFactory $view
|
|
|
|
) {
|
|
|
|
parent::__construct($auth, $config);
|
|
|
|
|
|
|
|
$this->cache = $cache;
|
|
|
|
$this->repository = $repository;
|
2021-07-17 17:47:07 +00:00
|
|
|
$this->view = $view;
|
2019-06-22 20:33:11 +00:00
|
|
|
}
|
|
|
|
|
2018-04-08 20:46:32 +00:00
|
|
|
/**
|
|
|
|
* Handle all incoming requests for the authentication routes and render the
|
2021-07-17 17:47:07 +00:00
|
|
|
* base authentication view component. React will take over at this point and
|
2018-04-08 20:46:32 +00:00
|
|
|
* turn the login area into a SPA.
|
|
|
|
*/
|
|
|
|
public function index(): View
|
|
|
|
{
|
2019-06-22 20:33:11 +00:00
|
|
|
return $this->view->make('templates/auth.core');
|
2018-04-08 20:46:32 +00:00
|
|
|
}
|
|
|
|
|
2015-12-11 02:58:17 +00:00
|
|
|
/**
|
|
|
|
* Handle a login request to the application.
|
|
|
|
*
|
2020-04-25 20:01:16 +00:00
|
|
|
* @return \Illuminate\Http\JsonResponse|void
|
2017-12-17 19:07:38 +00:00
|
|
|
*
|
2018-04-01 22:46:16 +00:00
|
|
|
* @throws \Pterodactyl\Exceptions\DisplayException
|
2017-12-17 19:07:38 +00:00
|
|
|
* @throws \Illuminate\Validation\ValidationException
|
2015-12-11 02:58:17 +00:00
|
|
|
*/
|
2018-04-01 22:46:16 +00:00
|
|
|
public function login(Request $request): JsonResponse
|
2015-12-11 02:58:17 +00:00
|
|
|
{
|
2018-04-07 17:35:15 +00:00
|
|
|
$username = $request->input('user');
|
2017-11-18 21:09:58 +00:00
|
|
|
$useColumn = $this->getField($username);
|
2015-12-11 02:58:17 +00:00
|
|
|
|
2017-04-14 18:33:15 +00:00
|
|
|
if ($this->hasTooManyLoginAttempts($request)) {
|
2016-09-03 21:09:00 +00:00
|
|
|
$this->fireLockoutEvent($request);
|
2017-12-17 19:07:38 +00:00
|
|
|
$this->sendLockoutResponse($request);
|
2021-07-17 17:47:07 +00:00
|
|
|
return;
|
2015-12-11 02:58:17 +00:00
|
|
|
}
|
|
|
|
|
2017-11-18 21:09:58 +00:00
|
|
|
try {
|
2021-07-17 17:47:07 +00:00
|
|
|
/** @var \Pterodactyl\Models\User $user */
|
2017-11-18 21:09:58 +00:00
|
|
|
$user = $this->repository->findFirstWhere([[$useColumn, '=', $username]]);
|
|
|
|
} catch (RecordNotFoundException $exception) {
|
2021-07-17 17:47:07 +00:00
|
|
|
$this->sendFailedLoginResponse($request);
|
|
|
|
return;
|
2015-12-14 02:30:57 +00:00
|
|
|
}
|
|
|
|
|
2018-04-07 21:17:51 +00: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
|
2021-07-17 17:47:07 +00:00
|
|
|
// can proceed to the next step in the login process.
|
2021-01-23 20:33:34 +00:00
|
|
|
if (!password_verify($request->input('password'), $user->password)) {
|
2021-07-17 17:47:07 +00:00
|
|
|
$this->sendFailedLoginResponse($request, $user);
|
|
|
|
return;
|
2018-04-07 21:17:51 +00:00
|
|
|
}
|
|
|
|
|
2021-07-17 17:47:07 +00:00
|
|
|
$webauthnKeys = $user->webauthnKeys()->get();
|
|
|
|
|
|
|
|
if (sizeof($webauthnKeys) > 0) {
|
2019-06-22 20:33:11 +00:00
|
|
|
$token = Str::random(64);
|
2021-01-23 20:09:16 +00:00
|
|
|
$this->cache->put($token, $user->id, CarbonImmutable::now()->addMinutes(5));
|
2019-06-22 20:33:11 +00:00
|
|
|
|
2021-07-17 17:47:07 +00:00
|
|
|
$publicKey = Webauthn::getAuthenticateData($user);
|
|
|
|
$request->session()->put(self::SESSION_PUBLICKEY_REQUEST, $publicKey);
|
|
|
|
$request->session()->save();
|
|
|
|
|
|
|
|
$methods = ['webauthn'];
|
|
|
|
if ($user->use_totp) {
|
|
|
|
$methods[] = 'totp';
|
|
|
|
}
|
|
|
|
|
2020-07-03 06:01:02 +00:00
|
|
|
return new JsonResponse([
|
2021-07-17 17:47:07 +00:00
|
|
|
'complete' => false,
|
|
|
|
'methods' => $methods,
|
|
|
|
'confirmation_token' => $token,
|
|
|
|
'webauthn' => [
|
|
|
|
'public_key' => $publicKey,
|
2019-06-22 20:33:11 +00:00
|
|
|
],
|
|
|
|
]);
|
2021-07-17 17:47:07 +00:00
|
|
|
} else if ($user->use_totp) {
|
|
|
|
$token = Str::random(64);
|
|
|
|
$this->cache->put($token, $user->id, CarbonImmutable::now()->addMinutes(5));
|
|
|
|
|
|
|
|
return new JsonResponse([
|
|
|
|
'complete' => false,
|
|
|
|
'methods' => ['totp'],
|
|
|
|
'confirmation_token' => $token,
|
|
|
|
]);
|
2017-04-14 18:33:15 +00:00
|
|
|
}
|
2015-12-11 02:58:17 +00:00
|
|
|
|
2019-06-22 04:39:24 +00:00
|
|
|
$this->auth->guard()->login($user, true);
|
2017-04-14 18:33:15 +00:00
|
|
|
|
2019-06-22 19:28:44 +00:00
|
|
|
return $this->sendLoginResponse($user, $request);
|
2017-02-02 03:58:48 +00:00
|
|
|
}
|
2015-12-06 18:58:49 +00:00
|
|
|
}
|