Skip to content

Commit

Permalink
Fix retrieveByCredentials and validateCredentials closes (#36 #35)
Browse files Browse the repository at this point in the history
* Removing auth from retrieveByCredentials to validateCredentials

* Added StyleCI analysis
  • Loading branch information
SaschaDens committed Apr 16, 2016
1 parent f3f8142 commit 56c6371
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 18 deletions.
20 changes: 15 additions & 5 deletions spec/Dsdevbe/LdapConnector/LdapUserProviderSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,42 @@
namespace spec\Dsdevbe\LdapConnector;

use Dsdevbe\LdapConnector\Adapter\LdapInterface;
use Illuminate\Contracts\Hashing\Hasher as HasherContract;
use Illuminate\Contracts\Auth\Authenticatable;
use PhpSpec\ObjectBehavior;

class LdapUserProviderSpec extends ObjectBehavior
{
public function let(HasherContract $hasher, LdapInterface $interface)
public function let(LdapInterface $interface)
{
$this->beConstructedWith($hasher, $interface);
$this->beConstructedWith($interface);
}

public function it_is_initializable()
{
$this->shouldHaveType('Dsdevbe\LdapConnector\LdapUserProvider');
}

public function it_validate_user_by_credentials(LdapInterface $interface)
public function it_validate_user_by_credentials()
{
$user = [
'username' => 'john.doe@example.com',
'password' => 'johnpassdoe',
];

$interface->connect($user['username'], $user['password'])->shouldBeCalled();
$this->retrieveByCredentials($user);
}

public function it_validate_password_against_ldap(LdapInterface $interface, Authenticatable $user)
{
$credentials = [
'username' => 'john.doe@example.com',
'password' => 'johnpassdoe',
];

$interface->connect($credentials['username'], $credentials['password'])->shouldBeCalled();
$this->validateCredentials($user, $credentials);
}

public function it_retrieves_user_by_id(LdapInterface $interface)
{
$identifier = 'john.doe@example.com';
Expand Down
8 changes: 8 additions & 0 deletions src/Dsdevbe/LdapConnector/Adapter/Adldap.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ public function getUserInfo($username, $password = null)
return $this->mapDataToUserModel($user, $password);
}

/**
* @return bool
*/
public function isConnectedToLdap()
{
return $this->_ldap->getConnection()->isBound();
}

protected function mapDataToUserModel(adLDAPUserModel $user, $password)
{
$model = new UserModel([
Expand Down
5 changes: 5 additions & 0 deletions src/Dsdevbe/LdapConnector/Adapter/LdapInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,9 @@ public function connect($username, $password);
* @return UserModel
*/
public function getUserInfo($username, $password = null);

/**
* @return bool
*/
public function isConnectedToLdap();
}
2 changes: 1 addition & 1 deletion src/Dsdevbe/LdapConnector/LdapConnectorServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function boot()
}
$ldap = new Adldap($app['hash'], $config['adldap']);

return new LdapUserProvider($app['hash'], $ldap);
return new LdapUserProvider($ldap);
});
}

Expand Down
17 changes: 5 additions & 12 deletions src/Dsdevbe/LdapConnector/LdapUserProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,16 @@
use Dsdevbe\LdapConnector\Adapter\LdapInterface;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Contracts\Auth\UserProvider as UserProviderInterface;
use Illuminate\Contracts\Hashing\Hasher as HasherContract;

class LdapUserProvider implements UserProviderInterface
{
/**
* @var HasherContract
*/
protected $_hasher;

/**
* @var LdapInterface;
*/
protected $_adapter;

public function __construct(HasherContract $hasher, LdapInterface $adapter)
public function __construct(LdapInterface $adapter)
{
$this->_hasher = $hasher;
$this->_adapter = $adapter;
}

Expand Down Expand Up @@ -77,10 +70,10 @@ public function updateRememberToken(Authenticatable $user, $token)
*/
public function retrieveByCredentials(array $credentials)
{
$username = $credentials['username'];
$password = $credentials['password'];
if ($this->_adapter->isConnectedToLdap()) {
$username = $credentials['username'];
$password = $credentials['password'];

if ($this->_adapter->connect($username, $password)) {
return $this->_adapter->getUserInfo($username, $password);
}
}
Expand All @@ -95,6 +88,6 @@ public function retrieveByCredentials(array $credentials)
*/
public function validateCredentials(Authenticatable $user, array $credentials)
{
return $this->_hasher->check($credentials['password'], $user->getAuthPassword());
return $this->_adapter->connect($credentials['username'], $credentials['password']);
}
}

0 comments on commit 56c6371

Please sign in to comment.