|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Security\Http\Tests\Authenticator; |
| 13 | + |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | +use Symfony\Component\HttpFoundation\Request; |
| 16 | +use Symfony\Component\HttpFoundation\Response; |
| 17 | +use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
| 18 | +use Symfony\Component\Security\Http\Authenticator\AbstractLoginFormAuthenticator; |
| 19 | +use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge; |
| 20 | +use Symfony\Component\Security\Http\Authenticator\Passport\Passport; |
| 21 | +use Symfony\Component\Security\Http\Authenticator\Passport\SelfValidatingPassport; |
| 22 | + |
| 23 | +class AbstractLoginFormAuthenticatorTest extends TestCase |
| 24 | +{ |
| 25 | + /** |
| 26 | + * @dataProvider provideSupportsData |
| 27 | + */ |
| 28 | + public function testSupports(string $loginUrl, Request $request, bool $expected) |
| 29 | + { |
| 30 | + $authenticator = new ConcreteFormAuthenticator($loginUrl); |
| 31 | + $this->assertSame($expected, $authenticator->supports($request)); |
| 32 | + } |
| 33 | + |
| 34 | + public function provideSupportsData(): iterable |
| 35 | + { |
| 36 | + yield [ |
| 37 | + '/login', |
| 38 | + Request::create('http://localhost/login', Request::METHOD_POST, [], [], [], [ |
| 39 | + 'DOCUMENT_ROOT' => '/var/www/app/public', |
| 40 | + 'PHP_SELF' => '/index.php', |
| 41 | + 'SCRIPT_FILENAME' => '/var/www/app/public/index.php', |
| 42 | + 'SCRIPT_NAME' => '/index.php', |
| 43 | + ]), |
| 44 | + true, |
| 45 | + ]; |
| 46 | + yield [ |
| 47 | + '/login', |
| 48 | + Request::create('http://localhost/somepath', Request::METHOD_POST, [], [], [], [ |
| 49 | + 'DOCUMENT_ROOT' => '/var/www/app/public', |
| 50 | + 'PHP_SELF' => '/index.php', |
| 51 | + 'SCRIPT_FILENAME' => '/var/www/app/public/index.php', |
| 52 | + 'SCRIPT_NAME' => '/index.php', |
| 53 | + ]), |
| 54 | + false, |
| 55 | + ]; |
| 56 | + yield [ |
| 57 | + '/folder/login', |
| 58 | + Request::create('http://localhost/folder/login', Request::METHOD_POST, [], [], [], [ |
| 59 | + 'DOCUMENT_ROOT' => '/var/www/app/public', |
| 60 | + 'PHP_SELF' => '/folder/index.php', |
| 61 | + 'SCRIPT_FILENAME' => '/var/www/app/public/index.php', |
| 62 | + 'SCRIPT_NAME' => '/folder/index.php', |
| 63 | + ]), |
| 64 | + true, |
| 65 | + ]; |
| 66 | + yield [ |
| 67 | + '/folder/login', |
| 68 | + Request::create('http://localhost/folder/somepath', Request::METHOD_POST, [], [], [], [ |
| 69 | + 'DOCUMENT_ROOT' => '/var/www/app/public', |
| 70 | + 'PHP_SELF' => '/folder/index.php', |
| 71 | + 'SCRIPT_FILENAME' => '/var/www/app/public/index.php', |
| 72 | + 'SCRIPT_NAME' => '/folder/index.php', |
| 73 | + ]), |
| 74 | + false, |
| 75 | + ]; |
| 76 | + yield [ |
| 77 | + '/index.php/login', |
| 78 | + Request::create('http://localhost/index.php/login', Request::METHOD_POST, [], [], [], [ |
| 79 | + 'DOCUMENT_ROOT' => '/var/www/app/public', |
| 80 | + 'PHP_SELF' => '/index.php', |
| 81 | + 'SCRIPT_FILENAME' => '/var/www/app/public/index.php', |
| 82 | + 'SCRIPT_NAME' => '/index.php', |
| 83 | + ]), |
| 84 | + true, |
| 85 | + ]; |
| 86 | + yield [ |
| 87 | + '/index.php/login', |
| 88 | + Request::create('http://localhost/index.php/somepath', Request::METHOD_POST, [], [], [], [ |
| 89 | + 'DOCUMENT_ROOT' => '/var/www/app/public', |
| 90 | + 'PHP_SELF' => '/index.php', |
| 91 | + 'SCRIPT_FILENAME' => '/var/www/app/public/index.php', |
| 92 | + 'SCRIPT_NAME' => '/index.php', |
| 93 | + ]), |
| 94 | + false, |
| 95 | + ]; |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +class ConcreteFormAuthenticator extends AbstractLoginFormAuthenticator |
| 100 | +{ |
| 101 | + private $loginUrl; |
| 102 | + |
| 103 | + public function __construct(string $loginUrl) |
| 104 | + { |
| 105 | + $this->loginUrl = $loginUrl; |
| 106 | + } |
| 107 | + |
| 108 | + protected function getLoginUrl(Request $request): string |
| 109 | + { |
| 110 | + return $this->loginUrl; |
| 111 | + } |
| 112 | + |
| 113 | + public function authenticate(Request $request): Passport |
| 114 | + { |
| 115 | + return new SelfValidatingPassport(new UserBadge('dummy')); |
| 116 | + } |
| 117 | + |
| 118 | + public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response |
| 119 | + { |
| 120 | + return null; |
| 121 | + } |
| 122 | +} |
0 commit comments