|
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
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return $response; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* It's possible to transfer a frontend user session via a GET/POST parameter 'FE_SESSION_KEY'. |
|
91
|
|
|
* In the future, this logic should be moved into the FrontendUserAuthentication object directly, |
|
92
|
|
|
* but only if FrontendUserAuthentication does not request superglobals (like $_COOKIE) anymore. |
|
93
|
|
|
* |
|
94
|
|
|
* @param FrontendUserAuthentication $frontendUser |
|
95
|
|
|
* @param ServerRequestInterface $request |
|
96
|
|
|
* @param string $frontendSessionKey |
|
97
|
|
|
* @return ServerRequestInterface |
|
98
|
|
|
*/ |
|
99
|
|
|
protected function transferFrontendUserSession( |
|
100
|
|
|
FrontendUserAuthentication $frontendUser, |
|
101
|
|
|
ServerRequestInterface $request, |
|
102
|
|
|
string $frontendSessionKey |
|
103
|
|
|
): ServerRequestInterface { |
|
104
|
|
|
[$sessionId, $hash] = explode('-', $frontendSessionKey); |
|
105
|
|
|
// If the session key hash check is OK, set the cookie |
|
106
|
|
|
if (hash_equals(md5($sessionId . '/' . $GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey']), (string)$hash)) { |
|
107
|
|
|
$cookieName = FrontendUserAuthentication::getCookieName(); |
|
108
|
|
|
|
|
109
|
|
|
// keep the global cookie overwriting for now, as long as FrontendUserAuthentication does not |
|
110
|
|
|
// use the request object for fetching the cookie information. |
|
111
|
|
|
$_COOKIE[$cookieName] = $sessionId; |
|
112
|
|
|
if (isset($_SERVER['HTTP_COOKIE'])) { |
|
113
|
|
|
// See https://forge.typo3.org/issues/27740 |
|
114
|
|
|
$_SERVER['HTTP_COOKIE'] .= ';' . $cookieName . '=' . $sessionId; |
|
115
|
|
|
} |
|
116
|
|
|
// Add the cookie to the Server Request object |
|
117
|
|
|
$cookieParams = $request->getCookieParams(); |
|
118
|
|
|
$cookieParams[$cookieName] = $sessionId; |
|
119
|
|
|
$request = $request->withCookieParams($cookieParams); |
|
120
|
|
|
$frontendUser->forceSetCookie = true; |
|
121
|
|
|
$frontendUser->dontSetCookie = false; |
|
122
|
|
|
} |
|
123
|
|
|
return $request; |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|