Skip to content

Commit

Permalink
Don't register lcobucci encoder if lcobucci/jwt is not installed
Browse files Browse the repository at this point in the history
  • Loading branch information
chalasr committed Sep 2, 2017
1 parent 7f21311 commit 1566b77
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
7 changes: 7 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Lexik\Bundle\JWTAuthenticationBundle\DependencyInjection;

use Lcobucci\JWT\Token;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

Expand Down Expand Up @@ -40,6 +41,12 @@ public function getConfigTreeBuilder()
->children()
->scalarNode('service')
->defaultValue('lexik_jwt_authentication.encoder.default')
->validate()
->ifTrue(function ($encoder) {
return 'lexik_jwt_authentication.encoder.lcobucci' === $encoder && !class_exists(Token::class);
})
->thenInvalid('You must require "lcobucci/jwt" in your composer.json for using the lcobucci encoder.')
->end()
->end()
->scalarNode('signature_algorithm')
->defaultValue('RS256')
Expand Down
6 changes: 6 additions & 0 deletions DependencyInjection/LexikJWTAuthenticationExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Lexik\Bundle\JWTAuthenticationBundle\DependencyInjection;

use Lcobucci\JWT\Token;
use Lexik\Bundle\JWTAuthenticationBundle\Encoder\JWTEncoderInterface;
use Lexik\Bundle\JWTAuthenticationBundle\Services\JWSProvider\JWSProviderInterface;
use Lexik\Bundle\JWTAuthenticationBundle\Services\JWTManagerInterface;
Expand Down Expand Up @@ -31,6 +32,11 @@ public function load(array $configs, ContainerBuilder $container)
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.xml');

if (!class_exists(Token::class)) {
$container->removeDefinition('lexik_jwt_authentication.encoder.lcobucci');
$container->removeDefinition('lexik_jwt_authentication.jws_provider.lcobucci');
}

$container->setParameter('lexik_jwt_authentication.private_key_path', $config['private_key_path']);
$container->setParameter('lexik_jwt_authentication.public_key_path', $config['public_key_path']);
$container->setParameter('lexik_jwt_authentication.pass_phrase', $config['pass_phrase']);
Expand Down
1 change: 1 addition & 0 deletions Services/KeyLoader/OpenSSLKeyLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public function loadKey($type)
}
$sslError .= "\n ".$msg;
}

throw new \RuntimeException(
sprintf('Failed to load %s key "%s": %s', $type, $path, $sslError)
);
Expand Down

0 comments on commit 1566b77

Please sign in to comment.