2016-09-15 23:19:40 -05:00
|
|
|
<?php
|
2017-10-21 01:40:00 -05:00
|
|
|
/**
|
|
|
|
* LoginController.php
|
2020-01-31 00:32:04 -06:00
|
|
|
* Copyright (c) 2020 james@firefly-iii.org
|
2017-10-21 01:40:00 -05:00
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
2017-10-21 01:40:00 -05:00
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
2017-10-21 01:40:00 -05:00
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2017-10-21 01:40:00 -05:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2019-10-01 23:37:26 -05:00
|
|
|
* GNU Affero General Public License for more details.
|
2017-10-21 01:40:00 -05:00
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2017-10-21 01:40:00 -05:00
|
|
|
*/
|
2017-09-14 10:40:02 -05:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2016-09-15 23:19:40 -05:00
|
|
|
namespace FireflyIII\Http\Controllers\Auth;
|
|
|
|
|
2019-02-16 12:04:36 -06:00
|
|
|
use Adldap;
|
2020-11-14 23:13:56 -06:00
|
|
|
use Cookie;
|
2018-03-08 13:44:56 -06:00
|
|
|
use DB;
|
2021-09-18 03:20:19 -05:00
|
|
|
use FireflyIII\Exceptions\FireflyException;
|
2016-09-15 23:19:40 -05:00
|
|
|
use FireflyIII\Http\Controllers\Controller;
|
2020-01-01 08:34:38 -06:00
|
|
|
use FireflyIII\Providers\RouteServiceProvider;
|
2021-09-18 03:26:12 -05:00
|
|
|
use Illuminate\Contracts\Foundation\Application;
|
2020-03-17 09:01:00 -05:00
|
|
|
use Illuminate\Contracts\View\Factory;
|
2021-09-18 03:26:12 -05:00
|
|
|
use Illuminate\Contracts\View\View;
|
2016-09-15 23:19:40 -05:00
|
|
|
use Illuminate\Foundation\Auth\AuthenticatesUsers;
|
2021-07-22 23:26:42 -05:00
|
|
|
use Illuminate\Foundation\Auth\ThrottlesLogins;
|
2020-03-17 09:01:00 -05:00
|
|
|
use Illuminate\Http\JsonResponse;
|
|
|
|
use Illuminate\Http\RedirectResponse;
|
2017-09-14 09:13:25 -05:00
|
|
|
use Illuminate\Http\Request;
|
2021-09-18 03:20:19 -05:00
|
|
|
use Illuminate\Http\Response;
|
2021-09-18 03:26:12 -05:00
|
|
|
use Illuminate\Routing\Redirector;
|
2020-01-01 08:34:38 -06:00
|
|
|
use Illuminate\Validation\ValidationException;
|
2019-02-16 00:20:25 -06:00
|
|
|
use Log;
|
2017-09-14 09:13:25 -05:00
|
|
|
|
2017-12-17 07:06:14 -06:00
|
|
|
/**
|
|
|
|
* Class LoginController
|
2017-12-17 11:23:10 -06: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.
|
2018-07-22 01:10:16 -05:00
|
|
|
*
|
|
|
|
* @codeCoverageIgnore
|
2017-12-17 07:06:14 -06:00
|
|
|
*/
|
2016-09-15 23:19:40 -05:00
|
|
|
class LoginController extends Controller
|
|
|
|
{
|
2021-07-22 23:26:42 -05:00
|
|
|
use AuthenticatesUsers, ThrottlesLogins;
|
2016-09-15 23:19:40 -05:00
|
|
|
|
|
|
|
/**
|
2017-09-09 15:03:27 -05:00
|
|
|
* Where to redirect users after login.
|
2016-09-15 23:19:40 -05:00
|
|
|
*
|
2017-09-09 15:03:27 -05:00
|
|
|
* @var string
|
2016-09-15 23:19:40 -05:00
|
|
|
*/
|
2021-06-12 00:39:16 -05:00
|
|
|
protected string $redirectTo = RouteServiceProvider::HOME;
|
|
|
|
|
|
|
|
private string $username;
|
2016-09-16 00:22:57 -05:00
|
|
|
|
|
|
|
/**
|
2017-09-09 15:03:27 -05:00
|
|
|
* Create a new controller instance.
|
2020-01-01 08:34:38 -06:00
|
|
|
*
|
|
|
|
* @return void
|
2016-12-28 11:49:30 -06:00
|
|
|
*/
|
2017-09-09 15:03:27 -05:00
|
|
|
public function __construct()
|
2016-12-28 11:49:30 -06:00
|
|
|
{
|
2017-09-12 11:24:42 -05:00
|
|
|
parent::__construct();
|
2021-06-12 00:39:16 -05:00
|
|
|
$this->username = 'email';
|
2017-09-09 15:03:27 -05:00
|
|
|
$this->middleware('guest')->except('logout');
|
2016-12-28 11:49:30 -06:00
|
|
|
}
|
2021-03-28 04:46:23 -05:00
|
|
|
|
2017-11-22 13:20:57 -06:00
|
|
|
/**
|
2020-01-01 08:34:38 -06:00
|
|
|
* Handle a login request to the application.
|
2018-07-21 01:06:24 -05:00
|
|
|
*
|
2020-03-17 09:01:00 -05:00
|
|
|
* @param Request $request
|
2020-01-01 08:34:38 -06:00
|
|
|
*
|
2021-05-24 01:54:58 -05:00
|
|
|
* @return JsonResponse|RedirectResponse
|
2017-11-22 13:20:57 -06:00
|
|
|
*
|
2020-08-14 02:59:56 -05:00
|
|
|
* @throws ValidationException
|
2017-11-22 13:20:57 -06:00
|
|
|
*/
|
|
|
|
public function login(Request $request)
|
|
|
|
{
|
2021-10-09 03:07:09 -05:00
|
|
|
Log::channel('audit')->info(sprintf('User is trying to login using "%s"', $request->get($this->username())));
|
2021-09-18 03:20:19 -05:00
|
|
|
Log::info('User is trying to login.');
|
2021-06-12 00:39:16 -05:00
|
|
|
|
|
|
|
$guard = config('auth.defaults.guard');
|
|
|
|
|
|
|
|
// if the user logs in using LDAP the field is also changed (per LDAP config)
|
|
|
|
if ('ldap' === $guard) {
|
|
|
|
Log::debug('User wishes to login using LDAP.');
|
|
|
|
$this->username = config('firefly.ldap_auth_field');
|
2019-02-16 16:01:14 -06:00
|
|
|
}
|
2020-01-01 08:34:38 -06:00
|
|
|
|
2021-06-12 00:39:16 -05:00
|
|
|
|
2017-11-22 13:20:57 -06:00
|
|
|
$this->validateLogin($request);
|
2021-06-12 00:39:16 -05:00
|
|
|
Log::debug('Login data is valid.');
|
2017-11-22 13:20:57 -06:00
|
|
|
|
2019-08-16 23:35:45 -05:00
|
|
|
/** Copied directly from AuthenticatesUsers, but with logging added: */
|
2017-11-22 13:20:57 -06:00
|
|
|
// If the class is using the ThrottlesLogins trait, we can automatically throttle
|
|
|
|
// the login attempts for this application. We'll key this by the username and
|
|
|
|
// the IP address of the client making these requests into this application.
|
2019-08-16 23:35:45 -05:00
|
|
|
if (method_exists($this, 'hasTooManyLoginAttempts') && $this->hasTooManyLoginAttempts($request)) {
|
2021-06-12 00:39:16 -05:00
|
|
|
Log::channel('audit')->info(sprintf('Login for user "%s" was locked out.', $request->get($this->username())));
|
|
|
|
Log::error(sprintf('Login for user "%s" was locked out.', $request->get($this->username())));
|
2017-11-22 13:20:57 -06:00
|
|
|
$this->fireLockoutEvent($request);
|
|
|
|
|
2021-04-07 00:28:43 -05:00
|
|
|
$this->sendLockoutResponse($request);
|
2017-11-22 13:20:57 -06:00
|
|
|
}
|
2019-08-16 23:35:45 -05:00
|
|
|
/** Copied directly from AuthenticatesUsers, but with logging added: */
|
2017-11-22 13:20:57 -06:00
|
|
|
if ($this->attemptLogin($request)) {
|
2021-06-20 11:33:17 -05:00
|
|
|
Log::channel('audit')->info(sprintf('User "%s" has been logged in.', $request->get($this->username())));
|
2019-03-24 08:48:12 -05:00
|
|
|
Log::debug(sprintf('Redirect after login is %s.', $this->redirectPath()));
|
|
|
|
|
2020-08-29 05:10:13 -05:00
|
|
|
// if you just logged in, it can't be that you have a valid 2FA cookie.
|
|
|
|
|
2019-08-16 23:35:45 -05:00
|
|
|
return $this->sendLoginResponse($request);
|
2017-11-22 13:20:57 -06:00
|
|
|
}
|
2021-06-12 00:39:16 -05:00
|
|
|
Log::warning('Login attempt failed.');
|
2017-11-22 13:20:57 -06:00
|
|
|
|
2019-08-16 23:35:45 -05:00
|
|
|
/** Copied directly from AuthenticatesUsers, but with logging added: */
|
2017-11-22 13:20:57 -06:00
|
|
|
// If the login attempt was unsuccessful we will increment the number of attempts
|
|
|
|
// to login and redirect the user back to the login form. Of course, when this
|
|
|
|
// user surpasses their maximum number of attempts they will get locked out.
|
|
|
|
$this->incrementLoginAttempts($request);
|
2021-06-20 11:33:17 -05:00
|
|
|
Log::channel('audit')->info(sprintf('Login failed. Attempt for user "%s" failed.', $request->get($this->username())));
|
2019-08-16 23:35:45 -05:00
|
|
|
|
2020-10-26 13:15:57 -05:00
|
|
|
$this->sendFailedLoginResponse($request);
|
2017-11-22 13:20:57 -06:00
|
|
|
}
|
|
|
|
|
2017-09-14 09:13:25 -05:00
|
|
|
/**
|
2021-03-21 03:15:40 -05:00
|
|
|
* Log the user out of the application.
|
2017-09-14 09:13:25 -05:00
|
|
|
*
|
2021-03-21 03:15:40 -05:00
|
|
|
* @param Request $request
|
|
|
|
*
|
2021-09-18 03:20:19 -05:00
|
|
|
* @return Response
|
2017-09-14 09:13:25 -05:00
|
|
|
*/
|
2021-03-21 03:15:40 -05:00
|
|
|
public function logout(Request $request)
|
2017-09-14 09:13:25 -05:00
|
|
|
{
|
2021-03-21 03:15:40 -05:00
|
|
|
$authGuard = config('firefly.authentication_guard');
|
2021-06-12 06:15:01 -05:00
|
|
|
$logoutUri = config('firefly.custom_logout_url');
|
2021-03-21 03:15:40 -05:00
|
|
|
if ('remote_user_guard' === $authGuard && '' !== $logoutUri) {
|
|
|
|
return redirect($logoutUri);
|
2017-11-06 13:17:39 -06:00
|
|
|
}
|
2021-03-21 03:15:40 -05:00
|
|
|
if ('remote_user_guard' === $authGuard && '' === $logoutUri) {
|
|
|
|
session()->flash('error', trans('firefly.cant_logout_guard'));
|
2017-09-14 09:13:25 -05:00
|
|
|
}
|
|
|
|
|
2021-03-21 03:15:40 -05:00
|
|
|
// also logout current 2FA tokens.
|
|
|
|
$cookieName = config('google2fa.cookie_name', 'google2fa_token');
|
|
|
|
Cookie::forget($cookieName);
|
2018-10-13 08:06:56 -05:00
|
|
|
|
2021-03-21 03:15:40 -05:00
|
|
|
$this->guard()->logout();
|
2017-09-14 09:13:25 -05:00
|
|
|
|
2021-03-21 03:15:40 -05:00
|
|
|
$request->session()->invalidate();
|
2020-05-28 23:07:03 -05:00
|
|
|
|
2021-03-21 03:15:40 -05:00
|
|
|
$request->session()->regenerateToken();
|
2020-05-28 23:07:03 -05:00
|
|
|
|
2021-03-21 03:15:40 -05:00
|
|
|
if ($response = $this->loggedOut($request)) {
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $request->wantsJson()
|
2021-09-18 03:20:19 -05:00
|
|
|
? new Response('', 204)
|
2021-03-21 03:15:40 -05:00
|
|
|
: redirect('/');
|
2017-09-14 09:13:25 -05:00
|
|
|
}
|
2020-01-01 08:34:38 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the failed login response instance.
|
|
|
|
*
|
2020-03-17 09:01:00 -05:00
|
|
|
* @param Request $request
|
2020-01-01 08:34:38 -06:00
|
|
|
*
|
2021-05-24 01:54:58 -05:00
|
|
|
* @return void
|
2020-03-17 09:01:00 -05:00
|
|
|
*
|
2020-08-14 02:59:56 -05:00
|
|
|
* @throws ValidationException
|
2020-01-01 08:34:38 -06:00
|
|
|
*/
|
|
|
|
protected function sendFailedLoginResponse(Request $request)
|
|
|
|
{
|
|
|
|
$exception = ValidationException::withMessages(
|
|
|
|
[
|
|
|
|
$this->username() => [trans('auth.failed')],
|
|
|
|
]
|
|
|
|
);
|
|
|
|
$exception->redirectTo = route('login');
|
|
|
|
|
|
|
|
throw $exception;
|
|
|
|
}
|
|
|
|
|
2020-06-11 10:55:38 -05:00
|
|
|
/**
|
2021-03-21 03:15:40 -05:00
|
|
|
* Show the application's login form.
|
2020-06-11 10:55:38 -05:00
|
|
|
*
|
2021-05-24 01:54:58 -05:00
|
|
|
* @param Request $request
|
|
|
|
*
|
2021-09-18 03:26:12 -05:00
|
|
|
* @return Factory|Application|View|Redirector|RedirectResponse
|
2021-09-18 03:20:19 -05:00
|
|
|
* @throws FireflyException
|
2020-06-11 10:55:38 -05:00
|
|
|
*/
|
2021-03-21 03:15:40 -05:00
|
|
|
public function showLoginForm(Request $request)
|
2020-06-11 10:55:38 -05:00
|
|
|
{
|
2021-03-21 03:15:40 -05:00
|
|
|
Log::channel('audit')->info('Show login form (1.1).');
|
2020-06-11 10:55:38 -05:00
|
|
|
|
2021-06-12 00:39:16 -05:00
|
|
|
$count = DB::table('users')->count();
|
|
|
|
$guard = config('auth.defaults.guard');
|
|
|
|
$title = (string)trans('firefly.login_page_title');
|
|
|
|
|
|
|
|
if (0 === $count && 'web' === $guard) {
|
|
|
|
return redirect(route('register'));
|
|
|
|
}
|
|
|
|
|
|
|
|
// switch to LDAP settings:
|
|
|
|
if ('ldap' === $guard) {
|
|
|
|
Log::debug('User wishes to login using LDAP.');
|
|
|
|
$this->username = config('firefly.ldap_auth_field');
|
2021-03-21 03:15:40 -05:00
|
|
|
}
|
2020-11-14 23:13:56 -06:00
|
|
|
|
2021-06-12 00:39:16 -05:00
|
|
|
// throw warning if still using login_provider
|
|
|
|
$ldapWarning = false;
|
|
|
|
if ('ldap' === config('firefly.login_provider')) {
|
|
|
|
$ldapWarning = true;
|
|
|
|
}
|
|
|
|
// is allowed to register, etc.
|
2021-03-21 03:15:40 -05:00
|
|
|
$singleUserMode = app('fireflyconfig')->get('single_user_mode', config('firefly.configuration.single_user_mode'))->data;
|
|
|
|
$allowRegistration = true;
|
|
|
|
$allowReset = true;
|
|
|
|
if (true === $singleUserMode && $count > 0) {
|
|
|
|
$allowRegistration = false;
|
|
|
|
}
|
2020-06-11 10:55:38 -05:00
|
|
|
|
2021-03-21 03:15:40 -05:00
|
|
|
// single user mode is ignored when the user is not using eloquent:
|
2021-06-12 00:39:16 -05:00
|
|
|
if ('web' !== $guard) {
|
2021-03-21 03:15:40 -05:00
|
|
|
$allowRegistration = false;
|
|
|
|
$allowReset = false;
|
|
|
|
}
|
2020-06-11 10:55:38 -05:00
|
|
|
|
2021-03-21 03:15:40 -05:00
|
|
|
$email = $request->old('email');
|
|
|
|
$remember = $request->old('remember');
|
2020-06-11 10:55:38 -05:00
|
|
|
|
2021-03-21 03:15:40 -05:00
|
|
|
$storeInCookie = config('google2fa.store_in_cookie', false);
|
|
|
|
if (false !== $storeInCookie) {
|
|
|
|
$cookieName = config('google2fa.cookie_name', 'google2fa_token');
|
|
|
|
request()->cookies->set($cookieName, 'invalid');
|
2020-06-11 10:55:38 -05:00
|
|
|
}
|
2021-06-12 00:39:16 -05:00
|
|
|
$usernameField = $this->username();
|
2021-03-28 04:46:23 -05:00
|
|
|
|
2021-06-12 00:39:16 -05:00
|
|
|
return prefixView('auth.login', compact('allowRegistration', 'email', 'remember', 'ldapWarning', 'allowReset', 'title', 'usernameField'));
|
2020-06-11 10:55:38 -05:00
|
|
|
}
|
|
|
|
|
2021-06-12 00:39:16 -05:00
|
|
|
/**
|
|
|
|
* Get the login username to be used by the controller.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function username()
|
|
|
|
{
|
|
|
|
return $this->username;
|
|
|
|
}
|
2016-09-15 23:19:40 -05:00
|
|
|
}
|