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