[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/vendor/lcobucci/jwt/src/ -> Token.php (source)

   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  
   8  namespace Lcobucci\JWT;
   9  
  10  use DateTimeImmutable;
  11  use DateTimeInterface;
  12  use Generator;
  13  use Lcobucci\JWT\Claim\Factory;
  14  use Lcobucci\JWT\Claim\Validatable;
  15  use Lcobucci\JWT\Signer\Key;
  16  use Lcobucci\JWT\Token\DataSet;
  17  use Lcobucci\JWT\Token\RegisteredClaims;
  18  use OutOfBoundsException;
  19  use function current;
  20  use function func_num_args;
  21  use function in_array;
  22  use function is_array;
  23  use function sprintf;
  24  use function trigger_error;
  25  use const E_USER_DEPRECATED;
  26  
  27  /**
  28   * Basic structure of the JWT
  29   *
  30   * @author Luís Otávio Cobucci Oblonczyk <[email protected]>
  31   * @since 0.1.0
  32   */
  33  class Token
  34  {
  35      /**
  36       * The token headers
  37       *
  38       * @var DataSet
  39       */
  40      private $headers;
  41  
  42      /**
  43       * The token claim set
  44       *
  45       * @var DataSet
  46       */
  47      private $claims;
  48  
  49      /**
  50       * The token signature
  51       *
  52       * @var Signature
  53       */
  54      private $signature;
  55  
  56      /**
  57       * @internal This serves just as compatibility layer
  58       *
  59       * @var Factory
  60       */
  61      private $claimFactory;
  62  
  63      /**
  64       * Initializes the object
  65       *
  66       * @param array|DataSet $headers
  67       * @param array|DataSet $claims
  68       * @param Signature|null $signature
  69       * @param array $payload
  70       * @param Factory|null $claimFactory
  71       */
  72      public function __construct(
  73          $headers = ['alg' => 'none'],
  74          $claims = [],
  75          Signature $signature = null,
  76          array $payload = ['', ''],
  77          Factory $claimFactory = null
  78      ) {
  79          $this->headers = $this->convertToDataSet($headers, $payload[0]);
  80          $this->claims = $this->convertToDataSet($claims, $payload[1]);
  81          $this->signature = $signature ?: Signature::fromEmptyData();
  82          $this->claimFactory = $claimFactory ?: new Factory();
  83      }
  84  
  85      /**
  86       * @param array|DataSet $data
  87       * @param string $payload
  88       */
  89      private function convertToDataSet($data, $payload)
  90      {
  91          if ($data instanceof DataSet) {
  92              return $data;
  93          }
  94  
  95          return new DataSet($data, $payload);
  96      }
  97  
  98      /** @return DataSet */
  99      public function headers()
 100      {
 101          return $this->headers;
 102      }
 103  
 104      /**
 105       * Returns the token headers
 106       *
 107       * @deprecated This method has been removed from the interface in v4.0
 108       * @see Token::headers()
 109       *
 110       * @return array
 111       */
 112      public function getHeaders()
 113      {
 114          $items = [];
 115  
 116          foreach ($this->headers->all() as $name => $value) {
 117              if (! in_array($name, RegisteredClaims::ALL, true) || ! $this->claims->has($name)) {
 118                  $items[$name] = $value;
 119                  continue;
 120              }
 121  
 122              $items[$name] = $this->claimFactory->create($name, $value);
 123          }
 124  
 125          return $items;
 126      }
 127  
 128      /**
 129       * Returns if the header is configured
 130       *
 131       * @deprecated This method has been removed from the interface in v4.0
 132       * @see Token::headers()
 133       * @see DataSet::has()
 134       *
 135       * @param string $name
 136       *
 137       * @return boolean
 138       */
 139      public function hasHeader($name)
 140      {
 141          return $this->headers->has($name);
 142      }
 143  
 144      /**
 145       * Returns the value of a token header
 146       *
 147       * @deprecated This method has been removed from the interface in v4.0
 148       * @see Token::headers()
 149       * @see DataSet::has()
 150       *
 151       * @param string $name
 152       * @param mixed $default
 153       *
 154       * @return mixed
 155       *
 156       * @throws OutOfBoundsException
 157       */
 158      public function getHeader($name, $default = null)
 159      {
 160          if (func_num_args() === 1 && ! $this->headers->has($name)) {
 161              throw new OutOfBoundsException(sprintf('Requested header "%s" is not configured', $name));
 162          }
 163  
 164          return $this->headers->get($name, $default);
 165      }
 166  
 167      /** @return DataSet */
 168      public function claims()
 169      {
 170          return $this->claims;
 171      }
 172  
 173      /**
 174       * Returns the token claim set
 175       *
 176       * @deprecated This method has been removed from the interface in v4.0
 177       * @see Token::claims()
 178       *
 179       * @return array
 180       */
 181      public function getClaims()
 182      {
 183          $items = [];
 184  
 185          foreach ($this->claims->all() as $name => $value) {
 186              $items[$name] = $this->claimFactory->create($name, $value);
 187          }
 188  
 189          return $items;
 190      }
 191  
 192      /**
 193       * Returns if the claim is configured
 194       *
 195       * @deprecated This method has been removed from the interface in v4.0
 196       * @see Token::claims()
 197       * @see DataSet::has()
 198       *
 199       * @param string $name
 200       *
 201       * @return boolean
 202       */
 203      public function hasClaim($name)
 204      {
 205          return $this->claims->has($name);
 206      }
 207  
 208      /**
 209       * Returns the value of a token claim
 210       *
 211       * @deprecated This method has been removed from the interface in v4.0
 212       * @see Token::claims()
 213       * @see DataSet::get()
 214       *
 215       * @param string $name
 216       * @param mixed $default
 217       *
 218       * @return mixed
 219       *
 220       * @throws OutOfBoundsException
 221       */
 222      public function getClaim($name, $default = null)
 223      {
 224          if (func_num_args() === 1 && ! $this->claims->has($name)) {
 225              throw new OutOfBoundsException(sprintf('Requested header "%s" is not configured', $name));
 226          }
 227  
 228          $value = $this->claims->get($name, $default);
 229  
 230          if ($value instanceof DateTimeImmutable && in_array($name, RegisteredClaims::DATE_CLAIMS, true)) {
 231              return $value->getTimestamp();
 232          }
 233  
 234          if ($name === RegisteredClaims::AUDIENCE && is_array($value)) {
 235              if (count($value) > 1) {
 236                  trigger_error('You will only get the first array entry as a string. Use Token::claims()->get() instead.', E_USER_DEPRECATED);
 237              }
 238              return current($value);
 239          }
 240  
 241          return $value;
 242      }
 243  
 244      /**
 245       * Verify if the key matches with the one that created the signature
 246       *
 247       * @deprecated This method has been removed from the interface in v4.0
 248       * @see \Lcobucci\JWT\Validation\Validator
 249       *
 250       * @param Signer $signer
 251       * @param Key|string $key
 252       *
 253       * @return boolean
 254       */
 255      public function verify(Signer $signer, $key)
 256      {
 257          if ($this->headers->get('alg') !== $signer->getAlgorithmId()) {
 258              return false;
 259          }
 260  
 261          return $this->signature->verify($signer, $this->getPayload(), $key);
 262      }
 263  
 264      /**
 265       * Validates if the token is valid
 266       *
 267       * @deprecated This method has been removed from the interface in v4.0
 268       * @see \Lcobucci\JWT\Validation\Validator
 269       *
 270       * @param ValidationData $data
 271       *
 272       * @return boolean
 273       */
 274      public function validate(ValidationData $data)
 275      {
 276          foreach ($this->getValidatableClaims() as $claim) {
 277              if (!$claim->validate($data)) {
 278                  return false;
 279              }
 280          }
 281  
 282          return true;
 283      }
 284  
 285      /**
 286       * Determine if the token is expired.
 287       *
 288       * @param DateTimeInterface|null $now Defaults to the current time.
 289       *
 290       * @return bool
 291       */
 292      public function isExpired(DateTimeInterface $now = null)
 293      {
 294          if (! $this->claims->has('exp')) {
 295              return false;
 296          }
 297  
 298          if ($now === null) {
 299              trigger_error('Not providing the current time is deprecated. Please pass an instance of DateTimeInterface.', E_USER_DEPRECATED);
 300          }
 301  
 302          $now = $now ?: new DateTimeImmutable();
 303  
 304          return $now >= $this->claims->get(RegisteredClaims::EXPIRATION_TIME);
 305      }
 306  
 307      /**
 308       * @param string $audience
 309       *
 310       * @return bool
 311       */
 312      public function isPermittedFor($audience)
 313      {
 314          return in_array($audience, $this->claims->get(RegisteredClaims::AUDIENCE, []), true);
 315      }
 316  
 317      /**
 318       * @param string $id
 319       *
 320       * @return bool
 321       */
 322      public function isIdentifiedBy($id)
 323      {
 324          return $this->claims->get(RegisteredClaims::ID) === $id;
 325      }
 326  
 327      /**
 328       * @param string $subject
 329       *
 330       * @return bool
 331       */
 332      public function isRelatedTo($subject)
 333      {
 334          return $this->claims->get(RegisteredClaims::SUBJECT) === $subject;
 335      }
 336  
 337      /**
 338       * @param list<string> $issuers
 339       *
 340       * @return bool
 341       */
 342      public function hasBeenIssuedBy(...$issuers)
 343      {
 344          return in_array($this->claims->get(RegisteredClaims::ISSUER), $issuers, true);
 345      }
 346  
 347      /**
 348       * @param DateTimeInterface $now
 349       *
 350       * @return bool
 351       */
 352      public function hasBeenIssuedBefore(DateTimeInterface $now)
 353      {
 354          return $now >= $this->claims->get(RegisteredClaims::ISSUED_AT);
 355      }
 356  
 357      /**
 358       * @param DateTimeInterface $now
 359       *
 360       * @return bool
 361       */
 362      public function isMinimumTimeBefore(DateTimeInterface $now)
 363      {
 364          return $now >= $this->claims->get(RegisteredClaims::NOT_BEFORE);
 365      }
 366  
 367      /**
 368       * Yields the validatable claims
 369       *
 370       * @return Generator
 371       */
 372      private function getValidatableClaims()
 373      {
 374          foreach ($this->getClaims() as $claim) {
 375              if ($claim instanceof Validatable) {
 376                  yield $claim;
 377              }
 378          }
 379      }
 380  
 381      /**
 382       * Returns the token payload
 383       *
 384       * @deprecated This method has been removed from the interface in v4.0
 385       * @see Token::payload()
 386       *
 387       * @return string
 388       */
 389      public function getPayload()
 390      {
 391          return $this->payload();
 392      }
 393  
 394      /**
 395       * Returns the token payload
 396       *
 397       * @return string
 398       */
 399      public function payload()
 400      {
 401          return $this->headers->toString() . '.' . $this->claims->toString();
 402      }
 403  
 404      /** @return Signature */
 405      public function signature()
 406      {
 407          return $this->signature;
 408      }
 409  
 410      /**
 411       * Returns an encoded representation of the token
 412       *
 413       * @deprecated This method has been removed from the interface in v4.0
 414       * @see Token::toString()
 415       *
 416       * @return string
 417       */
 418      public function __toString()
 419      {
 420          return $this->toString();
 421      }
 422  
 423      /** @return string */
 424      public function toString()
 425      {
 426          return $this->headers->toString() . '.'
 427               . $this->claims->toString() . '.'
 428               . $this->signature->toString();
 429      }
 430  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer