-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[security] Fetching current stored context when not explicitly specified #13342
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,9 @@ | |
use Symfony\Component\DependencyInjection\ContainerInterface; | ||
use Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderAdapter; | ||
use Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderInterface; | ||
use Symfony\Component\HttpFoundation\RequestStack; | ||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;< 8000 /td> | ||
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; | ||
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface; | ||
use Symfony\Component\Templating\Helper\Helper; | ||
|
||
|
@@ -25,20 +27,33 @@ | |
*/ | ||
class LogoutUrlHelper extends Helper | ||
{ | ||
private $container; | ||
private $requestStack; | ||
private $listeners = array(); | ||
private $router; | ||
private $tokenStorage; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param ContainerInterface $container A ContainerInterface instance | ||
* @param UrlGeneratorInterface $router A Router instance | ||
* @param ContainerInterface|RequestStack $requestStack A ContainerInterface instance or RequestStack | ||
* @param UrlGeneratorInterface $router The router service | ||
* @param TokenStorageInterface|null $tokenStorage The token storage service | ||
* | ||
* @deprecated Passing a ContainerInterface as a first argument is deprecated since 2.7 and will be removed in 3.0. | ||
*/ | ||
public function __construct(ContainerInterface $container, UrlGeneratorInterface $router) | ||
public function __construct($requestStack, UrlGeneratorInterface $router, TokenStorageInterface $tokenStorage = null) | ||
{ | ||
$this->container = $container; | ||
if ($requestStack instanceof ContainerInterface) { | ||
$this->requestStack = $requestStack->get('request_stack'); | ||
trigger_error('The '.__CLASS__.' constructor will require a RequestStack instead of a ContainerInterface instance in 3.0.', E_USER_DEPRECATED); | ||
} elseif ($requestStack instanceof RequestStack) { | ||
$this->requestStack = $requestStack; | ||
} else { | ||
throw new \InvalidArgumentException(sprintf('%s takes either a RequestStack or a ContainerInterface object as its first argument.', __METHOD__)); | ||
} | ||
|
||
$this->router = $router; | ||
$this->tokenStorage = $tokenStorage; | ||
} | ||
|
||
/** | ||
|
@@ -64,7 +79,7 @@ public function registerListener($key, $logoutPath, $csrfTokenId, $csrfParameter | |
/** | ||
* Generates the absolute logout path for the firewall. | ||
* | ||
* @p 10000 aram string $key The firewall key | ||
* @param string|null $key The firewall key or null to use the current firewall key | ||
* | ||
* @return string The logout path | ||
*/ | ||
|
@@ -76,7 +91,7 @@ public function getLogoutPath($key) | |
/** | ||
* Generates the absolute logout URL for the firewall. | ||
* | ||
* @param string $key The firewall key | ||
* @param string|null $key The firewall key or null to use the current firewall key | ||
* | ||
* @return string The logout URL | ||
*/ | ||
|
@@ -88,15 +103,27 @@ public function getLogoutUrl($key) | |
/** | ||
* Generates the logout URL for the firewall. | ||
* | ||
* @param string $key The firewall key | ||
* @param string|null $key The firewall key or null to use the current firewall key | ||
* @param bool|string $referenceType The type of reference (one of the constants in UrlGeneratorInterface) | ||
* | ||
* @return string The logout URL | ||
* | ||
* @throws \InvalidArgumentException if no LogoutListener is registered for the key | ||
* @throws \InvalidArgumentException if no LogoutListener is registered for the key or the key could not be found automatically. | ||
*/ | ||
private function generateLogoutUrl($key, $referenceType) | ||
{ | ||
// Fetch the current provider key from token, if possible | ||
if (null === $key && null !== $this->tokenStorage) { | ||
$token = $this->tokenStorage->getToken(); | ||
if (null !== $token && method_exists($token, 'getProviderKey')) { | ||
$key = $token->getProviderKey(); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what if it cannot be retrieved ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You mean the token? I can add an additional check for this. |
||
} | ||
|
||
if (null === $key) { | ||
throw new \InvalidArgumentException('Unable to find the current firewall LogoutListener, please provide the provider key manually.'); | ||
} | ||
|
||
if (!array_key_exists($key, $this->listeners)) { | ||
throw new \InvalidArgumentException(sprintf('No LogoutListener found for firewall key "%s".', $key)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it be better to throw a specific exception when key is null? Probably in the condition above when we are not able to find the corresponding key? Something like "Unable to find the current firewall LogoutListener, please specify the provider key." There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @fabpot the feature ia about handling the current context. By specifying null, it will use the current provider key so we can reuse a twig template with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I do understand the change, but if the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok, I'll see if I can find an exception that can be added. |
||
} | ||
|
@@ -106,7 +133,7 @@ private function generateLogoutUrl($key, $referenceType) | |
$parameters = null !== $csrfTokenManager ? array($csrfParameter => (string) $csrfTokenManager->getToken($csrfTokenId)) : array(); | ||
|
||
if ('/' === $logoutPath[0]) { | ||
$request = $this->container->get('request_stack')->getCurrentRequest(); | ||
$request = $this->requestStack->getCurrentRequest(); | ||
|
||
$url = UrlGeneratorInterface::ABSOLUTE_URL === $referenceType ? $request->getUriForPath($logoutPath) : $request->getBasePath().$logoutPath; | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
phpdoc update