| <?php |
|
|
| namespace Kanboard\Auth; |
|
|
| use LogicException; |
| use Kanboard\Core\Base; |
| use Kanboard\Core\Ldap\Client as LdapClient; |
| use Kanboard\Core\Ldap\ClientException as LdapException; |
| use Kanboard\Core\Ldap\User as LdapUser; |
| use Kanboard\Core\Security\PasswordAuthenticationProviderInterface; |
|
|
| |
| |
| |
| |
| |
| |
| class LdapAuth extends Base implements PasswordAuthenticationProviderInterface |
| { |
| |
| |
| |
| |
| |
| |
| protected $userInfo = null; |
|
|
| |
| |
| |
| |
| |
| |
| protected $username = ''; |
|
|
| |
| |
| |
| |
| |
| |
| protected $password = ''; |
|
|
| |
| |
| |
| |
| |
| |
| public function getName() |
| { |
| return 'LDAP'; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| public function authenticate() |
| { |
| try { |
|
|
| $client = LdapClient::connect($this->getLdapUsername(), $this->getLdapPassword()); |
| $client->setLogger($this->logger); |
|
|
| $user = LdapUser::getUser($client, $this->username); |
|
|
| if ($user === null) { |
| $this->logger->info('User ('.$this->username.') not found in LDAP server'); |
| return false; |
| } |
|
|
| if ($user->getUsername() === '') { |
| throw new LogicException('Username not found in LDAP profile, check the parameter LDAP_USER_ATTRIBUTE_USERNAME'); |
| } |
|
|
| $this->logger->info('Authenticate this user: '.$user->getDn()); |
|
|
| if ($client->authenticate($user->getDn(), $this->password)) { |
| $this->userInfo = $user; |
| return true; |
| } |
|
|
| } catch (LdapException $e) { |
| $this->logger->error($e->getMessage()); |
| } |
|
|
| return false; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| public function getUser() |
| { |
| return $this->userInfo; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| public function setUsername($username) |
| { |
| $this->username = $username; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| public function setPassword($password) |
| { |
| $this->password = $password; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| public function getLdapUsername() |
| { |
| switch ($this->getLdapBindType()) { |
| case 'proxy': |
| return LDAP_USERNAME; |
| case 'user': |
| return sprintf(LDAP_USERNAME, $this->username); |
| default: |
| return null; |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| public function getLdapPassword() |
| { |
| switch ($this->getLdapBindType()) { |
| case 'proxy': |
| return LDAP_PASSWORD; |
| case 'user': |
| return $this->password; |
| default: |
| return null; |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| public function getLdapBindType() |
| { |
| if (LDAP_BIND_TYPE !== 'user' && LDAP_BIND_TYPE !== 'proxy' && LDAP_BIND_TYPE !== 'anonymous') { |
| throw new LogicException('Wrong value for the parameter LDAP_BIND_TYPE'); |
| } |
|
|
| return LDAP_BIND_TYPE; |
| } |
| } |
|
|