From 05949b3c61cea3bd42fa327fe4e0bf0112189e66 Mon Sep 17 00:00:00 2001 From: Ralf Lang Date: Sun, 29 Oct 2017 08:28:49 +0000 Subject: [PATCH 1/2] Adding a Mock Driver and tests --- lib/Horde/Auth/Mock.php | 169 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 169 insertions(+) create mode 100644 lib/Horde/Auth/Mock.php diff --git a/lib/Horde/Auth/Mock.php b/lib/Horde/Auth/Mock.php new file mode 100644 index 0000000..7ff5262 --- /dev/null +++ b/lib/Horde/Auth/Mock.php @@ -0,0 +1,169 @@ + + * @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 + * @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: + *
+     * '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
+     * 
+ */ + 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']); + } + +} From 77114dcf93e36f92d677ca67084146124e2ed574 Mon Sep 17 00:00:00 2001 From: Ralf Lang Date: Sun, 29 Oct 2017 08:30:18 +0000 Subject: [PATCH 2/2] Update package.xml --- package.xml | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/package.xml b/package.xml index 05f13db..6af5f3d 100644 --- a/package.xml +++ b/package.xml @@ -23,7 +23,7 @@ slusarz@horde.org yes - 2017-10-27 + 2017-10-29 2.2.3 2.2.0 @@ -34,13 +34,14 @@ LGPL-2.1 -* +* [rla] Add Mock authentication driver. + @@ -64,6 +65,7 @@ + @@ -180,6 +182,9 @@ + + + @@ -190,6 +195,7 @@ + @@ -311,6 +317,8 @@ + + @@ -327,6 +335,7 @@ + @@ -372,6 +381,9 @@ + + + @@ -1127,10 +1139,10 @@ stable stable - 2017-10-27 + 2017-10-29 LGPL-2.1 -* +* [rla] Add Mock authentication driver.