ValidationData.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /**
  3. * This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
  4. *
  5. * @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
  6. */
  7. namespace Lcobucci\JWT;
  8. /**
  9. * Class that wraps validation values
  10. *
  11. * @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
  12. * @since 2.0.0
  13. */
  14. class ValidationData
  15. {
  16. /**
  17. * The list of things to be validated
  18. *
  19. * @var array
  20. */
  21. private $items;
  22. /**
  23. * Initializes the object
  24. *
  25. * @param int $currentTime
  26. */
  27. public function __construct($currentTime = null)
  28. {
  29. $currentTime = $currentTime ?: time();
  30. $this->items = [
  31. 'jti' => null,
  32. 'iss' => null,
  33. 'aud' => null,
  34. 'sub' => null,
  35. 'iat' => $currentTime,
  36. 'nbf' => $currentTime,
  37. 'exp' => $currentTime
  38. ];
  39. }
  40. /**
  41. * Configures the id
  42. *
  43. * @param string $id
  44. */
  45. public function setId($id)
  46. {
  47. $this->items['jti'] = (string) $id;
  48. }
  49. /**
  50. * Configures the issuer
  51. *
  52. * @param string $issuer
  53. */
  54. public function setIssuer($issuer)
  55. {
  56. $this->items['iss'] = (string) $issuer;
  57. }
  58. /**
  59. * Configures the audience
  60. *
  61. * @param string $audience
  62. */
  63. public function setAudience($audience)
  64. {
  65. $this->items['aud'] = (string) $audience;
  66. }
  67. /**
  68. * Configures the subject
  69. *
  70. * @param string $subject
  71. */
  72. public function setSubject($subject)
  73. {
  74. $this->items['sub'] = (string) $subject;
  75. }
  76. /**
  77. * Configures the time that "iat", "nbf" and "exp" should be based on
  78. *
  79. * @param int $currentTime
  80. */
  81. public function setCurrentTime($currentTime)
  82. {
  83. $this->items['iat'] = (int) $currentTime;
  84. $this->items['nbf'] = (int) $currentTime;
  85. $this->items['exp'] = (int) $currentTime;
  86. }
  87. /**
  88. * Returns the requested item
  89. *
  90. * @param string $name
  91. *
  92. * @return mixed
  93. */
  94. public function get($name)
  95. {
  96. return isset($this->items[$name]) ? $this->items[$name] : null;
  97. }
  98. /**
  99. * Returns if the item is present
  100. *
  101. * @param string $name
  102. *
  103. * @return boolean
  104. */
  105. public function has($name)
  106. {
  107. return !empty($this->items[$name]);
  108. }
  109. }