2018-04-07 12:35:15 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Http\Controllers\Auth;
|
|
|
|
|
|
|
|
use Illuminate\Http\Request;
|
2018-05-28 14:59:48 -07:00
|
|
|
use Pterodactyl\Models\User;
|
2018-04-07 12:35:15 -05:00
|
|
|
use Illuminate\Auth\AuthManager;
|
|
|
|
use Illuminate\Http\JsonResponse;
|
|
|
|
use PragmaRX\Google2FA\Google2FA;
|
|
|
|
use Illuminate\Auth\Events\Failed;
|
|
|
|
use Pterodactyl\Exceptions\DisplayException;
|
|
|
|
use Pterodactyl\Http\Controllers\Controller;
|
|
|
|
use Illuminate\Contracts\Auth\Authenticatable;
|
|
|
|
use Illuminate\Contracts\Encryption\Encrypter;
|
|
|
|
use Illuminate\Foundation\Auth\AuthenticatesUsers;
|
2018-05-28 14:59:48 -07:00
|
|
|
use Pterodactyl\Traits\Helpers\ProvidesJWTServices;
|
2018-04-07 12:35:15 -05:00
|
|
|
use Illuminate\Contracts\Cache\Repository as CacheRepository;
|
|
|
|
use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
|
|
|
|
|
|
|
|
abstract class AbstractLoginController extends Controller
|
|
|
|
{
|
2018-05-28 14:59:48 -07:00
|
|
|
use AuthenticatesUsers, ProvidesJWTServices;
|
2018-04-07 12:35:15 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Illuminate\Auth\AuthManager
|
|
|
|
*/
|
|
|
|
protected $auth;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Illuminate\Contracts\Cache\Repository
|
|
|
|
*/
|
|
|
|
protected $cache;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Illuminate\Contracts\Encryption\Encrypter
|
|
|
|
*/
|
|
|
|
protected $encrypter;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \PragmaRX\Google2FA\Google2FA
|
|
|
|
*/
|
|
|
|
protected $google2FA;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\UserRepositoryInterface
|
|
|
|
*/
|
|
|
|
protected $repository;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 = '/';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* LoginController constructor.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Auth\AuthManager $auth
|
|
|
|
* @param \Illuminate\Contracts\Cache\Repository $cache
|
|
|
|
* @param \Illuminate\Contracts\Encryption\Encrypter $encrypter
|
|
|
|
* @param \PragmaRX\Google2FA\Google2FA $google2FA
|
|
|
|
* @param \Pterodactyl\Contracts\Repository\UserRepositoryInterface $repository
|
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
AuthManager $auth,
|
|
|
|
CacheRepository $cache,
|
|
|
|
Encrypter $encrypter,
|
|
|
|
Google2FA $google2FA,
|
|
|
|
UserRepositoryInterface $repository
|
|
|
|
) {
|
|
|
|
$this->auth = $auth;
|
|
|
|
$this->cache = $cache;
|
|
|
|
$this->encrypter = $encrypter;
|
|
|
|
$this->google2FA = $google2FA;
|
|
|
|
$this->repository = $repository;
|
|
|
|
|
|
|
|
$this->lockoutTime = config('auth.lockout.time');
|
|
|
|
$this->maxLoginAttempts = config('auth.lockout.attempts');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the failed login response instance.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @param \Illuminate\Contracts\Auth\Authenticatable|null $user
|
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\DisplayException
|
|
|
|
*/
|
|
|
|
protected function sendFailedLoginResponse(Request $request, Authenticatable $user = null)
|
|
|
|
{
|
|
|
|
$this->incrementLoginAttempts($request);
|
|
|
|
$this->fireFailedLoginEvent($user, [
|
|
|
|
$this->getField($request->input('user')) => $request->input('user'),
|
|
|
|
]);
|
|
|
|
|
2018-04-08 16:16:04 -05:00
|
|
|
if ($request->route()->named('auth.login-checkpoint')) {
|
2018-04-08 15:18:13 -05:00
|
|
|
throw new DisplayException(trans('auth.two_factor.checkpoint_failed'));
|
2018-04-07 16:17:51 -05:00
|
|
|
}
|
|
|
|
|
2018-04-07 12:35:15 -05:00
|
|
|
throw new DisplayException(trans('auth.failed'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send the response after the user was authenticated.
|
|
|
|
*
|
2018-05-28 14:59:48 -07:00
|
|
|
* @param \Pterodactyl\Models\User $user
|
2018-04-07 12:35:15 -05:00
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @return \Illuminate\Http\JsonResponse
|
|
|
|
*/
|
2018-05-28 14:59:48 -07:00
|
|
|
protected function sendLoginResponse(User $user, Request $request): JsonResponse
|
2018-04-07 12:35:15 -05:00
|
|
|
{
|
|
|
|
$request->session()->regenerate();
|
|
|
|
$this->clearLoginAttempts($request);
|
|
|
|
|
2018-05-28 14:59:48 -07:00
|
|
|
$this->auth->guard()->login($user, true);
|
|
|
|
|
|
|
|
return response()->json([
|
|
|
|
'complete' => true,
|
|
|
|
'intended' => $this->redirectPath(),
|
2018-07-14 22:42:58 -07:00
|
|
|
'user' => $user->toVueObject(),
|
2018-05-28 14:59:48 -07:00
|
|
|
]);
|
2018-04-07 12:35:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if the user is logging in using an email or username,.
|
|
|
|
*
|
|
|
|
* @param string $input
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function getField(string $input = null): string
|
|
|
|
{
|
|
|
|
return str_contains($input, '@') ? 'email' : 'username';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fire a failed login event.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Contracts\Auth\Authenticatable|null $user
|
|
|
|
* @param array $credentials
|
|
|
|
*/
|
|
|
|
protected function fireFailedLoginEvent(Authenticatable $user = null, array $credentials = [])
|
|
|
|
{
|
|
|
|
event(new Failed($user, $credentials));
|
|
|
|
}
|
|
|
|
}
|