Files
pterodactyl-panel/app/Http/Controllers/Auth/LoginController.php
T

154 lines
4.6 KiB
PHP
Raw Normal View History

2015-12-06 13:58:49 -05:00
<?php
2016-01-19 19:10:39 -05:00
/**
2016-01-20 16:05:16 -05:00
* Pterodactyl - Panel
2016-01-19 19:10:39 -05:00
* Copyright (c) 2015 - 2016 Dane Everitt <dane@daneeveritt.com>
2016-01-20 15:56:40 -05:00
* Some Modifications (c) 2015 Dylan Seidt <dylan.seidt@gmail.com>
2016-01-19 19:10:39 -05:00
*
2016-01-20 15:56:40 -05:00
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
2016-01-19 19:10:39 -05:00
*
2016-01-20 15:56:40 -05:00
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
2016-01-19 19:10:39 -05:00
*
2016-01-20 15:56:40 -05:00
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
2016-01-19 19:10:39 -05:00
*/
2015-12-06 13:58:49 -05:00
namespace Pterodactyl\Http\Controllers\Auth;
use Pterodactyl\Models\User;
use Auth;
2015-12-10 21:58:17 -05:00
use Alert;
use Validator;
2015-12-06 13:58:49 -05:00
use Pterodactyl\Http\Controllers\Controller;
2015-12-10 19:40:59 -06:00
use PragmaRX\Google2FA\Google2FA;
2015-12-06 13:58:49 -05:00
use Illuminate\Http\Request;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
2016-09-03 17:09:00 -04:00
use Illuminate\Foundation\Auth\AuthenticatesUsers;
class LoginController extends Controller
2015-12-06 13:58:49 -05:00
{
/*
|--------------------------------------------------------------------------
2016-09-03 17:09:00 -04:00
| Login Controller
2015-12-06 13:58:49 -05:00
|--------------------------------------------------------------------------
|
2016-09-03 17:09:00 -04:00
| This controller handles authenticating users for the application and
| redirecting them to your home screen. The controller uses a trait
| to conveniently provide its functionality to your applications.
2015-12-06 13:58:49 -05:00
|
*/
2016-09-03 17:09:00 -04:00
use AuthenticatesUsers;
2015-12-06 13:58:49 -05:00
/**
2016-09-03 17:09:00 -04:00
* Where to redirect users after login / registration.
2015-12-06 13:58:49 -05:00
*
* @var string
*/
2016-09-03 17:09:00 -04:00
protected $redirectTo = '/';
2015-12-06 13:58:49 -05:00
/**
* Lockout time for failed login requests.
*
* @var integer
*/
protected $lockoutTime = 120;
/**
* After how many attempts should logins be throttled and locked.
*
* @var integer
*/
2015-12-10 21:58:17 -05:00
protected $maxLoginAttempts = 3;
2015-12-06 13:58:49 -05:00
/**
2016-09-03 17:09:00 -04:00
* Create a new controller instance.
2015-12-06 13:58:49 -05:00
*
* @return void
*/
public function __construct()
{
2016-09-03 17:09:00 -04:00
$this->middleware('guest', ['except' => 'logout']);
2015-12-06 13:58:49 -05:00
}
2015-12-10 21:58:17 -05:00
/**
* Handle a login request to the application.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
2016-09-03 17:09:00 -04:00
public function login(Request $request)
2015-12-10 21:58:17 -05:00
{
$this->validate($request, [
'email' => 'required|email',
'password' => 'required',
]);
2016-09-03 17:09:00 -04:00
if ($lockedOut = $this->hasTooManyLoginAttempts($request)) {
$this->fireLockoutEvent($request);
2015-12-10 21:58:17 -05:00
return $this->sendLockoutResponse($request);
}
2015-12-13 21:30:57 -05:00
// Is the email & password valid?
if (!Auth::attempt([
'email' => $request->input('email'),
'password' => $request->input('password')
], $request->has('remember'))) {
2016-09-03 17:09:00 -04:00
if (!$lockedOut) {
2015-12-13 21:30:57 -05:00
$this->incrementLoginAttempts($request);
}
2016-09-03 17:09:00 -04:00
return $this->sendFailedLoginResponse($request);
2015-12-13 21:30:57 -05:00
}
2015-12-10 21:58:17 -05:00
$G2FA = new Google2FA();
2015-12-13 21:30:57 -05:00
$user = User::select('use_totp', 'totp_secret')->where('email', $request->input('email'))->first();
2015-12-10 21:58:17 -05:00
// Verify TOTP Token was Valid
if($user->use_totp === 1) {
if(!$G2FA->verifyKey($user->totp_secret, $request->input('totp_token'))) {
2015-12-13 21:30:57 -05:00
Auth::logout();
2016-09-03 17:09:00 -04:00
if (!$lockedOut) {
2015-12-10 21:58:17 -05:00
$this->incrementLoginAttempts($request);
}
Alert::danger(trans('auth.totp_failed'))->flash();
2016-09-03 17:09:00 -04:00
return $this->sendFailedLoginResponse($request);
2015-12-10 21:58:17 -05:00
}
}
2016-09-03 17:09:00 -04:00
return $this->sendLoginResponse($request);
2015-12-10 21:58:17 -05:00
}
/**
* Check if the provided user has TOTP enabled.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function checkTotp(Request $request)
{
return response()->json(User::select('id')->where('email', $request->input('email'))->where('use_totp', 1)->first());
}
2015-12-06 13:58:49 -05:00
}