@@ -543,25 +543,25 @@ If you create a Guard login system that's used by a browser and you're experienc
543
543
problems with your session or CSRF tokens, the cause could be bad behavior by your
544
544
authenticator. When a Guard authenticator is meant to be used by a browser, you
545
545
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.
550
550
551
551
This is an edge-case, and unless you're having session or CSRF token issues, you
552
552
can ignore this. Here is an example of good and bad behavior::
553
553
554
- public function getCredentials (Request $request)
554
+ public function supports (Request $request)
555
555
{
556
556
// GOOD behavior: only authenticate on a specific route
557
557
if ($request->attributes->get('_route') !== 'login_route' || !$request->isMethod('POST')) {
558
- return null ;
558
+ return true ;
559
559
}
560
560
561
561
// 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 ;
565
565
}
566
566
567
567
The problem occurs when your browser-based authenticator tries to authenticate
@@ -578,60 +578,32 @@ under your firewall.
578
578
// src/Security/MyIpAuthenticator.php
579
579
// ...
580
580
581
- + use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface ;
581
+ + use Symfony\Component\Security\Core\Security ;
582
582
583
583
class MyIpAuthenticator
584
584
{
585
- + private $tokenStorage ;
585
+ + private $security ;
586
586
587
- + public function __construct(TokenStorageInterface $tokenStorage )
587
+ + public function __construct(Security $security )
588
588
+ {
589
- + $this->tokenStorage = $tokenStorage ;
589
+ + $this->security = $security ;
590
590
+ }
591
591
592
- public function getCredentials (Request $request)
592
+ public function supports (Request $request)
593
593
{
594
594
+ // if there is already an authenticated user (likely due to the session)
595
595
+ // 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;
599
598
+ }
600
599
601
- return array('ip' => $request->getClientIp());
600
+ + // the user is not logged in, so the authenticator should continue
601
+ + return true;
602
602
}
603
603
}
604
604
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.
635
607
636
608
Frequently Asked Questions
637
609
--------------------------
0 commit comments