Improved TOTp handling in login.
Cleaned up the code a bit, also checks TOTP before attemping to verify user. This addresses the potential for an attacker to try at a password and/or confirm that the password is correct unless they have a valid TOTP code for the request. A failed TOTP response will trigger a throttle count on the login as well.
This commit is contained in:
parent
7345385442
commit
288ee1a258
5 changed files with 113 additions and 101 deletions
|
@ -4,8 +4,9 @@ namespace Pterodactyl\Http\Controllers\Auth;
|
||||||
|
|
||||||
use Pterodactyl\Models\User;
|
use Pterodactyl\Models\User;
|
||||||
|
|
||||||
use Validator;
|
|
||||||
use Auth;
|
use Auth;
|
||||||
|
use Alert;
|
||||||
|
use Validator;
|
||||||
|
|
||||||
use Pterodactyl\Http\Controllers\Controller;
|
use Pterodactyl\Http\Controllers\Controller;
|
||||||
use PragmaRX\Google2FA\Google2FA;
|
use PragmaRX\Google2FA\Google2FA;
|
||||||
|
@ -28,73 +29,6 @@ class AuthController extends Controller
|
||||||
|
|
||||||
use AuthenticatesAndRegistersUsers, ThrottlesLogins;
|
use AuthenticatesAndRegistersUsers, ThrottlesLogins;
|
||||||
|
|
||||||
/**
|
|
||||||
* Handle a login request to the application.
|
|
||||||
*
|
|
||||||
* @param \Illuminate\Http\Request $request
|
|
||||||
* @return \Illuminate\Http\Response
|
|
||||||
*/
|
|
||||||
public function postLogin(Request $request)
|
|
||||||
{
|
|
||||||
$this->validate($request, [
|
|
||||||
$this->loginUsername() => 'required', 'password' => 'required',
|
|
||||||
]);
|
|
||||||
|
|
||||||
$throttles = $this->isUsingThrottlesLoginsTrait();
|
|
||||||
|
|
||||||
if ($throttles && $this->hasTooManyLoginAttempts($request)) {
|
|
||||||
return $this->sendLockoutResponse($request);
|
|
||||||
}
|
|
||||||
|
|
||||||
$credentials = $this->getCredentials($request);
|
|
||||||
|
|
||||||
if (Auth::attempt($credentials, $request->has('remember'))) {
|
|
||||||
if(User::select('id')->where('email', $request->input('email'))->where('use_totp', 1)->exists()) {
|
|
||||||
$validator = Validator::make($request->all(), [
|
|
||||||
'totp_token' => 'required|numeric'
|
|
||||||
]);
|
|
||||||
|
|
||||||
if($validator->fails()) {
|
|
||||||
Auth::logout();
|
|
||||||
return redirect('auth/login')->withErrors($validator)->withInput();
|
|
||||||
}
|
|
||||||
|
|
||||||
$google2fa = new Google2FA();
|
|
||||||
|
|
||||||
if($google2fa->verifyKey(User::where('email', $request->input('email'))->first()->totp_secret, $request->input('totp_token'))) {
|
|
||||||
return $this->handleUserWasAuthenticated($request, $throttles);
|
|
||||||
} else {
|
|
||||||
Auth::logout();
|
|
||||||
$validator->errors()->add('field', trans('validation.welcome'));
|
|
||||||
return redirect('auth/login')->withErrors($validator)->withInput();
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return $this->handleUserWasAuthenticated($request, $throttles);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($throttles) {
|
|
||||||
$this->incrementLoginAttempts($request);
|
|
||||||
}
|
|
||||||
|
|
||||||
return redirect($this->loginPath())
|
|
||||||
->withInput($request->only($this->loginUsername(), 'remember'))
|
|
||||||
->withErrors([
|
|
||||||
$this->loginUsername() => $this->getFailedLoginMessage(),
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Post-Authentication redirect location.
|
* Post-Authentication redirect location.
|
||||||
*
|
*
|
||||||
|
@ -121,7 +55,7 @@ class AuthController extends Controller
|
||||||
*
|
*
|
||||||
* @var integer
|
* @var integer
|
||||||
*/
|
*/
|
||||||
protected $maxLoginAttempts = 5;
|
protected $maxLoginAttempts = 3;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new authentication controller instance.
|
* Create a new authentication controller instance.
|
||||||
|
@ -162,4 +96,70 @@ class AuthController extends Controller
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handle a login request to the application.
|
||||||
|
*
|
||||||
|
* @param \Illuminate\Http\Request $request
|
||||||
|
* @return \Illuminate\Http\Response
|
||||||
|
*/
|
||||||
|
public function postLogin(Request $request)
|
||||||
|
{
|
||||||
|
|
||||||
|
$this->validate($request, [
|
||||||
|
'email' => 'required|email',
|
||||||
|
'password' => 'required',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$throttled = $this->isUsingThrottlesLoginsTrait();
|
||||||
|
if ($throttled && $this->hasTooManyLoginAttempts($request)) {
|
||||||
|
return $this->sendLockoutResponse($request);
|
||||||
|
}
|
||||||
|
|
||||||
|
$G2FA = new Google2FA();
|
||||||
|
$user = User::select('use_totp', 'totp_secret')->where('email', $request->input($this->loginUsername()))->first();
|
||||||
|
|
||||||
|
// Verify TOTP Token was Valid
|
||||||
|
if($user->use_totp === 1) {
|
||||||
|
if(!$G2FA->verifyKey($user->totp_secret, $request->input('totp_token'))) {
|
||||||
|
|
||||||
|
if ($throttled) {
|
||||||
|
$this->incrementLoginAttempts($request);
|
||||||
|
}
|
||||||
|
|
||||||
|
Alert::danger(trans('auth.totp_failed'))->flash();
|
||||||
|
return redirect()->route('auth.login')->withInput($request->only('email', 'remember'));
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Attempt to Login
|
||||||
|
if (Auth::attempt([
|
||||||
|
'email' => $request->input('email'),
|
||||||
|
'password' => $request->input('password')
|
||||||
|
], $request->has('remember'))) {
|
||||||
|
return $this->handleUserWasAuthenticated($request, $throttled);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($throttled) {
|
||||||
|
$this->incrementLoginAttempts($request);
|
||||||
|
}
|
||||||
|
|
||||||
|
return redirect()->route('auth.login')
|
||||||
|
->withInput($request->only('email', 'remember'))
|
||||||
|
->withErrors([
|
||||||
|
'email' => $this->getFailedLoginMessage(),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,19 +10,21 @@ class AuthRoutes {
|
||||||
|
|
||||||
public function map(Router $router) {
|
public function map(Router $router) {
|
||||||
$router->group(['prefix' => 'auth'], function () use ($router) {
|
$router->group(['prefix' => 'auth'], function () use ($router) {
|
||||||
|
|
||||||
$router->get('login', [ 'as' => 'auth.login', 'uses' => 'Auth\AuthController@getLogin' ]);
|
$router->get('login', [ 'as' => 'auth.login', 'uses' => 'Auth\AuthController@getLogin' ]);
|
||||||
$router->post('login/totp', [ 'as' => 'auth.login.totp', 'uses' => 'Auth\AuthController@checkTotp' ]);
|
$router->post('login', [ 'uses' => 'Auth\AuthController@postLogin' ]);
|
||||||
$router->post('login', [ 'as' => 'auth.login.submit', 'uses' => 'Auth\AuthController@postLogin' ]);
|
$router->post('login/totp', [ 'uses' => 'Auth\AuthController@checkTotp' ]);
|
||||||
|
|
||||||
|
|
||||||
$router->get('password', [ 'as' => 'auth.password', 'uses' => 'Auth\PasswordController@getEmail' ]);
|
$router->get('password', [ 'as' => 'auth.password', 'uses' => 'Auth\PasswordController@getEmail' ]);
|
||||||
$router->post('password', [ 'as' => 'auth.password.submit', 'uses' => 'Auth\PasswordController@postEmail' ], function () {
|
$router->post('password', [ 'as' => 'auth.password.submit', 'uses' => 'Auth\PasswordController@postEmail' ], function () {
|
||||||
return redirect('auth/password')->with('sent', true);
|
return redirect('auth/password')->with('sent', true);
|
||||||
});
|
});
|
||||||
|
$router->post('password/verify', [ 'uses' => 'Auth\PasswordController@postReset' ]);
|
||||||
$router->get('password/verify/{token}', [ 'as' => 'auth.verify', 'uses' => 'Auth\PasswordController@getReset' ]);
|
$router->get('password/verify/{token}', [ 'as' => 'auth.verify', 'uses' => 'Auth\PasswordController@getReset' ]);
|
||||||
$router->post('password/verify', [ 'as' => 'auth.verify.submit', 'uses' => 'Auth\PasswordController@postReset' ]);
|
|
||||||
|
|
||||||
$router->get('logout', [ 'as' => 'auth.logout', 'uses' => 'Auth\AuthController@getLogout' ]);
|
$router->get('logout', [ 'as' => 'auth.logout', 'uses' => 'Auth\AuthController@getLogout' ]);
|
||||||
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -21,5 +21,6 @@ return [
|
||||||
'sendlink' => 'Send Password Reset Link',
|
'sendlink' => 'Send Password Reset Link',
|
||||||
'emailsent' => 'Your password reset email is on its way.',
|
'emailsent' => 'Your password reset email is on its way.',
|
||||||
'remeberme' => 'Remeber Me',
|
'remeberme' => 'Remeber Me',
|
||||||
|
'totp_failed' => 'The TOTP token provided was invalid. Please ensure that the token generated by your device was valid.'
|
||||||
|
|
||||||
];
|
];
|
||||||
|
|
|
@ -12,7 +12,6 @@ return [
|
||||||
'password' => 'Password',
|
'password' => 'Password',
|
||||||
'email' => 'Email',
|
'email' => 'Email',
|
||||||
'whoops' => 'Whoops',
|
'whoops' => 'Whoops',
|
||||||
'failed' => 'Your request could not be processed. Please try again later.',
|
|
||||||
'success' => 'Success',
|
'success' => 'Success',
|
||||||
'location' => 'Location',
|
'location' => 'Location',
|
||||||
'node' => 'Node',
|
'node' => 'Node',
|
||||||
|
|
|
@ -24,6 +24,14 @@
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
@endif
|
@endif
|
||||||
|
@foreach (Alert::getMessages() as $type => $messages)
|
||||||
|
@foreach ($messages as $message)
|
||||||
|
<div class="alert alert-{{ $type }} alert-dismissable" role="alert">
|
||||||
|
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
|
||||||
|
{{ $message }}
|
||||||
|
</div>
|
||||||
|
@endforeach
|
||||||
|
@endforeach
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label for="email" class="control-label">{{ trans('strings.email') }}</label>
|
<label for="email" class="control-label">{{ trans('strings.email') }}</label>
|
||||||
<div>
|
<div>
|
||||||
|
@ -76,32 +84,34 @@
|
||||||
<div class="col-md-3"></div>
|
<div class="col-md-3"></div>
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
$(document).ready(function() {
|
$(document).ready(function() {
|
||||||
$("#login-form").one("submit", function(event) {
|
$('#login-form').submit(function (event) {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
var check_email = $("#email").val();
|
var check_email = $('#email').val();
|
||||||
$.ajax({
|
$.ajax({
|
||||||
type: 'POST',
|
type: 'POST',
|
||||||
url: '/auth/login/totp',
|
url: '/auth/login/totp',
|
||||||
|
headers: {
|
||||||
|
'X-CSRF-TOKEN': '{{ csrf_token() }}'
|
||||||
|
},
|
||||||
data: {
|
data: {
|
||||||
email: check_email,
|
email: check_email
|
||||||
_token: '{!! csrf_token() !!}'
|
|
||||||
}
|
}
|
||||||
}).done(function (data) {
|
}).done(function (data) {
|
||||||
if (typeof data.id !== 'undefined') {
|
if (typeof data.id !== 'undefined') {
|
||||||
$("#openTOTP").modal('show');
|
$('#openTOTP').modal('show');
|
||||||
$('#openTOTP').on('shown.bs.modal', function() {
|
$('#openTOTP').on('shown.bs.modal', function() {
|
||||||
$("#totp_token").focus();
|
$('#totp_token').focus();
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
$("#login-form").submit();
|
$('#login-form').submit();
|
||||||
}
|
}
|
||||||
}).fail(function (jqXHR) {
|
}).fail(function (jqXHR) {
|
||||||
alert("{{ trans('strings.failed') }}");
|
alert('Unable to validate potential TOTP need.');
|
||||||
|
console.error(jqXHR);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
$("#totp-form").submit(function() {
|
$('#totp-form').submit(function () {
|
||||||
$('#login-form :input').not(':submit').clone().hide().appendTo('#totp-form');
|
return $('#login-form :input').not(':submit').clone().hide().appendTo('#totp-form');
|
||||||
return true;
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
Loading…
Reference in a new issue