1 | <?php |
||||
2 | /** |
||||
3 | * This file is part of the O2System Framework package. |
||||
4 | * |
||||
5 | * For the full copyright and license information, please view the LICENSE |
||||
6 | * file that was distributed with this source code. |
||||
7 | * |
||||
8 | * @author Steeve Andrian Salim |
||||
9 | * @copyright Copyright (c) Steeve Andrian Salim |
||||
10 | */ |
||||
11 | |||||
12 | // ------------------------------------------------------------------------ |
||||
13 | |||||
14 | namespace O2System\Security\Protections; |
||||
15 | |||||
16 | // ------------------------------------------------------------------------ |
||||
17 | |||||
18 | use O2System\Kernel\Http\Message\Request; |
||||
19 | use O2System\Security\Protections\Throttle\Consumer; |
||||
20 | use O2System\Security\Protections\Throttle\Rate; |
||||
21 | |||||
22 | /** |
||||
23 | * Class Throttle |
||||
24 | * @package O2System\Security\Protections |
||||
25 | */ |
||||
26 | class Throttle |
||||
27 | { |
||||
28 | protected $key; |
||||
29 | protected $repository; |
||||
30 | protected $rate; |
||||
31 | protected $consumer; |
||||
32 | |||||
33 | public function __construct() |
||||
34 | { |
||||
35 | $this->repository = new Throttle\Repository(); |
||||
36 | } |
||||
37 | |||||
38 | public function setKey($key) |
||||
39 | { |
||||
40 | $this->key = $key; |
||||
41 | } |
||||
42 | |||||
43 | public function getConsumerData($consumerId) |
||||
44 | { |
||||
45 | if (class_exists('\O2System\Framework', false) or class_exists('\O2System\Reactor', false)) { |
||||
46 | return cache()->get('throttle-' . $consumerId); |
||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
47 | } |
||||
48 | } |
||||
49 | |||||
50 | public function rate(array $config = []) |
||||
51 | { |
||||
52 | $this->rate = new Rate($config); |
||||
53 | } |
||||
54 | |||||
55 | public function request(Request $request) |
||||
56 | { |
||||
57 | $this->consumer = new Consumer([ |
||||
58 | 'ipAddress' => $request->getClientIpAddress(), |
||||
59 | 'userAgent' => $request->getClientUserAgent(), |
||||
60 | ]); |
||||
61 | |||||
62 | $consumerId = empty($this->key) ? '' : $this->key . '.'; |
||||
63 | $consumerId .= $this->consumer->getId(); |
||||
64 | |||||
65 | if ($this->repository->has($consumerId)) { |
||||
66 | $consumerData = $this->repository->get($consumerId); |
||||
67 | $consumerData[ 'id' ] = $consumerId; |
||||
68 | $consumerData[ 'currentCallTime' ] = $request->getTime(); |
||||
69 | $consumerData[ 'attempts' ] = 1; |
||||
70 | |||||
71 | $this->consumer->merge($consumerData); |
||||
0 ignored issues
–
show
It seems like
$consumerData can also be of type false and null ; however, parameter $values of O2System\Spl\DataStructu...SplArrayObject::merge() does only seem to accept array , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
72 | } else { |
||||
73 | $consumerData = $this->consumer->getArrayCopy(); |
||||
74 | $consumerData[ 'id' ] = $consumerId; |
||||
75 | $consumerData[ 'lastCallTime' ] = $consumerData[ 'currentCallTime' ] = $request->getTime(); |
||||
76 | $consumerData[ 'attempts' ] = $consumerData[ 'attempts' ] + 1; |
||||
77 | |||||
78 | $this->repository->store($consumerId, $consumerData); |
||||
79 | } |
||||
80 | } |
||||
81 | |||||
82 | public function verify() |
||||
83 | { |
||||
84 | $currentTime = strtotime(date('r')); |
||||
85 | $timeCall = abs($this->consumer[ 'lastCallTime' ] - $currentTime); |
||||
86 | |||||
87 | $this->consumer[ 'lastCallTime' ] = $currentTime; |
||||
88 | |||||
89 | if ($timeCall > $this->rate[ 'span' ]) { |
||||
90 | return false; |
||||
91 | } elseif ($this->consumer[ 'attempts' ] > $this->rate[ 'attempts' ]) { |
||||
92 | return false; |
||||
93 | } |
||||
94 | |||||
95 | return true; |
||||
96 | } |
||||
97 | } |