Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Security] Fix AuthenticationUtils::getLastUsername() returning null #53509

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 1 commit into from
Jan 18, 2024
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
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ public function getLastUsername()
$request = $this->getRequest();

if ($request->attributes->has(Security::LAST_USERNAME)) {
return $request->attributes->get(Security::LAST_USERNAME, '');
return $request->attributes->get(Security::LAST_USERNAME) ?? '';
}

return $request->hasSession() ? $request->getSession()->get(Security::LAST_USERNAME, '') : '';
return $request->hasSession() ? ($request->getSession()->get(Security::LAST_USERNAME) ?? '') : '';
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* 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\Authentication;

use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;

class AuthenticationUtilsTest extends TestCase
{
public function testLastAuthenticationErrorWhenRequestHasAttribute()
{
$request = Request::create('/');
$request->attributes->set(Security::AUTHENTICATION_ERROR, 'my error');

$requestStack = new RequestStack();
$requestStack->push($request);

$utils = new AuthenticationUtils($requestStack);
$this->assertSame('my error', $utils->getLastAuthenticationError());
}

public function testLastAuthenticationErrorInSession()
{
$request = Request::create('/');

$session = new Session(new MockArraySessionStorage());
$session->set(Security::AUTHENTICATION_ERROR, 'session error');
$request->setSession($session);

$requestStack = new RequestStack();
$requestStack->push($request);

$utils = new AuthenticationUtils($requestStack);
$this->assertSame('session error', $utils->getLastAuthenticationError());
$this->assertFalse($session->has(Security::AUTHENTICATION_ERROR));
}

public function testLastAuthenticationErrorInSessionWithoutClearing()
{
$request = Request::create('/');

$session = new Session(new MockArraySessionStorage());
$session->set(Security::AUTHENTICATION_ERROR, 'session error');
$request->setSession($session);

$requestStack = new RequestStack();
$requestStack->push($request);

$utils = new AuthenticationUtils($requestStack);
$this->assertSame('session error', $utils->getLastAuthenticationError(false));
$this->assertTrue($session->has(Security::AUTHENTICATION_ERROR));
}

public function testLastUserNameIsDefinedButNull()
{
$request = Request::create('/');
$request->attributes->set(Security::LAST_USERNAME, null);

$requestStack = new RequestStack();
$requestStack->push($request);

$utils = new AuthenticationUtils($requestStack);
$this->assertSame('', $utils->getLastUsername());
}

public function testLastUserNameIsDefined()
{
$request = Request::create('/');
$request->attributes->set(Security::LAST_USERNAME, 'user');

$requestStack = new RequestStack();
$requestStack->push($request);

$utils = new AuthenticationUtils($requestStack);
$this->assertSame('user', $utils->getLastUsername());
}

public function testLastUserNameIsDefinedInSessionButNull()
{
$request = Request::create('/');

$session = new Session(new MockArraySessionStorage());
$session->set(Security::LAST_USERNAME, null);
$request->setSession($session);

$requestStack = new RequestStack();
$requestStack->push($request);

$utils = new AuthenticationUtils($requestStack);
$this->assertSame('', $utils->getLastUsername());
}

public function testLastUserNameIsDefinedInSession()
{
$request = Request::create('/');

$session = new Session(new MockArraySessionStorage());
$session->set(Security::LAST_USERNAME, 'user');
$request->setSession($session);

$requestStack = new RequestStack();
$requestStack->push($request);

$utils = new AuthenticationUtils($requestStack);
$this->assertSame('user', $utils->getLastUsername());
}
}