1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Platine Framework |
5
|
|
|
* |
6
|
|
|
* Platine Framework is a lightweight, high-performance, simple and elegant |
7
|
|
|
* PHP Web framework |
8
|
|
|
* |
9
|
|
|
* This content is released under the MIT License (MIT) |
10
|
|
|
* |
11
|
|
|
* Copyright (c) 2020 Platine Framework |
12
|
|
|
* |
13
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
14
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
15
|
|
|
* in the Software without restriction, including without limitation the rights |
16
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
17
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
18
|
|
|
* furnished to do so, subject to the following conditions: |
19
|
|
|
* |
20
|
|
|
* The above copyright notice and this permission notice shall be included in all |
21
|
|
|
* copies or substantial portions of the Software. |
22
|
|
|
* |
23
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
24
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
25
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
26
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
27
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
28
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
29
|
|
|
* SOFTWARE. |
30
|
|
|
*/ |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @file SessionAuthentication.php |
34
|
|
|
* |
35
|
|
|
* The Authentication using session feature class |
36
|
|
|
* |
37
|
|
|
* @package Platine\Framework\Auth\Authentication |
38
|
|
|
* @author Platine Developers team |
39
|
|
|
* @copyright Copyright (c) 2020 |
40
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
41
|
|
|
* @link https://www.platine-php.com |
42
|
|
|
* @version 1.0.0 |
43
|
|
|
* @filesource |
44
|
|
|
*/ |
45
|
|
|
|
46
|
|
|
declare(strict_types=1); |
47
|
|
|
|
48
|
|
|
namespace Platine\Framework\Auth\Authentication; |
49
|
|
|
|
50
|
|
|
use Platine\Framework\App\Application; |
51
|
|
|
use Platine\Framework\Auth\AuthenticationInterface; |
52
|
|
|
use Platine\Framework\Auth\Entity\User; |
53
|
|
|
use Platine\Framework\Auth\Enum\UserStatus; |
54
|
|
|
use Platine\Framework\Auth\Event\AuthInvalidPasswordEvent; |
55
|
|
|
use Platine\Framework\Auth\Event\AuthLoginEvent; |
56
|
|
|
use Platine\Framework\Auth\Exception\AccountLockedException; |
57
|
|
|
use Platine\Framework\Auth\Exception\AccountNotFoundException; |
58
|
|
|
use Platine\Framework\Auth\Exception\InvalidCredentialsException; |
59
|
|
|
use Platine\Framework\Auth\Exception\MissingCredentialsException; |
60
|
|
|
use Platine\Framework\Auth\IdentityInterface; |
61
|
|
|
use Platine\Framework\Auth\Repository\UserRepository; |
62
|
|
|
use Platine\Security\Hash\HashInterface; |
63
|
|
|
use Platine\Session\Session; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* class SessionAuthentication |
67
|
|
|
* @package Platine\Framework\Auth\Authentication |
68
|
|
|
*/ |
69
|
|
|
class SessionAuthentication implements AuthenticationInterface |
70
|
|
|
{ |
71
|
|
|
/** |
72
|
|
|
* Create new instance |
73
|
|
|
* @param Application $app |
74
|
|
|
* @param HashInterface $hash |
75
|
|
|
* @param Session $session |
76
|
|
|
* @param UserRepository $userRepository |
77
|
|
|
*/ |
78
|
|
|
public function __construct( |
79
|
|
|
protected Application $app, |
80
|
|
|
protected HashInterface $hash, |
81
|
|
|
protected Session $session, |
82
|
|
|
protected UserRepository $userRepository |
83
|
|
|
) { |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* {@inheritdoc} |
88
|
|
|
*/ |
89
|
|
|
public function getUser(): IdentityInterface |
90
|
|
|
{ |
91
|
|
|
if ($this->isLogged() === false) { |
92
|
|
|
throw new AccountNotFoundException('User not logged', 401); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$id = $this->session->get('user.id'); |
96
|
|
|
$user = $this->userRepository->find($id); |
97
|
|
|
|
98
|
|
|
if ($user === null) { |
99
|
|
|
throw new AccountNotFoundException( |
100
|
|
|
'Can not find the logged user information, may be data is corrupted', |
101
|
|
|
401 |
102
|
|
|
); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return $user; |
|
|
|
|
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* {@inheritdoc} |
110
|
|
|
*/ |
111
|
|
|
public function isLogged(): bool |
112
|
|
|
{ |
113
|
|
|
return $this->session->has('user'); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* {@inheritdoc} |
118
|
|
|
*/ |
119
|
|
|
public function login( |
120
|
|
|
array $credentials = [], |
121
|
|
|
bool $remeberMe = false, |
122
|
|
|
bool $withPassword = true |
123
|
|
|
): bool { |
124
|
|
|
if (!isset($credentials['username'])) { |
125
|
|
|
throw new MissingCredentialsException( |
126
|
|
|
'Missing username information', |
127
|
|
|
401 |
128
|
|
|
); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
if ($withPassword && !isset($credentials['password'])) { |
132
|
|
|
throw new MissingCredentialsException( |
133
|
|
|
'Missing password information', |
134
|
|
|
401 |
135
|
|
|
); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
$username = $credentials['username']; |
139
|
|
|
$password = $credentials['password'] ?? ''; |
140
|
|
|
|
141
|
|
|
$user = $this->getUserEntity($username, $password, $withPassword); |
142
|
|
|
if ($user === null) { |
143
|
|
|
throw new AccountNotFoundException( |
144
|
|
|
sprintf( |
145
|
|
|
'Can not find the user [%s]', |
146
|
|
|
$username |
147
|
|
|
), |
148
|
|
|
401 |
149
|
|
|
); |
150
|
|
|
} elseif ($user->status === UserStatus::LOCKED) { |
|
|
|
|
151
|
|
|
throw new AccountLockedException( |
152
|
|
|
sprintf('User [%s] is locked', $username), |
153
|
|
|
401 |
154
|
|
|
); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
if ($withPassword && $this->hash->verify($password, $user->password) === false) { |
|
|
|
|
158
|
|
|
$this->app->dispatch(new AuthInvalidPasswordEvent($user)); |
159
|
|
|
|
160
|
|
|
throw new InvalidCredentialsException( |
161
|
|
|
sprintf('Invalid credentials for user [%s]', $username), |
162
|
|
|
401 |
163
|
|
|
); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
$permissions = []; |
167
|
|
|
$roles = $user->roles; |
|
|
|
|
168
|
|
|
foreach ($roles as $role) { |
169
|
|
|
$rolePermissions = $role->permissions; |
170
|
|
|
foreach ($rolePermissions as $permission) { |
171
|
|
|
$permissions[] = $permission->code; |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
$data = [ |
176
|
|
|
'id' => $user->id, |
|
|
|
|
177
|
|
|
'username' => $user->username, |
|
|
|
|
178
|
|
|
'lastname' => $user->lastname, |
|
|
|
|
179
|
|
|
'firstname' => $user->firstname, |
|
|
|
|
180
|
|
|
'permissions' => array_unique($permissions), |
181
|
|
|
]; |
182
|
|
|
|
183
|
|
|
$this->session->set('user', array_merge($data, $this->getUserData($user))); |
184
|
|
|
|
185
|
|
|
// Inform the system that the user just login successfully |
186
|
|
|
$this->app->dispatch(new AuthLoginEvent($user)); |
187
|
|
|
|
188
|
|
|
return $this->isLogged(); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* {@inheritdoc} |
193
|
|
|
*/ |
194
|
|
|
public function logout(bool $destroy = true): void |
195
|
|
|
{ |
196
|
|
|
$this->session->remove('user'); |
197
|
|
|
|
198
|
|
|
if ($destroy) { |
199
|
|
|
$params = session_get_cookie_params(); |
200
|
|
|
setcookie( |
201
|
|
|
(string) session_name(), |
202
|
|
|
'', |
203
|
|
|
time() - 42000, |
204
|
|
|
$params['path'], |
205
|
|
|
$params['domain'], |
206
|
|
|
$params['secure'], |
207
|
|
|
$params['httponly'] |
208
|
|
|
); |
209
|
|
|
session_unset(); |
210
|
|
|
session_destroy(); |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Return the user entity |
216
|
|
|
* @param string $username |
217
|
|
|
* @param string $password |
218
|
|
|
* @param bool $withPassword wether to use password to login |
219
|
|
|
* @return User|null |
220
|
|
|
*/ |
221
|
|
|
protected function getUserEntity( |
222
|
|
|
string $username, |
223
|
|
|
string $password, |
|
|
|
|
224
|
|
|
bool $withPassword = true |
|
|
|
|
225
|
|
|
): ?User { |
226
|
|
|
return $this->userRepository->with('roles.permissions') |
|
|
|
|
227
|
|
|
->findBy(['username' => $username]); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Return the user additional data |
232
|
|
|
* @param User $user |
233
|
|
|
* @return array<string, mixed> |
234
|
|
|
*/ |
235
|
|
|
protected function getUserData(User $user): array |
|
|
|
|
236
|
|
|
{ |
237
|
|
|
return []; |
238
|
|
|
} |
239
|
|
|
} |
240
|
|
|
|