From d0251553146256bfa03ee63d77fcc90582a1289d Mon Sep 17 00:00:00 2001 From: Tim Almdal Date: Sat, 8 Nov 2008 01:56:59 +0000 Subject: The start of an authentication module. This provides the installation and a basic install test. There is no interface at the moment to do authentication. It is dependent on the install of the user module. --- modules/auth/config/auth.php | 37 +++++++ modules/auth/config/basic_auth.php | 35 ++++++ modules/auth/helpers/auth_installer.php | 63 +++++++++++ modules/auth/libraries/Auth.php | 93 ++++++++++++++++ modules/auth/libraries/drivers/Auth.php | 37 +++++++ modules/auth/libraries/drivers/Auth/Basic.php | 146 +++++++++++++++++++++++++ modules/auth/models/password.php | 22 ++++ modules/auth/tests/Auth_Installer_Test.php | 36 ++++++ modules/gallery_unit_test/controllers/test.php | 2 + 9 files changed, 471 insertions(+) create mode 100644 modules/auth/config/auth.php create mode 100644 modules/auth/config/basic_auth.php create mode 100644 modules/auth/helpers/auth_installer.php create mode 100644 modules/auth/libraries/Auth.php create mode 100644 modules/auth/libraries/drivers/Auth.php create mode 100644 modules/auth/libraries/drivers/Auth/Basic.php create mode 100644 modules/auth/models/password.php create mode 100644 modules/auth/tests/Auth_Installer_Test.php (limited to 'modules') diff --git a/modules/auth/config/auth.php b/modules/auth/config/auth.php new file mode 100644 index 00000000..b63cc114 --- /dev/null +++ b/modules/auth/config/auth.php @@ -0,0 +1,37 @@ +where("name", "auth")->find()->version; + } catch (Exception $e) { + if ($e->getCode() == 44) { + $base_version = 0; + } else { + Kohana::log("error", $e); + throw $e; + } + } + Kohana::log("debug", "base_version: $base_version"); + + if ($base_version == 0) { + $db->query("CREATE TABLE IF NOT EXISTS `passwords` ( + `id` int(9) NOT NULL auto_increment, + `user_id` int(9) NOT NULL, + `password` varchar(1128) NOT NULL, + `logins` int(10) unsigned NOT NULL default '0', + `last_login` int(10) unsigned NOT NULL default '0', + PRIMARY KEY (`id`), + UNIQUE KEY (`user_id`)) + ENGINE=InnoDB DEFAULT CHARSET=utf8;"); + + $user_module = ORM::factory("module")->where("name", "auth")->find(); + $user_module->name = "auth"; + $user_module->version = 1; + $user_module->save(); + + $user = ORM::factory("user")->where("name", "admin")->find(); + Auth::instance()->set_user_password($user->id, "admin"); + } + } + + public static function uninstall() { + $db = Database::instance(); + $db->query("DROP TABLE IF EXISTS `passwords`;"); + $auth_module = ORM::factory("module")->where("name", "auth")->find(); + $auth_module->delete(); + } +} \ No newline at end of file diff --git a/modules/auth/libraries/Auth.php b/modules/auth/libraries/Auth.php new file mode 100644 index 00000000..9bc59ec7 --- /dev/null +++ b/modules/auth/libraries/Auth.php @@ -0,0 +1,93 @@ +_driver = $driver; + $this->_config = $config; + + Kohana::log('debug', 'Auth Library initialized'); + } + + /** + * @see Auth_Driver::set_user_password + */ + public function set_user_password($user_id, $password) { + return $this->_driver->set_user_password($user_id, $password); + } + + /** + * @see Auth_Driver::is_valid_password + */ + public function is_valid_password($user_id, $password) { + return $this->_driver->is_valid_password($user_id, $password); + } +} diff --git a/modules/auth/libraries/drivers/Auth.php b/modules/auth/libraries/drivers/Auth.php new file mode 100644 index 00000000..6a6d31b6 --- /dev/null +++ b/modules/auth/libraries/drivers/Auth.php @@ -0,0 +1,37 @@ +_config = $config; + + Kohana::log('debug', 'Auth_Basic_Driver Library initialized'); + } + + /** + * @see Auth_Driver::set_user_password + * + * @param int $user_id + * @param string $password + * @return void + */ + public function set_user_password($user_id, $password_text) { + $password = ORM::factory("password")->where('user_id', $user_id)->find(); + $password->password = $this->_hash_password($password_text); + if (empty($password->user_id)) { + $password->user_id = $user_id; + } + $password->save(); + } + + /** + * Validates a user id password combination. + * + * @param int user_id + * @param string password + * @return boolean + */ + public function is_valid_password($user_id, $password_text) { + $password = ORM::factory("password") + ->where('user_id', $user_id) + ->find(); + if ($password->loaded != true) { + return false; + } + + // Get the salt from the stored password + $salt = $this->_find_salt($password->password); + $hashed = $this->_hash_password($password_text, $salt); + + return $hashed === $password->password; + } + + /** + * Creates a hashed password from a plaintext password, inserting salt + * based on the configured salt pattern. + * + * @param string plaintext password + * @return string hashed password string + */ + private function _hash_password($password, $salt = FALSE) { + if ($salt === FALSE) { + // Create a salt seed, same length as the number of offsets in the pattern + $salt = substr($this->_hash(uniqid(NULL, TRUE)), 0, count($this->_config['salt_pattern'])); + } + + // Password hash that the salt will be inserted into + $hash = $this->_hash($salt . $password); + + // Change salt to an array + $salt = str_split($salt, 1); + + // Returned password + $password = ''; + + // Used to calculate the length of splits + $last_offset = 0; + + foreach ($this->_config['salt_pattern'] as $offset) { + // Split a new part of the hash off + $part = substr($hash, 0, $offset - $last_offset); + + // Cut the current part out of the hash + $hash = substr($hash, $offset - $last_offset); + + // Add the part to the password, appending the salt character + $password .= $part . array_shift($salt); + + // Set the last offset to the current offset + $last_offset = $offset; + } + + // Return the password, with the remaining hash appended + return $password . $hash; + } + + /** + * Perform a hash, using the configured method. + * + * @param string string to hash + * @return string + */ + private function _hash($str) { + return hash($this->_config['hash_method'], $str); + } + + /** + * Finds the salt from a password, based on the configured salt pattern. + * + * @param string hashed password + * @return string + */ + private function _find_salt($password) { + $salt = ''; + + foreach ($this->_config['salt_pattern'] as $i => $offset) { + // Find salt characters... take a good long look.. + $salt .= substr($password, $offset + $i, 1); + } + + return $salt; + } +} + diff --git a/modules/auth/models/password.php b/modules/auth/models/password.php new file mode 100644 index 00000000..fd1fee58 --- /dev/null +++ b/modules/auth/models/password.php @@ -0,0 +1,22 @@ +find(1); + + $auth = Auth::instance(array('driver' => 'Basic')); + + $auth->set_user_password($user->id, "test_password"); + + $this->assert_false($auth->is_valid_password($user->id, "invalid_password")); + $this->assert_true($auth->is_valid_password($user->id, "test_password")); + } +} diff --git a/modules/gallery_unit_test/controllers/test.php b/modules/gallery_unit_test/controllers/test.php index 543d3183..0761dc12 100644 --- a/modules/gallery_unit_test/controllers/test.php +++ b/modules/gallery_unit_test/controllers/test.php @@ -61,6 +61,7 @@ class Test_Controller extends Controller { // We probably don't want to uninstall and reinstall the core every time, but let's start off // this way. Uninstall modules first and core last. Ignore errors during uninstall. try { + auth_installer::uninstall(); user_installer::uninstall(); core_installer::uninstall(); } catch (Exception $e) { @@ -68,6 +69,7 @@ class Test_Controller extends Controller { core_installer::install(); user_installer::install(); + auth_installer::install(); print new Unit_Test(); } -- cgit v1.2.3