|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Quantum PHP Framework |
|
5
|
|
|
* |
|
6
|
|
|
* An open source software development framework for PHP |
|
7
|
|
|
* |
|
8
|
|
|
* @package Quantum |
|
9
|
|
|
* @author Arman Ag. <[email protected]> |
|
10
|
|
|
* @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org) |
|
11
|
|
|
* @link http://quantum.softberg.org/ |
|
12
|
|
|
* @since 2.9.7 |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace Quantum\Libraries\Auth\Adapters; |
|
16
|
|
|
|
|
17
|
|
|
use Quantum\Libraries\Auth\Contracts\AuthenticatableInterface; |
|
18
|
|
|
use Quantum\Libraries\Database\Exceptions\DatabaseException; |
|
19
|
|
|
use Quantum\Libraries\Auth\Contracts\AuthServiceInterface; |
|
20
|
|
|
use Quantum\Libraries\Session\Exceptions\SessionException; |
|
21
|
|
|
use Quantum\Libraries\Auth\Exceptions\AuthException; |
|
22
|
|
|
use Quantum\Config\Exceptions\ConfigException; |
|
23
|
|
|
use Quantum\Libraries\Auth\Constants\AuthKeys; |
|
24
|
|
|
use Quantum\Libraries\Auth\Traits\AuthTrait; |
|
25
|
|
|
use Quantum\App\Exceptions\BaseException; |
|
26
|
|
|
use Quantum\Di\Exceptions\DiException; |
|
27
|
|
|
use Quantum\Libraries\Hasher\Hasher; |
|
28
|
|
|
use Quantum\Libraries\Mailer\Mailer; |
|
29
|
|
|
use Quantum\Libraries\Auth\User; |
|
30
|
|
|
use ReflectionException; |
|
31
|
|
|
use Exception; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Class WebAuth |
|
35
|
|
|
* @package Quantum\Libraries\Auth |
|
36
|
|
|
*/ |
|
37
|
|
|
class SessionAuthAdapter implements AuthenticatableInterface |
|
38
|
|
|
{ |
|
39
|
|
|
|
|
40
|
|
|
use AuthTrait; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param AuthServiceInterface $authService |
|
44
|
|
|
* @param Mailer $mailer |
|
45
|
|
|
* @param Hasher $hasher |
|
46
|
|
|
* @throws AuthException |
|
47
|
|
|
*/ |
|
48
|
|
|
public function __construct(AuthServiceInterface $authService, Mailer $mailer, Hasher $hasher) |
|
49
|
|
|
{ |
|
50
|
|
|
$this->authService = $authService; |
|
51
|
|
|
$this->mailer = $mailer; |
|
|
|
|
|
|
52
|
|
|
$this->hasher = $hasher; |
|
53
|
|
|
|
|
54
|
|
|
$this->verifySchema($this->authService->userSchema()); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @inheritDoc |
|
59
|
|
|
* @throws AuthException |
|
60
|
|
|
* @throws ConfigException |
|
61
|
|
|
* @throws DatabaseException |
|
62
|
|
|
* @throws DiException |
|
63
|
|
|
* @throws ReflectionException |
|
64
|
|
|
* @throws SessionException |
|
65
|
|
|
* @throws Exception |
|
66
|
|
|
*/ |
|
67
|
|
|
public function signin(string $username, string $password, bool $remember = false) |
|
68
|
|
|
{ |
|
69
|
|
|
$user = $this->getUser($username, $password); |
|
70
|
|
|
|
|
71
|
|
|
if ($remember) { |
|
72
|
|
|
$this->setRememberToken($user); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
if ($this->isTwoFactorEnabled()) { |
|
76
|
|
|
return $this->twoStepVerification($user); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
session()->regenerateId(); |
|
80
|
|
|
session()->set(self::AUTH_USER, $this->getVisibleFields($user)); |
|
81
|
|
|
|
|
82
|
|
|
return true; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @inheritDoc |
|
87
|
|
|
* @throws ConfigException |
|
88
|
|
|
* @throws DiException |
|
89
|
|
|
* @throws ReflectionException |
|
90
|
|
|
* @throws BaseException |
|
91
|
|
|
*/ |
|
92
|
|
|
public function signout(): bool |
|
93
|
|
|
{ |
|
94
|
|
|
if (session()->has(self::AUTH_USER)) { |
|
95
|
|
|
session()->delete(self::AUTH_USER); |
|
96
|
|
|
session()->regenerateId(); |
|
97
|
|
|
$this->removeRememberToken(); |
|
98
|
|
|
|
|
99
|
|
|
return true; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
return false; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @inheritDoc |
|
107
|
|
|
* @throws BaseException |
|
108
|
|
|
* @throws ConfigException |
|
109
|
|
|
* @throws DiException |
|
110
|
|
|
* @throws ReflectionException |
|
111
|
|
|
*/ |
|
112
|
|
|
public function user(): ?User |
|
113
|
|
|
{ |
|
114
|
|
|
if (session()->has(self::AUTH_USER) && is_array(session()->get(self::AUTH_USER))) { |
|
115
|
|
|
return (new User())->setData(session()->get(self::AUTH_USER)); |
|
|
|
|
|
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
if (cookie()->has($this->keyFields[AuthKeys::REMEMBER_TOKEN])) { |
|
119
|
|
|
$user = $this->checkRememberToken(); |
|
120
|
|
|
|
|
121
|
|
|
if ($user) { |
|
122
|
|
|
session()->set(self::AUTH_USER, $this->getVisibleFields($user)); |
|
123
|
|
|
return $this->user(); |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
return null; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Refresh user data |
|
132
|
|
|
* @param string $uuid |
|
133
|
|
|
* @return bool |
|
134
|
|
|
* @throws BaseException |
|
135
|
|
|
* @throws ConfigException |
|
136
|
|
|
* @throws DiException |
|
137
|
|
|
* @throws ReflectionException |
|
138
|
|
|
*/ |
|
139
|
|
|
public function refreshUser(string $uuid): bool |
|
140
|
|
|
{ |
|
141
|
|
|
$user = $this->authService->get('uuid', $uuid); |
|
142
|
|
|
|
|
143
|
|
|
if (!$user) { |
|
144
|
|
|
return false; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
$sessionData = session()->get(self::AUTH_USER); |
|
148
|
|
|
$sessionData = array_merge($sessionData, $this->getVisibleFields($user)); |
|
|
|
|
|
|
149
|
|
|
|
|
150
|
|
|
session()->set(self::AUTH_USER, $sessionData); |
|
151
|
|
|
|
|
152
|
|
|
return true; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Verify OTP |
|
157
|
|
|
* @param int $otp |
|
158
|
|
|
* @param string $otpToken |
|
159
|
|
|
* @return bool |
|
160
|
|
|
* @throws AuthException |
|
161
|
|
|
* @throws BaseException |
|
162
|
|
|
* @throws ConfigException |
|
163
|
|
|
* @throws DiException |
|
164
|
|
|
* @throws ReflectionException |
|
165
|
|
|
*/ |
|
166
|
|
|
public function verifyOtp(int $otp, string $otpToken): bool |
|
167
|
|
|
{ |
|
168
|
|
|
$user = $this->verifyAndUpdateOtp($otp, $otpToken); |
|
169
|
|
|
|
|
170
|
|
|
session()->set(self::AUTH_USER, $this->getVisibleFields($user)); |
|
171
|
|
|
|
|
172
|
|
|
return true; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Check Remember Token |
|
177
|
|
|
* @return User|false |
|
178
|
|
|
*/ |
|
179
|
|
|
private function checkRememberToken() |
|
180
|
|
|
{ |
|
181
|
|
|
$user = $this->authService->get( |
|
182
|
|
|
$this->keyFields[AuthKeys::REMEMBER_TOKEN], |
|
183
|
|
|
cookie()->get($this->keyFields[AuthKeys::REMEMBER_TOKEN]) |
|
184
|
|
|
); |
|
185
|
|
|
|
|
186
|
|
|
if (!$user) { |
|
187
|
|
|
return false; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
if ($this->isTwoFactorEnabled() && !empty($user->getFieldValue($this->keyFields[AuthKeys::OTP_TOKEN]))) { |
|
191
|
|
|
return false; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
return $user; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* Set Remember Token |
|
199
|
|
|
* @param User $user |
|
200
|
|
|
*/ |
|
201
|
|
|
private function setRememberToken(User $user) |
|
202
|
|
|
{ |
|
203
|
|
|
$rememberToken = $this->generateToken(); |
|
204
|
|
|
|
|
205
|
|
|
$this->authService->update( |
|
206
|
|
|
$this->keyFields[AuthKeys::USERNAME], |
|
207
|
|
|
$user->getFieldValue($this->keyFields[AuthKeys::USERNAME]), |
|
208
|
|
|
[$this->keyFields[AuthKeys::REMEMBER_TOKEN] => $rememberToken] |
|
209
|
|
|
); |
|
210
|
|
|
|
|
211
|
|
|
cookie()->set($this->keyFields[AuthKeys::REMEMBER_TOKEN], $rememberToken); |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* Remove Remember token |
|
216
|
|
|
*/ |
|
217
|
|
|
private function removeRememberToken() |
|
218
|
|
|
{ |
|
219
|
|
|
if (cookie()->has($this->keyFields[AuthKeys::REMEMBER_TOKEN])) { |
|
220
|
|
|
$user = $this->authService->get( |
|
221
|
|
|
$this->keyFields[AuthKeys::REMEMBER_TOKEN], |
|
222
|
|
|
cookie()->get($this->keyFields[AuthKeys::REMEMBER_TOKEN]) |
|
223
|
|
|
); |
|
224
|
|
|
|
|
225
|
|
|
if ($user) { |
|
226
|
|
|
$this->authService->update( |
|
227
|
|
|
$this->keyFields[AuthKeys::REMEMBER_TOKEN], |
|
228
|
|
|
$user->getFieldValue($this->keyFields[AuthKeys::REMEMBER_TOKEN]), |
|
229
|
|
|
[$this->keyFields[AuthKeys::REMEMBER_TOKEN] => ''] |
|
230
|
|
|
); |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
cookie()->delete($this->keyFields[AuthKeys::REMEMBER_TOKEN]); |
|
234
|
|
|
} |
|
235
|
|
|
} |
|
236
|
|
|
} |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..