1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Jitamin. |
5
|
|
|
* |
6
|
|
|
* Copyright (C) Jitamin Team |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Jitamin\Auth; |
13
|
|
|
|
14
|
|
|
use Base32\Base32; |
15
|
|
|
use Jitamin\Foundation\Base; |
16
|
|
|
use Jitamin\Foundation\Security\PostAuthenticationProviderInterface; |
17
|
|
|
use Otp\GoogleAuthenticator; |
18
|
|
|
use Otp\Otp; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* TOTP Authentication Provider. |
22
|
|
|
*/ |
23
|
|
|
class TotpAuth extends Base implements PostAuthenticationProviderInterface |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* User pin code. |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $code = ''; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Private key. |
34
|
|
|
* |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
protected $secret = ''; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Get authentication provider name. |
41
|
|
|
* |
42
|
|
|
* @return string |
43
|
|
|
*/ |
44
|
|
|
public function getName() |
45
|
|
|
{ |
46
|
|
|
return t('Time-based One-time Password Algorithm'); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Authenticate the user. |
51
|
|
|
* |
52
|
|
|
* @return bool |
53
|
|
|
*/ |
54
|
|
|
public function authenticate() |
55
|
|
|
{ |
56
|
|
|
$otp = new Otp(); |
57
|
|
|
|
58
|
|
|
return $otp->checkTotp(Base32::decode($this->secret), $this->code); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Called before to prompt the user. |
63
|
|
|
*/ |
64
|
|
|
public function beforeCode() |
65
|
|
|
{ |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Set validation code. |
70
|
|
|
* |
71
|
|
|
* @param string $code |
72
|
|
|
*/ |
73
|
|
|
public function setCode($code) |
74
|
|
|
{ |
75
|
|
|
$this->code = $code; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Generate secret. |
80
|
|
|
* |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
|
|
public function generateSecret() |
84
|
|
|
{ |
85
|
|
|
$this->secret = GoogleAuthenticator::generateRandom(); |
86
|
|
|
|
87
|
|
|
return $this->secret; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Set secret token. |
92
|
|
|
* |
93
|
|
|
* @param string $secret |
94
|
|
|
*/ |
95
|
|
|
public function setSecret($secret) |
96
|
|
|
{ |
97
|
|
|
$this->secret = $secret; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Get secret token. |
102
|
|
|
* |
103
|
|
|
* @return string |
104
|
|
|
*/ |
105
|
|
|
public function getSecret() |
106
|
|
|
{ |
107
|
|
|
return $this->secret; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Get QR code url. |
112
|
|
|
* |
113
|
|
|
* @param string $label |
114
|
|
|
* |
115
|
|
|
* @return string |
116
|
|
|
*/ |
117
|
|
View Code Duplication |
public function getQrCodeUrl($label) |
|
|
|
|
118
|
|
|
{ |
119
|
|
|
if (empty($this->secret)) { |
120
|
|
|
return ''; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
$options = ['issuer' => TOTP_ISSUER]; |
124
|
|
|
|
125
|
|
|
return GoogleAuthenticator::getQrCodeUrl('totp', $label, $this->secret, null, $options); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Get key url (empty if no url can be provided). |
130
|
|
|
* |
131
|
|
|
* @param string $label |
132
|
|
|
* |
133
|
|
|
* @return string |
134
|
|
|
*/ |
135
|
|
View Code Duplication |
public function getKeyUrl($label) |
|
|
|
|
136
|
|
|
{ |
137
|
|
|
if (empty($this->secret)) { |
138
|
|
|
return ''; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
$options = ['issuer' => TOTP_ISSUER]; |
142
|
|
|
|
143
|
|
|
return GoogleAuthenticator::getKeyUri('totp', $label, $this->secret, null, $options); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.