1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Cortex\Auth\Traits; |
6
|
|
|
|
7
|
|
|
use PragmaRX\Google2FA\Google2FA; |
8
|
|
|
use Rinvex\Auth\Contracts\AuthenticatableTwoFactorContract; |
9
|
|
|
|
10
|
|
|
trait TwoFactorAuthenticatesUsers |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Verify TwoFactor authentication. |
14
|
|
|
* |
15
|
|
|
* @param \Rinvex\Auth\Contracts\AuthenticatableTwoFactorContract $user |
16
|
|
|
* @param int $token |
17
|
|
|
* |
18
|
|
|
* @return bool |
19
|
|
|
*/ |
20
|
|
|
protected function attemptTwoFactor(AuthenticatableTwoFactorContract $user, int $token): bool |
21
|
|
|
{ |
22
|
|
|
return $this->isValidTwoFactorTotp($user, $token) || $this->isValidTwoFactorBackup($user, $token) || $this->isValidTwoFactorPhone($user, $token); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Invalidate given backup code for the given user. |
27
|
|
|
* |
28
|
|
|
* @param \Rinvex\Auth\Contracts\AuthenticatableTwoFactorContract $user |
29
|
|
|
* @param int $token |
30
|
|
|
* |
31
|
|
|
* @return void |
32
|
|
|
*/ |
33
|
|
|
protected function invalidateTwoFactorBackup(AuthenticatableTwoFactorContract $user, int $token): void |
34
|
|
|
{ |
35
|
|
|
$settings = $user->getTwoFactor(); |
36
|
|
|
$backup = array_get($settings, 'totp.backup'); |
|
|
|
|
37
|
|
|
|
38
|
|
|
unset($backup[array_search($token, $backup)]); |
39
|
|
|
|
40
|
|
|
array_set($settings, 'totp.backup', $backup); |
|
|
|
|
41
|
|
|
|
42
|
|
|
// Update TwoFactor OTP backup codes |
43
|
|
|
$user->fill(['two_factor' => $settings])->forceSave(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Determine if the given token is a valid TwoFactor Phone token. |
48
|
|
|
* |
49
|
|
|
* @param \Rinvex\Auth\Contracts\AuthenticatableTwoFactorContract $user |
50
|
|
|
* @param int $token |
51
|
|
|
* |
52
|
|
|
* @return bool |
53
|
|
|
*/ |
54
|
|
|
protected function isValidTwoFactorPhone(AuthenticatableTwoFactorContract $user, int $token): bool |
55
|
|
|
{ |
56
|
|
|
$settings = $user->getTwoFactor(); |
57
|
|
|
$authyId = array_get($settings, 'phone.authy_id'); |
|
|
|
|
58
|
|
|
|
59
|
|
|
return in_array(mb_strlen($token), [6, 7, 8]) && app('rinvex.authy.token')->verify($token, $authyId)->succeed(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Determine if the given token is a valid TwoFactor Backup code. |
64
|
|
|
* |
65
|
|
|
* @param \Rinvex\Auth\Contracts\AuthenticatableTwoFactorContract $user |
66
|
|
|
* @param int $token |
67
|
|
|
* |
68
|
|
|
* @return bool |
69
|
|
|
*/ |
70
|
|
|
protected function isValidTwoFactorBackup(AuthenticatableTwoFactorContract $user, int $token): bool |
71
|
|
|
{ |
72
|
|
|
$backup = array_get($user->getTwoFactor(), 'totp.backup', []); |
|
|
|
|
73
|
|
|
$result = mb_strlen($token) === 10 && in_array($token, $backup); |
74
|
|
|
! $result || $this->invalidateTwoFactorBackup($user, $token); |
75
|
|
|
|
76
|
|
|
return $result; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Determine if the given token is a valid TwoFactor TOTP token. |
81
|
|
|
* |
82
|
|
|
* @param \Rinvex\Auth\Contracts\AuthenticatableTwoFactorContract $user |
83
|
|
|
* @param int $token |
84
|
|
|
* |
85
|
|
|
* @return bool |
86
|
|
|
*/ |
87
|
|
|
protected function isValidTwoFactorTotp(AuthenticatableTwoFactorContract $user, int $token): bool |
88
|
|
|
{ |
89
|
|
|
$totpProvider = app(Google2FA::class); |
90
|
|
|
$secret = array_get($user->getTwoFactor(), 'totp.secret'); |
|
|
|
|
91
|
|
|
|
92
|
|
|
return mb_strlen($token) === 6 && $totpProvider->verifyKey($secret, $token); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.