PageRenderTime 51ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/simplesamlphp-1.10.0/lib/SimpleSAML/Auth/TimeLimitedToken.php

https://bitbucket.org/sahkoinenaanestys/sahkoinenaanestys
PHP | 86 lines | 37 code | 22 blank | 27 comment | 1 complexity | 99d66740921ab2d8d20f1771df259682 MD5 | raw file
  1. <?php
  2. class SimpleSAML_Auth_TimeLimitedToken {
  3. var $secretSalt;
  4. var $lifetime;
  5. var $skew;
  6. /**
  7. * @param $secretSalt Must be random and unique per installation
  8. * @param $lifeTime Token lifetime in seconds
  9. * @param $skew Allowed time skew between server that generates and the one that calculates the token
  10. */
  11. public function __construct( $lifetime = 900, $secretSalt = NULL, $skew = 1) {
  12. if ($secretSalt === NULL) {
  13. $secretSalt = SimpleSAML_Utilities::getSecretSalt();
  14. }
  15. $this->secretSalt = $secretSalt;
  16. $this->lifetime = $lifetime;
  17. $this->skew = $skew;
  18. }
  19. public function addVerificationData($data) {
  20. $this->secretSalt .= '|' . $data;
  21. }
  22. /**
  23. * Calculate the current time offset to the current time slot.
  24. * With some amount of time skew
  25. */
  26. private function get_offset() {
  27. return ( (time() - $this->skew) % ($this->lifetime + $this->skew) );
  28. }
  29. /**
  30. * Calculate the given time slot for a given offset.
  31. */
  32. private function calculate_time_slot($offset) {
  33. #echo 'lifetime is: ' . $this->lifetime;
  34. $timeslot = floor( (time() - $offset) / ($this->lifetime + $this->skew) );
  35. return $timeslot;
  36. }
  37. /**
  38. * Calculates a token value for a given offset
  39. */
  40. private function calculate_tokenvalue($offset) {
  41. // A secret salt that should be randomly generated for each installation.
  42. #echo 'Secret salt is: ' . $this->secretSalt;
  43. #echo '<p>Calculating sha1( ' . $this->calculate_time_slot($offset) . ':' . $this->secretSalt . ' )<br />';
  44. return sha1( $this->calculate_time_slot($offset) . ':' . $this->secretSalt);
  45. }
  46. /**
  47. * Generates a token which contains of a offset and a token value. Using current offset
  48. */
  49. public function generate_token() {
  50. $current_offset = $this->get_offset();
  51. return dechex($current_offset) . '-' . $this->calculate_tokenvalue($current_offset);
  52. }
  53. /**
  54. * Validates a full token, by calculating the token value for the provided
  55. * offset and compares.
  56. */
  57. public function validate_token($token) {
  58. $splittedtoken = explode('-', $token);
  59. $offset = hexdec($splittedtoken[0]);
  60. $value = $splittedtoken[1];
  61. #echo 'compare [' . $this->calculate_tokenvalue($offset). '] with [' . $value . '] offset was [' . $offset. ']';
  62. return ($this->calculate_tokenvalue($offset) === $value);
  63. }
  64. }