Skip to content

Commit 87818ca

Browse files
committed
updating the guard session migration details for Symfony 3.4 changes
1 parent 414b1ba commit 87818ca

File tree

1 file changed

+20
-48
lines changed

1 file changed

+20
-48
lines changed

security/guard_authentication.rst

Lines changed: 20 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -543,25 +543,25 @@ If you create a Guard login system that's used by a browser and you're experienc
543543
problems with your session or CSRF tokens, the cause could be bad behavior by your
544544
authenticator. When a Guard authenticator is meant to be used by a browser, you
545545
should *not* authenticate the user on *every* request. In other words, you need to
546-
make sure the ``getCredentials()`` method *only* returns a non-null value when
547-
you actually *need* to authenticate the user. Why? Because, when ``getCredentials()``
548-
returns a non-null value, for security purposes, the user's session is "migrated"
549-
to a new session id.
546+
make sure the ``supports()`` method *only* returns ``true`` when
547+
you actually *need* to authenticate the user. Why? Because, when ``supports()``
548+
returns true (and authentication is ultimately successful), for security purposes,
549+
the user's session is "migrated" to a new session id.
550550

551551
This is an edge-case, and unless you're having session or CSRF token issues, you
552552
can ignore this. Here is an example of good and bad behavior::
553553

554-
public function getCredentials(Request $request)
554+
public function supports(Request $request)
555555
{
556556
// GOOD behavior: only authenticate on a specific route
557557
if ($request->attributes->get('_route') !== 'login_route' || !$request->isMethod('POST')) {
558-
return null;
558+
return true;
559559
}
560560

561561
// e.g. your login system authenticates by the user's IP address
562-
// BAD behavior: authentication will now execute on every request
563-
// even if the user is already authenticated (due to the session)
564-
return array('ip' => $request->getClientIp());
562+
// BAD behavior: So, you decide to *always* return true so that
563+
// you can check the user's IP address on every request
564+
return true;
565565
}
566566

567567
The problem occurs when your browser-based authenticator tries to authenticate
@@ -578,60 +578,32 @@ under your firewall.
578578
// src/Security/MyIpAuthenticator.php
579579
// ...
580580
581-
+ use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
581+
+ use Symfony\Component\Security\Core\Security;
582582
583583
class MyIpAuthenticator
584584
{
585-
+ private $tokenStorage;
585+
+ private $security;
586586
587-
+ public function __construct(TokenStorageInterface $tokenStorage)
587+
+ public function __construct(Security $security)
588588
+ {
589-
+ $this->tokenStorage = $tokenStorage;
589+
+ $this->security = $security;
590590
+ }
591591
592-
public function getCredentials(Request $request)
592+
public function supports(Request $request)
593593
{
594594
+ // if there is already an authenticated user (likely due to the session)
595595
+ // then return null and skip authentication: there is no need.
596-
+ $user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null;
597-
+ if (is_object($user)) {
598-
+ return null;
596+
+ if ($this->security->getUser()) {
597+
+ return false;
599598
+ }
600599
601-
return array('ip' => $request->getClientIp());
600+
+ // the user is not logged in, so the authenticator should continue
601+
+ return true;
602602
}
603603
}
604604
605-
You'll also need to update your service configuration to pass the token storage:
606-
607-
.. configuration-block::
608-
609-
.. code-block:: yaml
610-
611-
# app/config/services.yml
612-
services:
613-
app.token_authenticator:
614-
class: AppBundle\Security\TokenAuthenticator
615-
arguments: ['@security.token_storage']
616-
617-
.. code-block:: xml
618-
619-
<!-- app/config/services.xml -->
620-
<services>
621-
<service id="app.token_authenticator" class="AppBundle\Security\TokenAuthenticator">
622-
<argument type="service" id="security.token_storage" />
623-
</service>
624-
</services>
625-
626-
.. code-block:: php
627-
628-
// app/config/services.php
629-
use AppBundle\Security\TokenAuthenticator;
630-
use Symfony\Component\DependencyInjection\Definition;
631-
use Symfony\Component\DependencyInjection\Reference;
632-
633-
$container->register('app.token_authenticator', TokenAuthenticator::class)
634-
->addArgument(new Reference('security.token_storage'));
605+
If you use autowiring, the ``Security`` service will automatically be passed to
606+
your authenticator.
635607

636608
Frequently Asked Questions
637609
--------------------------

0 commit comments

Comments
 (0)