2020-06-11 06:55:13 +02:00
|
|
|
<?php
|
2020-06-30 19:05:35 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* RemoteUserGuard.php
|
|
|
|
* Copyright (c) 2020 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.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2020-06-11 06:55:13 +02:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace FireflyIII\Support\Authentication;
|
|
|
|
|
|
|
|
use FireflyIII\Exceptions\FireflyException;
|
|
|
|
use FireflyIII\User;
|
|
|
|
use Illuminate\Contracts\Auth\Authenticatable;
|
|
|
|
use Illuminate\Contracts\Auth\Guard;
|
|
|
|
use Illuminate\Contracts\Auth\UserProvider;
|
|
|
|
use Illuminate\Contracts\Foundation\Application;
|
2023-01-01 14:25:52 +01:00
|
|
|
use Illuminate\Http\Request;
|
2023-02-22 18:03:31 +01:00
|
|
|
use Psr\Container\ContainerExceptionInterface;
|
|
|
|
use Psr\Container\NotFoundExceptionInterface;
|
2021-09-18 10:26:12 +02:00
|
|
|
|
2020-06-11 06:55:13 +02:00
|
|
|
/**
|
|
|
|
* Class RemoteUserGuard
|
|
|
|
*/
|
|
|
|
class RemoteUserGuard implements Guard
|
|
|
|
{
|
2023-11-30 17:28:44 +01:00
|
|
|
protected Application $application;
|
2023-11-05 10:16:53 +01:00
|
|
|
protected UserProvider $provider;
|
2023-12-20 19:35:52 +01:00
|
|
|
protected null|User $user;
|
2020-06-11 06:55:13 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new authentication guard.
|
|
|
|
*
|
2023-02-22 18:03:31 +01:00
|
|
|
* @throws ContainerExceptionInterface
|
|
|
|
* @throws NotFoundExceptionInterface
|
2020-06-11 06:55:13 +02:00
|
|
|
*/
|
2022-10-31 05:40:59 +01:00
|
|
|
public function __construct(UserProvider $provider, Application $app)
|
2020-06-11 06:55:13 +02:00
|
|
|
{
|
2023-12-20 19:35:52 +01:00
|
|
|
/** @var null|Request $request */
|
2023-01-01 14:25:52 +01:00
|
|
|
$request = $app->get('request');
|
2023-10-29 06:33:43 +01:00
|
|
|
app('log')->debug(sprintf('Created RemoteUserGuard for %s "%s"', $request?->getMethod(), $request?->getRequestUri()));
|
2020-06-11 06:55:13 +02:00
|
|
|
$this->application = $app;
|
|
|
|
$this->provider = $provider;
|
|
|
|
$this->user = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function authenticate(): void
|
|
|
|
{
|
2023-10-29 06:33:43 +01:00
|
|
|
app('log')->debug(sprintf('Now at %s', __METHOD__));
|
2023-01-01 14:25:52 +01:00
|
|
|
if (null !== $this->user) {
|
2023-10-29 06:33:43 +01:00
|
|
|
app('log')->debug(sprintf('%s is found: #%d, "%s".', get_class($this->user), $this->user->id, $this->user->email));
|
2020-06-11 06:55:13 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2020-10-31 18:46:01 -04:00
|
|
|
// Get the user identifier from $_SERVER or apache filtered headers
|
2020-07-31 06:21:53 +02:00
|
|
|
$header = config('auth.guard_header', 'REMOTE_USER');
|
2023-01-01 14:52:46 +01:00
|
|
|
$userID = request()->server($header) ?? null;
|
2023-01-15 15:54:41 +01:00
|
|
|
|
2023-01-01 14:52:46 +01:00
|
|
|
if (function_exists('apache_request_headers')) {
|
2023-10-29 06:33:43 +01:00
|
|
|
app('log')->debug('Use apache_request_headers to find user ID.');
|
2023-01-01 14:52:46 +01:00
|
|
|
$userID = request()->server($header) ?? apache_request_headers()[$header] ?? null;
|
|
|
|
}
|
2023-01-19 06:45:45 +01:00
|
|
|
|
|
|
|
if (null === $userID || '' === $userID) {
|
2023-10-29 06:32:00 +01:00
|
|
|
app('log')->error(sprintf('No user in header "%s".', $header));
|
2023-12-20 19:35:52 +01:00
|
|
|
|
2020-07-31 06:21:53 +02:00
|
|
|
throw new FireflyException('The guard header was unexpectedly empty. See the logs.');
|
2020-06-11 06:55:13 +02:00
|
|
|
}
|
|
|
|
|
2023-10-29 06:33:43 +01:00
|
|
|
app('log')->debug(sprintf('User ID found in header is "%s"', $userID));
|
2023-01-19 06:45:45 +01:00
|
|
|
|
2020-10-24 18:40:17 +02:00
|
|
|
/** @var User $retrievedUser */
|
|
|
|
$retrievedUser = $this->provider->retrieveById($userID);
|
2020-06-11 06:55:13 +02:00
|
|
|
|
2020-10-03 07:03:41 +02:00
|
|
|
// store email address if present in header and not already set.
|
2021-03-21 09:15:40 +01:00
|
|
|
$header = config('auth.guard_email');
|
2020-10-03 07:03:41 +02:00
|
|
|
|
2020-11-14 18:41:29 +01:00
|
|
|
if (null !== $header) {
|
2022-12-29 19:42:26 +01:00
|
|
|
$emailAddress = (string)(request()->server($header) ?? apache_request_headers()[$header] ?? null);
|
2021-09-18 10:20:19 +02:00
|
|
|
$preference = app('preferences')->getForUser($retrievedUser, 'remote_guard_alt_email');
|
2020-11-14 18:41:29 +01:00
|
|
|
|
2022-12-11 06:20:27 +01:00
|
|
|
if ('' !== $emailAddress && null === $preference && $emailAddress !== $userID) {
|
|
|
|
app('preferences')->setForUser($retrievedUser, 'remote_guard_alt_email', $emailAddress);
|
|
|
|
}
|
|
|
|
// if the pref isn't null and the object returned isn't null, update the email address.
|
|
|
|
if ('' !== $emailAddress && null !== $preference && $emailAddress !== $preference->data) {
|
2020-11-14 18:41:29 +01:00
|
|
|
app('preferences')->setForUser($retrievedUser, 'remote_guard_alt_email', $emailAddress);
|
|
|
|
}
|
2020-10-03 07:03:41 +02:00
|
|
|
}
|
|
|
|
|
2023-10-29 06:33:43 +01:00
|
|
|
app('log')->debug(sprintf('Result of getting user from provider: %s', $retrievedUser->email));
|
2020-10-24 18:40:17 +02:00
|
|
|
$this->user = $retrievedUser;
|
2020-06-11 06:55:13 +02:00
|
|
|
}
|
|
|
|
|
2023-06-21 12:34:58 +02:00
|
|
|
public function guest(): bool
|
|
|
|
{
|
2023-10-29 06:33:43 +01:00
|
|
|
app('log')->debug(sprintf('Now at %s', __METHOD__));
|
2023-12-20 19:35:52 +01:00
|
|
|
|
2023-06-21 12:34:58 +02:00
|
|
|
return !$this->check();
|
|
|
|
}
|
|
|
|
|
2022-03-29 15:00:29 +02:00
|
|
|
public function check(): bool
|
2020-06-11 06:55:13 +02:00
|
|
|
{
|
2023-10-29 06:33:43 +01:00
|
|
|
app('log')->debug(sprintf('Now at %s', __METHOD__));
|
2023-12-20 19:35:52 +01:00
|
|
|
|
2023-11-04 14:18:49 +01:00
|
|
|
return null !== $this->user();
|
2020-06-11 06:55:13 +02:00
|
|
|
}
|
|
|
|
|
2023-06-21 12:34:58 +02:00
|
|
|
public function user(): ?User
|
2020-06-11 06:55:13 +02:00
|
|
|
{
|
2023-10-29 06:33:43 +01:00
|
|
|
app('log')->debug(sprintf('Now at %s', __METHOD__));
|
2023-06-21 12:34:58 +02:00
|
|
|
$user = $this->user;
|
|
|
|
if (null === $user) {
|
2023-10-29 06:33:43 +01:00
|
|
|
app('log')->debug('User is NULL');
|
2023-12-20 19:35:52 +01:00
|
|
|
|
2023-06-21 12:34:58 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $user;
|
2020-06-11 06:55:13 +02:00
|
|
|
}
|
|
|
|
|
2022-12-30 20:25:04 +01:00
|
|
|
public function hasUser(): bool
|
2020-06-11 06:55:13 +02:00
|
|
|
{
|
2023-10-29 06:33:43 +01:00
|
|
|
app('log')->debug(sprintf('Now at %s', __METHOD__));
|
2023-12-20 19:35:52 +01:00
|
|
|
|
2023-01-15 15:54:41 +01:00
|
|
|
throw new FireflyException('Did not implement RemoteUserGuard::hasUser()');
|
2020-06-11 06:55:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-11-30 17:28:44 +01:00
|
|
|
* @SuppressWarnings(PHPMD.ShortMethodName)
|
2020-06-11 06:55:13 +02:00
|
|
|
*/
|
2023-12-20 19:35:52 +01:00
|
|
|
public function id(): null|int|string
|
2020-06-11 06:55:13 +02:00
|
|
|
{
|
2023-10-29 06:33:43 +01:00
|
|
|
app('log')->debug(sprintf('Now at %s', __METHOD__));
|
2023-12-20 19:35:52 +01:00
|
|
|
|
2023-11-05 10:16:53 +01:00
|
|
|
return $this->user?->id;
|
2020-06-11 06:55:13 +02:00
|
|
|
}
|
|
|
|
|
2023-12-21 05:07:26 +01:00
|
|
|
public function setUser(null|Authenticatable|User $user): void
|
2020-06-11 06:55:13 +02:00
|
|
|
{
|
2023-10-29 06:33:43 +01:00
|
|
|
app('log')->debug(sprintf('Now at %s', __METHOD__));
|
2023-11-04 11:31:14 +01:00
|
|
|
if ($user instanceof User) {
|
|
|
|
$this->user = $user;
|
2023-12-20 19:35:52 +01:00
|
|
|
|
2023-11-04 11:31:14 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
app('log')->error(sprintf('Did not set user at %s', __METHOD__));
|
2020-06-11 06:55:13 +02:00
|
|
|
}
|
2022-02-28 07:48:58 +01:00
|
|
|
|
2023-12-21 05:15:46 +01:00
|
|
|
public function validate(array $credentials = []): bool
|
2022-02-28 07:48:58 +01:00
|
|
|
{
|
2023-10-29 06:33:43 +01:00
|
|
|
app('log')->debug(sprintf('Now at %s', __METHOD__));
|
2023-12-20 19:35:52 +01:00
|
|
|
|
2022-03-29 15:00:29 +02:00
|
|
|
throw new FireflyException('Did not implement RemoteUserGuard::validate()');
|
2022-02-28 07:48:58 +01:00
|
|
|
}
|
2023-02-22 18:14:14 +01:00
|
|
|
|
|
|
|
public function viaRemember(): bool
|
|
|
|
{
|
2023-10-29 06:33:43 +01:00
|
|
|
app('log')->debug(sprintf('Now at %s', __METHOD__));
|
2023-12-20 19:35:52 +01:00
|
|
|
|
2023-02-22 18:14:14 +01:00
|
|
|
return false;
|
|
|
|
}
|
2020-06-11 06:55:13 +02:00
|
|
|
}
|