vendor/lexik/jwt-authentication-bundle/Services/JWSProvider/DefaultJWSProvider.php line 5

Open in your IDE?
  1. <?php
  2. namespace Lexik\Bundle\JWTAuthenticationBundle\Services\JWSProvider;
  3. @trigger_error(sprintf('The "%s" class is deprecated since version 2.5 and will be removed in 3.0. Use "%s" or create your own "%s" implementation instead.'DefaultJWSProvider::class, LcobucciJWSProvider::class, JWSProviderInterface::class), E_USER_DEPRECATED);
  4. use Lexik\Bundle\JWTAuthenticationBundle\Services\KeyLoader\KeyLoaderInterface;
  5. use Lexik\Bundle\JWTAuthenticationBundle\Signature\CreatedJWS;
  6. use Lexik\Bundle\JWTAuthenticationBundle\Signature\LoadedJWS;
  7. use Namshi\JOSE\JWS;
  8. /**
  9.  * JWS Provider, Namshi\JOSE library integration.
  10.  * Supports OpenSSL and phpseclib crypto engines.
  11.  *
  12.  * @final
  13.  *
  14.  * @author Robin Chalas <robin.chalas@gmail.com>
  15.  *
  16.  * @deprecated since version 2.5, to be removed in 3.0
  17.  */
  18. class DefaultJWSProvider implements JWSProviderInterface
  19. {
  20.     /**
  21.      * @var KeyLoaderInterface
  22.      */
  23.     private $keyLoader;
  24.     /**
  25.      * @var string
  26.      */
  27.     private $cryptoEngine;
  28.     /**
  29.      * @var string
  30.      */
  31.     private $signatureAlgorithm;
  32.     /**
  33.      * @var int
  34.      */
  35.     private $ttl;
  36.     /**
  37.      * @var int
  38.      */
  39.     private $clockSkew;
  40.     /**
  41.      * @param string $cryptoEngine
  42.      * @param string $signatureAlgorithm
  43.      * @param int    $ttl
  44.      * @param int    $clockSkew
  45.      *
  46.      * @throws \InvalidArgumentException If the given algorithm is not supported
  47.      */
  48.     public function __construct(KeyLoaderInterface $keyLoader$cryptoEngine$signatureAlgorithm$ttl$clockSkew)
  49.     {
  50.         if (null !== $ttl && !is_numeric($ttl)) {
  51.             throw new \InvalidArgumentException(sprintf('The TTL should be a numeric value, got %s instead.'$ttl));
  52.         }
  53.         if (null !== $clockSkew && !is_numeric($clockSkew)) {
  54.             throw new \InvalidArgumentException(sprintf('The clock skew should be a numeric value, got %s instead.'$clockSkew));
  55.         }
  56.         $cryptoEngine 'openssl' == $cryptoEngine 'OpenSSL' 'SecLib';
  57.         if (!$this->isAlgorithmSupportedForEngine($cryptoEngine$signatureAlgorithm)) {
  58.             throw new \InvalidArgumentException(sprintf('The algorithm "%s" is not supported for %s'$signatureAlgorithm$cryptoEngine));
  59.         }
  60.         $this->keyLoader $keyLoader;
  61.         $this->cryptoEngine $cryptoEngine;
  62.         $this->signatureAlgorithm $signatureAlgorithm;
  63.         $this->ttl $ttl;
  64.         $this->clockSkew $clockSkew;
  65.     }
  66.     /**
  67.      * {@inheritdoc}
  68.      */
  69.     public function create(array $payload, array $header = [])
  70.     {
  71.         $header['alg'] = $this->signatureAlgorithm;
  72.         $jws = new JWS($header$this->cryptoEngine);
  73.         $claims = ['iat' => time()];
  74.         if (null !== $this->ttl && !isset($payload['exp'])) {
  75.             $claims['exp'] = time() + $this->ttl;
  76.         }
  77.         $jws->setPayload($payload $claims);
  78.         $jws->sign(
  79.             $this->keyLoader->loadKey('private'),
  80.             $this->keyLoader->getPassphrase()
  81.         );
  82.         return new CreatedJWS($jws->getTokenString(), $jws->isSigned());
  83.     }
  84.     /**
  85.      * {@inheritdoc}
  86.      */
  87.     public function load($token)
  88.     {
  89.         $jws JWS::load($tokenfalsenull$this->cryptoEngine);
  90.         return new LoadedJWS(
  91.             $jws->getPayload(),
  92.             $jws->verify($this->keyLoader->loadKey('public'), $this->signatureAlgorithm),
  93.             null !== $this->ttl,
  94.             $jws->getHeader(),
  95.             $this->clockSkew
  96.         );
  97.     }
  98.     /**
  99.      * @param string $cryptoEngine
  100.      * @param string $signatureAlgorithm
  101.      *
  102.      * @return bool
  103.      */
  104.     private function isAlgorithmSupportedForEngine($cryptoEngine$signatureAlgorithm)
  105.     {
  106.         $signerClass sprintf('Namshi\\JOSE\\Signer\\%s\\%s'$cryptoEngine$signatureAlgorithm);
  107.         return class_exists($signerClass);
  108.     }
  109. }