Skip to content
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

Add a Horde_Auth_Mock driver #2

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
169 changes: 169 additions & 0 deletions lib/Horde/Auth/Mock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
<?php
/**
* Copyright 2017 Horde LLC (http://www.horde.org/)
*
* See the enclosed file COPYING for license information (LGPL). If you did
* not receive this file, see http://www.horde.org/licenses/lgpl21.
*
* @author Ralf Lang <[email protected]>
* @category Horde
* @license http://www.horde.org/licenses/lgpl21 LGPL-2.1
* @package Auth
*/

/**
* The Horde_Auth_Mock class provides an in-memory user list.
*
* It is meant to be used for throwaway setups, satellite systems or for
* providing a source of administrative accounts in Composite or Cascading driver
* The driver can also be used as a mock-like backend for integration tests
*
* @author Ralf Lang <[email protected]>
* @category Horde
* @copyright 2017 Horde LLC
* @license http://www.horde.org/licenses/lgpl21 LGPL-2.1
* @package Auth
*/
class Horde_Auth_Mock extends Horde_Auth_Base
{
/**
* An array of capabilities, so that the driver can report which
* operations it supports and which it doesn't.
*
* @var array
*/
protected $_capabilities = array(
'authenticate' => true,
'list' => true,
'update' => true,
'remove' => true,
'add' => true,
);

/**
* Constructor.
*
* @param array $params Optional parameters:
* <pre>
* 'users' - (array) Usernames are hash keys, passwords are values
* 'encryption' - (string) Optionally supply an encryption or hashing
* 'show_encryption' - (boolean) prepend encryption info to password string
* </pre>
*/
public function __construct(array $params = array())
{
$params = array_merge(
array(
'encryption' => 'plain',
'users' => array(),
'show_encryption' => false
),
$params
);
parent::__construct($params);
}

/**
* Adds a set of authentication credentials.
*
* @param string $userId The userId to add.
* @param array $credentials The credentials to use.
*
* @throws Horde_Auth_Exception
*/
public function addUser($userId, $credentials)
{
// TODO: use persistence callback if given
if ($this->exists($userId)) {
throw new Horde_Auth_Exception('User already exists');
}
$this->_params['users'][$userId] = Horde_Auth::getCryptedPassword(
$credentials['password'],
'',
$this->_params['encryption'],
$this->_params['show_encryption']);
}

/**
* Lists all users in the system.
*
* @param boolean $sort Sort the users?
*
* @return mixed The array of userIds.
*/
public function listUsers($sort = false)
{
return $this->_sort(array_keys($this->_params['users']), $sort);
}

/**
* Updates a set of authentication credentials for the life time of the driver.
*
* @param string $oldID The old userId.
* @param string $newID The new userId.
* @param array $credentials The new credentials
*
* @throws Horde_Auth_Exception
*/
public function updateUser($oldID, $newID, $credentials)
{
if (!$this->exists($oldID)) {
throw new Horde_Auth_Exception('User does not exist');
}
if ($this->exists($newID) && $newID != $oldID) {
throw new Horde_Auth_Exception('Cannot rename to existing user name');
}
$this->removeUser($oldID);
$this->addUser($newID, $credentials);
}

/**
* Deletes a set of authentication credentials for the life of the driver.
*
* @param string $userId The userId to delete.
*/
public function removeUser($userId)
{
// TODO: use persistence callback if given
unset($this->_params['users'][$userId]);
}

/**
* Authenticate.
*
* @param string $userId The userID to check.
* @param array $credentials An array of login credentials.
*
* @throws Horde_Auth_Exception
*/
protected function _authenticate($userId, $credentials)
{
if (!$this->exists($userId) ||
!$this->_comparePasswords(
$this->_params['users'][$userId],
$credentials['password']
)) {
throw new Horde_Auth_Exception('', Horde_Auth::REASON_BADLOGIN);
}
return;
}

/**
* Compare an encrypted password to a plaintext string to see if
* they match.
*
* @param string $encrypted The crypted password to compare against.
* @param string $plaintext The plaintext password to verify.
*
* @return boolean True if matched, false otherwise.
*/
protected function _comparePasswords($encrypted, $plaintext)
{
return $encrypted == Horde_Auth::getCryptedPassword(
$plaintext,
$encrypted,
$this->_params['encryption'],
$this->_params['show_encryption']);
}

}
20 changes: 16 additions & 4 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<email>[email protected]</email>
<active>yes</active>
</lead>
<date>2017-10-27</date>
<date>2017-10-29</date>
<version>
<release>2.2.3</release>
<api>2.2.0</api>
Expand All @@ -34,13 +34,14 @@
</stability>
<license uri="http://www.horde.org/licenses/lgpl21">LGPL-2.1</license>
<notes>
*
* [rla] Add Mock authentication driver.
</notes>
<contents>
<dir baseinstalldir="/" name="/">
<dir name="doc">
<dir name="Horde">
<dir name="Auth">
<file name="changelog.yml" role="doc" />
<file name="CHANGES" role="doc" />
</dir> <!-- /doc/Horde/Auth -->
</dir> <!-- /doc/Horde -->
Expand All @@ -64,6 +65,7 @@
<file name="Kolab.php" role="php" />
<file name="Ldap.php" role="php" />
<file name="Login.php" role="php" />
<file name="Mock.php" role="php" />
<file name="Msad.php" role="php" />
<file name="Pam.php" role="php" />
<file name="Passwd.php" role="php" />
Expand Down Expand Up @@ -180,6 +182,9 @@
</dir> <!-- /test/Horde/Auth/Unit/Sql -->
<file name="AuthTest.php" role="test" />
<file name="KolabTest.php" role="test" />
<file name="MockCryptShowTest.php" role="test" />
<file name="MockCryptTest.php" role="test" />
<file name="MockPlainTest.php" role="test" />
<file name="PasswdTest.php" role="test" />
</dir> <!-- /test/Horde/Auth/Unit -->
<file name="AllTests.php" role="test" />
Expand All @@ -190,6 +195,7 @@
</dir> <!-- /test/Horde -->
</dir> <!-- /test -->
<file name="LICENSE" role="doc" />
<file name="phpunit.xml.dist" role="php" />
</dir> <!-- / -->
</contents>
<dependencies>
Expand Down Expand Up @@ -311,6 +317,8 @@
</dependencies>
<phprelease>
<filelist>
<install as="" name="phpunit.xml.dist" />
<install as="changelog.yml" name="doc/Horde/Auth/changelog.yml" />
<install as="CHANGES" name="doc/Horde/Auth/CHANGES" />
<install as="Horde/Auth.php" name="lib/Horde/Auth.php" />
<install as="Horde/Auth/Auto.php" name="lib/Horde/Auth/Auto.php" />
Expand All @@ -327,6 +335,7 @@
<install as="Horde/Auth/Kolab.php" name="lib/Horde/Auth/Kolab.php" />
<install as="Horde/Auth/Ldap.php" name="lib/Horde/Auth/Ldap.php" />
<install as="Horde/Auth/Login.php" name="lib/Horde/Auth/Login.php" />
<install as="Horde/Auth/Mock.php" name="lib/Horde/Auth/Mock.php" />
<install as="Horde/Auth/Msad.php" name="lib/Horde/Auth/Msad.php" />
<install as="Horde/Auth/Pam.php" name="lib/Horde/Auth/Pam.php" />
<install as="Horde/Auth/Passwd.php" name="lib/Horde/Auth/Passwd.php" />
Expand Down Expand Up @@ -372,6 +381,9 @@
<install as="Horde/Auth/TestCase.php" name="test/Horde/Auth/TestCase.php" />
<install as="Horde/Auth/Unit/AuthTest.php" name="test/Horde/Auth/Unit/AuthTest.php" />
<install as="Horde/Auth/Unit/KolabTest.php" name="test/Horde/Auth/Unit/KolabTest.php" />
<install as="Horde/Auth/Unit/MockCryptShowTest.php" name="test/Horde/Auth/Unit/MockCryptShowTest.php" />
<install as="Horde/Auth/Unit/MockCryptTest.php" name="test/Horde/Auth/Unit/MockCryptTest.php" />
<install as="Horde/Auth/Unit/MockPlainTest.php" name="test/Horde/Auth/Unit/MockPlainTest.php" />
<install as="Horde/Auth/Unit/PasswdTest.php" name="test/Horde/Auth/Unit/PasswdTest.php" />
<install as="Horde/Auth/Unit/Sql/Base.php" name="test/Horde/Auth/Unit/Sql/Base.php" />
<install as="Horde/Auth/Unit/Sql/Locks.php" name="test/Horde/Auth/Unit/Sql/Locks.php" />
Expand Down Expand Up @@ -1127,10 +1139,10 @@
<stability>
<release>stable</release>
<api>stable</api></stability>
<date>2017-10-27</date>
<date>2017-10-29</date>
<license uri="http://www.horde.org/licenses/lgpl21">LGPL-2.1</license>
<notes>
*
* [rla] Add Mock authentication driver.
</notes>
</release>
</changelog>
Expand Down