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

119 lines
3.1 KiB
PHP
Raw Normal View History

<?php
namespace Pterodactyl\Http\Controllers\Auth;
use Illuminate\Http\Request;
use Pterodactyl\Models\User;
use Illuminate\Auth\AuthManager;
use Illuminate\Http\JsonResponse;
use Illuminate\Auth\Events\Failed;
2019-06-22 20:33:11 +00:00
use Illuminate\Contracts\Config\Repository;
use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Http\Controllers\Controller;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
abstract class AbstractLoginController extends Controller
{
2018-07-15 05:48:09 +00:00
use AuthenticatesUsers;
/**
* Lockout time for failed login requests.
*
* @var int
*/
protected $lockoutTime;
/**
* After how many attempts should logins be throttled and locked.
*
* @var int
*/
protected $maxLoginAttempts;
/**
* Where to redirect users after login / registration.
*
* @var string
*/
protected $redirectTo = '/';
2019-06-22 20:33:11 +00:00
/**
* @var \Illuminate\Auth\AuthManager
*/
protected $auth;
/**
* @var \Illuminate\Contracts\Config\Repository
*/
protected $config;
/**
* LoginController constructor.
*/
2019-06-22 20:33:11 +00:00
public function __construct(AuthManager $auth, Repository $config)
{
$this->lockoutTime = $config->get('auth.lockout.time');
$this->maxLoginAttempts = $config->get('auth.lockout.attempts');
2019-06-22 20:33:11 +00:00
$this->auth = $auth;
$this->config = $config;
}
/**
* Get the failed login response instance.
*
* @throws \Pterodactyl\Exceptions\DisplayException
*/
protected function sendFailedLoginResponse(Request $request, Authenticatable $user = null, string $message = null)
{
$this->incrementLoginAttempts($request);
$this->fireFailedLoginEvent($user, [
$this->getField($request->input('user')) => $request->input('user'),
]);
2018-04-08 21:16:04 +00:00
if ($request->route()->named('auth.login-checkpoint')) {
2021-01-23 20:33:34 +00:00
throw new DisplayException($message ?? trans('auth.two_factor.checkpoint_failed'));
}
throw new DisplayException(trans('auth.failed'));
}
/**
* Send the response after the user was authenticated.
*/
protected function sendLoginResponse(User $user, Request $request): JsonResponse
{
$request->session()->regenerate();
$this->clearLoginAttempts($request);
$this->auth->guard()->login($user, true);
2021-01-14 17:36:05 +00:00
return new JsonResponse([
2019-06-22 20:33:11 +00:00
'data' => [
'complete' => true,
'intended' => $this->redirectPath(),
2021-01-14 17:36:05 +00:00
'user' => $user->toReactObject(),
2019-06-22 20:33:11 +00:00
],
]);
}
/**
* Determine if the user is logging in using an email or username,.
*
* @param string $input
*/
protected function getField(string $input = null): string
{
return ($input && str_contains($input, '@')) ? 'email' : 'username';
}
/**
* Fire a failed login event.
*/
protected function fireFailedLoginEvent(Authenticatable $user = null, array $credentials = [])
{
2018-12-30 20:45:57 +00:00
event(new Failed('auth', $user, $credentials));
}
}