1 | <?php |
||
38 | class Google2FA |
||
39 | { |
||
40 | use QRCode, Base32; |
||
41 | |||
42 | /** |
||
43 | * Characters valid for Base 32. |
||
44 | */ |
||
45 | const VALID_FOR_B32 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567'; |
||
46 | |||
47 | /** |
||
48 | * Length of the Token generated. |
||
49 | */ |
||
50 | protected $oneTimePasswordLength = 6; |
||
51 | |||
52 | /** |
||
53 | * Interval between key regeneration. |
||
54 | */ |
||
55 | protected $keyRegeneration = 30; |
||
56 | |||
57 | /** |
||
58 | * Enforce Google Authenticator compatibility. |
||
59 | */ |
||
60 | protected $enforceGoogleAuthenticatorCompatibility = true; |
||
61 | |||
62 | /** |
||
63 | * Secret. |
||
64 | */ |
||
65 | protected $secret; |
||
66 | |||
67 | /** |
||
68 | * Window. |
||
69 | */ |
||
70 | protected $window = 1; // Keys will be valid for 60 seconds |
||
71 | |||
72 | /** |
||
73 | * Generate a digit secret key in base32 format. |
||
74 | * |
||
75 | * @param int $length |
||
76 | * |
||
77 | * @return string |
||
78 | */ |
||
79 | public function generateSecretKey($length = 16, $prefix = '') |
||
83 | |||
84 | /** |
||
85 | * Get key regeneration. |
||
86 | * |
||
87 | * @return mixed |
||
88 | */ |
||
89 | public function getKeyRegeneration() |
||
93 | |||
94 | /** |
||
95 | * Get OTP length. |
||
96 | * |
||
97 | * @return mixed |
||
98 | */ |
||
99 | public function getOneTimePasswordLength() |
||
103 | |||
104 | /** |
||
105 | * Get secret. |
||
106 | * |
||
107 | * @return mixed |
||
108 | */ |
||
109 | public function getSecret($secret = null) |
||
116 | |||
117 | /** |
||
118 | * Returns the current Unix Timestamp divided by the $keyRegeneration |
||
119 | * period. |
||
120 | * |
||
121 | * @return int |
||
122 | **/ |
||
123 | public function getTimestamp() |
||
127 | |||
128 | /** |
||
129 | * Get the OTP window. |
||
130 | * |
||
131 | * @return mixed |
||
132 | */ |
||
133 | public function getWindow($window = null) |
||
140 | |||
141 | /** |
||
142 | * Get/use a starting timestamp for key verification. |
||
143 | * |
||
144 | * @param string|int|null $timestamp |
||
145 | * |
||
146 | * @return int |
||
147 | */ |
||
148 | protected function makeTimestamp($timestamp = null) |
||
156 | |||
157 | /** |
||
158 | * Takes the secret key and the timestamp and returns the one time |
||
159 | * password. |
||
160 | * |
||
161 | * @param string $key - Secret key in binary form. |
||
162 | * @param int $counter - Timestamp as returned by getTimestamp. |
||
163 | * |
||
164 | * @throws SecretKeyTooShortException |
||
165 | * |
||
166 | * @return string |
||
167 | */ |
||
168 | public function oathHotp($key, $counter) |
||
181 | |||
182 | /** |
||
183 | * Get the current one time password for a key. |
||
184 | * |
||
185 | * @param string $initalizationKey |
||
186 | * |
||
187 | * @throws InvalidCharactersException |
||
188 | * @throws SecretKeyTooShortException |
||
189 | * |
||
190 | * @return string |
||
191 | */ |
||
192 | public function getCurrentOtp($initalizationKey) |
||
200 | |||
201 | /** |
||
202 | * Setter for the enforce Google Authenticator compatibility property. |
||
203 | * |
||
204 | * @param mixed $enforceGoogleAuthenticatorCompatibility |
||
205 | * |
||
206 | * @return $this |
||
207 | */ |
||
208 | public function setEnforceGoogleAuthenticatorCompatibility($enforceGoogleAuthenticatorCompatibility) |
||
214 | |||
215 | /** |
||
216 | * Set key regeneration. |
||
217 | * |
||
218 | * @param mixed $keyRegeneration |
||
219 | */ |
||
220 | public function setKeyRegeneration($keyRegeneration) |
||
224 | |||
225 | /** |
||
226 | * Set OTP length. |
||
227 | * |
||
228 | * @param mixed $oneTimePasswordLength |
||
229 | */ |
||
230 | public function setOneTimePasswordLength($oneTimePasswordLength) |
||
234 | |||
235 | /** |
||
236 | * Set secret. |
||
237 | * |
||
238 | * @param mixed $secret |
||
239 | */ |
||
240 | public function setSecret($secret) |
||
244 | |||
245 | /** |
||
246 | * Set the OTP window. |
||
247 | * |
||
248 | * @param mixed $window |
||
249 | */ |
||
250 | public function setWindow($window) |
||
254 | |||
255 | /** |
||
256 | * Verifies a user inputted key against the current timestamp. Checks $window |
||
257 | * keys either side of the timestamp. |
||
258 | * |
||
259 | * @param string $key - User specified key |
||
260 | * @param null|string $secret |
||
261 | * @param null|int $window |
||
262 | * @param null|int $timestamp |
||
263 | * @param null|int $oldTimestamp |
||
264 | * |
||
265 | * @return bool|int |
||
266 | */ |
||
267 | public function verify($key, $secret = null, $window = null, $timestamp = null, $oldTimestamp = null) |
||
277 | |||
278 | /** |
||
279 | * Verifies a user inputted key against the current timestamp. Checks $window |
||
280 | * keys either side of the timestamp. |
||
281 | * |
||
282 | * @param string $secret |
||
283 | * @param string $key - User specified key |
||
284 | * @param null|int $window |
||
285 | * @param null|int $timestamp |
||
286 | * @param null|int $oldTimestamp |
||
287 | * |
||
288 | * @return bool|int |
||
289 | */ |
||
290 | public function verifyKey($secret, $key, $window = null, $timestamp = null, $oldTimestamp = null) |
||
305 | |||
306 | public function findValidOTP($binarySeed, $key, $window, $startingTimestamp, $timestamp, $oldTimestamp) |
||
319 | |||
320 | /** |
||
321 | * Verifies a user inputted key against the current timestamp. Checks $window |
||
322 | * keys either side of the timestamp, but ensures that the given key is newer than |
||
323 | * the given oldTimestamp. Useful if you need to ensure that a single key cannot |
||
324 | * be used twice. |
||
325 | * |
||
326 | * @param string $secret |
||
327 | * @param string $key - User specified key |
||
328 | * @param int $oldTimestamp - The timestamp from the last verified key |
||
329 | * @param int|null $window |
||
330 | * @param int|null $timestamp |
||
331 | * |
||
332 | * @return bool|int - false (not verified) or the timestamp of the verified key |
||
333 | **/ |
||
334 | public function verifyKeyNewer($secret, $key, $oldTimestamp, $window = null, $timestamp = null) |
||
338 | |||
339 | /** |
||
340 | * Extracts the OTP from the SHA1 hash. |
||
341 | * |
||
342 | * @param string $hash |
||
343 | * |
||
344 | * @return int |
||
345 | **/ |
||
346 | public function oathTruncate($hash) |
||
353 | |||
354 | /** |
||
355 | * Remove invalid chars from a base 32 string. |
||
356 | * |
||
357 | * @param $string |
||
358 | * |
||
359 | * @return mixed |
||
360 | */ |
||
361 | public function removeInvalidChars($string) |
||
365 | |||
366 | /** |
||
367 | * Get the key regeneration time in seconds. |
||
368 | * |
||
369 | * @return int |
||
370 | */ |
||
371 | public function getKeyRegenerationTime() |
||
375 | } |
||
376 |