8000 [Security] Throw an explicit error when refreshing a token with a null user by alexandre-daubois · Pull Request #59590 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Security] Throw an explicit error when refreshing a token with a null user #59590

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
8000
Failed to load files.
Loading
Diff view
Diff view
[Security] Throw an explicit error when authenticating a token with a…
… null user
  • Loading branch information
alexandre-daubois committed Jan 28, 2025
commit cd427c310d8f7a6b722174f80d288a1e4b51c6fa
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ public function authenticate(RequestEvent $event): void
]);

if ($token instanceof TokenInterface) {
if (!$token->getUser()) {
throw new \UnexpectedValueException(\sprintf('Cannot authenticate a "%s" token because it doesn\'t store a user.', $token::class));
}

$originalToken = $token;
$token = $this->refreshUser($token);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Http\Firewall\ContextListener;
use Symfony\Component\Security\Http\Tests\Fixtures\NullUserToken;
use Symfony\Contracts\Service\ServiceLocatorTrait;

class ContextListenerTest extends TestCase
Expand All @@ -58,6 +59,30 @@ public function testUserProvidersNeedToImplementAnInterface()
$this->handleEventWithPreviousSession([new \stdClass()]);
}

public function testTokenReturnsNullUser()
{
$tokenStorage = new TokenStorage();
$tokenStorage->setToken(new NullUserToken());

$session = new Session(new MockArraySessionStorage());
$session->set('_security_context_key', serialize($tokenStorage->getToken()));

$request = new Request();
$request->setSession($session);
$request->cookies->set('MOCKSESSID', true);

$listener = new ContextListener($tokenStorage, [], 'context_key');

$this->expectException(\UnexpectedValueException::class);
$this->expectExceptionMessage('Cannot authenticate a "Symfony\Component\Security\Http\Tests\Fixtures\NullUserToken" token because it doesn\'t store a user.');

$listener->authenticate(new RequestEvent(
$this->createMock(HttpKernelInterface::class),
$request,
HttpKernelInterface::MAIN_REQUEST,
));
}

public function testOnKernelResponseWillAddSession()
{
$session = $this->runSessionOnKernelResponse(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Security\Http\Tests\Fixtures;

use Symfony\Component\Security\Core\Authentication\Token\AbstractToken;
use Symfony\Component\Security\Core\User\UserInterface;

class NullUserToken extends AbstractToken
{
public function getUser(): ?UserInterface
{
return null;
}
}
0