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
|
|
|
|
|
|
|
use Illuminate\Http\Request;
|
2018-04-01 17:46:16 -05:00
|
|
|
use Illuminate\Http\JsonResponse;
|
2017-11-18 15:09:58 -06:00
|
|
|
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
|
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
|
|
|
{
|
2015-12-10 21:58:17 -05:00
|
|
|
/**
|
|
|
|
* Handle a login request to the application.
|
|
|
|
*
|
2017-08-21 22:10:48 -05:00
|
|
|
* @param \Illuminate\Http\Request $request
|
2018-04-01 17:46:16 -05:00
|
|
|
* @return \Illuminate\Http\JsonResponse
|
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
|
|
|
*/
|
2018-04-01 17:46:16 -05:00
|
|
|
public function login(Request $request): JsonResponse
|
2015-12-10 21:58:17 -05:00
|
|
|
{
|
2018-04-07 12:35:15 -05:00
|
|
|
$username = $request->input('user');
|
2017-11-18 15:09:58 -06:00
|
|
|
$useColumn = $this->getField($username);
|
2015-12-10 21:58:17 -05:00
|
|
|
|
2018-04-08 15:18:13 -05:00
|
|
|
sleep(1);
|
|
|
|
|
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);
|
2015-12-10 21:58:17 -05:00
|
|
|
}
|
|
|
|
|
2017-11-18 15:09:58 -06:00
|
|
|
try {
|
|
|
|
$user = $this->repository->findFirstWhere([[$useColumn, '=', $username]]);
|
|
|
|
} catch (RecordNotFoundException $exception) {
|
2016-09-03 17:09:00 -04:00
|
|
|
return $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
|
|
|
|
// can proceede to the next step in the login process.
|
|
|
|
if (! password_verify($request->input('password'), $user->password)) {
|
|
|
|
return $this->sendFailedLoginResponse($request, $user);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the user is using 2FA we do not actually log them in at this step, we return
|
|
|
|
// a one-time token to link the 2FA credentials to this account via the UI.
|
2017-04-14 14:33:15 -04:00
|
|
|
if ($user->use_totp) {
|
2018-04-01 17:46:16 -05:00
|
|
|
$token = str_random(128);
|
|
|
|
$this->cache->put($token, [
|
|
|
|
'user_id' => $user->id,
|
|
|
|
'request_ip' => $request->ip(),
|
|
|
|
], 5);
|
|
|
|
|
2018-04-07 12:35:15 -05:00
|
|
|
return response()->json(['complete' => false, 'token' => $token]);
|
2015-12-10 21:58:17 -05:00
|
|
|
}
|
2017-04-14 14:33:15 -04:00
|
|
|
|
2017-11-18 15:09:58 -06:00
|
|
|
$this->auth->guard()->login($user, true);
|
2017-04-14 14:33:15 -04:00
|
|
|
|
2018-04-07 12:35:15 -05:00
|
|
|
return response()->json(['complete' => true]);
|
2017-04-01 01:58:05 +02:00
|
|
|
}
|
2015-12-06 13:58:49 -05:00
|
|
|
}
|