From b21135fae0f485cdc28edb0ead2c9587d06b270b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Holger=20L=C3=B6sken?= Date: Sun, 4 Feb 2024 18:18:08 +0100 Subject: [PATCH 1/5] #165: Login with token --- .../Module/Symfony/SessionAssertionsTrait.php | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/Codeception/Module/Symfony/SessionAssertionsTrait.php b/src/Codeception/Module/Symfony/SessionAssertionsTrait.php index 7d26314d..b630d65d 100644 --- a/src/Codeception/Module/Symfony/SessionAssertionsTrait.php +++ b/src/Codeception/Module/Symfony/SessionAssertionsTrait.php @@ -7,6 +7,7 @@ use Symfony\Component\BrowserKit\Cookie; use Symfony\Component\HttpFoundation\Session\SessionInterface; use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; +use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; use Symfony\Component\Security\Core\User\UserInterface; use Symfony\Component\Security\Guard\Token\PostAuthenticationGuardToken; @@ -32,12 +33,21 @@ trait SessionAssertionsTrait */ public function amLoggedInAs(UserInterface $user, string $firewallName = 'main', string $firewallContext = null): void { - $session = $this->getCurrentSession(); $roles = $user->getRoles(); - $token = $this->createAuthenticationToken($user, $firewallName, $roles); + $this->loginWithToken($token, $firewallContext, $firewallName); + } + + public function amLoggedInWithToken(TokenInterface $token, string $firewallName = 'main', string $firewallContext = null): void + { + $this->loginWithToken($token, $firewallName, $firewallContext); + } + + private function loginWithToken(TokenInterface $token, string $firewallName = 'main', string $firewallContext = null): void + { $this->getTokenStorage()->setToken($token); + $session = $this->getCurrentSession(); $sessionKey = $firewallContext ? "_security_{$firewallContext}" : "_security_{$firewallName}"; $session->set($sessionKey, serialize($token)); $session->save(); @@ -196,7 +206,7 @@ protected function getSymfonyMajorVersion(): int /** * @return UsernamePasswordToken|PostAuthenticationGuardToken|PostAuthenticationToken */ - protected function createAuthenticationToken(UserInterface $user, string $firewallName, array $roles) + public function createAuthenticationToken(UserInterface $user, string $firewallName, array $roles) { if ($this->getSymfonyMajorVersion() < 6) { return $this->config['guard'] From 7e659a1e33f8f35e6a6476228129679655375f91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Holger=20L=C3=B6sken?= Date: Sun, 4 Feb 2024 18:19:31 +0100 Subject: [PATCH 2/5] #165: Fix --- src/Codeception/Module/Symfony/SessionAssertionsTrait.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Codeception/Module/Symfony/SessionAssertionsTrait.php b/src/Codeception/Module/Symfony/SessionAssertionsTrait.php index b630d65d..be833dc7 100644 --- a/src/Codeception/Module/Symfony/SessionAssertionsTrait.php +++ b/src/Codeception/Module/Symfony/SessionAssertionsTrait.php @@ -206,7 +206,7 @@ protected function getSymfonyMajorVersion(): int /** * @return UsernamePasswordToken|PostAuthenticationGuardToken|PostAuthenticationToken */ - public function createAuthenticationToken(UserInterface $user, string $firewallName, array $roles) + protected function createAuthenticationToken(UserInterface $user, string $firewallName, array $roles) { if ($this->getSymfonyMajorVersion() < 6) { return $this->config['guard'] From 2b1748172e567dafe820ceb129df7cf84c9811c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Holger=20L=C3=B6sken?= Date: Sun, 4 Feb 2024 20:59:21 +0100 Subject: [PATCH 3/5] #165: Return proper interface --- src/Codeception/Module/Symfony/SessionAssertionsTrait.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Codeception/Module/Symfony/SessionAssertionsTrait.php b/src/Codeception/Module/Symfony/SessionAssertionsTrait.php index be833dc7..c9dfaeeb 100644 --- a/src/Codeception/Module/Symfony/SessionAssertionsTrait.php +++ b/src/Codeception/Module/Symfony/SessionAssertionsTrait.php @@ -204,9 +204,9 @@ protected function getSymfonyMajorVersion(): int } /** - * @return UsernamePasswordToken|PostAuthenticationGuardToken|PostAuthenticationToken + * @return TokenInterface */ - protected function createAuthenticationToken(UserInterface $user, string $firewallName, array $roles) + public function createAuthenticationToken(UserInterface $user, string $firewallName, array $roles) { if ($this->getSymfonyMajorVersion() < 6) { return $this->config['guard'] From 6d6613524c47d4ffe531c727079b881bd99bd2cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Holger=20L=C3=B6sken?= Date: Sun, 4 Feb 2024 21:03:28 +0100 Subject: [PATCH 4/5] Fix --- src/Codeception/Module/Symfony/SessionAssertionsTrait.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Codeception/Module/Symfony/SessionAssertionsTrait.php b/src/Codeception/Module/Symfony/SessionAssertionsTrait.php index c9dfaeeb..538d7184 100644 --- a/src/Codeception/Module/Symfony/SessionAssertionsTrait.php +++ b/src/Codeception/Module/Symfony/SessionAssertionsTrait.php @@ -43,7 +43,7 @@ public function amLoggedInWithToken(TokenInterface $token, string $firewallName $this->loginWithToken($token, $firewallName, $firewallContext); } - private function loginWithToken(TokenInterface $token, string $firewallName = 'main', string $firewallContext = null): void + protected function loginWithToken(TokenInterface $token, string $firewallName, ?string $firewallContext): void { $this->getTokenStorage()->setToken($token); From 6b0b251bb02522ba8e0397240052461d26cd0938 Mon Sep 17 00:00:00 2001 From: TavoNiievez Date: Wed, 1 May 2024 22:44:32 -0500 Subject: [PATCH 5/5] Update token logic --- .../Module/Symfony/SessionAssertionsTrait.php | 30 ++++++++++++++----- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/src/Codeception/Module/Symfony/SessionAssertionsTrait.php b/src/Codeception/Module/Symfony/SessionAssertionsTrait.php index 538d7184..47e40fc3 100644 --- a/src/Codeception/Module/Symfony/SessionAssertionsTrait.php +++ b/src/Codeception/Module/Symfony/SessionAssertionsTrait.php @@ -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; @@ -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 @@ -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(); @@ -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); } }