Implement Two-factor authentication
This commit is contained in:
parent
59ff1ebbe6
commit
4585753d04
6 changed files with 102 additions and 26 deletions
|
@ -8,6 +8,7 @@ use Validator;
|
||||||
use Auth;
|
use Auth;
|
||||||
|
|
||||||
use Pterodactyl\Http\Controllers\Controller;
|
use Pterodactyl\Http\Controllers\Controller;
|
||||||
|
use PragmaRX\Google2FA\Google2FA;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Foundation\Auth\ThrottlesLogins;
|
use Illuminate\Foundation\Auth\ThrottlesLogins;
|
||||||
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
|
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
|
||||||
|
@ -27,6 +28,73 @@ 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.
|
||||||
*
|
*
|
||||||
|
|
|
@ -35,7 +35,7 @@ class RedirectIfAuthenticated
|
||||||
public function handle($request, Closure $next)
|
public function handle($request, Closure $next)
|
||||||
{
|
{
|
||||||
if ($this->auth->check()) {
|
if ($this->auth->check()) {
|
||||||
return redirect('/home');
|
return redirect('/');
|
||||||
}
|
}
|
||||||
|
|
||||||
return $next($request);
|
return $next($request);
|
||||||
|
|
|
@ -3,12 +3,15 @@
|
||||||
namespace Pterodactyl\Http\Routes;
|
namespace Pterodactyl\Http\Routes;
|
||||||
|
|
||||||
use Illuminate\Routing\Router;
|
use Illuminate\Routing\Router;
|
||||||
|
use Request;
|
||||||
|
use Pterodactyl\Models\User as User;
|
||||||
|
|
||||||
class AuthRoutes {
|
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', [ 'as' => 'auth.login.submit', 'uses' => 'Auth\AuthController@postLogin' ]);
|
$router->post('login', [ 'as' => 'auth.login.submit', 'uses' => 'Auth\AuthController@postLogin' ]);
|
||||||
|
|
||||||
$router->get('password', [ 'as' => 'auth.password', 'uses' => 'Auth\PasswordController@getEmail' ]);
|
$router->get('password', [ 'as' => 'auth.password', 'uses' => 'Auth\PasswordController@getEmail' ]);
|
||||||
|
|
|
@ -12,6 +12,7 @@ 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',
|
||||||
|
|
|
@ -72,6 +72,7 @@ return [
|
||||||
'array' => 'The :attribute must contain :size items.',
|
'array' => 'The :attribute must contain :size items.',
|
||||||
],
|
],
|
||||||
'string' => 'The :attribute must be a string.',
|
'string' => 'The :attribute must be a string.',
|
||||||
|
'totp' => 'The totp token is invalid. Did it expire?',
|
||||||
'timezone' => 'The :attribute must be a valid zone.',
|
'timezone' => 'The :attribute must be a valid zone.',
|
||||||
'unique' => 'The :attribute has already been taken.',
|
'unique' => 'The :attribute has already been taken.',
|
||||||
'url' => 'The :attribute format is invalid.',
|
'url' => 'The :attribute format is invalid.',
|
||||||
|
|
|
@ -75,31 +75,34 @@
|
||||||
</div>
|
</div>
|
||||||
<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").submit(function(event){
|
$("#login-form").one("submit", function(event) {
|
||||||
// var check_email = $("#email").val();
|
event.preventDefault();
|
||||||
// $.ajax({
|
var check_email = $("#email").val();
|
||||||
// type: "POST",
|
$.ajax({
|
||||||
// url: "/auth/login/totp",
|
type: 'POST',
|
||||||
// async: false,
|
url: '/auth/login/totp',
|
||||||
// data: { check: check_email },
|
data: {
|
||||||
// success: function(data){
|
email: check_email,
|
||||||
// if(data == 'true'){
|
_token: '{!! csrf_token() !!}'
|
||||||
// $("#openTOTP").modal('show');
|
}
|
||||||
// $('#openTOTP').on('shown.bs.modal', function(){
|
}).done(function(data) {
|
||||||
// $("#totp_token").focus();
|
if (typeof data.id !== 'undefined') {
|
||||||
// })
|
$("#openTOTP").modal('show');
|
||||||
// event.preventDefault();
|
$('#openTOTP').on('shown.bs.modal', function() {
|
||||||
// }else{
|
$("#totp_token").focus();
|
||||||
// $(this).submit();
|
});
|
||||||
// }
|
} else {
|
||||||
// }
|
$("#login-form").submit();
|
||||||
// });
|
}
|
||||||
// });
|
}).fail(function(jqXHR) {
|
||||||
// $("#totp-form").submit(function(){
|
alert("{{ trans('strings.failed') }}");
|
||||||
// $('#login-form :input').not(':submit').clone().hide().appendTo('#totp-form');
|
});
|
||||||
// return true;
|
});
|
||||||
// });
|
$("#totp-form").submit(function() {
|
||||||
|
$('#login-form :input').not(':submit').clone().hide().appendTo('#totp-form');
|
||||||
|
return true;
|
||||||
|
});
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
@endsection
|
@endsection
|
||||||
|
|
Loading…
Reference in a new issue