Skip to content

Commit 5ef40f6

Browse files
codedgeTavoNiievez
andauthored
* Login with token * Update token logic --------- Co-authored-by: TavoNiievez <ganieves@outlook.com>
1 parent e02ff5a commit 5ef40f6

File tree

1 file changed

+32
-8
lines changed

1 file changed

+32
-8
lines changed

src/Codeception/Module/Symfony/SessionAssertionsTrait.php

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,14 @@
77
use Symfony\Component\BrowserKit\Cookie;
88
use Symfony\Component\HttpFoundation\Session\SessionInterface;
99
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
10+
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
1011
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
1112
use Symfony\Component\Security\Core\User\UserInterface;
1213
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;
1318
use Symfony\Component\Security\Http\Authenticator\Token\PostAuthenticationToken;
1419
use Symfony\Component\Security\Http\Logout\LogoutUrlGenerator;
1520
use function is_int;
@@ -32,12 +37,20 @@ trait SessionAssertionsTrait
3237
*/
3338
public function amLoggedInAs(UserInterface $user, string $firewallName = 'main', string $firewallContext = null): void
3439
{
35-
$session = $this->getCurrentSession();
36-
$roles = $user->getRoles();
40+
$token = $this->createAuthenticationToken($user, $firewallName);
41+
$this->loginWithToken($token, $firewallName, $firewallContext);
42+
}
3743

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+
{
3951
$this->getTokenStorage()->setToken($token);
4052

53+
$session = $this->getCurrentSession();
4154
$sessionKey = $firewallContext ? "_security_{$firewallContext}" : "_security_{$firewallName}";
4255
$session->set($sessionKey, serialize($token));
4356
$session->save();
@@ -174,6 +187,11 @@ protected function getLogoutUrlGenerator(): ?LogoutUrlGenerator
174187
return $this->getService('security.logout_url_generator');
175188
}
176189

190+
protected function getAuthenticator(): ?AuthenticatorInterface
191+
{
192+
return $this->getService(AuthenticatorInterface::class);
193+
}
194+
177195
protected function getCurrentSession(): SessionInterface
178196
{
179197
$container = $this->_getContainer();
@@ -194,18 +212,24 @@ protected function getSymfonyMajorVersion(): int
194212
}
195213

196214
/**
197-
* @return UsernamePasswordToken|PostAuthenticationGuardToken|PostAuthenticationToken
215+
* @return TokenInterface|GuardTokenInterface
198216
*/
199-
protected function createAuthenticationToken(UserInterface $user, string $firewallName, array $roles)
217+
protected function createAuthenticationToken(UserInterface $user, string $firewallName)
200218
{
219+
$roles = $user->getRoles();
201220
if ($this->getSymfonyMajorVersion() < 6) {
202221
return $this->config['guard']
203222
? new PostAuthenticationGuardToken($user, $firewallName, $roles)
204223
: new UsernamePasswordToken($user, null, $firewallName, $roles);
205224
}
206225

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);
210234
}
211235
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy