2016-03-25 01:18:05 -07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Auth;
|
|
|
|
|
|
|
|
use Validator;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
use Illuminate\Foundation\Auth\ThrottlesLogins;
|
2016-03-25 19:26:22 -07:00
|
|
|
use App\Models\Setting;
|
2016-07-13 05:50:24 -07:00
|
|
|
use App\Models\Ldap;
|
2016-03-25 19:26:22 -07:00
|
|
|
use App\Models\User;
|
2016-03-25 01:18:05 -07:00
|
|
|
use Auth;
|
|
|
|
use Config;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Input;
|
|
|
|
use Redirect;
|
|
|
|
use Log;
|
|
|
|
use View;
|
2016-10-29 05:50:55 -07:00
|
|
|
use PragmaRX\Google2FA\Google2FA;
|
2018-12-06 14:05:43 -08:00
|
|
|
use App\Models\LdapAd;
|
2016-03-25 01:18:05 -07:00
|
|
|
|
2016-04-07 13:21:09 -07:00
|
|
|
/**
|
|
|
|
* This controller handles authentication for the user, including local
|
|
|
|
* database users and LDAP users.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @version v1.0
|
|
|
|
*/
|
2016-12-14 04:30:56 -08:00
|
|
|
class LoginController extends Controller
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
|
|
|
|
2016-12-14 04:30:56 -08:00
|
|
|
use ThrottlesLogins;
|
2016-03-25 01:18:05 -07:00
|
|
|
|
|
|
|
// This tells the auth controller to use username instead of email address
|
|
|
|
protected $username = 'username';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Where to redirect users after login / registration.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $redirectTo = '/';
|
|
|
|
|
2018-12-06 14:05:43 -08:00
|
|
|
/**
|
|
|
|
* An LdapAd instance
|
|
|
|
*
|
|
|
|
* @var \App\Models\LdapAd
|
|
|
|
*/
|
|
|
|
protected $ldapAd;
|
|
|
|
|
2016-03-25 01:18:05 -07:00
|
|
|
/**
|
|
|
|
* Create a new authentication controller instance.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2018-12-06 14:05:43 -08:00
|
|
|
public function __construct(LdapAd $ldapAd)
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
2016-10-29 05:50:55 -07:00
|
|
|
$this->middleware('guest', ['except' => ['logout','postTwoFactorAuth','getTwoFactorAuth','getTwoFactorEnroll']]);
|
2017-10-02 17:21:18 -07:00
|
|
|
\Session::put('backUrl', \URL::previous());
|
2018-12-06 14:05:43 -08:00
|
|
|
|
|
|
|
$this->ldapAd = $ldapAd;
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
2018-03-13 20:07:52 -07:00
|
|
|
function showLoginForm(Request $request)
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
2018-03-13 20:07:52 -07:00
|
|
|
$this->loginViaRemoteUser($request);
|
2016-03-25 01:18:05 -07:00
|
|
|
if (Auth::check()) {
|
|
|
|
return redirect()->intended('dashboard');
|
|
|
|
}
|
2018-03-13 20:07:52 -07:00
|
|
|
|
|
|
|
if (Setting::getSettings()->login_common_disabled == "1") {
|
|
|
|
return view('errors.403');
|
|
|
|
}
|
|
|
|
|
2017-06-09 16:44:03 -07:00
|
|
|
return view('auth.login');
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
2018-12-06 14:05:43 -08:00
|
|
|
/**
|
|
|
|
* Log in a user by LDAP
|
|
|
|
*
|
|
|
|
* @author Wes Hulette <jwhulette@gmail.com>
|
|
|
|
*
|
|
|
|
* @since 5.0.0
|
|
|
|
*
|
|
|
|
* @param Request $request
|
|
|
|
*
|
|
|
|
* @return User
|
|
|
|
*
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
private function loginViaLdap(Request $request): User
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
return $this->ldapAd->ldapLogin($request->input('username'), $request->input('password'));
|
|
|
|
} catch (\Exception $ex) {
|
|
|
|
LOG::debug("LDAP user login: " . $ex->getMessage());
|
|
|
|
throw new \Exception($ex->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-13 20:07:52 -07:00
|
|
|
private function loginViaRemoteUser(Request $request)
|
|
|
|
{
|
|
|
|
$remote_user = $request->server('REMOTE_USER');
|
|
|
|
if (Setting::getSettings()->login_remote_user_enabled == "1" && isset($remote_user) && !empty($remote_user)) {
|
|
|
|
LOG::debug("Authenticatiing via REMOTE_USER.");
|
2018-07-16 21:03:19 -07:00
|
|
|
|
|
|
|
$pos = strpos($remote_user, '\\');
|
|
|
|
if ($pos > 0) {
|
|
|
|
$remote_user = substr($remote_user, $pos + 1);
|
|
|
|
};
|
|
|
|
|
2018-03-13 20:07:52 -07:00
|
|
|
try {
|
2018-07-19 10:22:08 -07:00
|
|
|
$user = User::where('username', '=', $remote_user)->whereNull('deleted_at')->where('activated', '=', '1')->first();
|
2018-03-13 20:07:52 -07:00
|
|
|
LOG::debug("Remote user auth lookup complete");
|
|
|
|
if(!is_null($user)) Auth::login($user, true);
|
|
|
|
} catch(Exception $e) {
|
|
|
|
LOG::error("There was an error authenticating the Remote user: " . $e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-12-01 02:25:53 -08:00
|
|
|
|
2016-03-25 01:18:05 -07:00
|
|
|
/**
|
|
|
|
* Account sign in form processing.
|
|
|
|
*
|
|
|
|
* @return Redirect
|
|
|
|
*/
|
2016-07-13 05:50:24 -07:00
|
|
|
public function login(Request $request)
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
2018-03-13 20:07:52 -07:00
|
|
|
if (Setting::getSettings()->login_common_disabled == "1") {
|
|
|
|
return view('errors.403');
|
|
|
|
}
|
|
|
|
|
2016-03-25 01:18:05 -07:00
|
|
|
$validator = $this->validator(Input::all());
|
|
|
|
|
|
|
|
if ($validator->fails()) {
|
2016-04-28 21:06:41 -07:00
|
|
|
return redirect()->back()->withInput()->withErrors($validator);
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
2016-12-01 02:04:15 -08:00
|
|
|
|
|
|
|
$this->maxLoginAttempts = config('auth.throttle.max_attempts');
|
|
|
|
$this->lockoutTime = config('auth.throttle.lockout_duration');
|
|
|
|
|
2016-12-14 04:30:56 -08:00
|
|
|
if ($lockedOut = $this->hasTooManyLoginAttempts($request)) {
|
2016-12-01 02:04:15 -08:00
|
|
|
$this->fireLockoutEvent($request);
|
|
|
|
return $this->sendLockoutResponse($request);
|
|
|
|
}
|
|
|
|
|
2016-10-29 05:50:55 -07:00
|
|
|
$user = null;
|
2016-11-30 20:39:43 -08:00
|
|
|
|
2016-03-25 01:18:05 -07:00
|
|
|
// Should we even check for LDAP users?
|
2016-03-25 19:26:22 -07:00
|
|
|
if (Setting::getSettings()->ldap_enabled=='1') {
|
2016-03-25 01:18:05 -07:00
|
|
|
LOG::debug("LDAP is enabled.");
|
2016-07-13 05:50:24 -07:00
|
|
|
try {
|
2018-12-06 14:05:43 -08:00
|
|
|
LOG::debug("Attempting to log user in by LDAP authentication.");
|
2018-03-13 20:07:52 -07:00
|
|
|
$user = $this->loginViaLdap($request);
|
2016-12-29 14:02:18 -08:00
|
|
|
Auth::login($user, true);
|
2016-11-30 20:39:43 -08:00
|
|
|
|
|
|
|
// If the user was unable to login via LDAP, log the error and let them fall through to
|
|
|
|
// local authentication.
|
2016-07-13 05:50:24 -07:00
|
|
|
} catch (\Exception $e) {
|
2016-11-30 20:39:43 -08:00
|
|
|
LOG::error("There was an error authenticating the LDAP user: ".$e->getMessage());
|
2016-07-13 05:50:24 -07:00
|
|
|
}
|
2016-10-29 05:50:55 -07:00
|
|
|
}
|
2016-03-25 01:18:05 -07:00
|
|
|
|
2016-10-29 05:50:55 -07:00
|
|
|
// If the user wasn't authenticated via LDAP, skip to local auth
|
2016-12-29 14:02:18 -08:00
|
|
|
if (!$user) {
|
|
|
|
LOG::debug("Authenticating user against database.");
|
2016-10-29 05:50:55 -07:00
|
|
|
// Try to log the user in
|
2018-07-16 23:48:46 -07:00
|
|
|
if (!Auth::attempt(['username' => $request->input('username'), 'password' => $request->input('password'), 'activated' => 1], $request->input('remember'))) {
|
2016-12-01 02:04:15 -08:00
|
|
|
|
2016-12-29 14:02:18 -08:00
|
|
|
if (!$lockedOut) {
|
|
|
|
$this->incrementLoginAttempts($request);
|
|
|
|
}
|
2016-12-01 02:04:15 -08:00
|
|
|
|
2016-12-29 14:02:18 -08:00
|
|
|
LOG::debug("Local authentication failed.");
|
|
|
|
return redirect()->back()->withInput()->with('error', trans('auth/message.account_not_found'));
|
|
|
|
} else {
|
2017-03-03 18:28:13 -08:00
|
|
|
|
2016-12-01 02:04:15 -08:00
|
|
|
$this->clearLoginAttempts($request);
|
2016-12-29 14:02:18 -08:00
|
|
|
}
|
2016-10-29 05:50:55 -07:00
|
|
|
}
|
2017-03-03 18:28:13 -08:00
|
|
|
|
|
|
|
if ($user = Auth::user()) {
|
|
|
|
$user->last_login = \Carbon::now();
|
|
|
|
\Log::debug('Last login:'.$user->last_login);
|
|
|
|
$user->save();
|
|
|
|
}
|
2016-10-29 05:50:55 -07:00
|
|
|
// Redirect to the users page
|
2017-01-12 07:09:44 -08:00
|
|
|
return redirect()->intended()->with('success', trans('auth/message.signin.success'));
|
2016-10-29 05:50:55 -07:00
|
|
|
}
|
2016-07-13 05:50:24 -07:00
|
|
|
|
2016-03-25 01:18:05 -07:00
|
|
|
|
2016-10-29 05:50:55 -07:00
|
|
|
/**
|
|
|
|
* Two factor enrollment page
|
|
|
|
*
|
|
|
|
* @return Redirect
|
|
|
|
*/
|
|
|
|
public function getTwoFactorEnroll()
|
|
|
|
{
|
2016-03-25 01:18:05 -07:00
|
|
|
|
2016-10-29 05:50:55 -07:00
|
|
|
if (!Auth::check()) {
|
|
|
|
return redirect()->route('login')->with('error', 'You must be logged in.');
|
|
|
|
}
|
2016-03-25 01:18:05 -07:00
|
|
|
|
2016-10-29 05:50:55 -07:00
|
|
|
$user = Auth::user();
|
2018-07-24 20:11:35 -07:00
|
|
|
$google2fa = app()->make('pragmarx.google2fa');
|
2016-07-14 23:49:32 -07:00
|
|
|
|
2016-10-29 05:50:55 -07:00
|
|
|
if ($user->two_factor_secret=='') {
|
2016-10-31 11:20:31 -07:00
|
|
|
$user->two_factor_secret = $google2fa->generateSecretKey(32);
|
2016-10-29 05:50:55 -07:00
|
|
|
$user->save();
|
|
|
|
}
|
2016-07-14 23:49:32 -07:00
|
|
|
|
2016-08-04 14:29:28 -07:00
|
|
|
|
2018-07-24 20:11:35 -07:00
|
|
|
$google2fa_url = $google2fa->getQRCodeInline(
|
2016-10-31 22:34:57 -07:00
|
|
|
urlencode(Setting::getSettings()->site_name),
|
2016-11-11 20:09:22 -08:00
|
|
|
urlencode($user->username),
|
2016-10-29 05:50:55 -07:00
|
|
|
$user->two_factor_secret
|
|
|
|
);
|
2016-03-25 01:18:05 -07:00
|
|
|
|
2017-06-09 16:44:03 -07:00
|
|
|
return view('auth.two_factor_enroll')->with('google2fa_url', $google2fa_url);
|
2016-08-04 14:29:28 -07:00
|
|
|
|
2016-10-29 05:50:55 -07:00
|
|
|
}
|
2016-03-25 01:18:05 -07:00
|
|
|
|
|
|
|
|
2016-10-29 05:50:55 -07:00
|
|
|
/**
|
|
|
|
* Two factor code form page
|
|
|
|
*
|
|
|
|
* @return Redirect
|
|
|
|
*/
|
2016-12-29 14:02:18 -08:00
|
|
|
public function getTwoFactorAuth()
|
|
|
|
{
|
2017-06-09 16:44:03 -07:00
|
|
|
return view('auth.two_factor');
|
2016-10-29 05:50:55 -07:00
|
|
|
}
|
2016-03-25 01:18:05 -07:00
|
|
|
|
2016-10-29 05:50:55 -07:00
|
|
|
/**
|
|
|
|
* Two factor code submission
|
|
|
|
*
|
|
|
|
* @return Redirect
|
|
|
|
*/
|
2016-12-29 14:02:18 -08:00
|
|
|
public function postTwoFactorAuth(Request $request)
|
|
|
|
{
|
2016-08-04 14:29:28 -07:00
|
|
|
|
2016-10-29 05:50:55 -07:00
|
|
|
if (!Auth::check()) {
|
|
|
|
return redirect()->route('login')->with('error', 'You must be logged in.');
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
2016-10-29 05:50:55 -07:00
|
|
|
$user = Auth::user();
|
|
|
|
$secret = $request->get('two_factor_secret');
|
2018-07-24 20:11:35 -07:00
|
|
|
$google2fa = app()->make('pragmarx.google2fa');
|
2016-10-29 05:50:55 -07:00
|
|
|
$valid = $google2fa->verifyKey($user->two_factor_secret, $secret);
|
2016-08-04 14:29:28 -07:00
|
|
|
|
2016-10-29 05:50:55 -07:00
|
|
|
if ($valid) {
|
|
|
|
$user->two_factor_enrolled = 1;
|
|
|
|
$user->save();
|
|
|
|
$request->session()->put('2fa_authed', 'true');
|
|
|
|
return redirect()->route('home')->with('success', 'You are logged in!');
|
|
|
|
}
|
2016-08-04 14:29:28 -07:00
|
|
|
|
2016-10-29 05:50:55 -07:00
|
|
|
return redirect()->route('two-factor')->with('error', 'Invalid two-factor code');
|
2016-03-25 01:18:05 -07:00
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-10-29 05:50:55 -07:00
|
|
|
|
2016-03-25 01:18:05 -07:00
|
|
|
/**
|
|
|
|
* Logout page.
|
|
|
|
*
|
|
|
|
* @return Redirect
|
|
|
|
*/
|
2016-10-29 05:50:55 -07:00
|
|
|
public function logout(Request $request)
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
2016-10-29 05:50:55 -07:00
|
|
|
$request->session()->forget('2fa_authed');
|
2018-03-13 20:07:52 -07:00
|
|
|
|
2016-03-25 01:18:05 -07:00
|
|
|
Auth::logout();
|
2018-03-13 20:07:52 -07:00
|
|
|
|
|
|
|
$settings = Setting::getSettings();
|
|
|
|
$customLogoutUrl = $settings->login_remote_user_custom_logout_url ;
|
|
|
|
if ($settings->login_remote_user_enabled == '1' && $customLogoutUrl != '') {
|
|
|
|
return redirect()->away($customLogoutUrl);
|
|
|
|
}
|
|
|
|
|
2016-10-29 05:50:55 -07:00
|
|
|
return redirect()->route('login')->with('success', 'You have successfully logged out!');
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a validator for an incoming registration request.
|
|
|
|
*
|
|
|
|
* @param array $data
|
|
|
|
* @return \Illuminate\Contracts\Validation\Validator
|
|
|
|
*/
|
|
|
|
protected function validator(array $data)
|
|
|
|
{
|
|
|
|
return Validator::make($data, [
|
|
|
|
'username' => 'required',
|
|
|
|
'password' => 'required',
|
|
|
|
]);
|
|
|
|
}
|
2016-12-01 02:04:15 -08:00
|
|
|
|
2016-12-14 05:06:51 -08:00
|
|
|
|
|
|
|
public function username()
|
2016-12-01 02:04:15 -08:00
|
|
|
{
|
2016-12-14 05:06:51 -08:00
|
|
|
return 'username';
|
2016-12-01 02:04:15 -08:00
|
|
|
}
|
|
|
|
|
2016-12-14 05:06:51 -08:00
|
|
|
/**
|
|
|
|
* Redirect the user after determining they are locked out.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
*/
|
|
|
|
protected function sendLockoutResponse(Request $request)
|
|
|
|
{
|
2016-12-14 06:30:51 -08:00
|
|
|
$seconds = $this->limiter()->availableIn(
|
|
|
|
$this->throttleKey($request)
|
|
|
|
);
|
|
|
|
|
|
|
|
$minutes = round($seconds / 60);
|
2016-12-14 05:06:51 -08:00
|
|
|
|
2016-12-29 14:02:18 -08:00
|
|
|
$message = \Lang::get('auth/message.throttle', ['minutes' => $minutes]);
|
2016-12-14 06:30:51 -08:00
|
|
|
|
|
|
|
return redirect()->back()
|
|
|
|
->withInput($request->only($this->username(), 'remember'))
|
|
|
|
->withErrors([$this->username() => $message]);
|
|
|
|
}
|
2016-12-14 05:06:51 -08:00
|
|
|
|
2016-12-14 06:30:51 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Override the lockout time and duration
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
*/
|
|
|
|
protected function hasTooManyLoginAttempts(Request $request)
|
|
|
|
{
|
|
|
|
$lockoutTime = config('auth.throttle.lockout_duration');
|
|
|
|
$maxLoginAttempts = config('auth.throttle.max_attempts');
|
|
|
|
|
|
|
|
return $this->limiter()->tooManyAttempts(
|
2016-12-29 14:02:18 -08:00
|
|
|
$this->throttleKey($request),
|
|
|
|
$maxLoginAttempts,
|
|
|
|
$lockoutTime
|
2016-12-14 06:30:51 -08:00
|
|
|
);
|
2016-12-14 05:06:51 -08:00
|
|
|
}
|
2017-02-02 18:14:25 -08:00
|
|
|
|
|
|
|
public function legacyAuthRedirect() {
|
|
|
|
return redirect()->route('login');
|
|
|
|
}
|
|
|
|
|
2017-10-02 16:00:42 -07:00
|
|
|
public function redirectTo()
|
|
|
|
{
|
|
|
|
return Session::get('backUrl') ? Session::get('backUrl') : $this->redirectTo;
|
|
|
|
}
|
|
|
|
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|