Skip to content

Commit

Permalink
Update token logic
Browse files Browse the repository at this point in the history
  • Loading branch information
TavoNiievez committed May 2, 2024
1 parent 6d66135 commit 6b0b251
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions src/Codeception/Module/Symfony/SessionAssertionsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Guard\Token\PostAuthenticationGuardToken;
use Symfony\Component\Security\Guard\Token\GuardTokenInterface;
use Symfony\Component\Security\Http\Authenticator\AuthenticatorInterface;
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
use Symfony\Component\Security\Http\Authenticator\Passport\SelfValidatingPassport;
use Symfony\Component\Security\Http\Authenticator\Token\PostAuthenticationToken;
use Symfony\Component\Security\Http\Logout\LogoutUrlGenerator;
use function is_int;
Expand All @@ -33,9 +37,8 @@ trait SessionAssertionsTrait
*/
public function amLoggedInAs(UserInterface $user, string $firewallName = 'main', string $firewallContext = null): void
{
$roles = $user->getRoles();
$token = $this->createAuthenticationToken($user, $firewallName, $roles);
$this->loginWithToken($token, $firewallContext, $firewallName);
$token = $this->createAuthenticationToken($user, $firewallName);
$this->loginWithToken($token, $firewallName, $firewallContext);
}

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

protected function getAuthenticator(): ?AuthenticatorInterface
{
return $this->getService(AuthenticatorInterface::class);
}

protected function getCurrentSession(): SessionInterface
{
$container = $this->_getContainer();
Expand All @@ -204,18 +212,24 @@ protected function getSymfonyMajorVersion(): int
}

/**
* @return TokenInterface
* @return TokenInterface|GuardTokenInterface
*/
public function createAuthenticationToken(UserInterface $user, string $firewallName, array $roles)
protected function createAuthenticationToken(UserInterface $user, string $firewallName)
{
$roles = $user->getRoles();
if ($this->getSymfonyMajorVersion() < 6) {
return $this->config['guard']
? new PostAuthenticationGuardToken($user, $firewallName, $roles)
: new UsernamePasswordToken($user, null, $firewallName, $roles);
}

return $this->config['authenticator']
? new PostAuthenticationToken($user, $firewallName, $roles)
: new UsernamePasswordToken($user, $firewallName, $roles);
if ($this->config['authenticator']) {
if ($authenticator = $this->getAuthenticator()) {
$passport = new SelfValidatingPassport(new UserBadge($user->getUserIdentifier(), fn () => $user));
return $authenticator->createToken($passport, $firewallName);
}
return new PostAuthenticationToken($user, $firewallName, $roles);
}
return new UsernamePasswordToken($user, $firewallName, $roles);
}
}

0 comments on commit 6b0b251

Please sign in to comment.