diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 27fe9150..09b756f5 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -24,10 +24,18 @@ public function getConfigTreeBuilder() ->addDefaultsIfNotSet() ->children() ->scalarNode('private_key_path') - ->cannotBeEmpty() + ->defaultNull() + ->validate() + ->ifString() + ->then($this->getKeyValidator()) + ->end() ->end() ->scalarNode('public_key_path') - ->cannotBeEmpty() + ->defaultNull() + ->validate() + ->ifString() + ->then($this->getKeyValidator()) + ->end() ->end() ->scalarNode('pass_phrase') ->defaultValue('') @@ -59,4 +67,15 @@ public function getConfigTreeBuilder() return $treeBuilder; } + + public function getKeyValidator() + { + return function ($path) { + if (!is_file($path) || !is_readable($path)) { + throw new \InvalidArgumentException(sprintf('The file "%s" doesn\'t exist or is not readable.%sIf the configured encoder doesn\'t need this to be configured, please don\'t set this option or leave it null.', $path, PHP_EOL)); + } + + return $path; + }; + } } diff --git a/Services/KeyLoader/AbstractKeyLoader.php b/Services/KeyLoader/AbstractKeyLoader.php index 308f5d33..d6211e78 100644 --- a/Services/KeyLoader/AbstractKeyLoader.php +++ b/Services/KeyLoader/AbstractKeyLoader.php @@ -9,6 +9,9 @@ */ abstract class AbstractKeyLoader implements KeyLoaderInterface { + const TYPE_PUBLIC = 'public'; + const TYPE_PRIVATE = 'private'; + /** * @var string */ @@ -55,27 +58,26 @@ public function getPassphrase() */ protected function getKeyPath($type) { - if ('public' === $type) { - return $this->publicKey; + if (!in_array($type, [self::TYPE_PUBLIC, self::TYPE_PRIVATE])) { + throw new \InvalidArgumentException(sprintf('The key type must be "public" or "private", "%s" given.', $type)); } - if ('private' === $type) { - return $this->privateKey; + $path = null; + + if (self::TYPE_PUBLIC === $type) { + $path = $this->publicKey; } - throw new \InvalidArgumentException(sprintf('The key type must be "public" or "private", "%s" given.', $type)); - } + if (self::TYPE_PRIVATE === $type) { + $path = $this->privateKey; + } - /** - * @param string $type The key type - * @param string $path The key path - * - * @throws \RuntimeException - */ - protected function createUnreadableKeyException($type, $path) - { - return new \RuntimeException( - sprintf('%s key "%s" does not exist or is not readable. Did you correctly set the "lexik_jwt_authentication.jwt_%s_key_path" config option?', ucfirst($type), $path, $type) - ); + if (!is_file($path) || !is_readable($path)) { + throw new \RuntimeException( + sprintf('%s key "%s" does not exist or is not readable. Did you correctly set the "lexik_jwt_authentication.jwt_%s_key_path" config option?', ucfirst($type), $path, $type) + ); + } + + return $path; } } diff --git a/Services/KeyLoader/OpenSSLKeyLoader.php b/Services/KeyLoader/OpenSSLKeyLoader.php index 425c96ec..837c350a 100644 --- a/Services/KeyLoader/OpenSSLKeyLoader.php +++ b/Services/KeyLoader/OpenSSLKeyLoader.php @@ -17,16 +17,11 @@ class OpenSSLKeyLoader extends AbstractKeyLoader */ public function loadKey($type) { - $path = $this->getKeyPath($type); - - if (!file_exists($path) || !is_readable($path)) { - throw $this->createUnreadableKeyException($type, $path); - } - + $path = $this->getKeyPath($type); $encryptedKey = file_get_contents($path); $key = call_user_func_array( sprintf('openssl_pkey_get_%s', $type), - $type == 'private' ? [$encryptedKey, $this->getPassphrase()] : [$encryptedKey] + self::TYPE_PRIVATE == $type ? [$encryptedKey, $this->getPassphrase()] : [$encryptedKey] ); if (!$key) { diff --git a/Services/KeyLoader/SecLibKeyLoader.php b/Services/KeyLoader/SecLibKeyLoader.php index 6a46c53a..b0db5954 100644 --- a/Services/KeyLoader/SecLibKeyLoader.php +++ b/Services/KeyLoader/SecLibKeyLoader.php @@ -16,12 +16,6 @@ class SecLibKeyLoader extends AbstractKeyLoader */ public function loadKey($type) { - $path = $this->getKeyPath($type); - - if (!file_exists($path) || !is_readable($path)) { - throw $this->createUnreadableKeyException($type, $path); - } - - return file_get_contents($path); + return file_get_contents($this->getKeyPath($type)); } }