Fix Authentication workflow

Fix ldap backends to use Credentials to work with. Fix
some tests to use include right files.

refs #4340
This commit is contained in:
Marius Hein 2013-06-25 12:24:52 +02:00
parent 8192c19424
commit a1327a384c
4 changed files with 27 additions and 15 deletions

View File

@ -6,6 +6,9 @@ namespace Icinga\Authentication;
class Backend class Backend
{ {
/**
* @var UserBackend
*/
protected $userBackend; protected $userBackend;
public function __construct($config) public function __construct($config)

View File

@ -5,6 +5,8 @@
namespace Icinga\Authentication\Backend; namespace Icinga\Authentication\Backend;
use Icinga\Authentication\User as User; use Icinga\Authentication\User as User;
use Icinga\Authentication\UserBackend;
use Icinga\Authentication\Credentials;
use Icinga\Protocol\Ldap; use Icinga\Protocol\Ldap;
class LdapUserBackend implements UserBackend class LdapUserBackend implements UserBackend
@ -16,14 +18,11 @@ class LdapUserBackend implements UserBackend
$this->connection = new Ldap\Connection($config); $this->connection = new Ldap\Connection($config);
} }
public function hasUsername($username) public function hasUsername(Credentials $credential)
{ {
if (!$username) {
return false;
}
return $this->connection->fetchOne( return $this->connection->fetchOne(
$this->selectUsername($username) $this->selectUsername($credential->getUsername())
) === $username; ) === $credential->getUsername();
} }
protected function stripAsterisks($string) protected function stripAsterisks($string)
@ -38,19 +37,15 @@ class LdapUserBackend implements UserBackend
->where('sAMAccountName', $this->stripAsterisks($username)); ->where('sAMAccountName', $this->stripAsterisks($username));
} }
public function authenticate($username, $password = null) public function authenticate(Credentials $credentials)
{ {
if (empty($username) || empty($password)) {
return false;
}
if (!$this->connection->testCredentials( if (!$this->connection->testCredentials(
$this->connection->fetchDN($this->selectUsername($username)), $this->connection->fetchDN($this->selectUsername($credentials->getUsername())),
$password $credentials->getPassword()
) ) { ) ) {
return false; return false;
} }
$user = new User($username); $user = new User($credentials->getUsername());
return $user; return $user;
} }

View File

@ -1,15 +1,28 @@
<?php <?php
// {{{ICINGA_LICENSE_HEADER}}} // {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}// {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}}} // {{{ICINGA_LICENSE_HEADER}}}}
namespace Icinga\Authentication; namespace Icinga\Authentication;
interface UserBackend interface UserBackend
{ {
/**
* Creates a new object
* @param $config
*/
public function __construct($config); public function __construct($config);
/**
* Test if the username exists
* @param Credentials $credentials
* @return boolean
*/
public function hasUsername(Credentials $credentials); public function hasUsername(Credentials $credentials);
/**
* Authenticate
* @param Credentials $credentials
* @return User
*/
public function authenticate(Credentials $credentials); public function authenticate(Credentials $credentials);
} }

View File

@ -7,6 +7,7 @@ namespace Tests\Icinga\Authentication;
require_once("../../library/Icinga/Authentication/Session.php"); require_once("../../library/Icinga/Authentication/Session.php");
require_once("../../library/Icinga/Authentication/PhpSession.php"); require_once("../../library/Icinga/Authentication/PhpSession.php");
require_once("../../library/Icinga/Application/Logger.php"); require_once("../../library/Icinga/Application/Logger.php");
require_once("../../library/Icinga/Exception/ConfigurationError.php");
require_once("Zend/Log.php"); require_once("Zend/Log.php");
use Icinga\Authentication\PhpSession as PhpSession; use Icinga\Authentication\PhpSession as PhpSession;