Skip to content

Commit

Permalink
Remove JWGuardFactory, create config token_extractors option
Browse files Browse the repository at this point in the history
CS Fixes
  • Loading branch information
chalasr committed Jun 30, 2016
1 parent 5dbae80 commit 7a46f92
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 132 deletions.
41 changes: 41 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,47 @@ public function getConfigTreeBuilder()
->end()
->end()
->end()
->arrayNode('token_extractors')
->addDefaultsIfNotSet()
->children()
->arrayNode('authorization_header')
->addDefaultsIfNotSet()
->children()
->booleanNode('enabled')
->defaultTrue()
->end()
->scalarNode('prefix')
->defaultValue('Bearer')
->end()
->scalarNode('name')
->defaultValue('Authorization')
->end()
->end()
->end()
->arrayNode('cookie')
->addDefaultsIfNotSet()
->children()
->booleanNode('enabled')
->defaultFalse()
->end()
->scalarNode('name')
->defaultValue('BEARER')
->end()
->end()
->end()
->arrayNode('query_parameter')
->addDefaultsIfNotSet()
->children()
->booleanNode('enabled')
->defaultFalse()
->end()
->scalarNode('name')
->defaultValue('bearer')
->end()
->end()
->end()
->end()
->end()
->scalarNode('user_identity_field')
->defaultValue('username')
->cannotBeEmpty()
Expand Down
40 changes: 38 additions & 2 deletions DependencyInjection/LexikJWTAuthenticationExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;

/**
Expand All @@ -22,7 +23,7 @@ public function load(array $configs, ContainerBuilder $container)
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);

$loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.xml');

$container->setParameter('lexik_jwt_authentication.private_key_path', $config['private_key_path']);
Expand All @@ -39,5 +40,40 @@ public function load(array $configs, ContainerBuilder $container)
);
$container->setParameter('lexik_jwt_authentication.encoder.encryption_algorithm', $encoderConfig['encryption_algorithm']);
$container->setParameter('lexik_jwt_authentication.encoder.encryption_engine', $encoderConfig['encryption_engine']);

// JWTTokenAuthenticator (Guard)
$jwtAuthenticatorId = 'lexik_jwt_authentication.security.guard.jwt_token_authenticator';
$jwtAuthenticator = $container->getDefinition($jwtAuthenticatorId);
$extractorsConfig = $config['token_extractors'];

if ($extractorsConfig['authorization_header']['enabled']) {
$authorizationHeaderExtractorId = 'lexik_jwt_authentication.extractor.authorization_header_extractor';
$container
->getDefinition($authorizationHeaderExtractorId)
->replaceArgument(0, $extractorsConfig['authorization_header']['prefix'])
->replaceArgument(1, $extractorsConfig['authorization_header']['name']);

$jwtAuthenticator->addMethodCall('addTokenExtractor', [new Reference($authorizationHeaderExtractorId)]);
}

if ($extractorsConfig['query_parameter']['enabled']) {
$queryParameterExtractorId = 'lexik_jwt_authentication.extractor.query_parameter_extractor';
$container
->getDefinition($queryParameterExtractorId)
->replaceArgument(0, $extractorsConfig['query_parameter']['name']);

$jwtAuthenticator->addMethodCall('addTokenExtractor', [new Reference($queryParameterExtractorId)]);
}

if ($extractorsConfig['cookie']['enabled']) {
$cookieExtractorId = 'lexik_jwt_authentication.extractor.cookie_extractor';
$container
->getDefinition($cookieExtractorId)
->replaceArgument(0, $extractorsConfig['cookie']['name']);

$jwtAuthenticator->addMethodCall('addTokenExtractor', [new Reference($cookieExtractorId)]);
}

$container->setAlias('lexik_jwt_authentication.jwt_token_authenticator', $jwtAuthenticatorId);
}
}
112 changes: 0 additions & 112 deletions DependencyInjection/Security/Factory/JWTGuardAuthenticationFactory.php

This file was deleted.

2 changes: 0 additions & 2 deletions LexikJWTAuthenticationBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Lexik\Bundle\JWTAuthenticationBundle;

use Lexik\Bundle\JWTAuthenticationBundle\DependencyInjection\Security\Factory\JWTFactory;
use Lexik\Bundle\JWTAuthenticationBundle\DependencyInjection\Security\Factory\JWTGuardAuthenticationFactory;
use Symfony\Bundle\SecurityBundle\DependencyInjection\SecurityExtension;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;
Expand All @@ -25,6 +24,5 @@ public function build(ContainerBuilder $container)
/** @var SecurityExtension $extension */
$extension = $container->getExtension('security');
$extension->addSecurityListenerFactory(new JWTFactory());
$extension->addSecurityListenerFactory(new JWTGuardAuthenticationFactory());
}
}
2 changes: 1 addition & 1 deletion Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
<argument>%lexik_jwt_authentication.encoder.encryption_algorithm%</argument>
</service>
<!-- JsonWebToken Authenticator -->
<service id="lexik_jwt_authentication.security.guard.jwt_authenticator" class="Lexik\Bundle\JWTAuthenticationBundle\Security\Guard\JWTTokenAuthenticator">
<service id="lexik_jwt_authentication.security.guard.jwt_token_authenticator" class="Lexik\Bundle\JWTAuthenticationBundle\Security\Guard\JWTTokenAuthenticator">
<argument type="service" id="lexik_jwt_authentication.encoder"/>
<argument type="service" id="event_dispatcher"/>
</service>
Expand Down
21 changes: 6 additions & 15 deletions Security/Guard/JWTTokenAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,6 @@
* # ...
* </code>
*
* Or if your firewall has only one guard authenticator, use:
*
* <code>
* firewalls:
* api:
* lexik_jwt_guard: ~
* # ...
* </code>
*
* Thanks Ryan Weaver (@weaverryan) for having shown us the way after introduced the component.
*
* @see http://knpuniversity.com/screencast/symfony-rest4/jwt-guard-authenticator
Expand All @@ -54,17 +45,17 @@ class JWTTokenAuthenticator extends AbstractGuardAuthenticator
/**
* @var JWTEncoderInterface
*/
protected $encoder;
private $encoder;

/**
* @var EventDispatcherInterface
*/
protected $dispatcher;
private $dispatcher;

/**
* @var TokenExtractorInterface[]
*/
protected $tokenExtractors = [];
private $tokenExtractors = [];

/**
* @param JWTEncoderInterface $encoder
Expand All @@ -88,7 +79,7 @@ public function start(Request $request, AuthenticationException $authException =
*/
public function getCredentials(Request $request)
{
if (false === ($jsonWebToken = $this->extractToken($request))) {
if (false === ($jsonWebToken = $this->getTokenFromRequest($request))) {
return;
}

Expand Down Expand Up @@ -162,10 +153,10 @@ public function addTokenExtractor(TokenExtractorInterface $extractor)
*
* @return bool|string
*/
protected function extractToken(Request $request)
private function getTokenFromRequest(Request $request)
{
foreach ($this->tokenExtractors as $extractor) {
if (($token = $extractor->extract($request))) {
if ($token = $extractor->extract($request)) {
return $token;
}
}
Expand Down

0 comments on commit 7a46f92

Please sign in to comment.