BioTorrents.de’s version of Gazelle
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

twofa.class.php 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. #declare(strict_types=1);
  3. class TwoFactorAuth
  4. {
  5. private $algorithm;
  6. private $period;
  7. private $digits;
  8. private $issuer;
  9. private static $_base32dict = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=';
  10. private static $_base32;
  11. private static $_base32lookup = [];
  12. private static $_supportedalgos = array('sha1', 'sha256', 'sha512', 'md5');
  13. public function __construct($issuer = null, $digits = 6, $period = 30, $algorithm = 'sha1')
  14. {
  15. $this->issuer = $issuer;
  16. $this->digits = $digits;
  17. $this->period = $period;
  18. $algorithm = strtolower(trim($algorithm));
  19. if (!in_array($algorithm, self::$_supportedalgos)) {
  20. $algorithm = 'sha1';
  21. }
  22. $this->algorithm = $algorithm;
  23. self::$_base32 = str_split(self::$_base32dict);
  24. self::$_base32lookup = array_flip(self::$_base32);
  25. }
  26. public function createSecret($bits = 210)
  27. {
  28. $secret = '';
  29. $bytes = ceil($bits / 5); //We use 5 bits of each byte (since we have a 32-character 'alphabet' / BASE32)
  30. $rnd = random_bytes($bytes);
  31. for ($i = 0; $i < $bytes; $i++) {
  32. $secret .= self::$_base32[ord($rnd[$i]) & 31]; //Mask out left 3 bits for 0-31 values
  33. }
  34. return $secret;
  35. }
  36. // Calculate the code with given secret and point in time
  37. public function getCode($secret, $time = null)
  38. {
  39. $secretkey = $this->base32Decode($secret);
  40. $timestamp = "\0\0\0\0" . pack('N*', $this->getTimeSlice($this->getTime($time))); // Pack time into binary string
  41. $hashhmac = hash_hmac($this->algorithm, $timestamp, $secretkey, true); // Hash it with users secret key
  42. $hashpart = substr($hashhmac, ord(substr($hashhmac, -1)) & 0x0F, 4); // Use last nibble of result as index/offset and grab 4 bytes of the result
  43. $value = unpack('N', $hashpart); // Unpack binary value
  44. $value = $value[1] & 0x7FFFFFFF; // Drop MSB, keep only 31 bits
  45. return str_pad($value % pow(10, $this->digits), $this->digits, '0', STR_PAD_LEFT);
  46. }
  47. // Check if the code is correct. This will accept codes starting from now +/- ($discrepancy * period) seconds
  48. public function verifyCode($secret, $code, $discrepancy = 1, $time = null)
  49. {
  50. $result = false;
  51. $timetamp = $this->getTime($time);
  52. // Always iterate all possible codes to prevent timing-attacks
  53. for ($i = -$discrepancy; $i <= $discrepancy; $i++) {
  54. $result |= hash_equals($this->getCode($secret, $timetamp + ($i * $this->period)), $code);
  55. }
  56. return (bool)$result;
  57. }
  58. // Get data-uri of QRCode
  59. public function getQRCodeImageAsDataUri($label, $secret, $size = 300)
  60. {
  61. if (exec('which qrencode')) {
  62. $QRCodeImage = shell_exec("qrencode -s ".(int)($size/40)." -m 3 -o - '".$this->getQRText($label, $secret)."'");
  63. } else {
  64. $curlhandle = curl_init();
  65. curl_setopt_array($curlhandle, array(
  66. CURLOPT_URL => 'https://chart.googleapis.com/chart?cht=qr&chs='.$size.'x'.$size.'&chld=L|1&chl='.rawurlencode($this->getQRText($label, $secret)),
  67. CURLOPT_RETURNTRANSFER => true,
  68. CURLOPT_CONNECTTIMEOUT => 10,
  69. CURLOPT_DNS_CACHE_TIMEOUT => 10,
  70. CURLOPT_TIMEOUT => 10,
  71. CURLOPT_SSL_VERIFYPEER => false,
  72. CURLOPT_USERAGENT => 'TwoFactorAuth'
  73. ));
  74. $QRCodeImage = curl_exec($curlhandle);
  75. curl_close($curlhandle);
  76. }
  77. return 'data:image/png;base64,'.base64_encode($QRCodeImage);
  78. }
  79. private function getTime($time)
  80. {
  81. return ($time === null) ? time() : $time;
  82. }
  83. private function getTimeSlice($time = null, $offset = 0)
  84. {
  85. return (int)floor($time / $this->period) + ($offset * $this->period);
  86. }
  87. // Builds a string to be encoded in a QR code
  88. public function getQRText($label, $secret)
  89. {
  90. $QRText = 'otpauth://totp/'.rawurlencode($label).'?secret='.rawurlencode($secret);
  91. $QRText .= ($this->issuer) ? '&issuer='.rawurlencode($this->issuer) : '';
  92. $QRText .= ($this->period != 30) ? '&period='.intval($this->period) : '';
  93. $QRText .= ($this->algorithm != 'sha1') ? '&algorithm='.rawurlencode(strtoupper($this->algorithm)) : '';
  94. $QRText .= ($this->digits != 6) ? '&digits='.intval($this->digits) : '';
  95. return $QRText;
  96. }
  97. private function base32Decode($value)
  98. {
  99. if (strlen($value)==0) {
  100. return '';
  101. }
  102. $buffer = '';
  103. foreach (str_split($value) as $char) {
  104. if ($char !== '=') {
  105. $buffer .= str_pad(decbin(self::$_base32lookup[$char]), 5, 0, STR_PAD_LEFT);
  106. }
  107. }
  108. $length = strlen($buffer);
  109. $blocks = trim(chunk_split(substr($buffer, 0, $length - ($length % 8)), 8, ' '));
  110. $output = '';
  111. foreach (explode(' ', $blocks) as $block) {
  112. $output .= chr(bindec(str_pad($block, 8, 0, STR_PAD_RIGHT)));
  113. }
  114. return $output;
  115. }
  116. }