Files
firefly-iii/app/Http/Middleware/Authenticate.php

149 lines
4.4 KiB
PHP
Raw Normal View History

<?php
2018-03-05 19:35:58 +01:00
/**
* Authenticate.php
2020-01-31 07:32:04 +01:00
* Copyright (c) 2019 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* 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/>.
*/
2018-05-11 10:08:34 +02:00
declare(strict_types=1);
namespace FireflyIII\Http\Middleware;
2015-02-06 04:39:52 +01:00
use Closure;
2018-04-16 19:25:33 +02:00
use FireflyIII\Exceptions\FireflyException;
2021-04-07 07:53:05 +02:00
use FireflyIII\User;
2018-02-09 14:47:37 +01:00
use Illuminate\Auth\AuthenticationException;
use Illuminate\Contracts\Auth\Factory as Auth;
use Illuminate\Http\Request;
2023-04-01 07:04:42 +02:00
use Illuminate\Support\Facades\Log;
/**
2018-02-09 14:47:37 +01:00
* Class Authenticate
*/
2015-02-11 07:35:10 +01:00
class Authenticate
{
2018-02-09 14:47:37 +01:00
/**
* The authentication factory instance.
*
* @var Auth
2018-02-09 14:47:37 +01:00
*/
2023-02-22 19:54:19 +01:00
protected Auth $auth;
2018-02-09 14:47:37 +01:00
/**
* Create a new middleware instance.
*
2023-06-21 12:34:58 +02:00
* @param Auth $auth
2018-02-09 14:47:37 +01:00
*
* @return void
*/
public function __construct(Auth $auth)
{
$this->auth = $auth;
}
2015-02-11 07:35:10 +01:00
/**
* Handle an incoming request.
*
2023-06-21 12:34:58 +02:00
* @param Request $request
* @param Closure $next
* @param string[] ...$guards
2018-02-09 14:47:37 +01:00
*
* @return mixed
*
2021-03-21 09:15:40 +01:00
* @throws FireflyException
* @throws AuthenticationException
2015-02-11 07:35:10 +01:00
*/
2018-02-09 14:47:37 +01:00
public function handle($request, Closure $next, ...$guards)
2015-02-11 07:35:10 +01:00
{
$this->authenticate($request, $guards);
2016-02-07 07:36:31 +01:00
2018-02-09 14:47:37 +01:00
return $next($request);
}
2021-03-28 11:46:23 +02:00
2018-02-09 14:47:37 +01:00
/**
* Determine if the user is logged in to any of the given guards.
*
2023-06-21 12:34:58 +02:00
* @param mixed $request
* @param array $guards
2018-02-09 14:47:37 +01:00
*
* @return mixed
2021-03-21 09:15:40 +01:00
* @throws FireflyException
* @throws AuthenticationException
2018-02-09 14:47:37 +01:00
*/
protected function authenticate($request, array $guards)
2018-02-09 14:47:37 +01:00
{
2022-11-04 05:11:05 +01:00
if (0 === count($guards)) {
// go for default guard:
/** @noinspection PhpUndefinedMethodInspection */
if ($this->auth->check()) {
// do an extra check on user object.
2018-07-09 19:24:08 +02:00
/** @noinspection PhpUndefinedMethodInspection */
/** @var User $user */
$user = $this->auth->authenticate();
$this->validateBlockedUser($user, $guards);
}
2018-07-13 15:50:42 +02:00
2023-02-22 19:54:19 +01:00
/** @noinspection PhpUndefinedMethodInspection */
return $this->auth->authenticate();
2018-02-09 14:47:37 +01:00
}
2021-04-07 07:28:43 +02:00
2018-02-09 14:47:37 +01:00
foreach ($guards as $guard) {
if ('api' !== $guard) {
2023-01-04 20:40:57 +01:00
$this->auth->guard($guard)->authenticate();
}
$result = $this->auth->guard($guard)->check();
if ($result) {
$user = $this->auth->guard($guard)->user();
$this->validateBlockedUser($user, $guards);
2022-12-31 06:57:05 +01:00
// According to PHPstan the method returns void, but we'll see.
return $this->auth->shouldUse($guard); // @phpstan-ignore-line
2018-02-09 14:47:37 +01:00
}
}
2018-02-09 14:47:37 +01:00
throw new AuthenticationException('Unauthenticated.', $guards);
2015-02-11 07:35:10 +01:00
}
/**
2023-06-21 12:34:58 +02:00
* @param User|null $user
* @param array $guards
* @return void
* @throws AuthenticationException
*/
private function validateBlockedUser(?User $user, array $guards): void
{
if (null === $user) {
Log::warning('User is null, throw exception?');
}
if (null !== $user) {
// Log::debug(get_class($user));
if (1 === (int)$user->blocked) {
$message = (string)trans('firefly.block_account_logout');
if ('email_changed' === $user->blocked_code) {
$message = (string)trans('firefly.email_changed_logout');
}
Log::warning('User is blocked, cannot use authentication method.');
app('session')->flash('logoutMessage', $message);
2023-02-22 19:54:19 +01:00
/** @noinspection PhpUndefinedMethodInspection */
$this->auth->logout(); // @phpstan-ignore-line (thinks function is undefined)
throw new AuthenticationException('Blocked account.', $guards);
}
}
}
2015-02-06 04:39:52 +01:00
}