Token.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. use BadMethodCallException;
  9. use DateTime;
  10. use DateTimeInterface;
  11. use Generator;
  12. use Lcobucci\JWT\Claim\Validatable;
  13. use OutOfBoundsException;
  14. /**
  15. * Basic structure of the JWT
  16. *
  17. * @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
  18. * @since 0.1.0
  19. */
  20. class Token
  21. {
  22. /**
  23. * The token headers
  24. *
  25. * @var array
  26. */
  27. private $headers;
  28. /**
  29. * The token claim set
  30. *
  31. * @var array
  32. */
  33. private $claims;
  34. /**
  35. * The token signature
  36. *
  37. * @var Signature
  38. */
  39. private $signature;
  40. /**
  41. * The encoded data
  42. *
  43. * @var array
  44. */
  45. private $payload;
  46. /**
  47. * Initializes the object
  48. *
  49. * @param array $headers
  50. * @param array $claims
  51. * @param array $payload
  52. * @param Signature $signature
  53. */
  54. public function __construct(
  55. array $headers = ['alg' => 'none'],
  56. array $claims = [],
  57. Signature $signature = null,
  58. array $payload = ['', '']
  59. ) {
  60. $this->headers = $headers;
  61. $this->claims = $claims;
  62. $this->signature = $signature;
  63. $this->payload = $payload;
  64. }
  65. /**
  66. * Returns the token headers
  67. *
  68. * @return array
  69. */
  70. public function getHeaders()
  71. {
  72. return $this->headers;
  73. }
  74. /**
  75. * Returns if the header is configured
  76. *
  77. * @param string $name
  78. *
  79. * @return boolean
  80. */
  81. public function hasHeader($name)
  82. {
  83. return array_key_exists($name, $this->headers);
  84. }
  85. /**
  86. * Returns the value of a token header
  87. *
  88. * @param string $name
  89. * @param mixed $default
  90. *
  91. * @return mixed
  92. *
  93. * @throws OutOfBoundsException
  94. */
  95. public function getHeader($name, $default = null)
  96. {
  97. if ($this->hasHeader($name)) {
  98. return $this->getHeaderValue($name);
  99. }
  100. if ($default === null) {
  101. throw new OutOfBoundsException('Requested header is not configured');
  102. }
  103. return $default;
  104. }
  105. /**
  106. * Returns the value stored in header
  107. *
  108. * @param string $name
  109. *
  110. * @return mixed
  111. */
  112. private function getHeaderValue($name)
  113. {
  114. $header = $this->headers[$name];
  115. if ($header instanceof Claim) {
  116. return $header->getValue();
  117. }
  118. return $header;
  119. }
  120. /**
  121. * Returns the token claim set
  122. *
  123. * @return array
  124. */
  125. public function getClaims()
  126. {
  127. return $this->claims;
  128. }
  129. /**
  130. * Returns if the claim is configured
  131. *
  132. * @param string $name
  133. *
  134. * @return boolean
  135. */
  136. public function hasClaim($name)
  137. {
  138. return array_key_exists($name, $this->claims);
  139. }
  140. /**
  141. * Returns the value of a token claim
  142. *
  143. * @param string $name
  144. * @param mixed $default
  145. *
  146. * @return mixed
  147. *
  148. * @throws OutOfBoundsException
  149. */
  150. public function getClaim($name, $default = null)
  151. {
  152. if ($this->hasClaim($name)) {
  153. return $this->claims[$name]->getValue();
  154. }
  155. if ($default === null) {
  156. throw new OutOfBoundsException('Requested claim is not configured');
  157. }
  158. return $default;
  159. }
  160. /**
  161. * Verify if the key matches with the one that created the signature
  162. *
  163. * @param Signer $signer
  164. * @param string $key
  165. *
  166. * @return boolean
  167. *
  168. * @throws BadMethodCallException When token is not signed
  169. */
  170. public function verify(Signer $signer, $key)
  171. {
  172. if ($this->signature === null) {
  173. throw new BadMethodCallException('This token is not signed');
  174. }
  175. if ($this->headers['alg'] !== $signer->getAlgorithmId()) {
  176. return false;
  177. }
  178. return $this->signature->verify($signer, $this->getPayload(), $key);
  179. }
  180. /**
  181. * Validates if the token is valid
  182. *
  183. * @param ValidationData $data
  184. *
  185. * @return boolean
  186. */
  187. public function validate(ValidationData $data)
  188. {
  189. foreach ($this->getValidatableClaims() as $claim) {
  190. if (!$claim->validate($data)) {
  191. return false;
  192. }
  193. }
  194. return true;
  195. }
  196. /**
  197. * Determine if the token is expired.
  198. *
  199. * @param DateTimeInterface $now Defaults to the current time.
  200. *
  201. * @return bool
  202. */
  203. public function isExpired(DateTimeInterface $now = null)
  204. {
  205. $exp = $this->getClaim('exp', false);
  206. if ($exp === false) {
  207. return false;
  208. }
  209. $now = $now ?: new DateTime();
  210. $expiresAt = new DateTime();
  211. $expiresAt->setTimestamp($exp);
  212. return $now > $expiresAt;
  213. }
  214. /**
  215. * Yields the validatable claims
  216. *
  217. * @return Generator
  218. */
  219. private function getValidatableClaims()
  220. {
  221. foreach ($this->claims as $claim) {
  222. if ($claim instanceof Validatable) {
  223. yield $claim;
  224. }
  225. }
  226. }
  227. /**
  228. * Returns the token payload
  229. *
  230. * @return string
  231. */
  232. public function getPayload()
  233. {
  234. return $this->payload[0] . '.' . $this->payload[1];
  235. }
  236. /**
  237. * Returns an encoded representation of the token
  238. *
  239. * @return string
  240. */
  241. public function __toString()
  242. {
  243. $data = implode('.', $this->payload);
  244. if ($this->signature === null) {
  245. $data .= '.';
  246. }
  247. return $data;
  248. }
  249. }