1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
|
<?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\Authentication;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Http\Authentication\DefaultAuthenticationSuccessHandler;
use Symfony\Component\Security\Http\HttpUtils;
class DefaultAuthenticationSuccessHandlerTest extends TestCase
{
#[\PHPUnit\Framework\Attributes\DataProvider('getRequestRedirections')]
public function testRequestRedirections(Request $request, $options, $redirectedUrl)
{
$urlGenerator = $this->createMock(UrlGeneratorInterface::class);
$urlGenerator->expects($this->any())->method('generate')->willReturn('http://localhost/login');
$httpUtils = new HttpUtils($urlGenerator);
$token = $this->createMock(TokenInterface::class);
$handler = new DefaultAuthenticationSuccessHandler($httpUtils, $options);
if ($request->hasSession()) {
$handler->setFirewallName('admin');
}
$this->assertSame('http://localhost'.$redirectedUrl, $handler->onAuthenticationSuccess($request, $token)->getTargetUrl());
}
public function testRequestRedirectionsWithTargetPathInSessions()
{
$session = $this->createMock(SessionInterface::class);
$session->expects($this->once())->method('get')->with('_security.admin.target_path')->willReturn('/admin/dashboard');
$session->expects($this->once())->method('remove')->with('_security.admin.target_path');
$requestWithSession = Request::create('/');
$requestWithSession->setSession($session);
$urlGenerator = $this->createMock(UrlGeneratorInterface::class);
$urlGenerator->expects($this->any())->method('generate')->willReturn('http://localhost/login');
$httpUtils = new HttpUtils($urlGenerator);
$token = $this->createMock(TokenInterface::class);
$handler = new DefaultAuthenticationSuccessHandler($httpUtils);
$handler->setFirewallName('admin');
$this->assertSame('http://localhost/admin/dashboard', $handler->onAuthenticationSuccess($requestWithSession, $token)->getTargetUrl());
}
public function testStatelessRequestRedirections()
{
$session = $this->createMock(SessionInterface::class);
$session->expects($this->never())->method('get')->with('_security.admin.target_path');
$session->expects($this->never())->method('remove')->with('_security.admin.target_path');
$statelessRequest = Request::create('/');
$statelessRequest->setSession($session);
$statelessRequest->attributes->set('_stateless', true);
$urlGenerator = $this->createMock(UrlGeneratorInterface::class);
$urlGenerator->expects($this->any())->method('generate')->willReturn('http://localhost/login');
$httpUtils = new HttpUtils($urlGenerator);
$token = $this->createMock(TokenInterface::class);
$handler = new DefaultAuthenticationSuccessHandler($httpUtils);
$handler->setFirewallName('admin');
$this->assertSame('http://localhost/', $handler->onAuthenticationSuccess($statelessRequest, $token)->getTargetUrl());
}
public static function getRequestRedirections()
{
return [
'default' => [
Request::create('/'),
[],
'/',
],
'forced target path' => [
Request::create('/'),
['always_use_default_target_path' => true, 'default_target_path' => '/dashboard'],
'/dashboard',
],
'target path as query string' => [
Request::create('/?_target_path=/dashboard'),
[],
'/dashboard',
],
'target path name as query string is customized' => [
Request::create('/?_my_target_path=/dashboard'),
['target_path_parameter' => '_my_target_path'],
'/dashboard',
],
'target path name as query string is customized and nested' => [
Request::create('/?_target_path[value]=/dashboard'),
['target_path_parameter' => '_target_path[value]'],
'/dashboard',
],
'target path as referer' => [
Request::create('/', 'GET', [], [], [], ['HTTP_REFERER' => 'http://localhost/dashboard']),
['use_referer' => true],
'/dashboard',
],
'target path as referer is ignored if not configured' => [
Request::create('/', 'GET', [], [], [], ['HTTP_REFERER' => 'http://localhost/dashboard']),
[],
'/',
],
'target path as referer when referer not set' => [
Request::create('/'),
['use_referer' => true],
'/',
],
'target path as referer when referer is ?' => [
Request::create('/', 'GET', [], [], [], ['HTTP_REFERER' => '?']),
['use_referer' => true],
'/',
],
'target path should be different than login URL' => [
Request::create('/', 'GET', [], [], [], ['HTTP_REFERER' => 'http://localhost/login']),
['use_referer' => true, 'login_path' => '/login'],
'/',
],
'target path should be different than login URL (query string does not matter)' => [
Request::create('/', 'GET', [], [], [], ['HTTP_REFERER' => 'http://localhost/login?t=1&p=2']),
['use_referer' => true, 'login_path' => '/login'],
'/',
],
'target path should be different than login URL (login_path as a route)' => [
Request::create('/', 'GET', [], [], [], ['HTTP_REFERER' => 'http://localhost/login?t=1&p=2']),
['use_referer' => true, 'login_path' => 'login_route'],
'/',
],
];
}
public function testTargetPathFromRequestWithInvalidUrl()
{
$httpUtils = $this->createMock(HttpUtils::class);
$options = ['target_path_parameter' => '_my_target_path'];
$token = $this->createMock(TokenInterface::class);
$request = $this->createMock(Request::class);
$request->expects($this->once())
->method('get')->with('_my_target_path')
->willReturn('some_route_name');
$logger = $this->createMock(LoggerInterface::class);
$logger->expects($this->once())
->method('debug')
->with('Ignoring query parameter "_my_target_path": not a valid URL.');
$handler = new DefaultAuthenticationSuccessHandler($httpUtils, $options, $logger);
$handler->onAuthenticationSuccess($request, $token);
}
public function testTargetPathWithAbsoluteUrlFromRequest()
{
$options = ['target_path_parameter' => '_my_target_path'];
$request = $this->createMock(Request::class);
$request->expects($this->once())
->method('get')->with('_my_target_path')
->willReturn('https://localhost/some-path');
$httpUtils = $this->createMock(HttpUtils::class);
$httpUtils->expects($this->once())
->method('createRedirectResponse')->with($request, 'https://localhost/some-path');
$handler = new DefaultAuthenticationSuccessHandler($httpUtils, $options);
$handler->onAuthenticationSuccess($request, $this->createMock(TokenInterface::class));
}
}
|