PageRenderTime 34ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/Upload/inc/3rdparty/2fa/GoogleAuthenticator.php

https://gitlab.com/mybbpl/ppm-1.8
PHP | 208 lines | 118 code | 21 blank | 69 comment | 30 complexity | 519fc23a9e821e9c3418d40d129a419f MD5 | raw file
  1. <?php
  2. /**
  3. * PHP Class for handling Google Authenticator 2-factor authentication
  4. *
  5. * @author Michael Kliewe
  6. * @copyright 2012 Michael Kliewe
  7. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  8. * @link http://www.phpgangsta.de/
  9. */
  10. class PHPGangsta_GoogleAuthenticator
  11. {
  12. protected $_codeLength = 6;
  13. /**
  14. * Create new secret.
  15. * 16 characters, randomly chosen from the allowed base32 characters.
  16. *
  17. * @param int $secretLength
  18. * @return string
  19. */
  20. public function createSecret($secretLength = 16)
  21. {
  22. $validChars = $this->_getBase32LookupTable();
  23. unset($validChars[32]);
  24. $secret = '';
  25. for ($i = 0; $i < $secretLength; $i++) {
  26. $secret .= $validChars[array_rand($validChars)];
  27. }
  28. return $secret;
  29. }
  30. /**
  31. * Calculate the code, with given secret and point in time
  32. *
  33. * @param string $secret
  34. * @param int|null $timeSlice
  35. * @return string
  36. */
  37. public function getCode($secret, $timeSlice = null)
  38. {
  39. if ($timeSlice === null) {
  40. $timeSlice = floor(time() / 30);
  41. }
  42. $secretkey = $this->_base32Decode($secret);
  43. // Pack time into binary string
  44. $time = chr(0).chr(0).chr(0).chr(0).pack('N*', $timeSlice);
  45. // Hash it with users secret key
  46. $hm = hash_hmac('SHA1', $time, $secretkey, true);
  47. // Use last nipple of result as index/offset
  48. $offset = ord(substr($hm, -1)) & 0x0F;
  49. // grab 4 bytes of the result
  50. $hashpart = substr($hm, $offset, 4);
  51. // Unpak binary value
  52. $value = unpack('N', $hashpart);
  53. $value = $value[1];
  54. // Only 32 bits
  55. $value = $value & 0x7FFFFFFF;
  56. $modulo = pow(10, $this->_codeLength);
  57. return str_pad($value % $modulo, $this->_codeLength, '0', STR_PAD_LEFT);
  58. }
  59. /**
  60. * Get QR-Code URL for image, from google charts
  61. *
  62. * @param string $name
  63. * @param string $secret
  64. * @param string $title
  65. * @return string
  66. */
  67. public function getQRCodeGoogleUrl($name, $secret, $title = null) {
  68. $urlencoded = urlencode('otpauth://totp/'.$name.'?secret='.$secret.'');
  69. if(isset($title)) {
  70. $urlencoded .= urlencode('&issuer='.urlencode($title));
  71. }
  72. return 'https://chart.googleapis.com/chart?chs=200x200&chld=M|0&cht=qr&chl='.$urlencoded.'';
  73. }
  74. /**
  75. * Check if the code is correct. This will accept codes starting from $discrepancy*30sec ago to $discrepancy*30sec from now
  76. *
  77. * @param string $secret
  78. * @param string $code
  79. * @param int $discrepancy This is the allowed time drift in 30 second units (8 means 4 minutes before or after)
  80. * @param int|null $currentTimeSlice time slice if we want use other that time()
  81. * @return bool
  82. */
  83. public function verifyCode($secret, $code, $discrepancy = 1, $currentTimeSlice = null)
  84. {
  85. if ($currentTimeSlice === null) {
  86. $currentTimeSlice = floor(time() / 30);
  87. }
  88. for ($i = -$discrepancy; $i <= $discrepancy; $i++) {
  89. $calculatedCode = $this->getCode($secret, $currentTimeSlice + $i);
  90. if ($calculatedCode == $code ) {
  91. return true;
  92. }
  93. }
  94. return false;
  95. }
  96. /**
  97. * Set the code length, should be >=6
  98. *
  99. * @param int $length
  100. * @return PHPGangsta_GoogleAuthenticator
  101. */
  102. public function setCodeLength($length)
  103. {
  104. $this->_codeLength = $length;
  105. return $this;
  106. }
  107. /**
  108. * Helper class to decode base32
  109. *
  110. * @param $secret
  111. * @return bool|string
  112. */
  113. protected function _base32Decode($secret)
  114. {
  115. if (empty($secret)) return '';
  116. $base32chars = $this->_getBase32LookupTable();
  117. $base32charsFlipped = array_flip($base32chars);
  118. $paddingCharCount = substr_count($secret, $base32chars[32]);
  119. $allowedValues = array(6, 4, 3, 1, 0);
  120. if (!in_array($paddingCharCount, $allowedValues)) return false;
  121. for ($i = 0; $i < 4; $i++){
  122. if ($paddingCharCount == $allowedValues[$i] &&
  123. substr($secret, -($allowedValues[$i])) != str_repeat($base32chars[32], $allowedValues[$i])) return false;
  124. }
  125. $secret = str_replace('=','', $secret);
  126. $secret = str_split($secret);
  127. $binaryString = "";
  128. for ($i = 0; $i < count($secret); $i = $i+8) {
  129. $x = "";
  130. if (!in_array($secret[$i], $base32chars)) return false;
  131. for ($j = 0; $j < 8; $j++) {
  132. $x .= str_pad(base_convert(@$base32charsFlipped[@$secret[$i + $j]], 10, 2), 5, '0', STR_PAD_LEFT);
  133. }
  134. $eightBits = str_split($x, 8);
  135. for ($z = 0; $z < count($eightBits); $z++) {
  136. $binaryString .= ( ($y = chr(base_convert($eightBits[$z], 2, 10))) || ord($y) == 48 ) ? $y:"";
  137. }
  138. }
  139. return $binaryString;
  140. }
  141. /**
  142. * Helper class to encode base32
  143. *
  144. * @param string $secret
  145. * @param bool $padding
  146. * @return string
  147. */
  148. protected function _base32Encode($secret, $padding = true)
  149. {
  150. if (empty($secret)) return '';
  151. $base32chars = $this->_getBase32LookupTable();
  152. $secret = str_split($secret);
  153. $binaryString = "";
  154. for ($i = 0; $i < count($secret); $i++) {
  155. $binaryString .= str_pad(base_convert(ord($secret[$i]), 10, 2), 8, '0', STR_PAD_LEFT);
  156. }
  157. $fiveBitBinaryArray = str_split($binaryString, 5);
  158. $base32 = "";
  159. $i = 0;
  160. while ($i < count($fiveBitBinaryArray)) {
  161. $base32 .= $base32chars[base_convert(str_pad($fiveBitBinaryArray[$i], 5, '0'), 2, 10)];
  162. $i++;
  163. }
  164. if ($padding && ($x = strlen($binaryString) % 40) != 0) {
  165. if ($x == 8) $base32 .= str_repeat($base32chars[32], 6);
  166. elseif ($x == 16) $base32 .= str_repeat($base32chars[32], 4);
  167. elseif ($x == 24) $base32 .= str_repeat($base32chars[32], 3);
  168. elseif ($x == 32) $base32 .= $base32chars[32];
  169. }
  170. return $base32;
  171. }
  172. /**
  173. * Get array with all 32 characters for decoding from/encoding to base32
  174. *
  175. * @return array
  176. */
  177. protected function _getBase32LookupTable()
  178. {
  179. return array(
  180. 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', // 7
  181. 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', // 15
  182. 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', // 23
  183. 'Y', 'Z', '2', '3', '4', '5', '6', '7', // 31
  184. '=' // padding char
  185. );
  186. }
  187. }