|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Ecodev\Felix\Model\Traits; |
|
6
|
|
|
|
|
7
|
|
|
use Doctrine\ORM\Mapping as ORM; |
|
8
|
|
|
use Exception; |
|
9
|
|
|
use GraphQL\Doctrine\Annotation as API; |
|
10
|
|
|
use OTPHP; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Trait for a user with second factor authentication using OTP. |
|
14
|
|
|
*/ |
|
15
|
|
|
trait HasOtp |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @ORM\Column(type="boolean", options={"default" = 0}) |
|
19
|
|
|
*/ |
|
20
|
|
|
private bool $otp = false; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @API\Exclude |
|
24
|
|
|
* |
|
25
|
|
|
* @ORM\Column(type="string", length=255, nullable=true) |
|
26
|
|
|
*/ |
|
27
|
|
|
private ?string $otpUri = null; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Must be implemented by the user class to use the login as the key label. |
|
31
|
|
|
*/ |
|
32
|
|
|
abstract public function getLogin(): ?string; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Enable 2FA for the user |
|
36
|
|
|
* This should be only enabled after otpUri has been generated, stored and verified. |
|
37
|
|
|
* |
|
38
|
|
|
* @API\Exclude |
|
39
|
|
|
*/ |
|
40
|
3 |
|
public function setOtp(bool $otp): void |
|
41
|
|
|
{ |
|
42
|
3 |
|
if ($otp && empty($this->otpUri)) { |
|
43
|
1 |
|
throw new Exception('Cannot enable OTP without a secret'); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
2 |
|
$this->otp = $otp; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Whether the user has 2FA enabled. |
|
51
|
|
|
*/ |
|
52
|
2 |
|
public function isOtp(): bool |
|
53
|
|
|
{ |
|
54
|
2 |
|
return $this->otp; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Returns the OTP provisionning URI (to display QR code). |
|
59
|
|
|
* |
|
60
|
|
|
* @API\Exclude |
|
61
|
|
|
*/ |
|
62
|
3 |
|
public function getOtpUri(): ?string |
|
63
|
|
|
{ |
|
64
|
3 |
|
return $this->otpUri; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Generate and store a new OTP secret. |
|
69
|
|
|
* |
|
70
|
|
|
* @param string $issuer identify the service that provided the OTP (application or host name) |
|
71
|
|
|
* |
|
72
|
|
|
* @API\Exclude |
|
73
|
|
|
*/ |
|
74
|
4 |
|
public function createOtpSecret(string $issuer): void |
|
75
|
|
|
{ |
|
76
|
4 |
|
$this->revokeOtpSecret(); |
|
77
|
|
|
|
|
78
|
4 |
|
$totp = OTPHP\TOTP::create(null); |
|
79
|
4 |
|
$label = $this->getLogin(); |
|
80
|
4 |
|
if (!$label) { |
|
81
|
1 |
|
throw new Exception('User must have a login to initialize OTP'); |
|
82
|
|
|
} |
|
83
|
3 |
|
$totp->setLabel($label); |
|
84
|
3 |
|
$totp->setIssuer($issuer); |
|
85
|
3 |
|
$this->otpUri = $totp->getProvisioningUri(); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Revoke the existing OTP secret |
|
90
|
|
|
* This will also disable 2FA. |
|
91
|
|
|
* |
|
92
|
|
|
* @API\Exclude |
|
93
|
|
|
*/ |
|
94
|
4 |
|
public function revokeOtpSecret(): void |
|
95
|
|
|
{ |
|
96
|
4 |
|
$this->otp = false; |
|
97
|
4 |
|
$this->otpUri = null; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Verify an OTP received from the user. |
|
102
|
|
|
* |
|
103
|
|
|
* @API\Exclude |
|
104
|
|
|
*/ |
|
105
|
1 |
|
public function verifyOtp(string $received): bool |
|
106
|
|
|
{ |
|
107
|
1 |
|
if (empty($this->otpUri)) { |
|
108
|
1 |
|
return false; |
|
109
|
|
|
} |
|
110
|
1 |
|
$otp = OTPHP\Factory::loadFromProvisioningUri($this->otpUri); |
|
111
|
|
|
|
|
112
|
1 |
|
return $otp->verify($received); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|