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

179 lines
5.7 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;
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;
2020-06-11 06:55:13 +02:00
/**
* Class RemoteUserGuard
*/
class RemoteUserGuard implements Guard
{
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.
*/
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 */
$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__));
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;
}
// 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')) {
2023-10-29 06:33:43 +01:00
app('log')->debug('Use apache_request_headers to find user ID.');
$userID = request()->server($header) ?? apache_request_headers()[$header] ?? null;
}
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
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));
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) {
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');
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) {
app('preferences')->setForUser($retrievedUser, 'remote_guard_alt_email', $emailAddress);
}
}
2023-10-29 06:33:43 +01:00
app('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
{
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
throw new FireflyException('Did not implement RemoteUserGuard::hasUser()');
2020-06-11 06:55:13 +02: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:46:05 +01:00
/**
* @throws FireflyException
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
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
}