8000 [SecurityBundle] Allow for custom logout request matcher by ro0NL · Pull Request #22572 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[SecurityBundle] Allow for custom logout request matcher #22572

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[SecurityBundle] Allow for custom logout request matcher
  • Loading branch information
ro0NL committed Apr 29, 2017
commit 09a1dbd2bf5770afb8d9c3f9881223a455227856
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ private function addFirewallsSection(ArrayNodeDefinition $rootNode, array $facto
->scalarNode('csrf_parameter')->defaultValue('_csrf_token')->end()
->scalarNode('csrf_token_generator')->cannotBeEmpty()->end()
->scalarNode('csrf_token_id')->defaultValue('logout')->end()
->scalarNode('request_matcher')->end()
->scalarNode('path')->defaultValue('/logout')->end()
->scalarNode('target')->defaultValue('/')->end()
->scalarNode('success_handler')->end()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -370,9 +370,10 @@ private function createFirewall(ContainerBuilder $container, $id, $firewall, &$a
$listener->replaceArgument(2, new Reference($logoutSuccessHandlerId));

// add CSRF provider
if (isset($firewall['logout']['csrf_token_generator'])) {
$listener->addArgument(new Reference($firewall['logout']['csrf_token_generator']));
}
$listener->replaceArgument(4, isset($firewall['logout']['csrf_token_generator']) ? new Reference($firewall['logout']['csrf_token_generator']) : null);

// add request matcher
$listener->replaceArgument(5, isset($firewall['logout']['request_matcher']) ? new Reference($firewall['logout']['request_matcher']) : null);

// add session logout handler
if (true === $firewall['logout']['invalidate_session'] && false === $firewall['stateless']) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
<argument type="service" id="security.http_utils" />
<argument type="service" id="security.logout.success_handler" />
<argument /> <!-- Options -->
<argument /> <!-- CSRF token manager -->
<argument /> <!-- Request matcher -->
</service>

<service id="security.logout.handler.session" class="Symfony\Component\Security\Http\Logout\SessionLogoutHandler" public="false" />
Expand Down
20 changes: 18 additions & 2 deletions src/Symfony/Component/Security/Http/Firewall/LogoutListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\RequestMatcherInterface;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Exception\LogoutException;
Expand All @@ -36,6 +37,7 @@ class LogoutListener implements ListenerInterface
private $successHandler;
private $httpUtils;
private $csrfTokenManager;
private $requestMatcher;

/**
* Constructor.
Expand All @@ -45,8 +47,9 @@ class LogoutListener implements ListenerInterface
* @param LogoutSuccessHandlerInterface $successHandler A LogoutSuccessHandlerInterface instance
* @param array $options An array of options to process a logout attempt
* @param CsrfTokenManagerInterface $csrfTokenManager A CsrfTokenManagerInterface instance
* @param RequestMatcherInterface|null $requestMatcher A RequestMatcherInterface instance
*/
public function __construct(TokenStorageInterface $tokenStorage, HttpUtils $httpUtils, LogoutSuccessHandlerInterface $successHandler, array $options = array(), CsrfTokenManagerInterface $csrfTokenManager = null)
public function __construct(TokenStorageInterface $tokenStorage, HttpUtils $httpUtils, LogoutSuccessHandlerInterface $successHandler, array $options = array(), CsrfTokenManagerInterface $csrfTokenManager = null, RequestMatcherInterface $requestMatcher = null)
{
$this->tokenStorage = $tokenStorage;
$this->httpUtils = $httpUtils;
Expand All @@ -57,6 +60,7 @@ public function __construct(TokenStorageInterface $tokenStorage, HttpUtils $http
), $options);
$this->successHandler = $successHandler;
$this->csrfTokenManager = $csrfTokenManager;
$this->requestMatcher = $requestMatcher;
$this->handlers = array();
}

Expand Down Expand Up @@ -127,6 +131,18 @@ public function handle(GetResponseEvent $event)
*/
protected function requiresLogout(Request $request)
{
return $this->httpUtils->checkRequestPath($request, $this->options['logout_path']);
if (!isset($this->options['logout_path']) && null === $this->requestMatcher) {
return false;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this condition be allowed at all? This seems invalid: you should pass one or the other. We could catch this with validation in MainConfiguration (and an exception here). We should probably also not allow both to be set.


if (isset($this->options['logout_path']) && !$this->httpUtils->checkRequestPath($request, $this->options['logout_path'])) {
return false;
}

if (null !== $this->requestMatcher && !$this->requestMatcher->matches($request)) {
return false;
}

return true;
}
}
0