2016-01-08 09:00:57 -06:00
|
|
|
<?php
|
2018-03-05 12:35:58 -06:00
|
|
|
|
2016-05-20 05:41:23 -05:00
|
|
|
/**
|
|
|
|
* Authenticate.php
|
2020-01-31 00:32:04 -06:00
|
|
|
* Copyright (c) 2019 james@firefly-iii.org
|
2016-05-20 05:41:23 -05:00
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
2016-10-04 23:52:15 -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/>.
|
2016-05-20 05:41:23 -05:00
|
|
|
*/
|
2016-01-08 09:00:57 -06:00
|
|
|
|
2018-05-11 03:08:34 -05:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2016-01-08 09:00:57 -06:00
|
|
|
namespace FireflyIII\Http\Middleware;
|
2015-02-05 21:39:52 -06:00
|
|
|
|
|
|
|
use Closure;
|
2018-04-16 12:25:33 -05:00
|
|
|
use FireflyIII\Exceptions\FireflyException;
|
2018-02-09 07:47:37 -06:00
|
|
|
use Illuminate\Auth\AuthenticationException;
|
|
|
|
use Illuminate\Contracts\Auth\Factory as Auth;
|
2018-04-16 12:25:33 -05:00
|
|
|
use Illuminate\Database\QueryException;
|
2020-03-17 09:02:57 -05:00
|
|
|
use Illuminate\Http\Request;
|
2016-03-14 14:53:56 -05:00
|
|
|
|
2016-01-09 01:20:55 -06:00
|
|
|
/**
|
2018-02-09 07:47:37 -06:00
|
|
|
* Class Authenticate
|
2016-01-09 01:20:55 -06:00
|
|
|
*/
|
2015-02-11 00:35:10 -06:00
|
|
|
class Authenticate
|
|
|
|
{
|
2018-02-09 07:47:37 -06:00
|
|
|
/**
|
|
|
|
* The authentication factory instance.
|
|
|
|
*
|
2020-03-17 09:02:57 -05:00
|
|
|
* @var Auth
|
2018-02-09 07:47:37 -06:00
|
|
|
*/
|
|
|
|
protected $auth;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new middleware instance.
|
|
|
|
*
|
2020-03-17 09:02:57 -05:00
|
|
|
* @param Auth $auth
|
2018-02-09 07:47:37 -06:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __construct(Auth $auth)
|
|
|
|
{
|
|
|
|
$this->auth = $auth;
|
|
|
|
}
|
|
|
|
|
2015-02-11 00:35:10 -06:00
|
|
|
/**
|
|
|
|
* Handle an incoming request.
|
|
|
|
*
|
2020-03-17 09:02:57 -05:00
|
|
|
* @param Request $request
|
|
|
|
* @param Closure $next
|
|
|
|
* @param string[] ...$guards
|
2018-02-09 07:47:37 -06:00
|
|
|
*
|
2018-04-27 23:23:13 -05:00
|
|
|
* @throws AuthenticationException
|
|
|
|
* @throws FireflyException
|
2020-03-17 09:02:57 -05:00
|
|
|
* @return mixed
|
|
|
|
*
|
2015-02-11 00:35:10 -06:00
|
|
|
*/
|
2018-02-09 07:47:37 -06:00
|
|
|
public function handle($request, Closure $next, ...$guards)
|
2015-02-11 00:35:10 -06:00
|
|
|
{
|
2018-09-19 09:50:16 -05:00
|
|
|
$this->authenticate($request, $guards);
|
2016-02-07 00:36:31 -06:00
|
|
|
|
2018-02-09 07:47:37 -06:00
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
|
2018-07-08 05:08:53 -05:00
|
|
|
|
2018-02-09 07:47:37 -06:00
|
|
|
/**
|
|
|
|
* Determine if the user is logged in to any of the given guards.
|
|
|
|
*
|
2019-02-13 10:38:41 -06:00
|
|
|
* @param $request
|
2020-03-17 09:02:57 -05:00
|
|
|
* @param array $guards
|
2018-02-09 07:47:37 -06:00
|
|
|
*
|
2019-02-13 10:38:41 -06:00
|
|
|
* @throws AuthenticationException
|
2018-04-16 12:25:33 -05:00
|
|
|
* @throws FireflyException
|
2020-03-17 09:02:57 -05:00
|
|
|
* @return mixed
|
2018-02-09 07:47:37 -06:00
|
|
|
*/
|
2018-09-19 09:50:16 -05:00
|
|
|
protected function authenticate($request, array $guards)
|
2018-02-09 07:47:37 -06:00
|
|
|
{
|
2018-04-16 12:25:33 -05:00
|
|
|
|
2018-02-09 07:47:37 -06:00
|
|
|
if (empty($guards)) {
|
2018-04-16 12:25:33 -05:00
|
|
|
try {
|
|
|
|
// go for default guard:
|
2018-07-09 12:24:08 -05:00
|
|
|
/** @noinspection PhpUndefinedMethodInspection */
|
2018-04-16 12:25:33 -05:00
|
|
|
if ($this->auth->check()) {
|
2018-02-09 07:47:37 -06:00
|
|
|
|
2018-04-16 12:25:33 -05:00
|
|
|
// do an extra check on user object.
|
2018-07-09 12:24:08 -05:00
|
|
|
/** @noinspection PhpUndefinedMethodInspection */
|
2018-04-16 12:25:33 -05:00
|
|
|
$user = $this->auth->authenticate();
|
2020-03-17 09:02:57 -05:00
|
|
|
if (1 === (int) $user->blocked) {
|
|
|
|
$message = (string) trans('firefly.block_account_logout');
|
2018-04-16 12:25:33 -05:00
|
|
|
if ('email_changed' === $user->blocked_code) {
|
2020-03-17 09:02:57 -05:00
|
|
|
$message = (string) trans('firefly.email_changed_logout');
|
2018-04-16 12:25:33 -05:00
|
|
|
}
|
|
|
|
app('session')->flash('logoutMessage', $message);
|
2018-07-09 12:24:08 -05:00
|
|
|
/** @noinspection PhpUndefinedMethodInspection */
|
2018-04-16 12:25:33 -05:00
|
|
|
$this->auth->logout();
|
|
|
|
|
|
|
|
throw new AuthenticationException('Blocked account.', $guards);
|
|
|
|
}
|
2018-02-09 07:47:37 -06:00
|
|
|
}
|
2018-04-16 12:25:33 -05:00
|
|
|
} catch (QueryException $e) {
|
2018-06-01 23:11:13 -05:00
|
|
|
// @codeCoverageIgnoreStart
|
2018-07-08 00:59:58 -05:00
|
|
|
throw new FireflyException(
|
|
|
|
sprintf(
|
|
|
|
'It seems the database has not yet been initialized. Did you run the correct upgrade or installation commands? Error: %s',
|
|
|
|
$e->getMessage()
|
|
|
|
)
|
|
|
|
);
|
2018-06-01 23:11:13 -05:00
|
|
|
// @codeCoverageIgnoreEnd
|
2017-09-26 01:52:16 -05:00
|
|
|
}
|
2018-07-13 08:50:42 -05:00
|
|
|
|
2018-07-09 12:24:08 -05:00
|
|
|
/** @noinspection PhpUndefinedMethodInspection */
|
2018-02-09 07:47:37 -06:00
|
|
|
return $this->auth->authenticate();
|
|
|
|
}
|
2016-05-20 10:53:03 -05:00
|
|
|
|
2018-03-03 10:16:47 -06:00
|
|
|
// @codeCoverageIgnoreStart
|
2018-02-09 07:47:37 -06:00
|
|
|
foreach ($guards as $guard) {
|
|
|
|
if ($this->auth->guard($guard)->check()) {
|
2018-07-09 12:24:08 -05:00
|
|
|
/** @noinspection PhpVoidFunctionResultUsedInspection */
|
2018-02-09 07:47:37 -06:00
|
|
|
return $this->auth->shouldUse($guard);
|
|
|
|
}
|
2015-07-24 06:17:47 -05:00
|
|
|
}
|
|
|
|
|
2018-02-09 07:47:37 -06:00
|
|
|
throw new AuthenticationException('Unauthenticated.', $guards);
|
2018-03-03 10:16:47 -06:00
|
|
|
// @codeCoverageIgnoreEnd
|
2015-02-11 00:35:10 -06:00
|
|
|
}
|
2015-02-05 21:39:52 -06:00
|
|
|
}
|