123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284 |
- <?php
- /**
- * This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
- *
- * @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
- */
- namespace Lcobucci\JWT;
- use BadMethodCallException;
- use DateTime;
- use DateTimeInterface;
- use Generator;
- use Lcobucci\JWT\Claim\Validatable;
- use OutOfBoundsException;
- /**
- * Basic structure of the JWT
- *
- * @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
- * @since 0.1.0
- */
- class Token
- {
- /**
- * The token headers
- *
- * @var array
- */
- private $headers;
- /**
- * The token claim set
- *
- * @var array
- */
- private $claims;
- /**
- * The token signature
- *
- * @var Signature
- */
- private $signature;
- /**
- * The encoded data
- *
- * @var array
- */
- private $payload;
- /**
- * Initializes the object
- *
- * @param array $headers
- * @param array $claims
- * @param array $payload
- * @param Signature $signature
- */
- public function __construct(
- array $headers = ['alg' => 'none'],
- array $claims = [],
- Signature $signature = null,
- array $payload = ['', '']
- ) {
- $this->headers = $headers;
- $this->claims = $claims;
- $this->signature = $signature;
- $this->payload = $payload;
- }
- /**
- * Returns the token headers
- *
- * @return array
- */
- public function getHeaders()
- {
- return $this->headers;
- }
- /**
- * Returns if the header is configured
- *
- * @param string $name
- *
- * @return boolean
- */
- public function hasHeader($name)
- {
- return array_key_exists($name, $this->headers);
- }
- /**
- * Returns the value of a token header
- *
- * @param string $name
- * @param mixed $default
- *
- * @return mixed
- *
- * @throws OutOfBoundsException
- */
- public function getHeader($name, $default = null)
- {
- if ($this->hasHeader($name)) {
- return $this->getHeaderValue($name);
- }
- if ($default === null) {
- throw new OutOfBoundsException('Requested header is not configured');
- }
- return $default;
- }
- /**
- * Returns the value stored in header
- *
- * @param string $name
- *
- * @return mixed
- */
- private function getHeaderValue($name)
- {
- $header = $this->headers[$name];
- if ($header instanceof Claim) {
- return $header->getValue();
- }
- return $header;
- }
- /**
- * Returns the token claim set
- *
- * @return array
- */
- public function getClaims()
- {
- return $this->claims;
- }
- /**
- * Returns if the claim is configured
- *
- * @param string $name
- *
- * @return boolean
- */
- public function hasClaim($name)
- {
- return array_key_exists($name, $this->claims);
- }
- /**
- * Returns the value of a token claim
- *
- * @param string $name
- * @param mixed $default
- *
- * @return mixed
- *
- * @throws OutOfBoundsException
- */
- public function getClaim($name, $default = null)
- {
- if ($this->hasClaim($name)) {
- return $this->claims[$name]->getValue();
- }
- if ($default === null) {
- throw new OutOfBoundsException('Requested claim is not configured');
- }
- return $default;
- }
- /**
- * Verify if the key matches with the one that created the signature
- *
- * @param Signer $signer
- * @param string $key
- *
- * @return boolean
- *
- * @throws BadMethodCallException When token is not signed
- */
- public function verify(Signer $signer, $key)
- {
- if ($this->signature === null) {
- throw new BadMethodCallException('This token is not signed');
- }
- if ($this->headers['alg'] !== $signer->getAlgorithmId()) {
- return false;
- }
- return $this->signature->verify($signer, $this->getPayload(), $key);
- }
- /**
- * Validates if the token is valid
- *
- * @param ValidationData $data
- *
- * @return boolean
- */
- public function validate(ValidationData $data)
- {
- foreach ($this->getValidatableClaims() as $claim) {
- if (!$claim->validate($data)) {
- return false;
- }
- }
- return true;
- }
- /**
- * Determine if the token is expired.
- *
- * @param DateTimeInterface $now Defaults to the current time.
- *
- * @return bool
- */
- public function isExpired(DateTimeInterface $now = null)
- {
- $exp = $this->getClaim('exp', false);
- if ($exp === false) {
- return false;
- }
- $now = $now ?: new DateTime();
- $expiresAt = new DateTime();
- $expiresAt->setTimestamp($exp);
- return $now > $expiresAt;
- }
- /**
- * Yields the validatable claims
- *
- * @return Generator
- */
- private function getValidatableClaims()
- {
- foreach ($this->claims as $claim) {
- if ($claim instanceof Validatable) {
- yield $claim;
- }
- }
- }
- /**
- * Returns the token payload
- *
- * @return string
- */
- public function getPayload()
- {
- return $this->payload[0] . '.' . $this->payload[1];
- }
- /**
- * Returns an encoded representation of the token
- *
- * @return string
- */
- public function __toString()
- {
- $data = implode('.', $this->payload);
- if ($this->signature === null) {
- $data .= '.';
- }
- return $data;
- }
- }
|