‪TYPO3CMS  9.5
FrontendUserAuthenticator.php
Go to the documentation of this file.
1 <?php
2 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 
18 use Psr\Http\Message\ResponseInterface;
19 use Psr\Http\Message\ServerRequestInterface;
20 use Psr\Http\Server\MiddlewareInterface;
21 use Psr\Http\Server\RequestHandlerInterface;
27 
32 class ‪FrontendUserAuthenticator implements MiddlewareInterface
33 {
42  public function ‪process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
43  {
44  $frontendUser = GeneralUtility::makeInstance(FrontendUserAuthentication::class);
45 
46  // List of page IDs where to look for frontend user records
47  $pid = $request->getParsedBody()['pid'] ?? $request->getQueryParams()['pid'] ?? 0;
48  if ($pid) {
49  $frontendUser->checkPid_value = implode(',', GeneralUtility::intExplode(',', $pid));
50  }
51 
52  // Check if a session is transferred, and update the cookie parameters
53  $frontendSessionKey = $request->getParsedBody()['FE_SESSION_KEY'] ?? $request->getQueryParams()['FE_SESSION_KEY'] ?? '';
54  if ($frontendSessionKey) {
55  $request = $this->‪transferFrontendUserSession($frontendUser, $request, $frontendSessionKey);
56  }
57 
58  // Authenticate now
59  $frontendUser->start();
60  $frontendUser->unpack_uc();
61 
62  // Keep the backwards-compatibility for TYPO3 v9, to have the fe_user within the global TSFE object
63  ‪$GLOBALS['TSFE']->fe_user = $frontendUser;
64 
65  // Call hook for possible manipulation of frontend user object
66  // This hook is kept for compatibility reasons, however, it should be fairly simple to add a custom middleware
67  // for this purpose
68  if (!empty(‪$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_fe.php']['initFEuser'])) {
69  trigger_error('The "initFEuser" hook will be removed in TYPO3 v10.0 in favor of PSR-15. Use a middleware instead.', E_USER_DEPRECATED);
70  $_params = ['pObj' => &‪$GLOBALS['TSFE']];
71  foreach (‪$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_fe.php']['initFEuser'] as $_funcRef) {
72  GeneralUtility::callUserFunction($_funcRef, $_params, ‪$GLOBALS['TSFE']);
73  }
74  }
75 
76  // Register the frontend user as aspect
77  $this->‪setFrontendUserAspect(GeneralUtility::makeInstance(Context::class), $frontendUser);
78 
79  return $handler->handle($request);
80  }
81 
92  protected function ‪transferFrontendUserSession(
93  ‪FrontendUserAuthentication $frontendUser,
94  ServerRequestInterface $request,
95  string $frontendSessionKey
96  ): ServerRequestInterface {
97  list($sessionId, $hash) = explode('-', $frontendSessionKey);
98  // If the session key hash check is OK, set the cookie
99  if (hash_equals(md5($sessionId . '/' . ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey']), (string)$hash)) {
101 
102  // keep the global cookie overwriting for now, as long as FrontendUserAuthentication does not
103  // use the request object for fetching the cookie information.
104  $_COOKIE[$cookieName] = $sessionId;
105  if (isset($_SERVER['HTTP_COOKIE'])) {
106  // See http://forge.typo3.org/issues/27740
107  $_SERVER['HTTP_COOKIE'] .= ';' . $cookieName . '=' . $sessionId;
108  }
109  // Add the cookie to the Server Request object
110  $cookieParams = $request->getCookieParams();
111  $cookieParams[$cookieName] = $sessionId;
112  $request = $request->withCookieParams($cookieParams);
113  // @deprecated: we override the current request because it was enriched by cookie information here.
114  ‪$GLOBALS['TYPO3_REQUEST'] = $request;
115  $frontendUser->forceSetCookie = true;
116  $frontendUser->dontSetCookie = false;
117  }
118  return $request;
119  }
120 
128  {
129  $context->‪setAspect('frontend.user', GeneralUtility::makeInstance(UserAspect::class, $user));
130  }
131 }
‪TYPO3\CMS\Frontend\Middleware\FrontendUserAuthenticator\transferFrontendUserSession
‪ServerRequestInterface transferFrontendUserSession(FrontendUserAuthentication $frontendUser, ServerRequestInterface $request, string $frontendSessionKey)
Definition: FrontendUserAuthenticator.php:92
‪TYPO3\CMS\Frontend\Middleware\FrontendUserAuthenticator\setFrontendUserAspect
‪setFrontendUserAspect(Context $context, AbstractUserAuthentication $user)
Definition: FrontendUserAuthenticator.php:127
‪TYPO3\CMS\Core\Context\Context
Definition: Context.php:49
‪TYPO3\CMS\Core\Context\Context\setAspect
‪setAspect(string $name, AspectInterface $aspect)
Definition: Context.php:141
‪TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication\getCookieName
‪static string getCookieName()
Definition: FrontendUserAuthentication.php:137
‪TYPO3\CMS\Frontend\Middleware
Definition: BackendUserAuthenticator.php:4
‪TYPO3\CMS\Frontend\Middleware\FrontendUserAuthenticator\process
‪ResponseInterface process(ServerRequestInterface $request, RequestHandlerInterface $handler)
Definition: FrontendUserAuthenticator.php:42
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication
Definition: FrontendUserAuthentication.php:28
‪TYPO3\CMS\Frontend\Middleware\FrontendUserAuthenticator
Definition: FrontendUserAuthenticator.php:33
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Core\Context\UserAspect
Definition: UserAspect.php:36
‪TYPO3\CMS\Core\Authentication\AbstractUserAuthentication
Definition: AbstractUserAuthentication.php:51