Skip to content

Commit 0579895

Browse files
committed
Update token logic
1 parent 6d66135 commit 0579895

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

src/Codeception/Module/Symfony/SessionAssertionsTrait.php

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
1212
use Symfony\Component\Security\Core\User\UserInterface;
1313
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;
1417
use Symfony\Component\Security\Http\Authenticator\Token\PostAuthenticationToken;
1518
use Symfony\Component\Security\Http\Logout\LogoutUrlGenerator;
1619
use 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

Comments
 (0)