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
|
|
|
|
18
|
|
|
namespace TYPO3\CMS\Frontend\Middleware; |
19
|
|
|
|
20
|
|
|
use Psr\Http\Message\ResponseInterface; |
21
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
22
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
23
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
24
|
|
|
use TYPO3\CMS\Core\Context\Context; |
25
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
26
|
|
|
use TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* This middleware authenticates a Frontend User (fe_users). |
30
|
|
|
*/ |
31
|
|
|
class FrontendUserAuthenticator implements MiddlewareInterface |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* @var Context |
35
|
|
|
*/ |
36
|
|
|
protected $context; |
37
|
|
|
|
38
|
|
|
public function __construct(Context $context) |
39
|
|
|
{ |
40
|
|
|
$this->context = $context; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Creates a frontend user authentication object, tries to authenticate a user and stores |
45
|
|
|
* it in the current request as attribute. |
46
|
|
|
* |
47
|
|
|
* @param ServerRequestInterface $request |
48
|
|
|
* @param RequestHandlerInterface $handler |
49
|
|
|
* @return ResponseInterface |
50
|
|
|
*/ |
51
|
|
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
52
|
|
|
{ |
53
|
|
|
$frontendUser = GeneralUtility::makeInstance(FrontendUserAuthentication::class); |
54
|
|
|
|
55
|
|
|
// List of page IDs where to look for frontend user records |
56
|
|
|
$pid = $request->getParsedBody()['pid'] ?? $request->getQueryParams()['pid'] ?? 0; |
57
|
|
|
if ($pid) { |
58
|
|
|
$frontendUser->checkPid_value = implode(',', GeneralUtility::intExplode(',', $pid)); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
// Check if a session is transferred, and update the cookie parameters |
62
|
|
|
$frontendSessionKey = $request->getParsedBody()['FE_SESSION_KEY'] ?? $request->getQueryParams()['FE_SESSION_KEY'] ?? ''; |
63
|
|
|
if ($frontendSessionKey) { |
64
|
|
|
$request = $this->transferFrontendUserSession($frontendUser, $request, $frontendSessionKey); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
// Authenticate now |
68
|
|
|
$frontendUser->start(); |
69
|
|
|
$frontendUser->unpack_uc(); |
70
|
|
|
// no matter if we have an active user we try to fetch matching groups which can |
71
|
|
|
// be set without an user (simulation for instance!) |
72
|
|
|
$frontendUser->fetchGroupData(); |
73
|
|
|
|
74
|
|
|
// Register the frontend user as aspect and within the request |
75
|
|
|
$userAspect = $frontendUser->createUserAspect(); |
76
|
|
|
$this->context->setAspect('frontend.user', $userAspect); |
77
|
|
|
$request = $request->withAttribute('frontend.user', $frontendUser); |
78
|
|
|
|
79
|
|
|
$response = $handler->handle($request); |
80
|
|
|
|
81
|
|
|
// Store session data for fe_users if it still exists |
82
|
|
|
if ($frontendUser instanceof FrontendUserAuthentication) { |
83
|
|
|
$frontendUser->storeSessionData(); |
84
|
|
|
if ($frontendUser->sendNoCacheHeaders) { |
85
|
|
|
$response = $this->applyHeadersToResponse($response); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $response; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* It's possible to transfer a frontend user session via a GET/POST parameter 'FE_SESSION_KEY'. |
94
|
|
|
* In the future, this logic should be moved into the FrontendUserAuthentication object directly, |
95
|
|
|
* but only if FrontendUserAuthentication does not request superglobals (like $_COOKIE) anymore. |
96
|
|
|
* |
97
|
|
|
* @param FrontendUserAuthentication $frontendUser |
98
|
|
|
* @param ServerRequestInterface $request |
99
|
|
|
* @param string $frontendSessionKey |
100
|
|
|
* @return ServerRequestInterface |
101
|
|
|
*/ |
102
|
|
|
protected function transferFrontendUserSession( |
103
|
|
|
FrontendUserAuthentication $frontendUser, |
104
|
|
|
ServerRequestInterface $request, |
105
|
|
|
string $frontendSessionKey |
106
|
|
|
): ServerRequestInterface { |
107
|
|
|
[$sessionId, $hash] = explode('-', $frontendSessionKey); |
108
|
|
|
// If the session key hash check is OK, set the cookie |
109
|
|
|
if (hash_equals(md5($sessionId . '/' . $GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey']), (string)$hash)) { |
110
|
|
|
$cookieName = FrontendUserAuthentication::getCookieName(); |
111
|
|
|
|
112
|
|
|
// keep the global cookie overwriting for now, as long as FrontendUserAuthentication does not |
113
|
|
|
// use the request object for fetching the cookie information. |
114
|
|
|
$_COOKIE[$cookieName] = $sessionId; |
115
|
|
|
if (isset($_SERVER['HTTP_COOKIE'])) { |
116
|
|
|
// See https://forge.typo3.org/issues/27740 |
117
|
|
|
$_SERVER['HTTP_COOKIE'] .= ';' . $cookieName . '=' . $sessionId; |
118
|
|
|
} |
119
|
|
|
// Add the cookie to the Server Request object |
120
|
|
|
$cookieParams = $request->getCookieParams(); |
121
|
|
|
$cookieParams[$cookieName] = $sessionId; |
122
|
|
|
$request = $request->withCookieParams($cookieParams); |
123
|
|
|
$frontendUser->forceSetCookie = true; |
124
|
|
|
$frontendUser->dontSetCookie = false; |
125
|
|
|
} |
126
|
|
|
return $request; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Adding headers to the response to avoid caching on the client side. |
131
|
|
|
* These headers will override any previous headers of these names sent. |
132
|
|
|
* Get the http headers to be sent if an authenticated user is available, |
133
|
|
|
* in order to disallow browsers to store the response on the client side. |
134
|
|
|
* |
135
|
|
|
* @param ResponseInterface $response |
136
|
|
|
* @return ResponseInterface the modified response object. |
137
|
|
|
*/ |
138
|
|
|
protected function applyHeadersToResponse(ResponseInterface $response): ResponseInterface |
139
|
|
|
{ |
140
|
|
|
$headers = [ |
141
|
|
|
'Expires' => 0, |
142
|
|
|
'Last-Modified' => gmdate('D, d M Y H:i:s') . ' GMT', |
143
|
|
|
'Cache-Control' => 'no-cache, must-revalidate', |
144
|
|
|
'Pragma' => 'no-cache' |
145
|
|
|
]; |
146
|
|
|
foreach ($headers as $headerName => $headerValue) { |
147
|
|
|
$response = $response->withHeader($headerName, (string)$headerValue); |
148
|
|
|
} |
149
|
|
|
return $response; |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|