Skip to content

Commit

Permalink
Merge pull request #70 from lexik/deprecated-fix
Browse files Browse the repository at this point in the history
fixed deprecated errors for symfony 2.6 plus
  • Loading branch information
slashfan committed Jun 5, 2015
2 parents 53ad877 + 455ad57 commit 12065ad
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 15 deletions.
13 changes: 13 additions & 0 deletions DependencyInjection/LexikJWTAuthenticationExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,19 @@ public function load(array $configs, ContainerBuilder $container)
$container->setParameter('lexik_jwt_authentication.user_identity_field', $config['user_identity_field']);

$container->setAlias('lexik_jwt_authentication.encoder', $config['encoder_service']);

// thanks https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/DependencyInjection/FOSUserExtension.php

if (interface_exists('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface')) {
$tokenStorageReference = new Reference('security.token_storage');
} else {
$tokenStorageReference = new Reference('security.context');
}

$container
->getDefinition('lexik_jwt_authentication.security.authentication.listener')
->replaceArgument(0, $tokenStorageReference)
;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
</service>
<!-- JWT Security Authentication Listener -->
<service id="lexik_jwt_authentication.security.authentication.listener" class="%lexik_jwt_authentication.security.authentication.listener.class%" public="false">
<argument type="service" id="security.context"/>
<argument /> <!-- security.token_storage or security.context for Symfony <2.6 -->
<argument type="service" id="security.authentication.manager" />
<argument /> <!-- Options -->
</service>
Expand Down
21 changes: 13 additions & 8 deletions Security/Firewall/JWTListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\SecurityContextInterface;
use Symfony\Component\Security\Http\Firewall\ListenerInterface;
Expand All @@ -20,9 +21,9 @@
class JWTListener implements ListenerInterface
{
/**
* @var SecurityContextInterface
* @var SecurityContextInterface|TokenStorageInterface
*/
protected $securityContext;
protected $tokenStorage;

/**
* @var AuthenticationManagerInterface
Expand All @@ -40,13 +41,17 @@ class JWTListener implements ListenerInterface
protected $tokenExtractors;

/**
* @param SecurityContextInterface $securityContext
* @param AuthenticationManagerInterface $authenticationManager
* @param array $config
* @param SecurityContextInterface|TokenStorageInterface $tokenStorage
* @param AuthenticationManagerInterface $authenticationManager
* @param array $config
*/
public function __construct(SecurityContextInterface $securityContext, AuthenticationManagerInterface $authenticationManager, array $config = array())
public function __construct($tokenStorage, AuthenticationManagerInterface $authenticationManager, array $config = array())
{
$this->securityContext = $securityContext;
if (!$tokenStorage instanceof TokenStorageInterface && !$tokenStorage instanceof SecurityContextInterface) {
throw new \InvalidArgumentException('Argument 1 should be an instance of Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface or Symfony\Component\Security\Core\SecurityContextInterface');
}

$this->tokenStorage = $tokenStorage;
$this->authenticationManager = $authenticationManager;
$this->config = array_merge(array('throw_exceptions' => false), $config);
$this->tokenExtractors = array();
Expand All @@ -67,7 +72,7 @@ public function handle(GetResponseEvent $event)
try {

$authToken = $this->authenticationManager->authenticate($token);
$this->securityContext->setToken($authToken);
$this->tokenStorage->setToken($authToken);

return;

Expand Down
18 changes: 12 additions & 6 deletions Tests/Security/Authentication/Firewall/JWTListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ public function testHandle()
{
// no token extractor : should return void

$listener = new JWTListener($this->getSecurityContextMock(), $this->getAuthenticationManagerMock());
$listener = new JWTListener($this->getTokenStorageMock(), $this->getAuthenticationManagerMock());
$this->assertNull($listener->handle($this->getEvent()));

// one token extractor with no result : should return void

$listener = new JWTListener($this->getSecurityContextMock(), $this->getAuthenticationManagerMock());
$listener = new JWTListener($this->getTokenStorageMock(), $this->getAuthenticationManagerMock());
$listener->addTokenExtractor($this->getAuthorizationHeaderTokenExtractorMock(false));
$this->assertNull($listener->handle($this->getEvent()));

Expand All @@ -32,7 +32,7 @@ public function testHandle()
$authenticationManager = $this->getAuthenticationManagerMock();
$authenticationManager->expects($this->once())->method('authenticate');

$listener = new JWTListener($this->getSecurityContextMock(), $authenticationManager);
$listener = new JWTListener($this->getTokenStorageMock(), $authenticationManager);
$listener->addTokenExtractor($this->getAuthorizationHeaderTokenExtractorMock('token'));
$listener->handle($this->getEvent());

Expand All @@ -47,7 +47,7 @@ public function testHandle()
->method('authenticate')
->will($this->throwException(new \Symfony\Component\Security\Core\Exception\AuthenticationException()));

$listener = new JWTListener($this->getSecurityContextMock(), $authenticationManager);
$listener = new JWTListener($this->getTokenStorageMock(), $authenticationManager);
$listener->addTokenExtractor($this->getAuthorizationHeaderTokenExtractorMock('token'));

$event = $this->getEvent();
Expand All @@ -70,10 +70,16 @@ public function getAuthenticationManagerMock()
/**
* @return \PHPUnit_Framework_MockObject_MockObject
*/
public function getSecurityContextMock()
public function getTokenStorageMock()
{
if (interface_exists('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface')) {
$class = 'Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface';
} else {
$class = 'Symfony\Component\Security\Core\SecurityContext';
}

return $this
->getMockBuilder('Symfony\Component\Security\Core\SecurityContext')
->getMockBuilder($class)
->disableOriginalConstructor()
->getMock();
}
Expand Down

0 comments on commit 12065ad

Please sign in to comment.