Files
firefly-iii/app/Support/Authentication/RemoteUserGuard.php

179 lines
5.6 KiB
PHP
Raw Normal View History

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;
2025-06-30 20:28:29 +02:00
use FireflyIII\Support\Facades\Preferences;
2020-06-11 06:55:13 +02:00
use FireflyIII\User;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Contracts\Auth\UserProvider;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Http\Request;
2025-02-22 08:07:52 +01:00
use Illuminate\Support\Facades\Log;
2020-06-11 06:55:13 +02:00
/**
* Class RemoteUserGuard
*/
class RemoteUserGuard implements Guard
{
2025-05-04 17:41:26 +02:00
protected Application $application;
protected ?User $user;
2020-06-11 06:55:13 +02:00
/**
* Create a new authentication guard.
*/
2025-05-04 13:47:00 +02:00
public function __construct(protected UserProvider $provider, Application $app)
2020-06-11 06:55:13 +02:00
{
2023-12-20 19:35:52 +01:00
/** @var null|Request $request */
$request = $app->get('request');
2025-02-22 08:07:52 +01:00
Log::debug(sprintf('Created RemoteUserGuard for %s "%s"', $request?->getMethod(), $request?->getRequestUri()));
2020-06-11 06:55:13 +02:00
$this->application = $app;
$this->user = null;
}
public function authenticate(): void
{
Log::debug(sprintf('Now at %s', __METHOD__));
2025-05-27 17:06:15 +02:00
if ($this->user instanceof User) {
2025-05-04 13:47:00 +02:00
Log::debug(sprintf('%s is found: #%d, "%s".', $this->user::class, $this->user->id, $this->user->email));
2020-06-11 06:55:13 +02:00
return;
}
// Get the user identifier from $_SERVER or apache filtered headers
$header = config('auth.guard_header', 'REMOTE_USER');
$userID = request()->server($header) ?? null;
if (function_exists('apache_request_headers')) {
Log::debug('Use apache_request_headers to find user ID.');
2025-05-11 14:08:32 +02:00
$userID = request()->server($header) ?? apache_request_headers()[$header] ?? null;
}
if (null === $userID || '' === $userID) {
2025-02-22 08:07:52 +01:00
Log::error(sprintf('No user in header "%s".', $header));
2023-12-20 19:35:52 +01:00
throw new FireflyException('The guard header was unexpectedly empty. See the logs.');
2020-06-11 06:55:13 +02:00
}
Log::debug(sprintf('User ID found in header is "%s"', $userID));
2020-10-24 18:40:17 +02:00
/** @var User $retrievedUser */
$retrievedUser = $this->provider->retrieveById($userID);
2020-06-11 06:55:13 +02:00
// store email address if present in header and not already set.
$header = config('auth.guard_email');
if (null !== $header) {
2025-05-11 14:08:32 +02:00
$emailAddress = (string) (request()->server($header) ?? apache_request_headers()[$header] ?? null);
2025-06-30 20:28:29 +02:00
$preference = Preferences::getForUser($retrievedUser, 'remote_guard_alt_email');
if ('' !== $emailAddress && null === $preference && $emailAddress !== $userID) {
2025-06-30 20:28:29 +02:00
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) {
2025-06-30 20:28:29 +02:00
Preferences::setForUser($retrievedUser, 'remote_guard_alt_email', $emailAddress);
}
}
2025-02-22 08:07:52 +01:00
Log::debug(sprintf('Result of getting user from provider: %s', $retrievedUser->email));
$this->user = $retrievedUser;
2020-06-11 06:55:13 +02:00
}
2023-06-21 12:34:58 +02:00
public function guest(): bool
{
2025-02-22 08:07:52 +01:00
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
{
2025-02-22 08:07:52 +01:00
Log::debug(sprintf('Now at %s', __METHOD__));
2023-12-20 19:35:52 +01:00
2025-05-27 17:06:15 +02:00
return $this->user() instanceof 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
{
2025-02-22 08:07:52 +01:00
Log::debug(sprintf('Now at %s', __METHOD__));
2023-06-21 12:34:58 +02:00
$user = $this->user;
2025-05-27 17:06:15 +02:00
if (!$user instanceof User) {
2025-02-22 08:07:52 +01:00
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
{
2025-02-22 08:07:52 +01:00
Log::debug(sprintf('Now at %s', __METHOD__));
2023-12-20 19:35:52 +01:00
throw new FireflyException('Did not implement RemoteUserGuard::hasUser()');
2020-06-11 06:55:13 +02:00
}
/**
2025-01-03 15:53:10 +01:00
* @SuppressWarnings("PHPMD.ShortMethodName")
2020-06-11 06:55:13 +02:00
*/
public function id(): null|int|string
2020-06-11 06:55:13 +02:00
{
2025-02-22 08:07:52 +01:00
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
}
public function setUser(null|Authenticatable|User $user): void // @phpstan-ignore-line
2020-06-11 06:55:13 +02:00
{
2025-02-22 08:07:52 +01:00
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;
}
2025-02-22 08:07:52 +01:00
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:46:05 +01:00
/**
* @throws FireflyException
*
2025-01-03 15:53:10 +01:00
* @SuppressWarnings("PHPMD.UnusedFormalParameter")
2023-12-21 05:46:05 +01:00
*/
2023-12-21 05:15:46 +01:00
public function validate(array $credentials = []): bool
2022-02-28 07:48:58 +01:00
{
2025-02-22 08:07:52 +01:00
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
{
2025-02-22 08:07:52 +01:00
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
}