LdapBindAuthenticationProvider.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Security\Core\Authentication\Provider;
  11. use Symfony\Component\Ldap\Exception\ConnectionException;
  12. use Symfony\Component\Ldap\LdapInterface;
  13. use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
  14. use Symfony\Component\Security\Core\Exception\BadCredentialsException;
  15. use Symfony\Component\Security\Core\Exception\LogicException;
  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. /**
  21. * LdapBindAuthenticationProvider authenticates a user against an LDAP server.
  22. *
  23. * The only way to check user credentials is to try to connect the user with its
  24. * credentials to the ldap.
  25. *
  26. * @author Charles Sarrazin <charles@sarraz.in>
  27. */
  28. class LdapBindAuthenticationProvider extends UserAuthenticationProvider
  29. {
  30. private $userProvider;
  31. private $ldap;
  32. private $dnString;
  33. private $queryString;
  34. private $searchDn;
  35. private $searchPassword;
  36. public function __construct(UserProviderInterface $userProvider, UserCheckerInterface $userChecker, string $providerKey, LdapInterface $ldap, string $dnString = '{username}', bool $hideUserNotFoundExceptions = true, string $searchDn = '', string $searchPassword = '')
  37. {
  38. parent::__construct($userChecker, $providerKey, $hideUserNotFoundExceptions);
  39. $this->userProvider = $userProvider;
  40. $this->ldap = $ldap;
  41. $this->dnString = $dnString;
  42. $this->searchDn = $searchDn;
  43. $this->searchPassword = $searchPassword;
  44. }
  45. /**
  46. * Set a query string to use in order to find a DN for the username.
  47. */
  48. public function setQueryString(string $queryString)
  49. {
  50. $this->queryString = $queryString;
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. protected function retrieveUser(string $username, UsernamePasswordToken $token)
  56. {
  57. if (AuthenticationProviderInterface::USERNAME_NONE_PROVIDED === $username) {
  58. throw new UsernameNotFoundException('Username can not be null.');
  59. }
  60. return $this->userProvider->loadUserByUsername($username);
  61. }
  62. /**
  63. * {@inheritdoc}
  64. */
  65. protected function checkAuthentication(UserInterface $user, UsernamePasswordToken $token)
  66. {
  67. $username = $token->getUsername();
  68. $password = $token->getCredentials();
  69. if ('' === (string) $password) {
  70. throw new BadCredentialsException('The presented password must not be empty.');
  71. }
  72. try {
  73. if ($this->queryString) {
  74. if ('' !== $this->searchDn && '' !== $this->searchPassword) {
  75. $this->ldap->bind($this->searchDn, $this->searchPassword);
  76. } else {
  77. throw new LogicException('Using the "query_string" config without using a "search_dn" and a "search_password" is not supported.');
  78. }
  79. $username = $this->ldap->escape($username, '', LdapInterface::ESCAPE_FILTER);
  80. $query = str_replace('{username}', $username, $this->queryString);
  81. $result = $this->ldap->query($this->dnString, $query)->execute();
  82. if (1 !== $result->count()) {
  83. throw new BadCredentialsException('The presented username is invalid.');
  84. }
  85. $dn = $result[0]->getDn();
  86. } else {
  87. $username = $this->ldap->escape($username, '', LdapInterface::ESCAPE_DN);
  88. $dn = str_replace('{username}', $username, $this->dnString);
  89. }
  90. $this->ldap->bind($dn, $password);
  91. } catch (ConnectionException $e) {
  92. throw new BadCredentialsException('The presented password is invalid.');
  93. }
  94. }
  95. }