Files
firefly-iii/app/Http/Controllers/Auth/LoginController.php

224 lines
7.5 KiB
PHP
Raw Normal View History

2016-09-16 06:19:40 +02:00
<?php
2017-10-21 08:40:00 +02:00
/**
* LoginController.php
2020-01-31 07:32:04 +01:00
* Copyright (c) 2020 james@firefly-iii.org
2017-10-21 08:40:00 +02:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2017-10-21 08:40:00 +02: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 08:40:00 +02:00
*
* This program is distributed in the hope that it will be useful,
2017-10-21 08:40:00 +02:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
2017-10-21 08:40:00 +02: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 08:40:00 +02:00
*/
2017-09-14 17:40:02 +02:00
declare(strict_types=1);
2016-09-16 06:19:40 +02:00
namespace FireflyIII\Http\Controllers\Auth;
2019-02-16 19:04:36 +01:00
use Adldap;
2018-03-08 20:44:56 +01:00
use DB;
2020-08-14 09:59:56 +02:00
use FireflyIII\Exceptions\FireflyException;
2016-09-16 06:19:40 +02:00
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Providers\RouteServiceProvider;
use Illuminate\Contracts\View\Factory;
2016-09-16 06:19:40 +02:00
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
2017-09-14 16:13:25 +02:00
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
use Illuminate\View\View;
use Log;
use Symfony\Component\HttpFoundation\Response;
2017-09-14 16:13:25 +02:00
/**
* Class LoginController
2017-12-17 18:23:10 +01: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 08:10:16 +02:00
*
* @codeCoverageIgnore
*/
2016-09-16 06:19:40 +02:00
class LoginController extends Controller
{
use AuthenticatesUsers;
/**
2017-09-09 22:03:27 +02:00
* Where to redirect users after login.
2016-09-16 06:19:40 +02:00
*
2017-09-09 22:03:27 +02:00
* @var string
2016-09-16 06:19:40 +02:00
*/
protected $redirectTo = RouteServiceProvider::HOME;
2016-09-16 07:22:57 +02:00
/**
2017-09-09 22:03:27 +02:00
* Create a new controller instance.
*
* @return void
2016-12-28 18:49:30 +01:00
*/
2017-09-09 22:03:27 +02:00
public function __construct()
2016-12-28 18:49:30 +01:00
{
2017-09-12 18:24:42 +02:00
parent::__construct();
2017-09-09 22:03:27 +02:00
$this->middleware('guest')->except('logout');
2020-08-14 09:59:56 +02:00
$loginProvider = config('firefly.login_provider');
$authGuard = config('firefly.authentication_guard');
2020-08-23 18:48:24 +02:00
$route = request()->route()->getName();
if (('eloquent' !== $loginProvider || 'web' !== $authGuard) && 'logout' !== $route) {
2020-08-14 09:59:56 +02:00
throw new FireflyException('Using external identity provider. Cannot continue.');
}
2016-12-28 18:49:30 +01:00
}
2017-09-14 16:13:25 +02:00
2017-11-22 20:20:57 +01:00
/**
* Handle a login request to the application.
2018-07-21 08:06:24 +02:00
*
* @param Request $request
*
* @return RedirectResponse|\Illuminate\Http\Response|JsonResponse
2017-11-22 20:20:57 +01:00
*
2020-08-14 09:59:56 +02:00
* @throws ValidationException
2017-11-22 20:20:57 +01:00
*/
public function login(Request $request)
{
2019-02-16 23:01:14 +01:00
Log::channel('audit')->info(sprintf('User is trying to login using "%s"', $request->get('email')));
Log::info(sprintf('User is trying to login.'));
2019-02-16 23:01:14 +01:00
if ('ldap' === config('auth.providers.users.driver')) {
/** @var Adldap\Connections\Provider $provider */
Adldap::getProvider('default');
2019-02-16 23:01:14 +01:00
}
2017-11-22 20:20:57 +01:00
$this->validateLogin($request);
2019-08-17 06:35:45 +02:00
/** Copied directly from AuthenticatesUsers, but with logging added: */
2017-11-22 20:20:57 +01: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-17 06:35:45 +02:00
if (method_exists($this, 'hasTooManyLoginAttempts') && $this->hasTooManyLoginAttempts($request)) {
2019-02-16 19:04:36 +01:00
Log::channel('audit')->info(sprintf('Login for user "%s" was locked out.', $request->get('email')));
2017-11-22 20:20:57 +01:00
$this->fireLockoutEvent($request);
return $this->sendLockoutResponse($request);
}
2019-08-17 06:35:45 +02:00
/** Copied directly from AuthenticatesUsers, but with logging added: */
2017-11-22 20:20:57 +01:00
if ($this->attemptLogin($request)) {
2019-02-16 19:04:36 +01:00
Log::channel('audit')->info(sprintf('User "%s" has been logged in.', $request->get('email')));
Log::debug(sprintf('Redirect after login is %s.', $this->redirectPath()));
2019-08-17 06:35:45 +02:00
return $this->sendLoginResponse($request);
2017-11-22 20:20:57 +01:00
}
2019-08-17 06:35:45 +02:00
/** Copied directly from AuthenticatesUsers, but with logging added: */
2017-11-22 20:20:57 +01: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);
2020-08-11 23:52:27 +02:00
Log::channel('audit')->info(sprintf('Login failed. Attempt for user "%s" failed.', $request->get('email')));
2019-08-17 06:35:45 +02:00
2017-11-22 20:20:57 +01:00
return $this->sendFailedLoginResponse($request);
}
2017-09-14 16:13:25 +02:00
/**
* Show the application's login form.
*
* @return Factory|\Illuminate\Http\Response|View
2017-09-14 16:13:25 +02:00
*/
2017-11-25 08:54:52 +01:00
public function showLoginForm(Request $request)
2017-09-14 16:13:25 +02:00
{
2020-08-13 13:54:32 +02:00
Log::channel('audit')->info('Show login form (1.0).');
2020-08-13 13:18:54 +02:00
2018-10-13 15:06:56 +02:00
$count = DB::table('users')->count();
$loginProvider = config('firefly.login_provider');
$title = (string) trans('firefly.login_page_title');
2018-10-13 15:06:56 +02:00
if (0 === $count && 'eloquent' === $loginProvider) {
2018-03-08 20:44:56 +01:00
return redirect(route('register')); // @codeCoverageIgnore
}
2017-09-14 16:13:25 +02:00
// is allowed to?
2019-02-13 17:38:41 +01:00
$singleUserMode = app('fireflyconfig')->get('single_user_mode', config('firefly.configuration.single_user_mode'))->data;
2017-09-14 16:13:25 +02:00
$allowRegistration = true;
2018-10-13 15:06:56 +02:00
$allowReset = true;
2019-08-17 06:24:49 +02:00
if (true === $singleUserMode && $count > 0) {
2017-09-14 16:13:25 +02:00
$allowRegistration = false;
}
2018-10-13 15:06:56 +02:00
// single user mode is ignored when the user is not using eloquent:
if ('eloquent' !== $loginProvider) {
$allowRegistration = false;
$allowReset = false;
}
2017-09-14 16:13:25 +02:00
$email = $request->old('email');
$remember = $request->old('remember');
2020-08-26 20:37:30 +02:00
$storeInCookie = config('google2fa.store_in_cookie', false);
if (false !== $storeInCookie) {
$cookieName = config('google2fa.cookie_name', 'google2fa_token');
request()->cookies->set($cookieName, 'invalid');
}
2020-05-29 06:07:03 +02:00
2019-08-17 06:24:49 +02:00
return view('auth.login', compact('allowRegistration', 'email', 'remember', 'allowReset', 'title'));
2017-09-14 16:13:25 +02:00
}
/**
* Get the failed login response instance.
*
* @param Request $request
*
* @return Response
*
2020-08-14 09:59:56 +02:00
* @throws ValidationException
*/
protected function sendFailedLoginResponse(Request $request)
{
$exception = ValidationException::withMessages(
[
$this->username() => [trans('auth.failed')],
]
);
$exception->redirectTo = route('login');
throw $exception;
}
2020-06-11 17:55:38 +02:00
/**
* Log the user out of the application.
*
* @param \Illuminate\Http\Request $request
*
* @return \Illuminate\Http\Response
*/
public function logout(Request $request)
{
$authGuard = config('firefly.authentication_guard');
$logoutUri = config('firefly.custom_logout_uri');
if ('remote_user_guard' === $authGuard && '' !== $logoutUri) {
return redirect($logoutUri);
}
$this->guard()->logout();
$request->session()->invalidate();
$request->session()->regenerateToken();
if ($response = $this->loggedOut($request)) {
return $response;
}
return $request->wantsJson()
? new \Illuminate\Http\Response('', 204)
: redirect('/');
}
2016-09-16 06:19:40 +02:00
}