|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 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\HttpKernel\Exception\BadRequestHttpException; |
| 17 | +use Symfony\Component\Security\Core\Exception\BadCredentialsException; |
| 18 | +use Symfony\Component\Security\Core\Security; |
| 19 | +use Symfony\Component\Security\Core\User\UserProviderInterface; |
| 20 | +use Symfony\Component\Security\Http\Authenticator\JsonLoginAuthenticator; |
| 21 | +use Symfony\Component\Security\Http\HttpUtils; |
| 22 | + |
| 23 | +class JsonLoginAuthenticatorTest extends TestCase |
| 24 | +{ |
| 25 | + private $userProvider; |
| 26 | + /** @var JsonLoginAuthenticator */ |
| 27 | + private $authenticator; |
| 28 | + |
| 29 | + protected function setUp(): void |
| 30 | + { |
| 31 | + $this->userProvider = $this->createMock(UserProviderInterface::class); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * @dataProvider provideSupportData |
| 36 | + */ |
| 37 | + public function testSupport($request) |
| 38 | + { |
| 39 | + $this->setUpAuthenticator(); |
| 40 | + |
| 41 | + $this->assertTrue($this->authenticator->supports($request)); |
| 42 | + } |
| 43 | + |
| 44 | + public function provideSupportData() |
| 45 | + { |
| 46 | + yield [new Request([], [], [], [], [], ['HTTP_CONTENT_TYPE' => 'application/json'], '{"username": "dunglas", "password": "foo"}')]; |
| 47 | + |
| 48 | + $request = new Request([], [], [], [], [], [], '{"username": "dunglas", "password": "foo"}'); |
| 49 | + $request->setRequestFormat('json-ld'); |
| 50 | + yield [$request]; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * @dataProvider provideSupportsWithCheckPathData |
| 55 | + */ |
| 56 | + public function testSupportsWithCheckPath($request, $result) |
| 57 | + { |
| 58 | + $this->setUpAuthenticator(['check_path' => '/api/login']); |
| 59 | + |
| 60 | + $this->assertSame($result, $this->authenticator->supports($request)); |
| 61 | + } |
| 62 | + |
| 63 | + public function provideSupportsWithCheckPathData() |
| 64 | + { |
| 65 | + yield [Request::create('/api/login', 'GET', [], [], [], ['HTTP_CONTENT_TYPE' => 'application/json']), true]; |
| 66 | + yield [Request::create('/login', 'GET', [], [], [], ['HTTP_CONTENT_TYPE' => 'application/json']), false]; |
| 67 | + } |
| 68 | + |
| 69 | + public function testGetCredentials() |
| 70 | + { |
| 71 | + $this->setUpAuthenticator(); |
| 72 | + |
| 73 | + $request = new Request([], [], [], [], [], ['HTTP_CONTENT_TYPE' => 'application/json'], '{"username": "dunglas", "password": "foo"}'); |
| 74 | + $this->assertEquals(['username' => 'dunglas', 'password' => 'foo'], $this->authenticator->getCredentials($request)); |
| 75 | + } |
| 76 | + |
| 77 | + public function testGetCredentialsCustomPath() |
| 78 | + { |
| 79 | + $this->setUpAuthenticator([ |
| 80 | + 'username_path' => 'authentication.username', |
| 81 | + 'password_path' => 'authentication.password', |
| 82 | + ]); |
| 83 | + |
| 84 | + $request = new Request([], [], [], [], [], ['HTTP_CONTENT_TYPE' => 'application/json'], '{"authentication": {"username": "dunglas", "password": "foo"}}'); |
| 85 | + $this->assertEquals(['username' => 'dunglas', 'password' => 'foo'], $this->authenticator->getCredentials($request)); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * @dataProvider provideInvalidGetCredentialsData |
| 90 | + */ |
| 91 | + public function testGetCredentialsInvalid($request, $errorMessage, $exceptionType = BadRequestHttpException::class) |
| 92 | + { |
| 93 | + $this->expectException($exceptionType); |
| 94 | + $this->expectExceptionMessage($errorMessage); |
| 95 | + |
| 96 | + $this->setUpAuthenticator(); |
| 97 | + |
| 98 | + $this->authenticator->getCredentials($request); |
| 99 | + } |
| 100 | + |
| 101 | + public function provideInvalidGetCredentialsData() |
| 102 | + { |
| 103 | + $request = new Request([], [], [], [], [], ['HTTP_CONTENT_TYPE' => 'application/json']); |
| 104 | + yield [$request, 'Invalid JSON.']; |
| 105 | + |
| 106 | + $request = new Request([], [], [], [], [], ['HTTP_CONTENT_TYPE' => 'application/json'], '{"usr": "dunglas", "password": "foo"}'); |
| 107 | + yield [$request, 'The key "username" must be provided']; |
| 108 | + |
| 109 | + $request = new Request([], [], [], [], [], ['HTTP_CONTENT_TYPE' => 'application/json'], '{"username": "dunglas", "pass": "foo"}'); |
| 110 | + yield [$request, 'The key "password" must be provided']; |
| 111 | + |
| 112 | + $request = new Request([], [], [], [], [], ['HTTP_CONTENT_TYPE' => 'application/json'], '{"username": 1, "password": "foo"}'); |
| 113 | + yield [$request, 'The key "username" must be a string.']; |
| 114 | + |
| 115 | + $request = new Request([], [], [], [], [], ['HTTP_CONTENT_TYPE' => 'application/json'], '{"username": "dunglas", "password": 1}'); |
| 116 | + yield [$request, 'The key "password" must be a string.']; |
| 117 | + |
| 118 | + $username = str_repeat('x', Security::MAX_USERNAME_LENGTH + 1); |
| 119 | + $request = new Request([], [], [], [], [], ['HTTP_CONTENT_TYPE' => 'application/json'], sprintf('{"username": "%s", "password": 1}', $username)); |
| 120 | + yield [$request, 'Invalid username.', BadCredentialsException::class]; |
| 121 | + } |
| 122 | + |
| 123 | + private function setUpAuthenticator(array $options = []) |
| 124 | + { |
| 125 | + $this->authenticator = new JsonLoginAuthenticator(new HttpUtils(), $this->userProvider, null, null, $options); |
| 126 | + } |
| 127 | +} |
0 commit comments