misc_pterodactyl-panel/app/Http/Controllers/Auth/LoginController.php

120 lines
3.5 KiB
PHP
Raw Normal View History

<?php
2016-12-07 22:46:38 +00:00
namespace Pterodactyl\Http\Controllers\Auth;
use Carbon\CarbonImmutable;
2019-06-22 20:33:11 +00:00
use Illuminate\Support\Str;
use Illuminate\Http\Request;
use Pterodactyl\Models\User;
use Illuminate\Http\JsonResponse;
2018-04-08 20:46:32 +00:00
use Illuminate\Contracts\View\View;
use LaravelWebauthn\Facades\Webauthn;
2019-06-22 20:33:11 +00:00
use Illuminate\Contracts\View\Factory as ViewFactory;
use Illuminate\Database\Eloquent\ModelNotFoundException;
class LoginController extends AbstractLoginController
{
private const SESSION_PUBLICKEY_REQUEST = 'webauthn.publicKeyRequest';
2019-06-22 20:33:11 +00:00
private const METHOD_TOTP = 'totp';
private const METHOD_WEBAUTHN = 'webauthn';
private ViewFactory $view;
2019-06-22 20:33:11 +00:00
/**
* LoginController constructor.
*/
2021-09-30 22:08:11 +00:00
public function __construct(ViewFactory $view) {
parent::__construct();
2019-06-22 20:33:11 +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-09-30 22:08:11 +00:00
* base authentication view component. React will take over at this point and
* turn the login area into an SPA.
2018-04-08 20:46:32 +00:00
*/
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
}
/**
* 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
*
* @throws \Pterodactyl\Exceptions\DisplayException
2017-12-17 19:07:38 +00:00
* @throws \Illuminate\Validation\ValidationException
*/
public function login(Request $request)
{
if ($this->hasTooManyLoginAttempts($request)) {
$this->fireLockoutEvent($request);
2017-12-17 19:07:38 +00:00
$this->sendLockoutResponse($request);
2021-08-07 23:10:24 +00:00
return;
}
try {
$username = $request->input('user');
/** @var \Pterodactyl\Models\User $user */
$user = User::query()->where($this->getField($username), $username)->firstOrFail();
} catch (ModelNotFoundException $exception) {
$this->sendFailedLoginResponse($request);
}
// 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 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)) {
$this->sendFailedLoginResponse($request, $user);
2021-08-07 23:10:24 +00:00
return;
}
2021-09-30 22:08:11 +00:00
$useTotp = $user->use_totp;
$webauthnKeys = $user->webauthnKeys()->get();
2021-09-30 22:08:11 +00:00
if (!$useTotp && count($webauthnKeys) < 1) {
return $this->sendLoginResponse($user, $request);
}
2021-09-30 22:08:11 +00:00
$methods = [];
if ($useTotp) {
$methods[] = self::METHOD_TOTP;
}
2021-08-07 23:10:24 +00:00
if (count($webauthnKeys) > 0) {
2021-09-30 22:08:11 +00:00
$methods[] = self::METHOD_WEBAUTHN;
}
2021-09-30 22:08:11 +00: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 20:33:11 +00:00
2021-09-30 22:08:11 +00:00
$response = [
'complete' => false,
'methods' => $methods,
'confirmation_token' => $token,
];
if (count($webauthnKeys) > 0) {
$publicKey = Webauthn::getAuthenticateData($user);
$request->session()->put(self::SESSION_PUBLICKEY_REQUEST, $publicKey);
2021-09-30 22:08:11 +00:00
$response['webauthn'] = [
'public_key' => $publicKey,
];
}
2021-09-30 22:08:11 +00:00
return new JsonResponse($response);
2017-02-02 03:58:48 +00:00
}
}