|
| 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\Core\Tests\Authentication\Provider; |
| 13 | + |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | +use Psr\Container\ContainerInterface; |
| 16 | +use Symfony\Component\EventDispatcher\EventDispatcher; |
| 17 | +use Symfony\Component\HttpFoundation\Request; |
| 18 | +use Symfony\Component\HttpFoundation\Response; |
| 19 | +use Symfony\Component\HttpFoundation\Session\Session; |
| 20 | +use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage; |
| 21 | +use Symfony\Component\Ldap\Adapter\AdapterInterface; |
| 22 | +use Symfony\Component\Ldap\Adapter\CollectionInterface; |
| 23 | +use Symfony\Component\Ldap\Adapter\ConnectionInterface; |
| 24 | +use Symfony\Component\Ldap\Adapter\QueryInterface; |
| 25 | +use Symfony\Component\Ldap\Entry; |
| 26 | +use Symfony\Component\Ldap\Exception\ConnectionException; |
| 27 | +use Symfony\Component\Ldap\Ldap; |
| 28 | +use Symfony\Component\Ldap\Security\CheckLdapCredentialsListener; |
| 29 | +use Symfony\Component\Ldap\Security\LdapAuthenticator; |
| 30 | +use Symfony\Component\Ldap\Security\LdapUserProvider; |
| 31 | +use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage; |
| 32 | +use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; |
| 33 | +use Symfony\Component\Security\Core\User\ChainUserProvider; |
| 34 | +use Symfony\Component\Security\Core\User\InMemoryUserProvider; |
| 35 | +use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface; |
| 36 | +use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface; |
| 37 | +use Symfony\Component\Security\Http\Authentication\AuthenticatorManager; |
| 38 | +use Symfony\Component\Security\Http\Authenticator\FormLoginAuthenticator; |
| 39 | +use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge; |
| 40 | +use Symfony\Component\Security\Http\Authenticator\Passport\Credentials\PasswordCredentials; |
| 41 | +use Symfony\Component\Security\Http\Event\CheckPassportEvent; |
| 42 | +use Symfony\Component\Security\Http\EventListener\UserProviderListener; |
| 43 | +use Symfony\Component\Security\Http\HttpUtils; |
| 44 | + |
| 45 | +class ChainProviderWithLdapTest extends TestCase |
| 46 | +{ |
| 47 | + public function provideChainWithLdapAndInMemory(): array |
| 48 | + { |
| 49 | + return [ |
| 50 | + 'in memory' => ['foo', 'foopass'], |
| 51 | + 'ldap' => ['bar', 'barpass'], |
| 52 | + ]; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * @dataProvider provideChainWithLdapAndInMemory |
| 57 | + */ |
| 58 | + public function testChainWithLdapAndInMemory(string $userIdentifier, string $pass) |
| 59 | + { |
| 60 | + $inMemoryProvider = new InMemoryUserProvider([ |
| 61 | + 'foo' => ['password' => 'foopass', 'roles' => ['ROLE_USER']], |
| 62 | + ]); |
| 63 | + |
| 64 | + $ldapAdapteur = $this->createMock(AdapterInterface::class); |
| 65 | + $ldapAdapteur |
| 66 | + ->method('getConnection') |
| 67 | + ->willReturn($connection = $this->createMock(ConnectionInterface::class)) |
| 68 | + ; |
| 69 | + |
| 70 | + $connection |
| 71 | + ->method('bind') |
| 72 | + ->willReturnCallback(static function (?string $user, ?string $pass): void { |
| 73 | + if ('admin' === $user && 'adminpass' === $pass) { |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + if ('bar' === $user && 'barpass' === $pass) { |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + throw new ConnectionException('failure when binding'); |
| 82 | + }) |
| 83 | + ; |
| 84 | + |
| 85 | + $ldapAdapteur |
| 86 | + ->method('escape') |
| 87 | + ->willReturnArgument(0) |
| 88 | + ; |
| 89 | + |
| 90 | + $ldapAdapteur |
| 91 | + ->method('createQuery') |
| 92 | + ->willReturn($query = $this->createMock(QueryInterface::class)) |
| 93 | + ; |
| 94 | + |
| 95 | + $query |
| 96 | + ->method('execute') |
| 97 | + ->willReturn($collection = $this->createMock(CollectionInterface::class)); |
| 98 | + |
| 99 | + $collection |
| 100 | + ->method('count') |
| 101 | + ->willReturn(1) |
| 102 | + ; |
| 103 | + |
| 104 | + $collection |
| 105 | + ->method('offsetGet') |
| 106 | + ->with(0) |
| 107 | + ->willReturn(new Entry('cn=bar,dc=example,dc=com', ['sAMAccountName' => ['bar'], 'userPassword' => ['barpass']])) |
| 108 | + ; |
| 109 | + |
| 110 | + $ldapProvider = new LdapUserProvider($ldap = new Ldap($ldapAdapteur), 'dc=example,dc=com', 'admin', 'adminpass', [], null, null, 'userPassword'); |
| 111 | + |
| 112 | + $chainUserProvider = new ChainUserProvider([$inMemoryProvider, $ldapProvider]); |
| 113 | + |
| 114 | + $httpUtils = $this->createMock(HttpUtils::class); |
| 115 | + $httpUtils |
| 116 | + ->method('checkRequestPath') |
| 117 | + ->willReturn(true) |
| 118 | + ; |
| 119 | + |
| 120 | + $failureHandler = $this->createMock(AuthenticationFailureHandlerInterface::class); |
| 121 | + $failureHandler |
| 122 | + ->method('onAuthenticationFailure') |
| 123 | + ->willReturn(new Response()) |
| 124 | + ; |
| 125 | + |
| 126 | + $formLoginAuthenticator = new FormLoginAuthenticator( |
| 127 | + $httpUtils, |
| 128 | + $chainUserProvider, |
| 129 | + $this->createMock(AuthenticationSuccessHandlerInterface::class), |
| 130 | + $failureHandler, |
| 131 | + [] |
| 132 | + ); |
| 133 | + |
| 134 | + $ldapAuthenticator = new LdapAuthenticator($formLoginAuthenticator, 'ldap-id'); |
| 135 | + |
| 136 | + $ldapLocator = new class($ldap) implements ContainerInterface { |
| 137 | + private $ldap; |
| 138 | + |
| 139 | + public function __construct(Ldap $ldap) |
| 140 | + { |
| 141 | + $this->ldap = $ldap; |
| 142 | + } |
| 143 | + |
| 144 | + public function get(string $id): Ldap |
| 145 | + { |
| 146 | + return $this->ldap; |
| 147 | + } |
| 148 | + |
| 149 | + public function has(string $id): bool |
| 150 | + { |
| 151 | + return 'ldap-id' === $id; |
| 152 | + } |
| 153 | + }; |
| 154 | + |
| 155 | + $eventDispatcher = new EventDispatcher(); |
| 156 | + $eventDispatcher->addListener(CheckPassportEvent::class, [new UserProviderListener($chainUserProvider), 'checkPassport']); |
| 157 | + $eventDispatcher->addListener(CheckPassportEvent::class, [new CheckLdapCredentialsListener($ldapLocator), 'onCheckPassport']); |
| 158 | + $eventDispatcher->addListener(CheckPassportEvent::class, function (CheckPassportEvent $event): void { |
| 159 | + $passport = $event->getPassport(); |
| 160 | + $userBadge = $passport->getBadge(UserBadge::class); |
| 161 | + if (null === $userBadge || null === $userBadge->getUser()) { |
| 162 | + return; |
| 163 | + } |
| 164 | + $credentials = $passport->getBadge(PasswordCredentials::class); |
| 165 | + if ($credentials->isResolved()) { |
| 166 | + return; |
| 167 | + } |
| 168 | + |
| 169 | + if ($credentials && 'foopass' === $credentials->getPassword()) { |
| 170 | + $credentials->markResolved(); |
| 171 | + } |
| 172 | + }); |
| 173 | + |
| 174 | + $authenticatorManager = new AuthenticatorManager( |
| 175 | + [$ldapAuthenticator], |
| 176 | + $tokenStorage = new TokenStorage(), |
| 177 | + $eventDispatcher, |
| 178 | + 'main' |
| 179 | + ); |
| 180 | + |
| 181 | + $request = Request::create('/login', 'POST', ['_username' => $userIdentifier, '_password' => $pass]); |
| 182 | + $request->setSession(new Session(new MockArraySessionStorage())); |
| 183 | + |
| 184 | + $this->assertTrue($authenticatorManager->supports($request)); |
| 185 | + $authenticatorManager->authenticateRequest($request); |
| 186 | + |
| 187 | + $this->assertInstanceOf(UsernamePasswordToken::class, $token = $tokenStorage->getToken()); |
| 188 | + $this->assertSame($userIdentifier, $token->getUserIdentifier()); |
| 189 | + } |
| 190 | +} |
0 commit comments