- md5
- b2642c09f2bd709fcd2e790e1f274cc3
- sha1
- a3d0d562a9b2cf54b6e4a979e5859085c0299bf4
- sha256
- 2996ae5ba5858623201dc2a11fd84e58dbe67facb38064af2c7427f063604958
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
<?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 DateTimeImmutable;
use DateTimeInterface;
use Generator;
use Lcobucci\JWT\Claim\Factory;
use Lcobucci\JWT\Claim\Validatable;
use Lcobucci\JWT\Signer\Key;
use Lcobucci\JWT\Token\DataSet;
use Lcobucci\JWT\Token\RegisteredClaims;
use OutOfBoundsException;
use function current;
use function func_num_args;
use function in_array;
use function is_array;
use function sprintf;
use function trigger_error;
use const E_USER_DEPRECATED;
/**
* Basic structure of the JWT
*
* @author Luís Otávio Cobucci Oblonczyk <[email protected]>
* @since 0.1.0
*/
class Token
{
/**
* The token headers
*
* @var DataSet
*/
private $headers;
/**
* The token claim set
*
* @var DataSet
*/
private $claims;
/**
* The token signature
*
* @var Signature
*/
private $signature;
/**
* @internal This serves just as compatibility layer
*
* @var Factory
*/
private $claimFactory;
/**
* Initializes the object
*
* @param array|DataSet $headers
* @param array|DataSet $claims
* @param Signature|null $signature
* @param array $payload
* @param Factory|null $claimFactory
*/
public function __construct(
$headers = ['alg' => 'none'],
$claims = [],
Signature $signature = null,
array $payload = ['', ''],
Factory $claimFactory = null
) {
$this->headers = $this->convertToDataSet($headers, $payload[0]);
$this->claims = $this->convertToDataSet($claims, $payload[1]);
$this->signature = $signature ?: Signature::fromEmptyData();
$this->claimFactory = $claimFactory ?: new Factory();
}
/**
* @param array|DataSet $data
* @param string $payload
*/
private function convertToDataSet($data, $payload)
{
if ($data instanceof DataSet) {
return $data;
}
return new DataSet($data, $payload);
}
/** @return DataSet */
public function headers()
{
return $this->headers;
}
/**
* Returns the token headers
*
* @deprecated This method has been removed from the interface in v4.0
* @see Token::headers()
*
* @return array
*/
public function getHeaders()
{
$items = [];
foreach ($this->headers->all() as $name => $value) {
if (! in_array($name, RegisteredClaims::ALL, true) || ! $this->claims->has($name)) {
$items[$name] = $value;
continue;
}
$items[$name] = $this->claimFactory->create($name, $value);
}
return $items;
}
/**
* Returns if the header is configured
*
* @deprecated This method has been removed from the interface in v4.0
* @see Token::headers()
* @see DataSet::has()
*
* @param string $name
*
* @return boolean
*/
public function hasHeader($name)
{
return $this->headers->has($name);
}
/**
* Returns the value of a token header
*
* @deprecated This method has been removed from the interface in v4.0
* @see Token::headers()
* @see DataSet::has()
*
* @param string $name
* @param mixed $default
*
* @return mixed
*
* @throws OutOfBoundsException
*/
public function getHeader($name, $default = null)
{
if (func_num_args() === 1 && ! $this->headers->has($name)) {
throw new OutOfBoundsException(sprintf('Requested header "%s" is not configured', $name));
}
return $this->headers->get($name, $default);
}
/** @return DataSet */
public function claims()
{
return $this->claims;
}
/**
* Returns the token claim set
*
* @deprecated This method has been removed from the interface in v4.0
* @see Token::claims()
*
* @return array
*/
public function getClaims()
{
$items = [];
foreach ($this->claims->all() as $name => $value) {
$items[$name] = $this->claimFactory->create($name, $value);
}
return $items;
}
/**
* Returns if the claim is configured
*
* @deprecated This method has been removed from the interface in v4.0
* @see Token::claims()
* @see DataSet::has()
*
* @param string $name
*
* @return boolean
*/
public function hasClaim($name)
{
return $this->claims->has($name);
}
/**
* Returns the value of a token claim
*
* @deprecated This method has been removed from the interface in v4.0
* @see Token::claims()
* @see DataSet::get()
*
* @param string $name
* @param mixed $default
*
* @return mixed
*
* @throws OutOfBoundsException
*/
public function getClaim($name, $default = null)
{
if (func_num_args() === 1 && ! $this->claims->has($name)) {
throw new OutOfBoundsException(sprintf('Requested header "%s" is not configured', $name));
}
$value = $this->claims->get($name, $default);
if ($value instanceof DateTimeImmutable && in_array($name, RegisteredClaims::DATE_CLAIMS, true)) {
return $value->getTimestamp();
}
if ($name === RegisteredClaims::AUDIENCE && is_array($value)) {
if (count($value) > 1) {
trigger_error('You will only get the first array entry as a string. Use Token::claims()->get() instead.', E_USER_DEPRECATED);
}
return current($value);
}
return $value;
}
/**
* Verify if the key matches with the one that created the signature
*
* @deprecated This method has been removed from the interface in v4.0
* @see \Lcobucci\JWT\Validation\Validator
*
* @param Signer $signer
* @param Key|string $key
*
* @return boolean
*/
public function verify(Signer $signer, $key)
{
if ($this->headers->get('alg') !== $signer->getAlgorithmId()) {
return false;
}
return $this->signature->verify($signer, $this->getPayload(), $key);
}
/**
* Validates if the token is valid
*
* @deprecated This method has been removed from the interface in v4.0
* @see \Lcobucci\JWT\Validation\Validator
*
* @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|null $now Defaults to the current time.
*
* @return bool
*/
public function isExpired(DateTimeInterface $now = null)
{
if (! $this->claims->has('exp')) {
return false;
}
if ($now === null) {
trigger_error('Not providing the current time is deprecated. Please pass an instance of DateTimeInterface.', E_USER_DEPRECATED);
}
$now = $now ?: new DateTimeImmutable();
return $now >= $this->claims->get(RegisteredClaims::EXPIRATION_TIME);
}
/**
* @param string $audience
*
* @return bool
*/
public function isPermittedFor($audience)
{
return in_array($audience, $this->claims->get(RegisteredClaims::AUDIENCE, []), true);
}
/**
* @param string $id
*
* @return bool
*/
public function isIdentifiedBy($id)
{
return $this->claims->get(RegisteredClaims::ID) === $id;
}
/**
* @param string $subject
*
* @return bool
*/
public function isRelatedTo($subject)
{
return $this->claims->get(RegisteredClaims::SUBJECT) === $subject;
}
/**
* @param list<string> $issuers
*
* @return bool
*/
public function hasBeenIssuedBy(...$issuers)
{
return in_array($this->claims->get(RegisteredClaims::ISSUER), $issuers, true);
}
/**
* @param DateTimeInterface $now
*
* @return bool
*/
public function hasBeenIssuedBefore(DateTimeInterface $now)
{
return $now >= $this->claims->get(RegisteredClaims::ISSUED_AT);
}
/**
* @param DateTimeInterface $now
*
* @return bool
*/
public function isMinimumTimeBefore(DateTimeInterface $now)
{
return $now >= $this->claims->get(RegisteredClaims::NOT_BEFORE);
}
/**
* Yields the validatable claims
*
* @return Generator
*/
private function getValidatableClaims()
{
foreach ($this->getClaims() as $claim) {
if ($claim instanceof Validatable) {
yield $claim;
}
}
}
/**
* Returns the token payload
*
* @deprecated This method has been removed from the interface in v4.0
* @see Token::payload()
*
* @return string
*/
public function getPayload()
{
return $this->payload();
}
/**
* Returns the token payload
*
* @return string
*/
public function payload()
{
return $this->headers->toString() . '.' . $this->claims->toString();
}
/** @return Signature */
public function signature()
{
return $this->signature;
}
/**
* Returns an encoded representation of the token
*
* @deprecated This method has been removed from the interface in v4.0
* @see Token::toString()
*
* @return string
*/
public function __toString()
{
return $this->toString();
}
/** @return string */
public function toString()
{
return $this->headers->toString() . '.'
. $this->claims->toString() . '.'
. $this->signature->toString();
}
}
The same content is in 75 releases, Joomla 4.2.0-beta3 to 4.4.14. See them all, or every version of this file.