|
| 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\Authentication\Provider; |
| 13 | + |
| 14 | +use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; |
| 15 | +use Symfony\Component\Security\Core\Exception\BadCredentialsException; |
| 16 | +use Symfony\Component\Security\Core\Exception\UsernameNotFoundException; |
| 17 | +use Symfony\Component\Security\Core\User\UserCheckerInterface; |
| 18 | +use Symfony\Component\Security\Core\User\UserInterface; |
| 19 | +use Symfony\Component\Security\Core\User\UserProviderInterface; |
| 20 | +use Symfony\Component\Ldap\LdapInterface; |
| 21 | +use Symfony\Component\Ldap\Exception\ConnectionException; |
| 22 | + |
| 23 | +/** |
| 24 | + * LdapQueryAuthenticationProvider authenticates a user against an LDAP server. |
| 25 | + * |
| 26 | + * The only way to check user credentials is to first find the DN via query and then bind |
| 27 | + * |
| 28 | + * @author Charles Sarrazin <charles@sarraz.in> |
| 29 | + * @author Lukas Kahwe Smith <smith@pooteeweet.org> |
| 30 | + */ |
| 31 | +class LdapQueryAuthenticationProvider extends UserAuthenticationProvider |
| 32 | +{ |
| 33 | + private $userProvider; |
| 34 | + private $ldap; |
| 35 | + private $dnString; |
| 36 | + private $queryString; |
| 37 | + |
| 38 | + /** |
| 39 | + * Constructor. |
| 40 | + * |
| 41 | + * @param UserProviderInterface $userProvider A UserProvider |
| 42 | + * @param UserCheckerInterface $userChecker A UserChecker |
| 43 | + * @param string $providerKey The provider key |
| 44 | + * @param LdapInterface $ldap A Ldap client |
| 45 | + * @param string $dnString A string with the query DN |
| 46 | + * @param string $queryString A string used to create the query query |
| 47 | + * @param bool $hideUserNotFoundExceptions Whether to hide user not found exception or not |
| 48 | + */ |
| 49 | + public function __construct(UserProviderInterface $userProvider, UserCheckerInterface $userChecker, $providerKey, LdapInterface $ldap, $dnString, $queryString = '{username}', $hideUserNotFoundExceptions = true) |
| 50 | + { |
| 51 | + parent::__construct($userChecker, $providerKey, $hideUserNotFoundExceptions); |
| 52 | + |
| 53 | + $this->userProvider = $userProvider; |
| 54 | + $this->ldap = $ldap; |
| 55 | + $this->dnString = $dnString; |
| 56 | + $this->queryString = $queryString; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * {@inheritdoc} |
| 61 | + */ |
| 62 | + protected function retrieveUser($username, UsernamePasswordToken $token) |
| 63 | + { |
| 64 | + if (AuthenticationProviderInterface::USERNAME_NONE_PROVIDED === $username) { |
| 65 | + throw new UsernameNotFoundException('Username can not be null'); |
| 66 | + } |
| 67 | + |
| 68 | + return $this->userProvider->loadUserByUsername($username); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * {@inheritdoc} |
| 73 | + */ |
| 74 | + protected function checkAuthentication(UserInterface $user, UsernamePasswordToken $token) |
| 75 | + { |
| 76 | + $username = $token->getUsername(); |
| 77 | + $password = $token->getCredentials(); |
| 78 | + |
| 79 | + if ('' === $password) { |
| 80 | + throw new BadCredentialsException('The presented password must not be empty.'); |
| 81 | + } |
| 82 | + |
| 83 | + try { |
| 84 | + $username = $this->ldap->escape($username, '', LdapInterface::ESCAPE_DN); |
| 85 | + $query = str_replace('{username}', $username, $this->queryString); |
| 86 | + |
| 87 | + $query = $this->ldap->query($this->dnString, $query); |
| 88 | + $result = $query->execute(); |
| 89 | + if (1 !== $result->count()) { |
| 90 | + throw new BadCredentialsException('The presented password is invalid.'); |
| 91 | + } |
| 92 | + |
| 93 | + $this->ldap->bind($result->getIterator()->current()->getDn(), $password); |
| 94 | + } catch (ConnectionException $e) { |
| 95 | + throw new BadCredentialsException('The presented password is invalid.'); |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments