|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Linna Framework. |
|
5
|
|
|
* |
|
6
|
|
|
* @author Sebastian Rapetti <[email protected]> |
|
7
|
|
|
* @copyright (c) 2018, Sebastian Rapetti |
|
8
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
|
9
|
|
|
*/ |
|
10
|
|
|
declare(strict_types=1); |
|
11
|
|
|
|
|
12
|
|
|
namespace Linna\Authentication; |
|
13
|
|
|
|
|
14
|
|
|
use Linna\Session\Session; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Provide methods for manage basic user authentication system. Checks for |
|
18
|
|
|
* correct login cover only the user name and the password. |
|
19
|
|
|
*/ |
|
20
|
|
|
class Authentication |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var array<mixed> Login status. |
|
24
|
|
|
*/ |
|
25
|
|
|
private array $data = ['user_name'=>'']; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var bool Indicate login status, true or false. |
|
29
|
|
|
*/ |
|
30
|
|
|
private bool $logged = false; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var Password Password class. |
|
34
|
|
|
*/ |
|
35
|
|
|
private Password $password; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var Session Session class. |
|
39
|
|
|
*/ |
|
40
|
|
|
protected Session $session; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Class constructor. |
|
44
|
|
|
* |
|
45
|
|
|
* <pre><code class="php">use Linna\Session\Session; |
|
46
|
|
|
* use Linna\Auth\Password; |
|
47
|
|
|
* |
|
48
|
|
|
* $session = new Session(); |
|
49
|
|
|
* $password = new Password(); |
|
50
|
|
|
* |
|
51
|
|
|
* $auth = new Authentication($session, $password); |
|
52
|
|
|
* </code></pre> |
|
53
|
|
|
* |
|
54
|
|
|
* @param Session $session Session class instance. |
|
55
|
|
|
* @param Password $password Password class instance. |
|
56
|
|
|
*/ |
|
57
|
19 |
|
public function __construct(Session $session, Password $password) |
|
58
|
|
|
{ |
|
59
|
19 |
|
$this->password = $password; |
|
60
|
19 |
|
$this->session = $session; |
|
61
|
19 |
|
$this->logged = $this->refresh(); |
|
62
|
19 |
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Utilize this method for check if an user in the current session, |
|
66
|
|
|
* is currently logged in. |
|
67
|
|
|
* |
|
68
|
|
|
* <pre><code class="php">if ($auth->isLogged()) { |
|
69
|
|
|
* //do actions |
|
70
|
|
|
* } |
|
71
|
|
|
* </code></pre> |
|
72
|
|
|
* |
|
73
|
|
|
* @return bool True if logged false if no. |
|
74
|
|
|
*/ |
|
75
|
16 |
|
public function isLogged(): bool |
|
76
|
|
|
{ |
|
77
|
16 |
|
return $this->logged; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Opposite to isLogged() method. |
|
82
|
|
|
* |
|
83
|
|
|
* Utilize this method for check if an user in the current session, |
|
84
|
|
|
* is currently not logged in. |
|
85
|
|
|
* |
|
86
|
|
|
* <pre><code class="php">if ($auth->isNotLogged()) { |
|
87
|
|
|
* //redirect or other action |
|
88
|
|
|
* } |
|
89
|
|
|
* |
|
90
|
|
|
* //do actions |
|
91
|
|
|
* </code></pre> |
|
92
|
|
|
* |
|
93
|
|
|
* @return bool True if not logged false if logged. |
|
94
|
|
|
*/ |
|
95
|
10 |
|
public function isNotLogged(): bool |
|
96
|
|
|
{ |
|
97
|
10 |
|
return !$this->logged; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Return array containing login data. |
|
102
|
|
|
* |
|
103
|
|
|
* <pre><code class="php">$data = $auth->getLoginData(); |
|
104
|
|
|
* |
|
105
|
|
|
* //var_dump result |
|
106
|
|
|
* //after session start and login, session data appear like below array: |
|
107
|
|
|
* //[ |
|
108
|
|
|
* // 'time' => 1479641396 |
|
109
|
|
|
* // 'expire' => 1800 |
|
110
|
|
|
* // 'loginTime' => 1479641395 |
|
111
|
|
|
* // 'login' => [ |
|
112
|
|
|
* // 'login' => true |
|
113
|
|
|
* // 'user_id' => 1 |
|
114
|
|
|
* // 'user_name' => 'root' |
|
115
|
|
|
* // ] |
|
116
|
|
|
* //] |
|
117
|
|
|
* var_dump($data); |
|
118
|
|
|
* </code></pre> |
|
119
|
|
|
* |
|
120
|
|
|
* @return array<mixed> Login data. |
|
121
|
|
|
*/ |
|
122
|
2 |
|
public function getLoginData(): array |
|
123
|
|
|
{ |
|
124
|
2 |
|
return $this->data; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Try to attemp login with the informations passed by param. |
|
129
|
|
|
* |
|
130
|
|
|
* <pre><code class="php">$user = ''; //user from login page form |
|
131
|
|
|
* $password = ''; //password from login page form |
|
132
|
|
|
* |
|
133
|
|
|
* $storedUser = ''; //user from stored informations |
|
134
|
|
|
* $storedPassword = ''; //password hash from stored informations |
|
135
|
|
|
* $storedId = ''; //user id from stored informations |
|
136
|
|
|
* |
|
137
|
|
|
* $auth->login($user, $password, $storedUser, $storedPassword, $storedId); |
|
138
|
|
|
* |
|
139
|
|
|
* //other operation after login |
|
140
|
|
|
* </code></pre> |
|
141
|
|
|
* |
|
142
|
|
|
* @param string $userName User name from browser input. |
|
143
|
|
|
* @param string $password Password from browser input. |
|
144
|
|
|
* @param string $storedUserName User name from persistent storage. |
|
145
|
|
|
* @param string $storedPassword Password hash from persistent storage. |
|
146
|
|
|
* @param int $storedId User id from persistent storage. |
|
147
|
|
|
* |
|
148
|
|
|
* @return bool True for successful login, false if login fails. |
|
149
|
|
|
*/ |
|
150
|
41 |
|
public function login(string $userName, string $password, string $storedUserName, string $storedPassword, int $storedId): bool |
|
151
|
|
|
{ |
|
152
|
41 |
|
if (\hash_equals($userName, $storedUserName) && $this->password->verify($password, $storedPassword)) { |
|
153
|
|
|
//write valid login on session |
|
154
|
12 |
|
$this->session->loginTime = \time(); |
|
155
|
12 |
|
$this->session->login = [ |
|
156
|
12 |
|
'login' => true, |
|
157
|
12 |
|
'user_id' => $storedId, |
|
158
|
12 |
|
'user_name' => $storedUserName, |
|
159
|
|
|
]; |
|
160
|
|
|
|
|
161
|
|
|
//update login data |
|
162
|
12 |
|
$this->data = $this->session->login; |
|
163
|
|
|
|
|
164
|
|
|
//regenerate session id |
|
165
|
12 |
|
$this->session->regenerate(); |
|
166
|
12 |
|
$this->logged = true; |
|
167
|
|
|
|
|
168
|
12 |
|
return true; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
29 |
|
return false; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Do logout and delete login information from session. |
|
176
|
|
|
* |
|
177
|
|
|
* <pre><code class="php">$auth->logout();</code></pre> |
|
178
|
|
|
* |
|
179
|
|
|
* @return bool True if logout is done. |
|
180
|
|
|
*/ |
|
181
|
2 |
|
public function logout(): bool |
|
182
|
|
|
{ |
|
183
|
|
|
//remove login data from session |
|
184
|
2 |
|
unset($this->session->login, $this->session->loginTime); |
|
185
|
|
|
|
|
186
|
|
|
//regenerate session id |
|
187
|
2 |
|
$this->session->regenerate(); |
|
188
|
2 |
|
$this->logged = false; |
|
189
|
|
|
|
|
190
|
2 |
|
return true; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* Check if user is logged, get login data from session and update it. |
|
195
|
|
|
* |
|
196
|
|
|
* <pre><code class="php">$auth->refresh();</code></pre> |
|
197
|
|
|
* |
|
198
|
|
|
* @return bool True if refresh is done false if no. |
|
199
|
|
|
*/ |
|
200
|
19 |
|
private function refresh(): bool |
|
201
|
|
|
{ |
|
202
|
|
|
//check for login data on in current session |
|
203
|
19 |
|
if (empty($this->session->login)) { |
|
204
|
19 |
|
return false; |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
//take time |
|
208
|
10 |
|
$time = \time(); |
|
209
|
|
|
|
|
210
|
|
|
//check if login expired |
|
211
|
10 |
|
if (($this->session->loginTime + $this->session->expire) < $time) { |
|
212
|
6 |
|
return false; |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
//update login data |
|
216
|
8 |
|
$this->session->loginTime = $time; |
|
217
|
8 |
|
$this->data = $this->session->login; |
|
218
|
|
|
|
|
219
|
8 |
|
return true; |
|
220
|
|
|
} |
|
221
|
|
|
} |
|
222
|
|
|
|