Passed
Push — master ( ef2180...939003 )
by
unknown
19:34
created

FrontendUserAuthenticator   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
eloc 42
c 1
b 0
f 0
dl 0
loc 96
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A ensureLoginRateLimit() 0 20 5
A sessionGarbageCollection() 0 3 1
B process() 0 42 6
A __construct() 0 4 1
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 Psr\Log\LoggerAwareInterface;
25
use Psr\Log\LoggerAwareTrait;
26
use Symfony\Component\RateLimiter\LimiterInterface;
27
use TYPO3\CMS\Core\Context\Context;
28
use TYPO3\CMS\Core\RateLimiter\RateLimiterFactory;
29
use TYPO3\CMS\Core\RateLimiter\RequestRateLimitedException;
30
use TYPO3\CMS\Core\Session\UserSessionManager;
31
use TYPO3\CMS\Core\Utility\GeneralUtility;
32
use TYPO3\CMS\Core\Utility\HttpUtility;
33
use TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication;
34
35
/**
36
 * This middleware authenticates a Frontend User (fe_users).
37
 */
38
class FrontendUserAuthenticator implements MiddlewareInterface, LoggerAwareInterface
39
{
40
    use LoggerAwareTrait;
41
42
    /**
43
     * @var Context
44
     */
45
    protected $context;
46
    protected RateLimiterFactory $rateLimiterFactory;
47
48
    public function __construct(Context $context, RateLimiterFactory $rateLimiterFactory)
49
    {
50
        $this->context = $context;
51
        $this->rateLimiterFactory = $rateLimiterFactory;
52
    }
53
54
    /**
55
     * Creates a frontend user authentication object, tries to authenticate a user and stores
56
     * it in the current request as attribute.
57
     *
58
     * @param ServerRequestInterface $request
59
     * @param RequestHandlerInterface $handler
60
     * @return ResponseInterface
61
     */
62
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
63
    {
64
        $frontendUser = GeneralUtility::makeInstance(FrontendUserAuthentication::class);
65
66
        // List of page IDs where to look for frontend user records
67
        $pid = $request->getParsedBody()['pid'] ?? $request->getQueryParams()['pid'] ?? 0;
68
        if ($pid) {
69
            $frontendUser->checkPid_value = implode(',', GeneralUtility::intExplode(',', $pid));
70
        }
71
72
        // Rate Limiting
73
        $rateLimiter = $this->ensureLoginRateLimit($frontendUser, $request);
74
75
        // Authenticate now
76
        $frontendUser->start();
77
        $frontendUser->unpack_uc();
78
        // no matter if we have an active user we try to fetch matching groups which can
79
        // be set without an user (simulation for instance!)
80
        $frontendUser->fetchGroupData();
81
82
        // Register the frontend user as aspect and within the request
83
        $userAspect = $frontendUser->createUserAspect();
84
        $this->context->setAspect('frontend.user', $userAspect);
85
        $request = $request->withAttribute('frontend.user', $frontendUser);
86
87
        if ($this->context->getAspect('frontend.user')->isLoggedIn() && $rateLimiter) {
0 ignored issues
show
Bug introduced by
The method isLoggedIn() does not exist on TYPO3\CMS\Core\Context\AspectInterface. It seems like you code against a sub-type of TYPO3\CMS\Core\Context\AspectInterface such as TYPO3\CMS\Core\Context\UserAspect. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

87
        if ($this->context->getAspect('frontend.user')->/** @scrutinizer ignore-call */ isLoggedIn() && $rateLimiter) {
Loading history...
88
            $rateLimiter->reset();
89
        }
90
91
        $response = $handler->handle($request);
92
93
        // Store session data for fe_users if it still exists
94
        if ($frontendUser instanceof FrontendUserAuthentication) {
0 ignored issues
show
introduced by
$frontendUser is always a sub-type of TYPO3\CMS\Frontend\Authe...ntendUserAuthentication.
Loading history...
95
            $frontendUser->storeSessionData();
96
            $response = $frontendUser->appendCookieToResponse($response);
97
            // Collect garbage in Frontend requests, which aren't fully cacheable (e.g. with cookies)
98
            if ($response->hasHeader('Set-Cookie')) {
99
                $this->sessionGarbageCollection();
100
            }
101
        }
102
103
        return $response;
104
    }
105
106
    /**
107
     * Garbage collection for fe_sessions (with a probability)
108
     */
109
    protected function sessionGarbageCollection(): void
110
    {
111
        UserSessionManager::create('FE')->collectGarbage();
112
    }
113
114
    protected function ensureLoginRateLimit(FrontendUserAuthentication $user, ServerRequestInterface $request): ?LimiterInterface
115
    {
116
        if (!$user->isActiveLogin($request)) {
117
            return null;
118
        }
119
        $loginRateLimiter = $this->rateLimiterFactory->createLoginRateLimiter($user, $request);
120
        $limit = $loginRateLimiter->consume();
121
        if ($limit && !$limit->isAccepted()) {
122
            $this->logger->debug('Login request has been rate limited for IP address {ipAddress}', ['ipAddress' => $request->getAttribute('normalizedParams')->getRemoteAddress()]);
0 ignored issues
show
Bug introduced by
The method debug() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

122
            $this->logger->/** @scrutinizer ignore-call */ 
123
                           debug('Login request has been rate limited for IP address {ipAddress}', ['ipAddress' => $request->getAttribute('normalizedParams')->getRemoteAddress()]);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
123
            $dateformat = $GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy'] . ' ' . $GLOBALS['TYPO3_CONF_VARS']['SYS']['hhmm'];
124
            $lockedUntil = $limit->getRetryAfter()->getTimestamp() > 0 ?
125
                ' until ' . date($dateformat, $limit->getRetryAfter()->getTimestamp()) : '';
126
            throw new RequestRateLimitedException(
127
                HttpUtility::HTTP_STATUS_403,
128
                'The login is locked' . $lockedUntil . ' due to too many failed login attempts from your IP address.',
129
                'Login Request Rate Limited',
130
                1616175847
131
            );
132
        }
133
        return $loginRateLimiter;
134
    }
135
}
136