|
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.0.0 |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace Quantum\Libraries\Auth; |
|
16
|
|
|
|
|
17
|
|
|
use Quantum\Libraries\Hasher\Hasher; |
|
18
|
|
|
use Quantum\Libraries\Mailer\Mailer; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Trait AuthTools |
|
22
|
|
|
* @package Quantum\Libraries\Auth |
|
23
|
|
|
*/ |
|
24
|
|
|
abstract class BaseAuth |
|
25
|
|
|
{ |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* User |
|
29
|
|
|
* @return mixed|null |
|
30
|
|
|
*/ |
|
31
|
|
|
protected abstract function user(); |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Check |
|
35
|
|
|
* @return bool |
|
36
|
|
|
*/ |
|
37
|
|
|
public function check() |
|
38
|
|
|
{ |
|
39
|
|
|
return !is_null($this->user()); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Check Verification |
|
44
|
|
|
* @return bool |
|
45
|
|
|
*/ |
|
46
|
|
|
public function checkVerification() |
|
47
|
|
|
{ |
|
48
|
|
|
if (isset($this->user()->verification_code) && !empty($this->user()->verification_code)){ |
|
49
|
|
|
return true; |
|
50
|
|
|
} |
|
51
|
|
|
return false; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Sign Up |
|
56
|
|
|
* @param array $userData |
|
57
|
|
|
* @param array|null $customData |
|
58
|
|
|
* @return mixed |
|
59
|
|
|
*/ |
|
60
|
|
|
public function signup(Mailer $mailer, $userData, $customData = null) |
|
61
|
|
|
{ |
|
62
|
|
|
$activationToken = $this->generateToken(); |
|
63
|
|
|
|
|
64
|
|
|
$userData[$this->keys['passwordKey']] = $this->hasher->hash($userData[$this->keys['passwordKey']]); |
|
65
|
|
|
$userData[$this->keys['activationTokenKey']] = $activationToken; |
|
66
|
|
|
|
|
67
|
|
|
$user = $this->authService->add($userData); |
|
68
|
|
|
|
|
69
|
|
|
$body = [ |
|
70
|
|
|
'user' => $user, |
|
71
|
|
|
'activationToken' => $activationToken |
|
72
|
|
|
]; |
|
73
|
|
|
|
|
74
|
|
|
if ($customData) { |
|
75
|
|
|
$body = array_merge($body, $customData); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
$this->sendMail($mailer, $user, $body); |
|
79
|
|
|
|
|
80
|
|
|
return $user; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Activate |
|
85
|
|
|
* @param string $token |
|
86
|
|
|
*/ |
|
87
|
|
|
public function activate($token) |
|
88
|
|
|
{ |
|
89
|
|
|
$this->authService->update( |
|
90
|
|
|
$this->keys['activationTokenKey'], |
|
91
|
|
|
$token, [$this->keys['activationTokenKey'] => ''] |
|
92
|
|
|
); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Forget |
|
97
|
|
|
* @param Mailer $mailer |
|
98
|
|
|
* @param string $email |
|
99
|
|
|
* @param string $template |
|
100
|
|
|
* @return string |
|
101
|
|
|
*/ |
|
102
|
|
|
public function forget(Mailer $mailer, $email) |
|
103
|
|
|
{ |
|
104
|
|
|
$user = $this->authService->get($this->keys['usernameKey'], $email); |
|
105
|
|
|
|
|
106
|
|
|
$resetToken = $this->generateToken(); |
|
107
|
|
|
|
|
108
|
|
|
$this->authService->update( |
|
109
|
|
|
$this->keys['usernameKey'], |
|
110
|
|
|
$email, |
|
111
|
|
|
[$this->keys['resetTokenKey'] => $resetToken] |
|
112
|
|
|
); |
|
113
|
|
|
|
|
114
|
|
|
$body = [ |
|
115
|
|
|
'user' => $user, |
|
116
|
|
|
'resetToken' => $resetToken |
|
117
|
|
|
]; |
|
118
|
|
|
|
|
119
|
|
|
$this->sendMail($mailer, $user, $body); |
|
120
|
|
|
|
|
121
|
|
|
return $resetToken; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Reset |
|
126
|
|
|
* @param string $token |
|
127
|
|
|
* @param string $password |
|
128
|
|
|
*/ |
|
129
|
|
|
public function reset($token, $password) |
|
130
|
|
|
{ |
|
131
|
|
|
$user = $this->authService->get($this->keys['resetTokenKey'], $token); |
|
132
|
|
|
|
|
133
|
|
|
if (!$this->isActivated($user)) { |
|
134
|
|
|
$this->activate($token); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
$this->authService->update( |
|
138
|
|
|
$this->keys['resetTokenKey'], |
|
139
|
|
|
$token, |
|
140
|
|
|
[$this->keys['passwordKey'] => $this->hasher->hash($password), $this->keys['resetTokenKey'] => ''] |
|
141
|
|
|
); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Filter Fields |
|
146
|
|
|
* @param array $user |
|
147
|
|
|
* @return mixed |
|
148
|
|
|
*/ |
|
149
|
|
|
protected function filterFields(array $user) |
|
150
|
|
|
{ |
|
151
|
|
|
if (count($this->authService->getVisibleFields())) { |
|
152
|
|
|
foreach ($user as $key => $value) { |
|
153
|
|
|
if (!in_array($key, $this->authService->getVisibleFields())) { |
|
154
|
|
|
unset($user[$key]); |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
return $user; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Generate Token |
|
164
|
|
|
* @return string |
|
165
|
|
|
*/ |
|
166
|
|
|
protected function generateToken() |
|
167
|
|
|
{ |
|
168
|
|
|
return base64_encode($this->hasher->hash(env('APP_KEY'))); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* Generate Otp Token |
|
174
|
|
|
* @param string $username |
|
175
|
|
|
* @return string |
|
176
|
|
|
*/ |
|
177
|
|
|
|
|
178
|
|
|
protected function generateOtpToken($username) |
|
179
|
|
|
{ |
|
180
|
|
|
$hasher = new Hasher(); |
|
181
|
|
|
return base64_encode($hasher->hash($username)); |
|
182
|
|
|
} |
|
183
|
|
|
/** |
|
184
|
|
|
* Is user account activated |
|
185
|
|
|
* @param mixed $user |
|
186
|
|
|
* @return bool |
|
187
|
|
|
*/ |
|
188
|
|
|
protected function isActivated($user) |
|
189
|
|
|
{ |
|
190
|
|
|
return empty($user[$this->keys['activationTokenKey']]) ? true : false; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* Send email |
|
195
|
|
|
* @param Mailer $mailer |
|
196
|
|
|
* @param array $user |
|
197
|
|
|
* @param array $body |
|
198
|
|
|
*/ |
|
199
|
|
|
protected function sendMail(Mailer $mailer, array $user, array $body) |
|
200
|
|
|
{ |
|
201
|
|
|
$fullName = (isset($user['firstname']) && isset($user['lastname'])) ? $user['firstname'] . ' ' . $user['lastname'] : ''; |
|
202
|
|
|
|
|
203
|
|
|
$mailer->setFrom(config()->get('app_email'), config()->get('app_name')) |
|
204
|
|
|
->setAddress($user[$this->keys['usernameKey']], $fullName) |
|
205
|
|
|
->setBody($body) |
|
206
|
|
|
->send(); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* Tow Step Verification |
|
211
|
|
|
* @param array $user |
|
212
|
|
|
* @param Mailer $mailer |
|
213
|
|
|
* @param string $otp_expiry_in |
|
214
|
|
|
* @param string $otp_token |
|
215
|
|
|
* @return array $user |
|
216
|
|
|
*/ |
|
217
|
|
|
|
|
218
|
|
|
protected function towStepVerification($mailer, $user, $otp_expiry_in, $otp_token) |
|
219
|
|
|
{ |
|
220
|
|
|
$body = [ |
|
221
|
|
|
'user' => $user, |
|
222
|
|
|
'code' => random_number(6) |
|
223
|
|
|
]; |
|
224
|
|
|
|
|
225
|
|
|
$this->authService->update($this->keys['usernameKey'], $user[$this->keys['usernameKey']], [ |
|
226
|
|
|
$this->keys['otpKey'] => $body['code'], |
|
227
|
|
|
$this->keys['otpExpiryIn'] => $otp_expiry_in, |
|
228
|
|
|
$this->keys['otpToken'] => $otp_token, |
|
229
|
|
|
]); |
|
230
|
|
|
|
|
231
|
|
|
|
|
232
|
|
|
$this->sendMail($mailer, $user, $body); |
|
233
|
|
|
|
|
234
|
|
|
return $user; |
|
235
|
|
|
} |
|
236
|
|
|
} |
|
237
|
|
|
|