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 KeyLoaderInterface $keyLoader
  42.      * @param string             $cryptoEngine
  43.      * @param string             $signatureAlgorithm
  44.      * @param int                $ttl
  45.      * @param int                $clockSkew
  46.      *
  47.      * @throws \InvalidArgumentException If the given algorithm is not supported
  48.      */
  49.     public function __construct(KeyLoaderInterface $keyLoader$cryptoEngine$signatureAlgorithm$ttl$clockSkew)
  50.     {
  51.         if (null !== $ttl && !is_numeric($ttl)) {
  52.             throw new \InvalidArgumentException(sprintf('The TTL should be a numeric value, got %s instead.'$ttl));
  53.         }
  54.         if (null !== $clockSkew && !is_numeric($clockSkew)) {
  55.             throw new \InvalidArgumentException(sprintf('The clock skew should be a numeric value, got %s instead.'$clockSkew));
  56.         }
  57.         $cryptoEngine 'openssl' == $cryptoEngine 'OpenSSL' 'SecLib';
  58.         if (!$this->isAlgorithmSupportedForEngine($cryptoEngine$signatureAlgorithm)) {
  59.             throw new \InvalidArgumentException(
  60.                 sprintf('The algorithm "%s" is not supported for %s'$signatureAlgorithm$cryptoEngine)
  61.             );
  62.         }
  63.         $this->keyLoader          $keyLoader;
  64.         $this->cryptoEngine       $cryptoEngine;
  65.         $this->signatureAlgorithm $signatureAlgorithm;
  66.         $this->ttl                $ttl;
  67.         $this->clockSkew          $clockSkew;
  68.     }
  69.     /**
  70.      * {@inheritdoc}
  71.      */
  72.     public function create(array $payload, array $header = [])
  73.     {
  74.         $header['alg'] = $this->signatureAlgorithm;
  75.         $jws           = new JWS($header$this->cryptoEngine);
  76.         $claims        = ['iat' => time()];
  77.         if (null !== $this->ttl && !isset($payload['exp'])) {
  78.             $claims['exp'] = time() + $this->ttl;
  79.         }
  80.         $jws->setPayload($payload $claims);
  81.         $jws->sign(
  82.             $this->keyLoader->loadKey('private'),
  83.             $this->keyLoader->getPassphrase()
  84.         );
  85.         return new CreatedJWS($jws->getTokenString(), $jws->isSigned());
  86.     }
  87.     /**
  88.      * {@inheritdoc}
  89.      */
  90.     public function load($token)
  91.     {
  92.         $jws JWS::load($tokenfalsenull$this->cryptoEngine);
  93.         return new LoadedJWS(
  94.             $jws->getPayload(),
  95.             $jws->verify($this->keyLoader->loadKey('public'), $this->signatureAlgorithm),
  96.             null !== $this->ttl,
  97.             $jws->getHeader(),
  98.             $this->clockSkew
  99.         );
  100.     }
  101.     /**
  102.      * @param string $cryptoEngine
  103.      * @param string $signatureAlgorithm
  104.      *
  105.      * @return bool
  106.      */
  107.     private function isAlgorithmSupportedForEngine($cryptoEngine$signatureAlgorithm)
  108.     {
  109.         $signerClass sprintf('Namshi\\JOSE\\Signer\\%s\\%s'$cryptoEngine$signatureAlgorithm);
  110.         return class_exists($signerClass);
  111.     }
  112. }