Improve code style in Security\Authentication

This commit is contained in:
Art4 2024-11-06 13:06:54 +01:00
parent ae36d66ddc
commit 94b4009397

View file

@ -9,6 +9,9 @@ namespace Friendica\Security;
use Exception; use Exception;
use Friendica\App; use Friendica\App;
use Friendica\App\BaseURL;
use Friendica\App\Mode;
use Friendica\App\Request;
use Friendica\Core\Config\Capability\IManageConfigValues; use Friendica\Core\Config\Capability\IManageConfigValues;
use Friendica\Core\PConfig\Capability\IManagePersonalConfigValues; use Friendica\Core\PConfig\Capability\IManagePersonalConfigValues;
use Friendica\Core\Hook; use Friendica\Core\Hook;
@ -25,6 +28,7 @@ use LightOpenID;
use Friendica\Core\L10n; use Friendica\Core\L10n;
use Friendica\Core\Worker; use Friendica\Core\Worker;
use Friendica\Model\Contact; use Friendica\Model\Contact;
use Friendica\Model\User\Cookie;
use Friendica\Util\Strings; use Friendica\Util\Strings;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
@ -35,9 +39,9 @@ class Authentication
{ {
/** @var IManageConfigValues */ /** @var IManageConfigValues */
private $config; private $config;
/** @var App\Mode */ /** @var Mode */
private $mode; private $mode;
/** @var App\BaseURL */ /** @var BaseURL */
private $baseUrl; private $baseUrl;
/** @var L10n */ /** @var L10n */
private $l10n; private $l10n;
@ -45,7 +49,7 @@ class Authentication
private $dba; private $dba;
/** @var LoggerInterface */ /** @var LoggerInterface */
private $logger; private $logger;
/** @var User\Cookie */ /** @var Cookie */
private $cookie; private $cookie;
/** @var IHandleUserSessions */ /** @var IHandleUserSessions */
private $session; private $session;
@ -70,18 +74,28 @@ class Authentication
* Authentication constructor. * Authentication constructor.
* *
* @param IManageConfigValues $config * @param IManageConfigValues $config
* @param App\Mode $mode * @param Mode $mode
* @param App\BaseURL $baseUrl * @param BaseURL $baseUrl
* @param L10n $l10n * @param L10n $l10n
* @param Database $dba * @param Database $dba
* @param LoggerInterface $logger * @param LoggerInterface $logger
* @param User\Cookie $cookie * @param Cookie $cookie
* @param IHandleUserSessions $session * @param IHandleUserSessions $session
* @param IManagePersonalConfigValues $pConfig * @param IManagePersonalConfigValues $pConfig
* @param App\Request $request * @param Request $request
*/ */
public function __construct(IManageConfigValues $config, App\Mode $mode, App\BaseURL $baseUrl, L10n $l10n, Database $dba, LoggerInterface $logger, User\Cookie $cookie, IHandleUserSessions $session, IManagePersonalConfigValues $pConfig, App\Request $request) public function __construct(
{ IManageConfigValues $config,
Mode $mode,
BaseURL $baseUrl,
L10n $l10n,
Database $dba,
LoggerInterface $logger,
Cookie $cookie,
IHandleUserSessions $session,
IManagePersonalConfigValues $pConfig,
Request $request
) {
$this->config = $config; $this->config = $config;
$this->mode = $mode; $this->mode = $mode;
$this->baseUrl = $baseUrl; $this->baseUrl = $baseUrl;
@ -97,12 +111,12 @@ class Authentication
/** /**
* Tries to auth the user from the cookie or session * Tries to auth the user from the cookie or session
* *
* @param App $a The Friendica Application context * @param App $app The Friendica Application context
* *
* @throws HttpException\InternalServerErrorException In case of Friendica internal exceptions * @throws HttpException\InternalServerErrorException In case of Friendica internal exceptions
* @throws Exception In case of general exceptions (like SQL Grammar) * @throws Exception In case of general exceptions (like SQL Grammar)
*/ */
public function withSession(App $a) public function withSession(App $app)
{ {
// When the "Friendica" cookie is set, take the value to authenticate and renew the cookie. // When the "Friendica" cookie is set, take the value to authenticate and renew the cookie.
if ($this->cookie->get('uid')) { if ($this->cookie->get('uid')) {
@ -133,7 +147,7 @@ class Authentication
// Do the authentication if not done by now // Do the authentication if not done by now
if (!$this->session->isAuthenticated()) { if (!$this->session->isAuthenticated()) {
$this->setForUser($a, $user); $this->setForUser($app, $user);
if ($this->config->get('system', 'paranoia')) { if ($this->config->get('system', 'paranoia')) {
$this->session->set('addr', $this->cookie->get('ip')); $this->session->set('addr', $this->cookie->get('ip'));
@ -145,7 +159,7 @@ class Authentication
if ($this->session->isVisitor()) { if ($this->session->isVisitor()) {
$contact = $this->dba->selectFirst('contact', ['id'], ['id' => $this->session->get('visitor_id')]); $contact = $this->dba->selectFirst('contact', ['id'], ['id' => $this->session->get('visitor_id')]);
if ($this->dba->isResult($contact)) { if ($this->dba->isResult($contact)) {
$a->setContactId($contact['id']); $app->setContactId($contact['id']);
} }
} }
@ -179,7 +193,7 @@ class Authentication
$this->baseUrl->redirect(); $this->baseUrl->redirect();
} }
$this->setForUser($a, $user); $this->setForUser($app, $user);
} }
} }
@ -218,7 +232,7 @@ class Authentication
/** /**
* Attempts to authenticate using login/password * Attempts to authenticate using login/password
* *
* @param App $a The Friendica Application context * @param App $app The Friendica Application context
* @param string $username * @param string $username
* @param string $password Clear password * @param string $password Clear password
* @param bool $remember Whether to set the session remember flag * @param bool $remember Whether to set the session remember flag
@ -230,7 +244,7 @@ class Authentication
* @throws HTTPException\MovedPermanentlyException * @throws HTTPException\MovedPermanentlyException
* @throws HTTPException\TemporaryRedirectException * @throws HTTPException\TemporaryRedirectException
*/ */
public function withPassword(App $a, string $username, string $password, bool $remember, string $return_path = '') public function withPassword(App $app, string $username, string $password, bool $remember, string $return_path = '')
{ {
$record = null; $record = null;
@ -271,7 +285,7 @@ class Authentication
$return_path = '/security/password_too_long?' . http_build_query(['return_path' => $return_path]); $return_path = '/security/password_too_long?' . http_build_query(['return_path' => $return_path]);
} }
$this->setForUser($a, $record, true, true); $this->setForUser($app, $record, true, true);
$this->baseUrl->redirect($return_path); $this->baseUrl->redirect($return_path);
} }
@ -279,7 +293,7 @@ class Authentication
/** /**
* Sets the provided user's authenticated session * Sets the provided user's authenticated session
* *
* @param App $a The Friendica application context * @param App $app The Friendica application context
* @param array $user_record The current "user" record * @param array $user_record The current "user" record
* @param bool $login_initial * @param bool $login_initial
* @param bool $interactive * @param bool $interactive
@ -293,7 +307,7 @@ class Authentication
* @throws HTTPException\InternalServerErrorException In case of Friendica specific exceptions * @throws HTTPException\InternalServerErrorException In case of Friendica specific exceptions
* *
*/ */
public function setForUser(App $a, array $user_record, bool $login_initial = false, bool $interactive = false, bool $refresh_login = true) public function setForUser(App $app, array $user_record, bool $login_initial = false, bool $interactive = false, bool $refresh_login = true)
{ {
$my_url = $this->baseUrl . '/profile/' . $user_record['nickname']; $my_url = $this->baseUrl . '/profile/' . $user_record['nickname'];
@ -315,12 +329,12 @@ class Authentication
$this->session->set('new_member', time() < ($member_since + (60 * 60 * 24 * 14))); $this->session->set('new_member', time() < ($member_since + (60 * 60 * 24 * 14)));
if (strlen($user_record['timezone'])) { if (strlen($user_record['timezone'])) {
$a->setTimeZone($user_record['timezone']); $app->setTimeZone($user_record['timezone']);
} }
$contact = $this->dba->selectFirst('contact', ['id'], ['uid' => $user_record['uid'], 'self' => true]); $contact = $this->dba->selectFirst('contact', ['id'], ['uid' => $user_record['uid'], 'self' => true]);
if ($this->dba->isResult($contact)) { if ($this->dba->isResult($contact)) {
$a->setContactId($contact['id']); $app->setContactId($contact['id']);
$this->session->set('cid', $contact['id']); $this->session->set('cid', $contact['id']);
} }
@ -442,10 +456,10 @@ class Authentication
if (Strings::compareLink($this->session->get('visitor_home') ?: '', $url)) { if (Strings::compareLink($this->session->get('visitor_home') ?: '', $url)) {
return; return;
} }
$this->session->set('my_url', $url); $this->session->set('my_url', $url);
$this->session->set('authenticated', 0); $this->session->set('authenticated', 0);
$remote_contact = Contact::getByURL($url, false, ['subscribe']); $remote_contact = Contact::getByURL($url, false, ['subscribe']);
if (!empty($remote_contact['subscribe'])) { if (!empty($remote_contact['subscribe'])) {
$this->session->set('remote_comment', $remote_contact['subscribe']); $this->session->set('remote_comment', $remote_contact['subscribe']);