DefaultLoginRateLimiter   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 17
c 2
b 0
f 0
dl 0
loc 30
ccs 0
cts 14
cp 0
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getLimiters() 0 17 4
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Biurad opensource projects.
7
 *
8
 * PHP version 7.4 and above required
9
 *
10
 * @author    Divine Niiquaye Ibok <[email protected]>
11
 * @copyright 2019 Biurad Group (https://biurad.com/)
12
 * @license   https://opensource.org/licenses/BSD-3-Clause License
13
 *
14
 * For the full copyright and license information, please view the LICENSE
15
 * file that was distributed with this source code.
16
 *
17
 */
18
19
namespace Biurad\Security\RateLimiter;
20
21
use Biurad\Http\Request as HttpRequest;
22
use Biurad\Security\Helper;
23
use Psr\Http\Message\ServerRequestInterface;
24
use Symfony\Component\RateLimiter\RateLimiterFactory;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\RateLimiter\RateLimiterFactory was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
25
26
/**
27
 * A default login throttling limiter.
28
 *
29
 * This limiter prevents breadth-first attacks by enforcing
30
 * a limit on username+IP and a (higher) limit on IP.
31
 *
32
 * @author Wouter de Jong <[email protected]>
33
 * @author Divine Niiquaye Ibok <[email protected]>
34
 */
35
final class DefaultLoginRateLimiter extends AbstractRequestRateLimiter
36
{
37
    private RateLimiterFactory $globalFactory;
38
    private RateLimiterFactory $localFactory;
39
    private string $userParameter;
40
41
    public function __construct(RateLimiterFactory $globalFactory, RateLimiterFactory $localFactory, string $userId = '_identifier')
42
    {
43
        $this->globalFactory = $globalFactory;
44
        $this->localFactory = $localFactory;
45
        $this->userParameter = $userId;
46
    }
47
48
    protected function getLimiters(ServerRequestInterface $request): array
49
    {
50
        if ($request instanceof HttpRequest) {
51
            $ip = $request->getRequest()->getClientIp();
52
            $username = $request->getRequest()->get($this->userParameter);
53
        } else {
54
            $username = Helper::getParameterValue($request, $this->userParameter);
55
        }
56
57
        $limiters = [$this->globalFactory->create($ip ?? $request->getServerParams()['REMOTE_ADDR'])];
58
59
        if (!empty($username)) {
60
            $username = \preg_match('//u', $username) ? \mb_strtolower($username, 'UTF-8') : \strtolower($username);
61
            $limiters[] = $this->localFactory->create($username . '-' . $ip ?? $request->getServerParams()['REMOTE_ADDR']);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $ip does not seem to be defined for all execution paths leading up to this point.
Loading history...
62
        }
63
64
        return $limiters;
65
    }
66
}
67