|
| 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 | +namespaceSymfony\Component\Security\Core\Tests\Authentication\Provider; |
| 13 | + |
| 14 | +usePHPUnit\Framework\TestCase; |
| 15 | +usePsr\Container\ContainerInterface; |
| 16 | +useSymfony\Component\EventDispatcher\EventDispatcher; |
| 17 | +useSymfony\Component\HttpFoundation\Request; |
| 18 | +useSymfony\Component\HttpFoundation\Response; |
| 19 | +useSymfony\Component\HttpFoundation\Session\Session; |
| 20 | +useSymfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage; |
| 21 | +useSymfony\Component\Ldap\Adapter\AdapterInterface; |
| 22 | +useSymfony\Component\Ldap\Adapter\CollectionInterface; |
| 23 | +useSymfony\Component\Ldap\Adapter\ConnectionInterface; |
| 24 | +useSymfony\Component\Ldap\Adapter\QueryInterface; |
| 25 | +useSymfony\Component\Ldap\Entry; |
| 26 | +useSymfony\Component\Ldap\Exception\ConnectionException; |
| 27 | +useSymfony\Component\Ldap\Ldap; |
| 28 | +useSymfony\Component\Ldap\Security\CheckLdapCredentialsListener; |
| 29 | +useSymfony\Component\Ldap\Security\LdapAuthenticator; |
| 30 | +useSymfony\Component\Ldap\Security\LdapUserProvider; |
| 31 | +useSymfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage; |
| 32 | +useSymfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; |
| 33 | +useSymfony\Component\Security\Core\User\ChainUserProvider; |
| 34 | +useSymfony\Component\Security\Core\User\InMemoryUserProvider; |
| 35 | +useSymfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface; |
| 36 | +useSymfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface; |
| 37 | +useSymfony\Component\Security\Http\Authentication\AuthenticatorManager; |
| 38 | +useSymfony\Component\Security\Http\Authenticator\FormLoginAuthenticator; |
| 39 | +useSymfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge; |
| 40 | +useSymfony\Component\Security\Http\Authenticator\Passport\Credentials\PasswordCredentials; |
| 41 | +useSymfony\Component\Security\Http\Event\CheckPassportEvent; |
| 42 | +useSymfony\Component\Security\Http\EventListener\UserProviderListener; |
| 43 | +useSymfony\Component\Security\Http\HttpUtils; |
| 44 | + |
| 45 | +class ChainProviderWithLdapTestextends TestCase |
| 46 | +{ |
| 47 | +publicfunctionprovideChainWithLdapAndInMemory():array |
| 48 | + { |
| 49 | +return [ |
| 50 | +'in memory' => ['foo','foopass'], |
| 51 | +'ldap' => ['bar','barpass'], |
| 52 | + ]; |
| 53 | + } |
| 54 | + |
| 55 | +/** |
| 56 | + * @dataProvider provideChainWithLdapAndInMemory |
| 57 | + */ |
| 58 | +publicfunctiontestChainWithLdapAndInMemory(string$userIdentifier,string$pass) |
| 59 | + { |
| 60 | +$inMemoryProvider =newInMemoryUserProvider([ |
| 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(staticfunction (?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 | +thrownewConnectionException('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(newEntry('cn=bar,dc=example,dc=com', ['sAMAccountName' => ['bar'],'userPassword' => ['barpass']])) |
| 108 | + ; |
| 109 | + |
| 110 | +$ldapProvider =newLdapUserProvider($ldap =newLdap($ldapAdapteur),'dc=example,dc=com','admin','adminpass', [],null,null,'userPassword'); |
| 111 | + |
| 112 | +$chainUserProvider =newChainUserProvider([$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(newResponse()) |
| 124 | + ; |
| 125 | + |
| 126 | +$formLoginAuthenticator =newFormLoginAuthenticator( |
| 127 | +$httpUtils, |
| 128 | +$chainUserProvider, |
| 129 | +$this->createMock(AuthenticationSuccessHandlerInterface::class), |
| 130 | +$failureHandler, |
| 131 | + [] |
| 132 | + ); |
| 133 | + |
| 134 | +$ldapAuthenticator =newLdapAuthenticator($formLoginAuthenticator,'ldap-id'); |
| 135 | + |
| 136 | +$ldapLocator =newclass($ldap)implements ContainerInterface { |
| 137 | +private$ldap; |
| 138 | + |
| 139 | +publicfunction__construct(Ldap$ldap) |
| 140 | + { |
| 141 | +$this->ldap =$ldap; |
| 142 | + } |
| 143 | + |
| 144 | +publicfunctionget(string$id):Ldap |
| 145 | + { |
| 146 | +return$this->ldap; |
| 147 | + } |
| 148 | + |
| 149 | +publicfunctionhas(string$id):bool |
| 150 | + { |
| 151 | +return'ldap-id' ===$id; |
| 152 | + } |
| 153 | + }; |
| 154 | + |
| 155 | +$eventDispatcher =newEventDispatcher(); |
| 156 | +$eventDispatcher->addListener(CheckPassportEvent::class, [newUserProviderListener($chainUserProvider),'checkPassport']); |
| 157 | +$eventDispatcher->addListener(CheckPassportEvent::class, [newCheckLdapCredentialsListener($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 =newAuthenticatorManager( |
| 175 | + [$ldapAuthenticator], |
| 176 | +$tokenStorage =newTokenStorage(), |
| 177 | +$eventDispatcher, |
| 178 | +'main' |
| 179 | + ); |
| 180 | + |
| 181 | +$request = Request::create('/login','POST', ['_username' =>$userIdentifier,'_password' =>$pass]); |
| 182 | +$request->setSession(newSession(newMockArraySessionStorage())); |
| 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 | +} |