From 56c6371fdc59c14bbd90ba926bea55377f0fd129 Mon Sep 17 00:00:00 2001 From: Sascha Dens Date: Sat, 16 Apr 2016 17:47:53 +0200 Subject: [PATCH] Fix retrieveByCredentials and validateCredentials closes (#36 #35) * Removing auth from retrieveByCredentials to validateCredentials * Added StyleCI analysis --- .../LdapConnector/LdapUserProviderSpec.php | 20 ++++++++++++++----- src/Dsdevbe/LdapConnector/Adapter/Adldap.php | 8 ++++++++ .../LdapConnector/Adapter/LdapInterface.php | 5 +++++ .../LdapConnectorServiceProvider.php | 2 +- .../LdapConnector/LdapUserProvider.php | 17 +++++----------- 5 files changed, 34 insertions(+), 18 deletions(-) diff --git a/spec/Dsdevbe/LdapConnector/LdapUserProviderSpec.php b/spec/Dsdevbe/LdapConnector/LdapUserProviderSpec.php index 73adb9a..6ae166c 100644 --- a/spec/Dsdevbe/LdapConnector/LdapUserProviderSpec.php +++ b/spec/Dsdevbe/LdapConnector/LdapUserProviderSpec.php @@ -3,14 +3,14 @@ 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() @@ -18,17 +18,27 @@ 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'; diff --git a/src/Dsdevbe/LdapConnector/Adapter/Adldap.php b/src/Dsdevbe/LdapConnector/Adapter/Adldap.php index e580758..30d798f 100644 --- a/src/Dsdevbe/LdapConnector/Adapter/Adldap.php +++ b/src/Dsdevbe/LdapConnector/Adapter/Adldap.php @@ -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([ diff --git a/src/Dsdevbe/LdapConnector/Adapter/LdapInterface.php b/src/Dsdevbe/LdapConnector/Adapter/LdapInterface.php index e7e52b0..eb36869 100644 --- a/src/Dsdevbe/LdapConnector/Adapter/LdapInterface.php +++ b/src/Dsdevbe/LdapConnector/Adapter/LdapInterface.php @@ -21,4 +21,9 @@ public function connect($username, $password); * @return UserModel */ public function getUserInfo($username, $password = null); + + /** + * @return bool + */ + public function isConnectedToLdap(); } diff --git a/src/Dsdevbe/LdapConnector/LdapConnectorServiceProvider.php b/src/Dsdevbe/LdapConnector/LdapConnectorServiceProvider.php index e61b2fe..fd4e3bc 100644 --- a/src/Dsdevbe/LdapConnector/LdapConnectorServiceProvider.php +++ b/src/Dsdevbe/LdapConnector/LdapConnectorServiceProvider.php @@ -29,7 +29,7 @@ public function boot() } $ldap = new Adldap($app['hash'], $config['adldap']); - return new LdapUserProvider($app['hash'], $ldap); + return new LdapUserProvider($ldap); }); } diff --git a/src/Dsdevbe/LdapConnector/LdapUserProvider.php b/src/Dsdevbe/LdapConnector/LdapUserProvider.php index a2f98d7..7079291 100644 --- a/src/Dsdevbe/LdapConnector/LdapUserProvider.php +++ b/src/Dsdevbe/LdapConnector/LdapUserProvider.php @@ -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; } @@ -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); } } @@ -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']); } }