Google2FA.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. <?php
  2. namespace PragmaRX\Google2FA;
  3. use PragmaRX\Google2FA\Exceptions\SecretKeyTooShortException;
  4. use PragmaRX\Google2FA\Support\Base32;
  5. use PragmaRX\Google2FA\Support\Constants;
  6. use PragmaRX\Google2FA\Support\QRCode;
  7. class Google2FA
  8. {
  9. use QRCode, Base32;
  10. /**
  11. * Length of the Token generated.
  12. */
  13. protected $oneTimePasswordLength = 6;
  14. /**
  15. * Interval between key regeneration.
  16. */
  17. protected $keyRegeneration = 30;
  18. /**
  19. * Secret.
  20. */
  21. protected $secret;
  22. /**
  23. * Window.
  24. */
  25. protected $window = 1; // Keys will be valid for 60 seconds
  26. /**
  27. * Find a valid One Time Password.
  28. *
  29. * @param $secret
  30. * @param $key
  31. * @param $window
  32. * @param $startingTimestamp
  33. * @param $timestamp
  34. * @param string $oldTimestamp
  35. *
  36. * @return bool
  37. */
  38. public function findValidOTP($secret, $key, $window, $startingTimestamp, $timestamp, $oldTimestamp = Constants::ARGUMENT_NOT_SET)
  39. {
  40. for (; $startingTimestamp <= $timestamp + $this->getWindow($window); $startingTimestamp++) {
  41. if (hash_equals($this->oathHotp($secret, $startingTimestamp), $key)) {
  42. return
  43. $oldTimestamp === Constants::ARGUMENT_NOT_SET
  44. ? true
  45. : $startingTimestamp;
  46. }
  47. }
  48. return false;
  49. }
  50. /**
  51. * Generate a digit secret key in base32 format.
  52. *
  53. * @param int $length
  54. * @param string $prefix
  55. *
  56. * @return string
  57. */
  58. public function generateSecretKey($length = 16, $prefix = '')
  59. {
  60. return $this->generateBase32RandomKey($length, $prefix);
  61. }
  62. /**
  63. * Get the current one time password for a key.
  64. *
  65. * @param $secret
  66. *
  67. * @return string
  68. */
  69. public function getCurrentOtp($secret)
  70. {
  71. return $this->oathHotp($secret, $this->getTimestamp());
  72. }
  73. /**
  74. * Get key regeneration.
  75. *
  76. * @return mixed
  77. */
  78. public function getKeyRegeneration()
  79. {
  80. return $this->keyRegeneration;
  81. }
  82. /**
  83. * Get OTP length.
  84. *
  85. * @return mixed
  86. */
  87. public function getOneTimePasswordLength()
  88. {
  89. return $this->oneTimePasswordLength;
  90. }
  91. /**
  92. * Get secret.
  93. *
  94. * @param string|null $secret
  95. *
  96. * @return mixed
  97. */
  98. public function getSecret($secret = null)
  99. {
  100. return
  101. is_null($secret)
  102. ? $this->secret
  103. : $secret;
  104. }
  105. /**
  106. * Returns the current Unix Timestamp divided by the $keyRegeneration
  107. * period.
  108. *
  109. * @return int
  110. **/
  111. public function getTimestamp()
  112. {
  113. return (int) floor(microtime(true) / $this->keyRegeneration);
  114. }
  115. /**
  116. * Get the OTP window.
  117. *
  118. * @param null|int $window
  119. *
  120. * @return mixed
  121. */
  122. public function getWindow($window = null)
  123. {
  124. return
  125. is_null($window)
  126. ? $this->window
  127. : $window;
  128. }
  129. /**
  130. * Make a window based starting timestamp.
  131. *
  132. * @param $window
  133. * @param $timestamp
  134. * @param $oldTimestamp
  135. *
  136. * @return mixed
  137. */
  138. private function makeStartingTimestamp($window, $timestamp, $oldTimestamp)
  139. {
  140. return $oldTimestamp === Constants::ARGUMENT_NOT_SET
  141. ? $timestamp - $this->getWindow($window)
  142. : max($timestamp - $this->getWindow($window), $oldTimestamp + 1);
  143. }
  144. /**
  145. * Get/use a starting timestamp for key verification.
  146. *
  147. * @param string|int|null $timestamp
  148. *
  149. * @return int
  150. */
  151. protected function makeTimestamp($timestamp = null)
  152. {
  153. if (is_null($timestamp)) {
  154. return $this->getTimestamp();
  155. }
  156. return (int) $timestamp;
  157. }
  158. /**
  159. * Takes the secret key and the timestamp and returns the one time
  160. * password.
  161. *
  162. * @param string $secret - Secret key in binary form.
  163. * @param int $counter - Timestamp as returned by getTimestamp.
  164. *
  165. * @throws SecretKeyTooShortException
  166. *
  167. * @return string
  168. */
  169. public function oathHotp($secret, $counter)
  170. {
  171. $secret = $this->base32Decode($this->getSecret($secret));
  172. if (strlen($secret) < 8) {
  173. throw new SecretKeyTooShortException();
  174. }
  175. // Counter must be 64-bit int
  176. $bin_counter = pack('N*', 0, $counter);
  177. $hash = hash_hmac('sha1', $bin_counter, $secret, true);
  178. return str_pad($this->oathTruncate($hash), $this->getOneTimePasswordLength(), '0', STR_PAD_LEFT);
  179. }
  180. /**
  181. * Extracts the OTP from the SHA1 hash.
  182. *
  183. * @param string $hash
  184. *
  185. * @return int
  186. **/
  187. public function oathTruncate($hash)
  188. {
  189. $offset = ord($hash[19]) & 0xf;
  190. $temp = unpack('N', substr($hash, $offset, 4));
  191. return substr($temp[1] & 0x7fffffff, -$this->getOneTimePasswordLength());
  192. }
  193. /**
  194. * Remove invalid chars from a base 32 string.
  195. *
  196. * @param $string
  197. *
  198. * @return mixed
  199. */
  200. public function removeInvalidChars($string)
  201. {
  202. return preg_replace('/[^'.Constants::VALID_FOR_B32.']/', '', $string);
  203. }
  204. /**
  205. * Setter for the enforce Google Authenticator compatibility property.
  206. *
  207. * @param mixed $enforceGoogleAuthenticatorCompatibility
  208. *
  209. * @return $this
  210. */
  211. public function setEnforceGoogleAuthenticatorCompatibility($enforceGoogleAuthenticatorCompatibility)
  212. {
  213. $this->enforceGoogleAuthenticatorCompatibility = $enforceGoogleAuthenticatorCompatibility;
  214. return $this;
  215. }
  216. /**
  217. * Set key regeneration.
  218. *
  219. * @param mixed $keyRegeneration
  220. */
  221. public function setKeyRegeneration($keyRegeneration)
  222. {
  223. $this->keyRegeneration = $keyRegeneration;
  224. }
  225. /**
  226. * Set OTP length.
  227. *
  228. * @param mixed $oneTimePasswordLength
  229. */
  230. public function setOneTimePasswordLength($oneTimePasswordLength)
  231. {
  232. $this->oneTimePasswordLength = $oneTimePasswordLength;
  233. }
  234. /**
  235. * Set secret.
  236. *
  237. * @param mixed $secret
  238. */
  239. public function setSecret($secret)
  240. {
  241. $this->secret = $secret;
  242. }
  243. /**
  244. * Set the OTP window.
  245. *
  246. * @param mixed $window
  247. */
  248. public function setWindow($window)
  249. {
  250. $this->window = $window;
  251. }
  252. /**
  253. * Verifies a user inputted key against the current timestamp. Checks $window
  254. * keys either side of the timestamp.
  255. *
  256. * @param string $key - User specified key
  257. * @param null|string $secret
  258. * @param null|int $window
  259. * @param null|int $timestamp
  260. * @param null|string|int $oldTimestamp
  261. *
  262. * @return bool|int
  263. */
  264. public function verify($key, $secret = null, $window = null, $timestamp = null, $oldTimestamp = Constants::ARGUMENT_NOT_SET)
  265. {
  266. return $this->verifyKey(
  267. $secret,
  268. $key,
  269. $window,
  270. $timestamp,
  271. $oldTimestamp
  272. );
  273. }
  274. /**
  275. * Verifies a user inputted key against the current timestamp. Checks $window
  276. * keys either side of the timestamp.
  277. *
  278. * @param string $secret
  279. * @param string $key - User specified key
  280. * @param null|int $window
  281. * @param null|int $timestamp
  282. * @param null|string|int $oldTimestamp
  283. *
  284. * @return bool|int
  285. */
  286. public function verifyKey($secret, $key, $window = null, $timestamp = null, $oldTimestamp = Constants::ARGUMENT_NOT_SET)
  287. {
  288. $timestamp = $this->makeTimestamp($timestamp);
  289. return $this->findValidOTP(
  290. $secret,
  291. $key,
  292. $window,
  293. $this->makeStartingTimestamp($window, $timestamp, $oldTimestamp),
  294. $timestamp,
  295. $oldTimestamp
  296. );
  297. }
  298. /**
  299. * Verifies a user inputted key against the current timestamp. Checks $window
  300. * keys either side of the timestamp, but ensures that the given key is newer than
  301. * the given oldTimestamp. Useful if you need to ensure that a single key cannot
  302. * be used twice.
  303. *
  304. * @param string $secret
  305. * @param string $key - User specified key
  306. * @param int $oldTimestamp - The timestamp from the last verified key
  307. * @param int|null $window
  308. * @param int|null $timestamp
  309. *
  310. * @return bool|int - false (not verified) or the timestamp of the verified key
  311. **/
  312. public function verifyKeyNewer($secret, $key, $oldTimestamp, $window = null, $timestamp = null)
  313. {
  314. return $this->verifyKey($secret, $key, $window, $timestamp, $oldTimestamp);
  315. }
  316. }