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
|
|
|
|
2021-01-23 12:09:16 -08:00
|
|
|
use Carbon\CarbonImmutable;
|
2019-06-22 13:33:11 -07:00
|
|
|
use Illuminate\Support\Str;
|
2015-12-06 13:58:49 -05:00
|
|
|
use Illuminate\Http\Request;
|
2021-09-21 21:30:08 -07:00
|
|
|
use Pterodactyl\Models\User;
|
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;
|
2021-07-17 11:47:07 -06:00
|
|
|
use LaravelWebauthn\Facades\Webauthn;
|
2019-06-22 13:33:11 -07:00
|
|
|
use Illuminate\Contracts\View\Factory as ViewFactory;
|
2021-09-21 21:30:08 -07:00
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
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
|
|
|
{
|
2021-07-17 11:47:07 -06:00
|
|
|
private const SESSION_PUBLICKEY_REQUEST = 'webauthn.publicKeyRequest';
|
2019-06-22 13:33:11 -07:00
|
|
|
|
2021-07-17 12:48:14 -06:00
|
|
|
private const METHOD_TOTP = 'totp';
|
|
|
|
private const METHOD_WEBAUTHN = 'webauthn';
|
|
|
|
|
2021-07-17 11:47:07 -06:00
|
|
|
private ViewFactory $view;
|
2019-06-22 13:33:11 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* LoginController constructor.
|
|
|
|
*/
|
2021-09-30 16:08:11 -06:00
|
|
|
public function __construct(ViewFactory $view) {
|
2021-09-21 21:30:08 -07:00
|
|
|
parent::__construct();
|
2019-06-22 13:33:11 -07:00
|
|
|
|
2021-07-17 11:47:07 -06:00
|
|
|
$this->view = $view;
|
2019-06-22 13:33:11 -07:00
|
|
|
}
|
|
|
|
|
2018-04-08 15:46:32 -05:00
|
|
|
/**
|
|
|
|
* Handle all incoming requests for the authentication routes and render the
|
2021-09-30 16:08:11 -06:00
|
|
|
* base authentication view component. React will take over at this point and
|
|
|
|
* turn the login area into an SPA.
|
2018-04-08 15:46:32 -05:00
|
|
|
*/
|
|
|
|
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.
|
|
|
|
*
|
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
|
|
|
*/
|
2021-07-17 12:48:14 -06:00
|
|
|
public function login(Request $request)
|
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);
|
2021-08-07 16:10:24 -07:00
|
|
|
|
2021-07-17 11:47:07 -06:00
|
|
|
return;
|
2015-12-10 21:58:17 -05:00
|
|
|
}
|
|
|
|
|
2017-11-18 15:09:58 -06:00
|
|
|
try {
|
2021-09-21 21:30:08 -07:00
|
|
|
$username = $request->input('user');
|
|
|
|
|
2021-07-17 11:47:07 -06:00
|
|
|
/** @var \Pterodactyl\Models\User $user */
|
2021-09-21 21:30:08 -07:00
|
|
|
$user = User::query()->where($this->getField($username), $username)->firstOrFail();
|
|
|
|
} catch (ModelNotFoundException $exception) {
|
2021-07-17 11:47:07 -06:00
|
|
|
$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
|
2021-07-17 11:47:07 -06:00
|
|
|
// can proceed to the next step in the login process.
|
2021-01-23 12:33:34 -08:00
|
|
|
if (!password_verify($request->input('password'), $user->password)) {
|
2021-07-17 11:47:07 -06:00
|
|
|
$this->sendFailedLoginResponse($request, $user);
|
2021-08-07 16:10:24 -07:00
|
|
|
|
2021-07-17 11:47:07 -06:00
|
|
|
return;
|
2018-04-07 16:17:51 -05:00
|
|
|
}
|
|
|
|
|
2021-09-30 16:08:11 -06:00
|
|
|
$useTotp = $user->use_totp;
|
2021-07-17 11:47:07 -06:00
|
|
|
$webauthnKeys = $user->webauthnKeys()->get();
|
|
|
|
|
2021-09-30 16:08:11 -06:00
|
|
|
if (!$useTotp && count($webauthnKeys) < 1) {
|
|
|
|
return $this->sendLoginResponse($user, $request);
|
2018-04-07 16:17:51 -05:00
|
|
|
}
|
|
|
|
|
2021-09-30 16:08:11 -06:00
|
|
|
$methods = [];
|
|
|
|
if ($useTotp) {
|
|
|
|
$methods[] = self::METHOD_TOTP;
|
|
|
|
}
|
2021-08-07 16:10:24 -07:00
|
|
|
if (count($webauthnKeys) > 0) {
|
2021-09-30 16:08:11 -06:00
|
|
|
$methods[] = self::METHOD_WEBAUTHN;
|
2017-04-14 14:33:15 -04:00
|
|
|
}
|
2015-12-10 21:58:17 -05:00
|
|
|
|
2021-09-30 16:08:11 -06:00
|
|
|
$token = Str::random(64);
|
|
|
|
|
|
|
|
$request->session()->put('auth_confirmation_token', [
|
|
|
|
'user_id' => $user->id,
|
|
|
|
'token_value' => $token,
|
|
|
|
'expires_at' => CarbonImmutable::now()->addMinutes(5),
|
|
|
|
]);
|
2019-06-22 13:33:11 -07:00
|
|
|
|
2021-09-30 16:08:11 -06:00
|
|
|
$response = [
|
|
|
|
'complete' => false,
|
|
|
|
'methods' => $methods,
|
|
|
|
'confirmation_token' => $token,
|
|
|
|
];
|
|
|
|
|
|
|
|
if (count($webauthnKeys) > 0) {
|
2021-07-17 11:47:07 -06:00
|
|
|
$publicKey = Webauthn::getAuthenticateData($user);
|
|
|
|
$request->session()->put(self::SESSION_PUBLICKEY_REQUEST, $publicKey);
|
2021-09-30 16:08:11 -06:00
|
|
|
|
|
|
|
$response['webauthn'] = [
|
|
|
|
'public_key' => $publicKey,
|
|
|
|
];
|
2017-04-14 14:33:15 -04:00
|
|
|
}
|
2015-12-10 21:58:17 -05:00
|
|
|
|
2021-09-30 16:08:11 -06:00
|
|
|
return new JsonResponse($response);
|
2017-02-01 22:58:48 -05:00
|
|
|
}
|
2015-12-06 13:58:49 -05:00
|
|
|
}
|