-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSecurity.php
More file actions
152 lines (133 loc) · 3.39 KB
/
Copy pathSecurity.php
File metadata and controls
152 lines (133 loc) · 3.39 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
<?php
/**
* @author : Jakiboy
* @package : FloatPHP
* @subpackage : Helpers Framework Component
* @version : 1.5.x
* @copyright : (c) 2018 - 2025 Jihad Sinnaour <[email protected]>
* @link : https://floatphp.com
* @license : MIT
*
* This file is a part of FloatPHP Framework.
*/
declare(strict_types=1);
namespace FloatPHP\Helpers\Framework;
use FloatPHP\Kernel\BaseController;
use FloatPHP\Classes\Filesystem\Arrayify;
use FloatPHP\Classes\Http\Server;
use FloatPHP\Helpers\Connection\Transient;
class Security extends BaseController
{
/**
* Force strong password.
*
* @access public
* @return object
*/
public function useStrongPassword() : self
{
$this->addFilter('auth-strong-pswd', function () : bool {
return true;
});
return $this;
}
/**
* Force token method.
*
* @access public
* @return object
*/
public function useTokenOnly() : self
{
if ( !Server::getBearerToken() ) {
$msg = $this->applyFilter('api-auth-method-msg', 'Access forbidden');
$this->setHttpResponse($msg, [], 'error', 403);
}
return $this;
}
/**
* Limit login attemps.
*
* @access public
* @param int $max
* @return object
*/
public function useLimitedAttempt(int $max = 3) : self
{
// Log failed authentication
$this->addAction('auth-failed', function ($user) {
if ( !empty($user) ) {
$transient = new Transient();
$key = "auth-{$user}";
if ( !($attempt = $transient->getTemp($key)) ) {
$transient->setTemp($key, 1, 0);
} else {
$transient->setTemp($key, $attempt + 1, 0);
}
}
});
// Apply attempts limit
$this->addAction('authenticate', function ($user) use ($max) {
if ( $user && $this->isType('string', $user) ) {
$key = "auth-{$user}";
$transient = new Transient();
$attempt = $transient->getTemp($key);
if ( $attempt >= $max ) {
$msg = $this->applyFilter('auth-attempt-msg', 'Access forbidden');
$msg = $this->translate($msg);
$this->setResponse($msg, [], 'error', 401);
}
}
});
return $this;
}
/**
* API authentication protection.
*
* @access public
* @param int $max
* @param int $seconds
* @param bool $address
* @param bool $method
* @return object
*/
public function useAccessProtection(int $max = 120, int $seconds = 60, bool $address = true, bool $method = true) : self
{
$args = [
'max' => $max,
'seconds' => $seconds,
'address' => $address,
'method' => $method
];
$this->addAction('api-authenticate', function ($request = []) use ($args) {
// Exception
$exception = (array)$this->applyFilter('api-exception', []);
if ( Arrayify::inArray($request['username'], $exception) ) {
return;
}
// Authentication
$transient = new Transient();
$key = "api-auth-{$request['username']}";
// Address check
if ( $args['address'] ) {
$key = "{$key}-{$request['address']}";
}
// Method check
if ( $args['method'] ) {
$key = "{$key}-{$request['method']}";
}
// Attempts
$attempts = 0;
if ( !($attempts = $transient->getTemp($key)) ) {
$transient->setTemp($key, 1, $args['seconds']);
} else {
$transient->setTemp($key, $attempts + 1, $args['seconds']);
}
if ( $attempts >= $args['max'] && $args['max'] !== 0 ) {
$msg = $this->applyFilter('api-auth-attempt-msg', 'Access forbidden');
$this->setHttpResponse($msg, [], 'error', 429);
}
});
return $this;
}
}