-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathHttpBackendUserAuthentication.php
More file actions
164 lines (133 loc) · 4.66 KB
/
Copy pathHttpBackendUserAuthentication.php
File metadata and controls
164 lines (133 loc) · 4.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
declare(strict_types=1);
namespace FriendsOfTYPO3\Interest\Authentication;
use FriendsOfTYPO3\Interest\Domain\Repository\TokenRepository;
use FriendsOfTYPO3\Interest\RequestHandler\Exception\InvalidArgumentException;
use FriendsOfTYPO3\Interest\RequestHandler\Exception\UnauthorizedAccessException;
use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
use TYPO3\CMS\Core\Authentication\LoginType;
use TYPO3\CMS\Core\Utility\GeneralUtility;
class HttpBackendUserAuthentication extends BackendUserAuthentication
{
/**
* Check if user is authenticated.
*
* @return bool
*/
public function isAuthenticated(): bool
{
return $this->getUserId() !== 0 && $this->getUserId() !== null;
}
/**
* Returns the user's UID.
*
* @return int
*/
public function getUserId(): int
{
return $this->user['uid'] ?? 0;
}
public function checkAuthentication(ServerRequestInterface $request): void
{
$this->authenticateBearerToken($request);
if (!$this->isAuthenticated()) {
// Check if the user is authenticated via basic HTTP authentication.
parent::checkAuthentication($request);
}
if (!$this->isAuthenticated()) {
return;
}
$this->unpack_uc();
$this->fetchGroupData();
$this->backendSetUC();
$this->workspaceInit();
}
/**
* Fetches login credentials from basic HTTP authentication header.
*
* @param ServerRequestInterface $request
* @return array
* @throws UnauthorizedAccessException
* @throws InvalidArgumentException
*/
public function getLoginFormData(ServerRequestInterface $request)
{
$authorizationHeader = $this->resolveAuthorizationHeader($request);
[$scheme, $authorizationData] = GeneralUtility::trimExplode(' ', $authorizationHeader, true);
if ($scheme === null) {
throw new UnauthorizedAccessException(
'No authorization scheme provided.',
$request
);
}
if (strtolower($scheme) !== 'basic') {
return $this->processLoginData([], $request);
}
$authorizationData = base64_decode($authorizationData, true);
if (!str_contains($authorizationData, ':')) {
throw new InvalidArgumentException(
'Authorization data couldn\'t be decoded. Missing ":" separating username and password.',
$request
);
}
[$username, $password] = explode(':', $authorizationData);
$loginData = [
'status' => LoginType::LOGIN->value,
'uname' => $username,
'uident' => $password,
];
return $this->processLoginData($loginData, $request);
}
/**
* Authenticates a token provided in the request.
*
* @param ServerRequestInterface $request
* @throws UnauthorizedAccessException
*/
protected function authenticateBearerToken(ServerRequestInterface $request): void
{
$authorizationHeader = $this->resolveAuthorizationHeader($request);
[$scheme, $token] = GeneralUtility::trimExplode(' ', $authorizationHeader, true);
if ($scheme === null) {
return;
}
if (is_string($scheme) && strtolower($scheme) !== 'bearer') {
return;
}
$backendUserId = GeneralUtility::makeInstance(TokenRepository::class)
->findBackendUserIdByToken($token);
if ($backendUserId === 0) {
throw new UnauthorizedAccessException(
'Invalid or expired bearer token.',
$request
);
}
$this->setBeUserByUid($backendUserId);
}
/**
* Returns the authentication service configuration with `BE_fetchUserIfNoSession` set to true.
*
* @return array
*/
protected function getAuthServiceConfiguration(): array
{
$configuration = parent::getAuthServiceConfiguration();
$configuration['BE_fetchUserIfNoSession'] = true;
return $configuration;
}
/**
* @param ServerRequestInterface $request
* @return string
* @throws UnauthorizedAccessException if no authorization scheme is provided.
*/
protected function resolveAuthorizationHeader(ServerRequestInterface $request): string
{
return $request->getHeader('authorization')[0]
?? $request->getHeader('redirect_http_authorization')[0]
?? throw new UnauthorizedAccessException(
'No authorization scheme provided.',
$request
);
}
}