Skip to content

Commit 10cf320

Browse files
authored
[BUGFIX] Correct JSON response on unauthorized access exception (#151)
Unauthorized requests return a correct JSON response, also when the authorization header is missing or the method is wrong.
1 parent 077b47e commit 10cf320

2 files changed

Lines changed: 20 additions & 9 deletions

File tree

Classes/Authentication/HttpBackendUserAuthentication.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,12 @@ public function getLoginFormData(ServerRequestInterface $request)
6161
);
6262
}
6363

64-
$authorizationHeader = $request->getHeader('authorization')[0]
65-
?? $request->getHeader('redirect_http_authorization')[0]
66-
?? '';
64+
$authorizationHeader = $this->resolveAuthorizationHeader($request);
6765

6866
[$scheme, $authorizationData] = GeneralUtility::trimExplode(' ', $authorizationHeader, true);
6967

7068
if ($scheme === null) {
71-
throw new InvalidArgumentException(
69+
throw new UnauthorizedAccessException(
7270
'No authorization scheme provided.',
7371
$request
7472
);
@@ -109,9 +107,7 @@ public function getLoginFormData(ServerRequestInterface $request)
109107
*/
110108
protected function authenticateBearerToken(ServerRequestInterface $request): void
111109
{
112-
$authorizationHeader = $request->getHeader('authorization')[0]
113-
?? $request->getHeader('redirect_http_authorization')[0]
114-
?? '';
110+
$authorizationHeader = $this->resolveAuthorizationHeader($request);
115111

116112
[$scheme, $token] = GeneralUtility::trimExplode(' ', $authorizationHeader, true);
117113

@@ -154,4 +150,19 @@ protected function getAuthServiceConfiguration(): array
154150

155151
return $configuration;
156152
}
153+
154+
/**
155+
* @param ServerRequestInterface $request
156+
* @return string
157+
* @throws UnauthorizedAccessException if no authorization scheme is provided.
158+
*/
159+
protected function resolveAuthorizationHeader(ServerRequestInterface $request): string
160+
{
161+
return $request->getHeader('authorization')[0]
162+
?? $request->getHeader('redirect_http_authorization')[0]
163+
?? throw new UnauthorizedAccessException(
164+
'No authorization scheme provided.',
165+
$request
166+
);
167+
}
157168
}

Classes/Router/HttpRequestRouter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ class HttpRequestRouter
3737
*/
3838
public static function route(ServerRequestInterface $request): ResponseInterface
3939
{
40-
self::initialize($request);
41-
4240
$extensionConfiguration = GeneralUtility::makeInstance(ExtensionConfiguration::class);
4341

4442
$entryPoint = substr(
@@ -54,6 +52,8 @@ public static function route(ServerRequestInterface $request): ResponseInterface
5452
);
5553

5654
try {
55+
self::initialize($request);
56+
5757
if (($entryPointParts[0] ?? null) === 'authenticate') {
5858
return GeneralUtility::makeInstance(
5959
AuthenticateRequestHandler::class,

0 commit comments

Comments
 (0)