Skip to content

Commit

Permalink
Make *_key_path config options not mandatory
Browse files Browse the repository at this point in the history
Logic fixes
  • Loading branch information
chalasr committed Jul 9, 2016
1 parent b2003fd commit 367ac72
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 33 deletions.
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($this->getKeyValidator())
->end()
->end()
->scalarNode('public_key_path')
->cannotBeEmpty()
->defaultNull()
->validate()
->ifString()
->then($this->getKeyValidator())
->end()
->end()
->scalarNode('pass_phrase')
->defaultValue('')
Expand Down Expand Up @@ -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;
};
}
}
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));
}
}

0 comments on commit 367ac72

Please sign in to comment.