Skip to content

[Configuration] Make *_key_path config options not mandatory #196

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 9, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,18 @@ public function getConfigTreeBuilder()
->addDefaultsIfNotSet()
->children()
->scalarNode('private_key_path')
->cannotBeEmpty()
->defaultNull()
->validate()
->ifString()
->then(self::validateKeyPath())
->end()
->end()
->scalarNode('public_key_path')
->cannotBeEmpty()
->defaultNull()
->validate()
->ifString()
->then(self::validateKeyPath())
->end()
->end()
->scalarNode('pass_phrase')
->defaultValue('')
Expand Down Expand Up @@ -59,4 +67,15 @@ public function getConfigTreeBuilder()

return $treeBuilder;
}

private static function validateKeyPath()
{
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;
};
}
}
36 changes: 19 additions & 17 deletions Services/KeyLoader/AbstractKeyLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
*/
abstract class AbstractKeyLoader implements KeyLoaderInterface
{
const TYPE_PUBLIC = 'public';
const TYPE_PRIVATE = 'private';

/**
* @var string
*/
Expand Down Expand Up @@ -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;
}
}
9 changes: 2 additions & 7 deletions Services/KeyLoader/OpenSSLKeyLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
8 changes: 1 addition & 7 deletions Services/KeyLoader/SecLibKeyLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}