-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Security][Ldap] Fixed issue with password attribute containing an array of values. #18881
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ | |
namespace Symfony\Component\Security\Core\User; | ||
|
||
use Symfony\Component\Ldap\Entry; | ||
use Symfony\Component\Security\Core\Exception\InvalidArgumentException; | ||
use Symfony\Component\Security\Core\Exception\UnsupportedUserException; | ||
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException; | ||
use Symfony\Component\Ldap\Exception\ConnectionException; | ||
|
@@ -31,6 +32,7 @@ class LdapUserProvider implements UserProviderInterface | |
private $searchPassword; | ||
private $defaultRoles; | ||
private $defaultSearch; | ||
private $passwordAttribute; | ||
|
||
/** | ||
* @param LdapInterface $ldap | ||
|
@@ -41,14 +43,15 @@ class LdapUserProvider implements UserProviderInterface | |
* @param string $uidKey | ||
* @param string $filter | ||
*/ | ||
public function __construct(LdapInterface $ldap, $baseDn, $searchDn = null, $searchPassword = null, array $defaultRoles = array(), $uidKey = 'sAMAccountName', $filter = '({uid_key}={username})') | ||
public function __construct(LdapInterface $ldap, $baseDn, $searchDn = null, $searchPassword = null, array $defaultRoles = array(), $uidKey = 'sAMAccountName', $filter = '({uid_key}={username})', $passwordAttribute = null) | ||
{ | ||
$this->ldap = $ldap; | ||
$this->baseDn = $baseDn; | ||
$this->searchDn = $searchDn; | ||
$this->searchPassword = $searchPassword; | ||
$this->defaultRoles = $defaultRoles; | ||
$this->defaultSearch = str_replace('{uid_key}', $uidKey, $filter); | ||
$this->passwordAttribute = $passwordAttribute; | ||
} | ||
|
||
/** | ||
|
@@ -99,8 +102,42 @@ public function supportsClass($class) | |
return $class === 'Symfony\Component\Security\Core\User\User'; | ||
} | ||
|
||
/** | ||
* Loads a user from an LDAP entry. | ||
* | ||
* @param string $username | ||
* @param Entry $entry | ||
* | ||
* @return User | ||
*/ | ||
private function loadUser($username, Entry $entry) | ||
{ | ||
return new User($username, $entry->getAttribute('userpassword'), $this->defaultRoles); | ||
$password = $this->getPassword($entry); | ||
|
||
return new User($username, $password, $this->defaultRoles); | ||
} | ||
|
||
/** | ||
* Fetches the password from an LDAP entry. | ||
* | ||
* @param null|Entry $entry | ||
*/ | ||
private function getPassword(Entry $entry) | ||
{ | ||
if (null === $this->passwordAttribute) { | ||
return; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. under what circumstance does it make sense to return There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It makes sense when you use LDAP Bind based authentication mechanisms, which do not rely on the password being provided as an LDAP attribute. |
||
} | ||
|
||
if (!$entry->hasAttribute($this->passwordAttribute)) { | ||
throw new InvalidArgumentException(sprintf('Missing attribute "%s" for user "%s".', $this->passwordAttribute, $entry->getDn())); | ||
} | ||
|
||
$values = $entry->getAttribute($this->passwordAttribute); | ||
|
||
if (1 !== count($values)) { | ||
throw new InvalidArgumentException(sprintf('Attribute "%s" has multiple values.', $this->passwordAttribute)); | ||
} | ||
|
||
return $values[0]; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could be inlined