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,13 @@ 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 ('Token with provider key "%s" did not originate from any of the guard authenticators of provider "%s". ' , $ token ->getGuardProviderKey (), $ this ->providerKey ));
99
95
}
100
96
101
- // no matching authenticator found - but there will be multiple GuardAuthenticationProvider
102
- // instances that will be checked if you have multiple firewalls.
97
+ return $ this ->authenticateViaGuard ($ guardAuthenticator , $ token );
103
98
}
104
99
105
100
private function authenticateViaGuard (GuardAuthenticatorInterface $ guardAuthenticator , PreAuthenticationGuardToken $ token )
@@ -108,18 +103,11 @@ private function authenticateViaGuard(GuardAuthenticatorInterface $guardAuthenti
108
103
$ user = $ guardAuthenticator ->getUser ($ token ->getCredentials (), $ this ->userProvider );
109
104
110
105
if (null === $ user ) {
111
- throw new UsernameNotFoundException (sprintf (
112
- 'Null returned from %s::getUser() ' ,
113
- get_class ($ guardAuthenticator )
114
- ));
106
+ throw new UsernameNotFoundException (sprintf ('Null returned from %s::getUser() ' , get_class ($ guardAuthenticator )));
115
107
}
116
108
117
109
if (!$ user instanceof UserInterface) {
118
- throw new \UnexpectedValueException (sprintf (
119
- 'The %s::getUser() method must return a UserInterface. You returned %s. ' ,
120
- get_class ($ guardAuthenticator ),
121
- is_object ($ user ) ? get_class ($ user ) : gettype ($ user )
122
- ));
110
+ throw new \UnexpectedValueException (sprintf ('The %s::getUser() method must return a UserInterface. You returned %s. ' , get_class ($ guardAuthenticator ), is_object ($ user ) ? get_class ($ user ) : gettype ($ user )));
123
111
}
124
112
125
113
$ this ->userChecker ->checkPreAuth ($ user );
@@ -131,18 +119,37 @@ private function authenticateViaGuard(GuardAuthenticatorInterface $guardAuthenti
131
119
// turn the UserInterface into a TokenInterface
132
120
$ authenticatedToken = $ guardAuthenticator ->createAuthenticatedToken ($ user , $ this ->providerKey );
133
121
if (!$ authenticatedToken instanceof TokenInterface) {
134
- throw new \UnexpectedValueException (sprintf (
135
- 'The %s::createAuthenticatedToken() method must return a TokenInterface. You returned %s. ' ,
136
- get_class ($ guardAuthenticator ),
137
- is_object ($ authenticatedToken ) ? get_class ($ authenticatedToken ) : gettype ($ authenticatedToken )
138
- ));
122
+ throw new \UnexpectedValueException (sprintf ('The %s::createAuthenticatedToken() method must return a TokenInterface. You returned %s. ' , get_class ($ guardAuthenticator ), is_object ($ authenticatedToken ) ? get_class ($ authenticatedToken ) : gettype ($ authenticatedToken )));
139
123
}
140
124
141
125
return $ authenticatedToken ;
142
126
}
143
127
128
+ private function findOriginatingAuthenticator (PreAuthenticationGuardToken $ token )
129
+ {
130
+ // find the *one* GuardAuthenticator that this token originated from
131
+ foreach ($ this ->guardAuthenticators as $ key => $ guardAuthenticator ) {
132
+ // get a key that's unique to *this* guard authenticator
133
+ // this MUST be the same as GuardAuthenticationListener
134
+ $ uniqueGuardKey = $ this ->providerKey .'_ ' .$ key ;
135
+
136
+ if ($ uniqueGuardKey === $ token ->getGuardProviderKey ()) {
137
+ return $ guardAuthenticator ;
138
+ }
139
+ }
140
+
141
+ // no matching authenticator found - but there will be multiple GuardAuthenticationProvider
142
+ // instances that will be checked if you have multiple firewalls.
143
+
144
+ return null ;
145
+ }
146
+
144
147
public function supports (TokenInterface $ token )
145
148
{
149
+ if ($ token instanceof PreAuthenticationGuardToken) {
150
+ return null !== $ this ->findOriginatingAuthenticator ($ token );
151
+ }
152
+
146
153
return $ token instanceof GuardTokenInterface;
147
154
}
148
155
}
0 commit comments