1 | <?php |
||
25 | class Google2FA extends Google2FAService |
||
26 | { |
||
27 | use Auth, Config, Request, Session; |
||
28 | |||
29 | protected $qrCodeBackend; |
||
30 | |||
31 | /** |
||
32 | * Construct the correct backend. |
||
33 | */ |
||
34 | 4 | protected function constructBackend(): void |
|
51 | |||
52 | /** |
||
53 | * Set the QRCode Backend. |
||
54 | * |
||
55 | * @param string $qrCodeBackend |
||
56 | * |
||
57 | * @return self |
||
58 | */ |
||
59 | public function setQrCodeBackend(string $qrCodeBackend) |
||
60 | { |
||
61 | $this->qrCodeBackend = $qrCodeBackend; |
||
62 | |||
63 | return $this; |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * Authenticator constructor. |
||
68 | * |
||
69 | * @param IlluminateRequest $request |
||
70 | */ |
||
71 | 4 | public function __construct(IlluminateRequest $request) |
|
77 | |||
78 | /** |
||
79 | * Authenticator boot. |
||
80 | * |
||
81 | * @param $request |
||
82 | * |
||
83 | * @return Google2FA |
||
84 | */ |
||
85 | 4 | public function boot($request) |
|
91 | |||
92 | /** |
||
93 | * The QRCode Backend. |
||
94 | * |
||
95 | * @return mixed |
||
96 | */ |
||
97 | 4 | public function getQRCodeBackend() |
|
102 | |||
103 | /** |
||
104 | * Get the user Google2FA secret. |
||
105 | * |
||
106 | * @throws InvalidSecretKey |
||
107 | * |
||
108 | * @return mixed |
||
109 | */ |
||
110 | 2 | protected function getGoogle2FASecretKey() |
|
114 | |||
115 | /** |
||
116 | * Check if the 2FA is activated for the user. |
||
117 | * |
||
118 | * @return bool |
||
119 | */ |
||
120 | 2 | public function isActivated() |
|
126 | |||
127 | /** |
||
128 | * Store the old OTP timestamp. |
||
129 | * |
||
130 | * @param $key |
||
131 | * |
||
132 | * @return mixed |
||
133 | */ |
||
134 | protected function storeOldTimestamp($key) |
||
135 | { |
||
136 | return $this->config('forbid_old_passwords') === true |
||
137 | ? $this->sessionPut(Constants::SESSION_OTP_TIMESTAMP, $key) |
||
138 | : $key; |
||
139 | } |
||
140 | |||
141 | /** |
||
142 | * Get the previous OTP timestamp. |
||
143 | * |
||
144 | * @return null|mixed |
||
145 | */ |
||
146 | 1 | protected function getOldTimestamp() |
|
147 | { |
||
148 | 1 | return $this->config('forbid_old_passwords') === true |
|
149 | ? $this->sessionGet(Constants::SESSION_OTP_TIMESTAMP) |
||
150 | 1 | : null; |
|
151 | } |
||
152 | |||
153 | /** |
||
154 | * Keep this OTP session alive. |
||
155 | */ |
||
156 | protected function keepAlive() |
||
157 | { |
||
158 | if ($this->config('keep_alive')) { |
||
159 | $this->updateCurrentAuthTime(); |
||
160 | } |
||
161 | } |
||
162 | |||
163 | /** |
||
164 | * Get minutes since last activity. |
||
165 | * |
||
166 | * @return int |
||
167 | */ |
||
168 | protected function minutesSinceLastActivity() |
||
169 | { |
||
170 | return Carbon::now()->diffInMinutes( |
||
171 | $this->sessionGet(Constants::SESSION_AUTH_TIME) |
||
172 | ); |
||
173 | } |
||
174 | |||
175 | /** |
||
176 | * Check if no user is authenticated using OTP. |
||
177 | * |
||
178 | * @return bool |
||
179 | */ |
||
180 | 1 | protected function noUserIsAuthenticated() |
|
184 | |||
185 | /** |
||
186 | * Check if OTP has expired. |
||
187 | * |
||
188 | * @return bool |
||
189 | */ |
||
190 | protected function passwordExpired() |
||
191 | { |
||
192 | if (($minutes = $this->config('lifetime')) !== 0 && $this->minutesSinceLastActivity() > $minutes) { |
||
193 | event(new OneTimePasswordExpired($this->getUser())); |
||
194 | |||
195 | $this->logout(); |
||
196 | |||
197 | return true; |
||
198 | } |
||
199 | |||
200 | $this->keepAlive(); |
||
201 | |||
202 | return false; |
||
203 | } |
||
204 | |||
205 | /** |
||
206 | * Verifies, in the current session, if a 2fa check has already passed. |
||
207 | * |
||
208 | * @return bool |
||
209 | */ |
||
210 | 1 | protected function twoFactorAuthStillValid() |
|
216 | |||
217 | /** |
||
218 | * Check if the module is enabled. |
||
219 | * |
||
220 | * @return mixed |
||
221 | */ |
||
222 | 1 | protected function isEnabled() |
|
226 | |||
227 | /** |
||
228 | * Set current auth as valid. |
||
229 | */ |
||
230 | public function login() |
||
231 | { |
||
232 | $this->sessionPut(Constants::SESSION_AUTH_PASSED, true); |
||
233 | $this->updateCurrentAuthTime(); |
||
234 | $this->generateCookieToken(); |
||
235 | |||
236 | } |
||
237 | |||
238 | /** |
||
239 | * OTP logout. |
||
240 | */ |
||
241 | public function logout() |
||
242 | { |
||
243 | $user = $this->getUser(); |
||
244 | |||
245 | $this->sessionForget(); |
||
246 | |||
247 | event(new LoggedOut($user)); |
||
248 | } |
||
249 | |||
250 | /** |
||
251 | * Update the current auth time. |
||
252 | */ |
||
253 | protected function updateCurrentAuthTime() |
||
254 | { |
||
255 | $this->sessionPut(Constants::SESSION_AUTH_TIME, Carbon::now()); |
||
256 | } |
||
257 | |||
258 | /** |
||
259 | * Verify the OTP. |
||
260 | * |
||
261 | * @param $secret |
||
262 | * @param $one_time_password |
||
263 | * |
||
264 | * @return mixed |
||
265 | */ |
||
266 | 1 | public function verifyGoogle2FA($secret, $one_time_password) |
|
276 | |||
277 | /** |
||
278 | * Verify the OTP and store the timestamp. |
||
279 | * |
||
280 | * @param $one_time_password |
||
281 | * |
||
282 | * @return mixed |
||
283 | */ |
||
284 | protected function verifyAndStoreOneTimePassword($one_time_password) |
||
285 | { |
||
293 | |||
294 | /** |
||
295 | * Generate token, store in session. |
||
296 | */ |
||
297 | private function generateCookieToken(): void |
||
332 | } |
||
333 |