vendor/lexik/jwt-authentication-bundle/Services/KeyLoader/OpenSSLKeyLoader.php line 5

Open in your IDE?
  1. <?php
  2. namespace Lexik\Bundle\JWTAuthenticationBundle\Services\KeyLoader;
  3. @trigger_error(sprintf('The "%s\OpenSSLKeyLoader" class is deprecated since version 2.5 and will be removed in 3.0. Use "%s" instead.'__NAMESPACE__RawKeyLoader::class), E_USER_DEPRECATED);
  4. /**
  5.  * Load crypto keys for the OpenSSL crypto engine.
  6.  *
  7.  * @author Robin Chalas <robin.chalas@gmail.com>
  8.  *
  9.  * @deprecated since version 2.5, to be removed in 3.0. Use RawKeyLoader instead
  10.  */
  11. class OpenSSLKeyLoader extends AbstractKeyLoader implements KeyDumperInterface
  12. {
  13.     /**
  14.      * {@inheritdoc}
  15.      *
  16.      * @throws \RuntimeException If the key cannot be read
  17.      * @throws \RuntimeException Either the key or the passphrase is not valid
  18.      */
  19.     public function loadKey($type)
  20.     {
  21.         if (!in_array($type, [self::TYPE_PUBLICself::TYPE_PRIVATE])) {
  22.             throw new \InvalidArgumentException(sprintf('The key type must be "public" or "private", "%s" given.'$type));
  23.         }
  24.         $keyPath $this->getKeyPath($type);
  25.         $rawKey = @file_get_contents($keyPath);
  26.         if (false === $rawKey) {
  27.             // Try invalidating the realpath cache
  28.             clearstatcache(true$keyPath);
  29.             $rawKey file_get_contents($keyPath);
  30.         }
  31.         $key call_user_func_array("openssl_pkey_get_$type"self::TYPE_PRIVATE == $type ? [$rawKey$this->getPassphrase()] : [$rawKey]);
  32.         if (!$key) {
  33.             $sslError '';
  34.             while ($msg trim(openssl_error_string(), " \n\r\t\0\x0B\"")) {
  35.                 if ('error:' === substr($msg06)) {
  36.                     $msg substr($msg6);
  37.                 }
  38.                 $sslError .= "\n $msg";
  39.             }
  40.             throw new \RuntimeException(sprintf('Failed to load %s key: %s'$type$sslError));
  41.         }
  42.         return $key;
  43.     }
  44.     /**
  45.      * {@inheritdoc}
  46.      */
  47.     public function dumpKey()
  48.     {
  49.         $key openssl_pkey_get_details($this->loadKey('public'));
  50.         if (!isset($key['key'])) {
  51.             return;
  52.         }
  53.         return $key['key'];
  54.     }
  55. }