13
13
14
14
use Symfony \Component \Security \Core \Authentication \Provider \AuthenticationProviderInterface ;
15
15
use Symfony \Component \Security \Core \Authentication \Token \AnonymousToken ;
16
+ use Symfony \Component \Security \Core \Exception \AuthenticationException ;
16
17
use Symfony \Component \Security \Core \Exception \BadCredentialsException ;
17
18
use Symfony \Component \Security \Core \Exception \UsernameNotFoundException ;
18
19
use Symfony \Component \Security \Guard \GuardAuthenticatorInterface ;
@@ -63,7 +64,7 @@ public function __construct(array $guardAuthenticators, UserProviderInterface $u
63
64
*/
64
65
public function authenticate (TokenInterface $ token )
65
66
{
66
- if (!$ this -> supports ( $ token) ) {
67
+ if (!$ token instanceof GuardTokenInterface ) {
67
68
throw new \InvalidArgumentException ('GuardAuthenticationProvider only supports GuardTokenInterface. ' );
68
69
}
69
70
@@ -87,19 +88,17 @@ public function authenticate(TokenInterface $token)
87
88
throw new AuthenticationExpiredException ();
88
89
}
89
90
90
- // find the *one* GuardAuthenticator that this token originated from
91
- foreach ($ this ->guardAuthenticators as $ key => $ guardAuthenticator ) {
92
- // get a key that's unique to *this* guard authenticator
93
- // this MUST be the same as GuardAuthenticationListener
94
- $ uniqueGuardKey = $ this ->providerKey .'_ ' .$ key ;
91
+ $ guardAuthenticator = $ this ->findOriginatingAuthenticator ($ token );
95
92
96
- if ($ uniqueGuardKey == $ token ->getGuardProviderKey ()) {
97
- return $ this ->authenticateViaGuard ($ guardAuthenticator , $ token );
98
- }
93
+ if (null === $ guardAuthenticator ) {
94
+ throw new AuthenticationException (sprintf (
95
+ 'Token with provider key "%s" did not originate from any of the guard authenticators of provider "%s". ' ,
96
+ $ token ->getGuardProviderKey (),
97
+ $ this ->providerKey
98
+ ));
99
99
}
100
100
101
- // no matching authenticator found - but there will be multiple GuardAuthenticationProvider
102
- // instances that will be checked if you have multiple firewalls.
101
+ return $ this ->authenticateViaGuard ($ guardAuthenticator , $ token );
103
102
}
104
103
105
104
private function authenticateViaGuard (GuardAuthenticatorInterface $ guardAuthenticator , PreAuthenticationGuardToken $ token )
@@ -141,8 +140,31 @@ private function authenticateViaGuard(GuardAuthenticatorInterface $guardAuthenti
141
140
return $ authenticatedToken ;
142
141
}
143
142
143
+ private function findOriginatingAuthenticator (PreAuthenticationGuardToken $ token )
144
+ {
145
+ // find the *one* GuardAuthenticator that this token originated from
146
+ foreach ($ this ->guardAuthenticators as $ key => $ guardAuthenticator ) {
147
+ // get a key that's unique to *this* guard authenticator
148
+ // this MUST be the same as GuardAuthenticationListener
149
+ $ uniqueGuardKey = $ this ->providerKey .'_ ' .$ key ;
150
+
151
+ if ($ uniqueGuardKey === $ token ->getGuardProviderKey ()) {
152
+ return $ guardAuthenticator ;
153
+ }
154
+ }
155
+
156
+ // no matching authenticator found - but there will be multiple GuardAuthenticationProvider
157
+ // instances that will be checked if you have multiple firewalls.
158
+
159
+ return null ;
160
+ }
161
+
144
162
public function supports (TokenInterface $ token )
145
163
{
164
+ if ($ token instanceof PreAuthenticationGuardToken) {
165
+ return null !== $ this ->findOriginatingAuthenticator ($ token );
166
+ }
167
+
146
168
return $ token instanceof GuardTokenInterface;
147
169
}
148
170
}
0 commit comments