Token.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. * @deprecated This method will be renamed on v4, the returned value will also change
  69. *
  70. * @return array
  71. */
  72. public function getHeaders()
  73. {
  74. return $this->headers;
  75. }
  76. /**
  77. * Returns if the header is configured
  78. *
  79. * @deprecated This method will be removed on v4
  80. *
  81. * @param string $name
  82. *
  83. * @return boolean
  84. */
  85. public function hasHeader($name)
  86. {
  87. return array_key_exists($name, $this->headers);
  88. }
  89. /**
  90. * Returns the value of a token header
  91. *
  92. * @deprecated This method will be removed on v4
  93. *
  94. * @param string $name
  95. * @param mixed $default
  96. *
  97. * @return mixed
  98. *
  99. * @throws OutOfBoundsException
  100. */
  101. public function getHeader($name, $default = null)
  102. {
  103. if ($this->hasHeader($name)) {
  104. return $this->getHeaderValue($name);
  105. }
  106. if ($default === null) {
  107. throw new OutOfBoundsException('Requested header is not configured');
  108. }
  109. return $default;
  110. }
  111. /**
  112. * Returns the value stored in header
  113. *
  114. * @param string $name
  115. *
  116. * @return mixed
  117. */
  118. private function getHeaderValue($name)
  119. {
  120. $header = $this->headers[$name];
  121. if ($header instanceof Claim) {
  122. return $header->getValue();
  123. }
  124. return $header;
  125. }
  126. /**
  127. * Returns the token claim set
  128. *
  129. * @deprecated This method will be renamed on v4, the returned value will also change
  130. *
  131. * @return array
  132. */
  133. public function getClaims()
  134. {
  135. return $this->claims;
  136. }
  137. /**
  138. * Returns if the claim is configured
  139. *
  140. * @deprecated This method will be removed on v4
  141. *
  142. * @param string $name
  143. *
  144. * @return boolean
  145. */
  146. public function hasClaim($name)
  147. {
  148. return array_key_exists($name, $this->claims);
  149. }
  150. /**
  151. * Returns the value of a token claim
  152. *
  153. * @deprecated This method will be removed on v4
  154. *
  155. * @param string $name
  156. * @param mixed $default
  157. *
  158. * @return mixed
  159. *
  160. * @throws OutOfBoundsException
  161. */
  162. public function getClaim($name, $default = null)
  163. {
  164. if ($this->hasClaim($name)) {
  165. return $this->claims[$name]->getValue();
  166. }
  167. if ($default === null) {
  168. throw new OutOfBoundsException('Requested claim is not configured');
  169. }
  170. return $default;
  171. }
  172. /**
  173. * Verify if the key matches with the one that created the signature
  174. *
  175. * @deprecated This method will be removed on v4, new validation API should be used
  176. *
  177. * @param Signer $signer
  178. * @param string $key
  179. *
  180. * @return boolean
  181. *
  182. * @throws BadMethodCallException When token is not signed
  183. */
  184. public function verify(Signer $signer, $key)
  185. {
  186. if ($this->signature === null) {
  187. throw new BadMethodCallException('This token is not signed');
  188. }
  189. if ($this->headers['alg'] !== $signer->getAlgorithmId()) {
  190. return false;
  191. }
  192. return $this->signature->verify($signer, $this->getPayload(), $key);
  193. }
  194. /**
  195. * Validates if the token is valid
  196. *
  197. * @deprecated This method will be removed on v4, new validation API should be used
  198. *
  199. * @param ValidationData $data
  200. *
  201. * @return boolean
  202. */
  203. public function validate(ValidationData $data)
  204. {
  205. foreach ($this->getValidatableClaims() as $claim) {
  206. if (!$claim->validate($data)) {
  207. return false;
  208. }
  209. }
  210. return true;
  211. }
  212. /**
  213. * Determine if the token is expired.
  214. *
  215. * @param DateTimeInterface $now Defaults to the current time.
  216. *
  217. * @return bool
  218. */
  219. public function isExpired(DateTimeInterface $now = null)
  220. {
  221. $exp = $this->getClaim('exp', false);
  222. if ($exp === false) {
  223. return false;
  224. }
  225. $now = $now ?: new DateTime();
  226. $expiresAt = new DateTime();
  227. $expiresAt->setTimestamp($exp);
  228. return $now > $expiresAt;
  229. }
  230. /**
  231. * Yields the validatable claims
  232. *
  233. * @return Generator
  234. */
  235. private function getValidatableClaims()
  236. {
  237. foreach ($this->claims as $claim) {
  238. if ($claim instanceof Validatable) {
  239. yield $claim;
  240. }
  241. }
  242. }
  243. /**
  244. * Returns the token payload
  245. *
  246. * @deprecated This method will be renamed on v4
  247. *
  248. * @return string
  249. */
  250. public function getPayload()
  251. {
  252. return $this->payload[0] . '.' . $this->payload[1];
  253. }
  254. /**
  255. * Returns an encoded representation of the token
  256. *
  257. * @return string
  258. */
  259. public function __toString()
  260. {
  261. $data = implode('.', $this->payload);
  262. if ($this->signature === null) {
  263. $data .= '.';
  264. }
  265. return $data;
  266. }
  267. }