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