1111use Symfony \Component \Security \Core \Authentication \Token \UsernamePasswordToken ;
1212use Symfony \Component \Security \Core \User \UserInterface ;
1313use Symfony \Component \Security \Guard \Token \PostAuthenticationGuardToken ;
14+ use Symfony \Component \Security \Http \Authenticator \AuthenticatorInterface ;
15+ use Symfony \Component \Security \Http \Authenticator \Passport \Badge \UserBadge ;
16+ use Symfony \Component \Security \Http \Authenticator \Passport \SelfValidatingPassport ;
1417use Symfony \Component \Security \Http \Authenticator \Token \PostAuthenticationToken ;
1518use Symfony \Component \Security \Http \Logout \LogoutUrlGenerator ;
1619use function is_int ;
@@ -33,9 +36,8 @@ trait SessionAssertionsTrait
3336 */
3437 public function amLoggedInAs (UserInterface $ user , string $ firewallName = 'main ' , string $ firewallContext = null ): void
3538 {
36- $ roles = $ user ->getRoles ();
37- $ token = $ this ->createAuthenticationToken ($ user , $ firewallName , $ roles );
38- $ this ->loginWithToken ($ token , $ firewallContext , $ firewallName );
39+ $ token = $ this ->createAuthenticationToken ($ user , $ firewallName );
40+ $ this ->loginWithToken ($ token , $ firewallName , $ firewallContext );
3941 }
4042
4143 public function amLoggedInWithToken (TokenInterface $ token , string $ firewallName = 'main ' , string $ firewallContext = null ): void
@@ -184,6 +186,11 @@ protected function getLogoutUrlGenerator(): ?LogoutUrlGenerator
184186 return $ this ->getService ('security.logout_url_generator ' );
185187 }
186188
189+ protected function getAuthenticator (): ?AuthenticatorInterface
190+ {
191+ return $ this ->getService (AuthenticatorInterface::class);
192+ }
193+
187194 protected function getCurrentSession (): SessionInterface
188195 {
189196 $ container = $ this ->_getContainer ();
@@ -206,16 +213,22 @@ protected function getSymfonyMajorVersion(): int
206213 /**
207214 * @return TokenInterface
208215 */
209- public function createAuthenticationToken (UserInterface $ user , string $ firewallName, array $ roles )
216+ public function createAuthenticationToken (UserInterface $ user , string $ firewallName )
210217 {
218+ $ roles = $ user ->getRoles ();
211219 if ($ this ->getSymfonyMajorVersion () < 6 ) {
212220 return $ this ->config ['guard ' ]
213221 ? new PostAuthenticationGuardToken ($ user , $ firewallName , $ roles )
214222 : new UsernamePasswordToken ($ user , null , $ firewallName , $ roles );
215223 }
216224
217- return $ this ->config ['authenticator ' ]
218- ? new PostAuthenticationToken ($ user , $ firewallName , $ roles )
219- : new UsernamePasswordToken ($ user , $ firewallName , $ roles );
225+ if ($ this ->config ['authenticator ' ]) {
226+ if ($ authenticator = $ this ->getAuthenticator ()) {
227+ $ passport = new SelfValidatingPassport (new UserBadge ($ user ->getUserIdentifier (), fn () => $ user ));
228+ return $ authenticator ->createToken ($ passport , $ firewallName );
229+ }
230+ return new PostAuthenticationToken ($ user , $ firewallName , $ roles );
231+ }
232+ return new UsernamePasswordToken ($ user , $ firewallName , $ roles );
220233 }
221234}
0 commit comments