Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #20516 [Security][SecurityBundle] Enhance automatic logout ur…
…l generation (ogizanagi) This PR was merged into the 3.3-dev branch. Discussion ---------- [Security][SecurityBundle] Enhance automatic logout url generation | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | yes | Tests pass? | yes | Fixed tickets | N/A | License | MIT | Doc PR | N/A This should help whenever: - [the token does not implement the `getProviderKey` method](https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Security/Http/Logout/LogoutUrlGenerator.php#L89-L99) - you've got multiple firewalls sharing a same context but a logout listener only define on one of them. ##### Behavior: > When not providing the firewall key: > >- Try to find the key from the token (unless it's an anonymous token) >- If found, try to get the listener from the key. If the listener is found, stop there. >- Try from the injected firewall key. If the listener is found, stop there. >- Try from the injected firewall context. If the listener is found, stop there. > >The behavior remains unchanged when providing explicitly the firewall key. No fallback. Commits ------- 5b7fe85 [Security][SecurityBundle] Enhance automatic logout url generation
- Loading branch information
Showing
7 changed files
with
265 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
src/Symfony/Bundle/SecurityBundle/EventListener/FirewallListener.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\SecurityBundle\EventListener; | ||
|
||
use Symfony\Bundle\SecurityBundle\Security\FirewallMap; | ||
use Symfony\Component\EventDispatcher\EventDispatcherInterface; | ||
use Symfony\Component\HttpKernel\Event\FinishRequestEvent; | ||
use Symfony\Component\HttpKernel\Event\GetResponseEvent; | ||
use Symfony\Component\Security\Http\Firewall; | ||
use Symfony\Component\Security\Http\FirewallMapInterface; | ||
use Symfony\Component\Security\Http\Logout\LogoutUrlGenerator; | ||
|
||
/** | ||
* @author Maxime Steinhausser <maxime.steinhausser@gmail.com> | ||
*/ | ||
class FirewallListener extends Firewall | ||
{ | ||
private $map; | ||
private $logoutUrlGenerator; | ||
|
||
public function __construct(FirewallMapInterface $map, EventDispatcherInterface $dispatcher, LogoutUrlGenerator $logoutUrlGenerator) | ||
{ | ||
$this->map = $map; | ||
$this->logoutUrlGenerator = $logoutUrlGenerator; | ||
|
||
parent::__construct($map, $dispatcher); | ||
} | ||
|
||
public function onKernelRequest(GetResponseEvent $event) | ||
{ | ||
if (!$event->isMasterRequest()) { | ||
return; | ||
} | ||
|
||
if ($this->map instanceof FirewallMap && $config = $this->map->getFirewallConfig($event->getRequest())) { | ||
$this->logoutUrlGenerator->setCurrentFirewall($config->getName(), $config->getContext()); | ||
} | ||
|
||
parent::onKernelRequest($event); | ||
} | ||
|
||
public function onKernelFinishRequest(FinishRequestEvent $event) | ||
{ | ||
if ($event->isMasterRequest()) { | ||
$this->logoutUrlGenerator->setCurrentFirewall(null); | ||
} | ||
|
||
parent::onKernelFinishRequest($event); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
src/Symfony/Component/Security/Http/Tests/Logout/LogoutUrlGeneratorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Security\Http\Tests\Logout; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\RequestStack; | ||
use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken; | ||
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage; | ||
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; | ||
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; | ||
use Symfony\Component\Security\Http\Logout\LogoutUrlGenerator; | ||
|
||
/** | ||
* @author Maxime Steinhausser <maxime.steinhausser@gmail.com> | ||
*/ | ||
class LogoutUrlGeneratorTest extends TestCase | ||
{ | ||
/** @var TokenStorage */ | ||
private $tokenStorage; | ||
/** @var LogoutUrlGenerator */ | ||
private $generator; | ||
|
||
protected function setUp() | ||
{ | ||
$requestStack = $this->getMockBuilder(RequestStack::class)->getMock(); | ||
$request = $this->getMockBuilder(Request::class)->getMock(); | ||
$requestStack->method('getCurrentRequest')->willReturn($request); | ||
|
||
$this->tokenStorage = new TokenStorage(); | ||
$this->generator = new LogoutUrlGenerator($requestStack, null, $this->tokenStorage); | ||
} | ||
|
||
public function testGetLogoutPath() | ||
{ | ||
$this->generator->registerListener('secured_area', '/logout', null, null); | ||
|
||
$this->assertSame('/logout', $this->generator->getLogoutPath('secured_area')); | ||
} | ||
|
||
/** | ||
* @expectedException \InvalidArgumentException | ||
* @expectedExceptionMessage No LogoutListener found for firewall key "unregistered_key". | ||
*/ | ||
public function testGetLogoutPathWithoutLogoutListenerRegisteredForKeyThrowsException() | ||
{ | ||
$this->generator->registerListener('secured_area', '/logout', null, null, null); | ||
|
||
$this->generator->getLogoutPath('unregistered_key'); | ||
} | ||
|
||
public function testGuessFromToken() | ||
{ | ||
$this->tokenStorage->setToken(new UsernamePasswordToken('user', 'password', 'secured_area')); | ||
$this->generator->registerListener('secured_area', '/logout', null, null); | ||
|
||
$this->assertSame('/logout', $this->generator->getLogoutPath()); | ||
} | ||
|
||
/** | ||
* @expectedException \InvalidArgumentException | ||
* @expectedExceptionMessage Unable to generate a logout url for an anonymous token. | ||
*/ | ||
public function testGuessFromAnonymousTokenThrowsException() | ||
{ | ||
$this->tokenStorage->setToken(new AnonymousToken('default', 'anon.')); | ||
|
||
$this->generator->getLogoutPath(); | ||
} | ||
|
||
public function testGuessFromCurrentFirewallKey() | ||
{ | ||
$this->generator->registerListener('secured_area', '/logout', null, null); | ||
$this->generator->setCurrentFirewall('secured_area'); | ||
|
||
$this->assertSame('/logout', $this->generator->getLogoutPath()); | ||
} | ||
|
||
public function testGuessFromCurrentFirewallContext() | ||
{ | ||
$this->generator->registerListener('secured_area', '/logout', null, null, null, 'secured'); | ||
$this->generator->setCurrentFirewall('admin', 'secured'); | ||
|
||
$this->assertSame('/logout', $this->generator->getLogoutPath()); | ||
} | ||
|
||
public function testGuessFromTokenWithoutProviderKeyFallbacksToCurrentFirewall() | ||
{ | ||
$this->tokenStorage->setToken($this->getMockBuilder(TokenInterface::class)->getMock()); | ||
$this->generator->registerListener('secured_area', '/logout', null, null); | ||
$this->generator->setCurrentFirewall('secured_area'); | ||
|
||
$this->assertSame('/logout', $this->generator->getLogoutPath()); | ||
} | ||
|
||
/** | ||
* @expectedException \InvalidArgumentException | ||
* @expectedExceptionMessage Unable to find the current firewall LogoutListener, please provide the provider key manually | ||
*/ | ||
public function testUnableToGuessThrowsException() | ||
{ | ||
$this->generator->registerListener('secured_area', '/logout', null, null); | ||
|
||
$this->generator->getLogoutPath(); | ||
} | ||
} |