Skip to content

Commit 4910afd

Browse files
committed
#165: Login with token
1 parent be24b2b commit 4910afd

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

src/Codeception/Module/Symfony/SessionAssertionsTrait.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
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;
@@ -32,12 +33,21 @@ trait SessionAssertionsTrait
3233
*/
3334
public function amLoggedInAs(UserInterface $user, string $firewallName = 'main', string $firewallContext = null): void
3435
{
35-
$session = $this->getCurrentSession();
3636
$roles = $user->getRoles();
37-
3837
$token = $this->createAuthenticationToken($user, $firewallName, $roles);
38+
$this->loginWithToken($token, $firewallContext, $firewallName);
39+
}
40+
41+
public function amLoggedInWithToken(TokenInterface $token, string $firewallName = 'main', string $firewallContext = null): void
42+
{
43+
$this->loginWithToken($token, $firewallName, $firewallContext);
44+
}
45+
46+
private function loginWithToken(TokenInterface $token, string $firewallName = 'main', string $firewallContext = null): void
47+
{
3948
$this->getTokenStorage()->setToken($token);
4049

50+
$session = $this->getCurrentSession();
4151
$sessionKey = $firewallContext ? "_security_{$firewallContext}" : "_security_{$firewallName}";
4252
$session->set($sessionKey, serialize($token));
4353
$session->save();
@@ -196,7 +206,7 @@ protected function getSymfonyMajorVersion(): int
196206
/**
197207
* @return UsernamePasswordToken|PostAuthenticationGuardToken|PostAuthenticationToken
198208
*/
199-
protected function createAuthenticationToken(UserInterface $user, string $firewallName, array $roles)
209+
public function createAuthenticationToken(UserInterface $user, string $firewallName, array $roles)
200210
{
201211
if ($this->getSymfonyMajorVersion() < 6) {
202212
return $this->config['guard']

0 commit comments

Comments
 (0)