11
11
use Symfony \Component \Security \Core \Authentication \Token \UsernamePasswordToken ;
12
12
use Symfony \Component \Security \Core \User \UserInterface ;
13
13
use Symfony \Component \Security \Guard \Token \PostAuthenticationGuardToken ;
14
+ use Symfony \Component \Security \Guard \Token \GuardTokenInterface ;
15
+ use Symfony \Component \Security \Http \Authenticator \AuthenticatorInterface ;
16
+ use Symfony \Component \Security \Http \Authenticator \Passport \Badge \UserBadge ;
17
+ use Symfony \Component \Security \Http \Authenticator \Passport \SelfValidatingPassport ;
14
18
use Symfony \Component \Security \Http \Authenticator \Token \PostAuthenticationToken ;
15
19
use Symfony \Component \Security \Http \Logout \LogoutUrlGenerator ;
16
20
use function is_int ;
@@ -33,9 +37,8 @@ trait SessionAssertionsTrait
33
37
*/
34
38
public function amLoggedInAs (UserInterface $ user , string $ firewallName = 'main ' , string $ firewallContext = null ): void
35
39
{
36
- $ roles = $ user ->getRoles ();
37
- $ token = $ this ->createAuthenticationToken ($ user , $ firewallName , $ roles );
38
- $ this ->loginWithToken ($ token , $ firewallContext , $ firewallName );
40
+ $ token = $ this ->createAuthenticationToken ($ user , $ firewallName );
41
+ $ this ->loginWithToken ($ token , $ firewallName , $ firewallContext );
39
42
}
40
43
41
44
public function amLoggedInWithToken (TokenInterface $ token , string $ firewallName = 'main ' , string $ firewallContext = null ): void
@@ -184,6 +187,11 @@ protected function getLogoutUrlGenerator(): ?LogoutUrlGenerator
184
187
return $ this ->getService ('security.logout_url_generator ' );
185
188
}
186
189
190
+ protected function getAuthenticator (): ?AuthenticatorInterface
191
+ {
192
+ return $ this ->getService (AuthenticatorInterface::class);
193
+ }
194
+
187
195
protected function getCurrentSession (): SessionInterface
188
196
{
189
197
$ container = $ this ->_getContainer ();
@@ -204,18 +212,24 @@ protected function getSymfonyMajorVersion(): int
204
212
}
205
213
206
214
/**
207
- * @return TokenInterface
215
+ * @return TokenInterface|GuardTokenInterface
208
216
*/
209
- public function createAuthenticationToken (UserInterface $ user , string $ firewallName, array $ roles )
217
+ protected function createAuthenticationToken (UserInterface $ user , string $ firewallName )
210
218
{
219
+ $ roles = $ user ->getRoles ();
211
220
if ($ this ->getSymfonyMajorVersion () < 6 ) {
212
221
return $ this ->config ['guard ' ]
213
222
? new PostAuthenticationGuardToken ($ user , $ firewallName , $ roles )
214
223
: new UsernamePasswordToken ($ user , null , $ firewallName , $ roles );
215
224
}
216
225
217
- return $ this ->config ['authenticator ' ]
218
- ? new PostAuthenticationToken ($ user , $ firewallName , $ roles )
219
- : new UsernamePasswordToken ($ user , $ firewallName , $ roles );
226
+ if ($ this ->config ['authenticator ' ]) {
227
+ if ($ authenticator = $ this ->getAuthenticator ()) {
228
+ $ passport = new SelfValidatingPassport (new UserBadge ($ user ->getUserIdentifier (), fn () => $ user ));
229
+ return $ authenticator ->createToken ($ passport , $ firewallName );
230
+ }
231
+ return new PostAuthenticationToken ($ user , $ firewallName , $ roles );
232
+ }
233
+ return new UsernamePasswordToken ($ user , $ firewallName , $ roles );
220
234
}
221
235
}
0 commit comments