7
7
use Symfony \Component \BrowserKit \Cookie ;
8
8
use Symfony \Component \HttpFoundation \Session \SessionInterface ;
9
9
use Symfony \Component \Security \Core \Authentication \Token \Storage \TokenStorageInterface ;
10
+ use Symfony \Component \Security \Core \Authentication \Token \TokenInterface ;
10
11
use Symfony \Component \Security \Core \Authentication \Token \UsernamePasswordToken ;
11
12
use Symfony \Component \Security \Core \User \UserInterface ;
12
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 ;
13
18
use Symfony \Component \Security \Http \Authenticator \Token \PostAuthenticationToken ;
14
19
use Symfony \Component \Security \Http \Logout \LogoutUrlGenerator ;
15
20
use function is_int ;
@@ -32,12 +37,20 @@ trait SessionAssertionsTrait
32
37
*/
33
38
public function amLoggedInAs (UserInterface $ user , string $ firewallName = 'main ' , string $ firewallContext = null ): void
34
39
{
35
- $ session = $ this ->getCurrentSession ();
36
- $ roles = $ user ->getRoles ();
40
+ $ token = $ this ->createAuthenticationToken ($ user , $ firewallName );
41
+ $ this ->loginWithToken ($ token , $ firewallName , $ firewallContext );
42
+ }
37
43
38
- $ token = $ this ->createAuthenticationToken ($ user , $ firewallName , $ roles );
44
+ public function amLoggedInWithToken (TokenInterface $ token , string $ firewallName = 'main ' , string $ firewallContext = null ): void
45
+ {
46
+ $ this ->loginWithToken ($ token , $ firewallName , $ firewallContext );
47
+ }
48
+
49
+ protected function loginWithToken (TokenInterface $ token , string $ firewallName , ?string $ firewallContext ): void
50
+ {
39
51
$ this ->getTokenStorage ()->setToken ($ token );
40
52
53
+ $ session = $ this ->getCurrentSession ();
41
54
$ sessionKey = $ firewallContext ? "_security_ {$ firewallContext }" : "_security_ {$ firewallName }" ;
42
55
$ session ->set ($ sessionKey , serialize ($ token ));
43
56
$ session ->save ();
@@ -174,6 +187,11 @@ protected function getLogoutUrlGenerator(): ?LogoutUrlGenerator
174
187
return $ this ->getService ('security.logout_url_generator ' );
175
188
}
176
189
190
+ protected function getAuthenticator (): ?AuthenticatorInterface
191
+ {
192
+ return $ this ->getService (AuthenticatorInterface::class);
193
+ }
194
+
177
195
protected function getCurrentSession (): SessionInterface
178
196
{
179
197
$ container = $ this ->_getContainer ();
@@ -194,18 +212,24 @@ protected function getSymfonyMajorVersion(): int
194
212
}
195
213
196
214
/**
197
- * @return UsernamePasswordToken|PostAuthenticationGuardToken|PostAuthenticationToken
215
+ * @return TokenInterface|GuardTokenInterface
198
216
*/
199
- protected function createAuthenticationToken (UserInterface $ user , string $ firewallName, array $ roles )
217
+ protected function createAuthenticationToken (UserInterface $ user , string $ firewallName )
200
218
{
219
+ $ roles = $ user ->getRoles ();
201
220
if ($ this ->getSymfonyMajorVersion () < 6 ) {
202
221
return $ this ->config ['guard ' ]
203
222
? new PostAuthenticationGuardToken ($ user , $ firewallName , $ roles )
204
223
: new UsernamePasswordToken ($ user , null , $ firewallName , $ roles );
205
224
}
206
225
207
- return $ this ->config ['authenticator ' ]
208
- ? new PostAuthenticationToken ($ user , $ firewallName , $ roles )
209
- : 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 );
210
234
}
211
235
}
0 commit comments