1 | <?php |
||
14 | class OTPHelper |
||
15 | { |
||
16 | /** @var array Allowed types of OTP */ |
||
17 | protected $allowedType = array( |
||
18 | 'hotp', |
||
19 | 'totp', |
||
20 | ); |
||
21 | |||
22 | /** @var array Allowed algorithms */ |
||
23 | protected $allowedAlgorithm = array( |
||
24 | 'sha1', |
||
25 | 'sha256', |
||
26 | 'sha512', |
||
27 | ); |
||
28 | |||
29 | /** @var string Label string for URI */ |
||
30 | protected $label; |
||
31 | |||
32 | /** @var string Issuer string for URI */ |
||
33 | protected $issuer; |
||
34 | |||
35 | /** @var string Additional parameters for URI */ |
||
36 | protected $parameters = ''; |
||
37 | |||
38 | /** |
||
39 | * Generate OTP key URI |
||
40 | * |
||
41 | * @param string $type OTP type |
||
42 | * @param string $secret Base32 encoded secret |
||
43 | * @param string $account Account name |
||
44 | * @param string $issuer Issuer name (optional) |
||
45 | * @param int $counter Counter for HOTP (optional) |
||
46 | * @param string $algorithm Algorithm name (optional) |
||
47 | * @param string $digits Number of digits for code (optional) |
||
48 | * @param string $period Period for TOTP codes (optional) |
||
49 | * |
||
50 | * @return string OTP key URI |
||
51 | */ |
||
52 | 12 | public function generateKeyURI($type, $secret, $account, $issuer = '', $counter = 0, $algorithm = '', $digits = '', $period = '') |
|
70 | |||
71 | /** |
||
72 | * Check if OTP type is supported |
||
73 | * |
||
74 | * @param string $type OTP type |
||
75 | * |
||
76 | * @throws \InvalidArgumentException When type is not supported |
||
77 | */ |
||
78 | 12 | protected function validateType($type) |
|
85 | |||
86 | /** |
||
87 | * Check if algorithm is supported |
||
88 | * |
||
89 | * @param string $algorithm Algorithm to use |
||
90 | * |
||
91 | * @throws \InvalidArgumentException When algorithm is not supported |
||
92 | */ |
||
93 | 11 | protected function validateAlgorithm($algorithm) |
|
100 | |||
101 | /** |
||
102 | * Format label string according to expected urlencoded standards. |
||
103 | * |
||
104 | * @param string $string The label string |
||
105 | * @param string $part Part of label |
||
106 | */ |
||
107 | 10 | protected function formatLabel($string, $part) |
|
120 | |||
121 | /** |
||
122 | * Format and and set account name |
||
123 | * |
||
124 | * @param string $account Account name |
||
125 | * |
||
126 | * @throws \InvalidArgumentException When given account name is an empty string |
||
127 | */ |
||
128 | 10 | protected function setAccount($account) |
|
137 | |||
138 | /** |
||
139 | * Format and set issuer |
||
140 | * |
||
141 | * @param string $issuer Issuer name |
||
142 | */ |
||
143 | 10 | protected function setIssuer($issuer) |
|
151 | |||
152 | /** |
||
153 | * Set parameter if it is defined |
||
154 | * |
||
155 | * @param string $data Data to set |
||
156 | * @param string $name Name of data |
||
157 | */ |
||
158 | 7 | protected function setParameter($data, $name) |
|
165 | |||
166 | /** |
||
167 | * Set counter value if hotp is being used |
||
168 | * |
||
169 | * @param string $type Type of OTP auth, either HOTP or TOTP |
||
170 | * @param int $counter Counter value |
||
171 | * |
||
172 | * @throws \InvalidArgumentException If counter is empty while using HOTP |
||
173 | */ |
||
174 | 8 | protected function setCounter($type, $counter) |
|
186 | } |
||
187 |