TOTP.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /*
  3. * Copyright (c) 2011 Le Lag
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. * THE SOFTWARE.
  20. */
  21. namespace OTPHP {
  22. /**
  23. * TOTP - One time password generator
  24. *
  25. * The TOTP class allow for the generation
  26. * and verification of one-time password using
  27. * the TOTP specified algorithm.
  28. *
  29. * This class is meant to be compatible with
  30. * Google Authenticator
  31. *
  32. * This class was originally ported from the rotp
  33. * ruby library available at https://github.com/mdp/rotp
  34. */
  35. class TOTP extends OTP {
  36. /**
  37. * The interval in seconds for a one-time password timeframe
  38. * Defaults to 30
  39. * @var integer
  40. */
  41. public $interval;
  42. public function __construct($s, $opt = Array()) {
  43. $this->interval = isset($opt['interval']) ? $opt['interval'] : 30;
  44. parent::__construct($s, $opt);
  45. }
  46. /**
  47. * Get the password for a specific timestamp value
  48. *
  49. * @param integer $timestamp the timestamp which is timecoded and
  50. * used to seed the hmac hash function.
  51. * @return integer the One Time Password
  52. */
  53. public function at($timestamp) {
  54. return $this->generateOTP($this->timecode($timestamp));
  55. }
  56. /**
  57. * Get the password for the current timestamp value
  58. *
  59. * @return integer the current One Time Password
  60. */
  61. public function now() {
  62. return $this->generateOTP($this->timecode(time()));
  63. }
  64. /**
  65. * Verify if a password is valid for a specific counter value
  66. *
  67. * @param integer $otp the one-time password
  68. * @param integer $timestamp the timestamp for the a given time, defaults to current time.
  69. * @return bool true if the counter is valid, false otherwise
  70. */
  71. public function verify($otp, $timestamp = null) {
  72. if($timestamp === null)
  73. $timestamp = time();
  74. return ($otp == $this->at($timestamp));
  75. }
  76. /**
  77. * Returns the uri for a specific secret for totp method.
  78. * Can be encoded as a image for simple configuration in
  79. * Google Authenticator.
  80. *
  81. * @param string $name the name of the account / profile
  82. * @return string the uri for the hmac secret
  83. */
  84. public function provisioning_uri($name) {
  85. return "otpauth://totp/".urlencode($name)."?secret={$this->secret}";
  86. }
  87. /**
  88. * Transform a timestamp in a counter based on specified internal
  89. *
  90. * @param integer $timestamp
  91. * @return integer the timecode
  92. */
  93. protected function timecode($timestamp) {
  94. return (int)( (((int)$timestamp * 1000) / ($this->interval * 1000)));
  95. }
  96. }
  97. }