Skip to content

Commit 6b0b251

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

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

src/Codeception/Module/Symfony/SessionAssertionsTrait.php

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
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\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;
1418
use Symfony\Component\Security\Http\Authenticator\Token\PostAuthenticationToken;
1519
use Symfony\Component\Security\Http\Logout\LogoutUrlGenerator;
1620
use function is_int;
@@ -33,9 +37,8 @@ trait SessionAssertionsTrait
3337
*/
3438
public function amLoggedInAs(UserInterface $user, string $firewallName = 'main', string $firewallContext = null): void
3539
{
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);
3942
}
4043

4144
public function amLoggedInWithToken(TokenInterface $token, string $firewallName = 'main', string $firewallContext = null): void
@@ -184,6 +187,11 @@ protected function getLogoutUrlGenerator(): ?LogoutUrlGenerator
184187
return $this->getService('security.logout_url_generator');
185188
}
186189

190+
protected function getAuthenticator(): ?AuthenticatorInterface
191+
{
192+
return $this->getService(AuthenticatorInterface::class);
193+
}
194+
187195
protected function getCurrentSession(): SessionInterface
188196
{
189197
$container = $this->_getContainer();
@@ -204,18 +212,24 @@ protected function getSymfonyMajorVersion(): int
204212
}
205213

206214
/**
207-
* @return TokenInterface
215+
* @return TokenInterface|GuardTokenInterface
208216
*/
209-
public function createAuthenticationToken(UserInterface $user, string $firewallName, array $roles)
217+
protected function createAuthenticationToken(UserInterface $user, string $firewallName)
210218
{
219+
$roles = $user->getRoles();
211220
if ($this->getSymfonyMajorVersion() < 6) {
212221
return $this->config['guard']
213222
? new PostAuthenticationGuardToken($user, $firewallName, $roles)
214223
: new UsernamePasswordToken($user, null, $firewallName, $roles);
215224
}
216225

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);
220234
}
221235
}

0 commit comments

Comments
 (0)