‪TYPO3CMS  ‪main
BackendUserAuthenticator.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Psr\Http\Message\ResponseInterface;
21 use Psr\Http\Message\ServerRequestInterface;
22 use Psr\Http\Server\RequestHandlerInterface;
32 
42 {
43  public function ‪__construct(
44  ‪Context $context,
45  protected readonly ‪LanguageServiceFactory $languageServiceFactory
46  ) {
47  parent::__construct($context);
48  }
49 
53  public function ‪process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
54  {
55  // Initializing a possible logged-in Backend User
56  // If the backend cookie is set,
57  // we proceed and check if a backend user is logged in.
58  $backendUserObject = null;
59  if (isset($request->getCookieParams()[‪BackendUserAuthentication::getCookieName()])) {
60  $backendUserObject = $this->‪initializeBackendUser($request);
61  }
62  ‪$GLOBALS['BE_USER'] = $backendUserObject;
63  // Load specific dependencies which are necessary for a valid Backend User
64  // like $GLOBALS['LANG'] for labels in the language of the BE User, the router, and ext_tables.php for all modules
65  // So things like Frontend Editing and Admin Panel can use this for generating links to the TYPO3 Backend.
66  if ($backendUserObject !== null) {
67  ‪$GLOBALS['LANG'] = $this->languageServiceFactory->createFromUserPreferences(‪$GLOBALS['BE_USER']);
69  $this->‪setBackendUserAspect(‪$GLOBALS['BE_USER']);
70  if ($this->context->getPropertyFromAspect('backend.user', 'isLoggedIn', false)
71  && (strtolower($request->getServerParams()['HTTP_CACHE_CONTROL'] ?? '') === 'no-cache'
72  || strtolower($request->getServerParams()['HTTP_PRAGMA'] ?? '') === 'no-cache')
73  ) {
74  // Detecting if shift-reload has been clicked to disable caching if so.
75  // This is only done if a backend user is logged in to prevent DoS-attacks for "casual" requests.
76  $cacheInstruction = $request->getAttribute('frontend.cache.instruction', new ‪CacheInstruction());
77  $cacheInstruction->disableCache('EXT:frontend: Logged in backend user forced reload disabled cache.');
78  $request = $request->withAttribute('frontend.cache.instruction', $cacheInstruction);
79  }
80  }
81 
82  $response = $handler->handle($request);
83 
84  // If, when building the response, the user is still available, then ensure that the headers are sent properly
85  if ($this->context->getAspect('backend.user')->isLoggedIn()) {
86  return $this->‪applyHeadersToResponse($response);
87  }
88  return $response;
89  }
90 
94  protected function ‪initializeBackendUser(ServerRequestInterface $request): ?‪FrontendBackendUserAuthentication
95  {
96  // New backend user object
97  $backendUserObject = GeneralUtility::makeInstance(FrontendBackendUserAuthentication::class);
98  try {
99  $backendUserObject->start($request);
100  } catch (‪MfaRequiredException $e) {
101  // Do nothing, as the user is not fully authenticated - has not
102  // passed required multi-factor authentication - via the backend.
103  return null;
104  }
105  if (!empty($backendUserObject->user['uid'])) {
106  $this->‪setBackendUserAspect($backendUserObject, (int)$backendUserObject->user['workspace_id']);
107  $backendUserObject->fetchGroupData();
108  }
109  // Unset the user initialization if any setting / restriction applies
110  if (!$this->‪isAuthenticated($backendUserObject, $request, $request->getAttribute('normalizedParams'))) {
111  $backendUserObject = null;
112  $this->‪setBackendUserAspect(null);
113  }
114  return $backendUserObject;
115  }
116 
121  protected function ‪isAuthenticated(‪FrontendBackendUserAuthentication $user, ServerRequestInterface $request, ‪NormalizedParams $normalizedParams): bool
122  {
123  // Check IP
124  $ipMask = trim(‪$GLOBALS['TYPO3_CONF_VARS']['BE']['IPmaskList'] ?? '');
125  if ($ipMask && !‪GeneralUtility::cmpIP($normalizedParams->‪getRemoteAddress(), $ipMask)) {
126  return false;
127  }
128  // Check SSL (https)
129  if ((bool)‪$GLOBALS['TYPO3_CONF_VARS']['BE']['lockSSL'] && !$normalizedParams->‪isHttps()) {
130  return false;
131  }
132  return $user->‪backendCheckLogin($request);
133  }
134 }
‪TYPO3\CMS\Core\Localization\LanguageServiceFactory
Definition: LanguageServiceFactory.php:25
‪TYPO3\CMS\Frontend\Middleware\BackendUserAuthenticator
Definition: BackendUserAuthenticator.php:42
‪TYPO3\CMS\Frontend\Middleware\BackendUserAuthenticator\initializeBackendUser
‪initializeBackendUser(ServerRequestInterface $request)
Definition: BackendUserAuthenticator.php:94
‪TYPO3\CMS\Backend\FrontendBackendUserAuthentication
Definition: FrontendBackendUserAuthentication.php:29
‪TYPO3\CMS\Backend\FrontendBackendUserAuthentication\backendCheckLogin
‪bool backendCheckLogin(ServerRequestInterface $request=null)
Definition: FrontendBackendUserAuthentication.php:70
‪TYPO3\CMS\Frontend\Middleware\BackendUserAuthenticator\isAuthenticated
‪isAuthenticated(FrontendBackendUserAuthentication $user, ServerRequestInterface $request, NormalizedParams $normalizedParams)
Definition: BackendUserAuthenticator.php:121
‪TYPO3\CMS\Frontend\Middleware\BackendUserAuthenticator\__construct
‪__construct(Context $context, protected readonly LanguageServiceFactory $languageServiceFactory)
Definition: BackendUserAuthenticator.php:43
‪TYPO3\CMS\Core\Middleware\BackendUserAuthenticator\setBackendUserAspect
‪setBackendUserAspect(?BackendUserAuthentication $user, int $alternativeWorkspaceId=null)
Definition: BackendUserAuthenticator.php:84
‪TYPO3\CMS\Core\Utility\GeneralUtility\cmpIP
‪static bool cmpIP(string $baseIP, string $list)
Definition: GeneralUtility.php:113
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication\getCookieName
‪static getCookieName()
Definition: BackendUserAuthentication.php:1800
‪TYPO3\CMS\Core\Context\Context
Definition: Context.php:54
‪TYPO3\CMS\Core\Http\NormalizedParams\getRemoteAddress
‪string getRemoteAddress()
Definition: NormalizedParams.php:420
‪TYPO3\CMS\Frontend\Middleware
Definition: BackendUserAuthenticator.php:18
‪TYPO3\CMS\Core\Middleware\BackendUserAuthenticator
Definition: BackendUserAuthenticator.php:51
‪TYPO3\CMS\Core\Authentication\Mfa\MfaRequiredException
Definition: MfaRequiredException.php:29
‪TYPO3\CMS\Core\Middleware\BackendUserAuthenticator\applyHeadersToResponse
‪ResponseInterface applyHeadersToResponse(ResponseInterface $response)
Definition: BackendUserAuthenticator.php:64
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:62
‪TYPO3\CMS\Frontend\Middleware\BackendUserAuthenticator\process
‪process(ServerRequestInterface $request, RequestHandlerInterface $handler)
Definition: BackendUserAuthenticator.php:53
‪TYPO3\CMS\Core\Http\NormalizedParams\isHttps
‪bool isHttps()
Definition: NormalizedParams.php:340
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Core\Bootstrap
Definition: Bootstrap.php:62
‪TYPO3\CMS\Frontend\Cache\CacheInstruction
Definition: CacheInstruction.php:29
‪TYPO3\CMS\Core\Core\Bootstrap\loadExtTables
‪static loadExtTables(bool $allowCaching=true, FrontendInterface $coreCache=null)
Definition: Bootstrap.php:495
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Http\NormalizedParams
Definition: NormalizedParams.php:38