PageRenderTime 49ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/auth/libraries/Auth.php

https://github.com/plusjade/pluspanda-php
PHP | 241 lines | 98 code | 40 blank | 103 comment | 7 complexity | 2c06a1a1dd404f0f54c8d3da783eec02 MD5 | raw file
  1. <?php defined('SYSPATH') OR die('No direct access allowed.');
  2. /**
  3. * User authorization library. Handles user login and logout, as well as secure
  4. * password hashing.
  5. *
  6. * @package Auth
  7. * @author Kohana Team
  8. * @copyright (c) 2007 Kohana Team
  9. * @license http://kohanaphp.com/license.html
  10. */
  11. class Auth_Core {
  12. // Session instance
  13. protected $session;
  14. // Configuration
  15. protected $config;
  16. /**
  17. * Create an instance of Auth.
  18. *
  19. * @return object
  20. */
  21. public static function factory($config = array())
  22. {
  23. return new Auth($config);
  24. }
  25. /**
  26. * Return a static instance of Auth.
  27. *
  28. * @return object
  29. */
  30. public static function instance($config = array())
  31. {
  32. static $instance;
  33. // Load the Auth instance
  34. empty($instance) and $instance = new Auth($config);
  35. return $instance;
  36. }
  37. /**
  38. * Loads Session and configuration options.
  39. *
  40. * @return void
  41. */
  42. public function __construct($config = array())
  43. {
  44. // Append default auth configuration
  45. $config += Kohana::config('auth');
  46. // Clean up the salt pattern and split it into an array
  47. $config['salt_pattern'] = preg_split('/,\s*/', Kohana::config('auth.salt_pattern'));
  48. // Save the config in the object
  49. $this->config = $config;
  50. // Set the driver class name
  51. $driver = 'Auth_'.$config['driver'].'_Driver';
  52. if ( ! Kohana::auto_load($driver))
  53. throw new Kohana_Exception('core.driver_not_found', $config['driver'], get_class($this));
  54. // Load the driver
  55. $driver = new $driver($config);
  56. if ( ! ($driver instanceof Auth_Driver))
  57. throw new Kohana_Exception('core.driver_implements', $config['driver'], get_class($this), 'Auth_Driver');
  58. // Load the driver for access
  59. $this->driver = $driver;
  60. Kohana::log('debug', 'Auth Library loaded');
  61. }
  62. /**
  63. * Check if there is an active session. Optionally allows checking for a
  64. * specific role.
  65. *
  66. * @param string role name
  67. * @return boolean
  68. */
  69. public function logged_in($role = NULL)
  70. {
  71. return $this->driver->logged_in($role);
  72. }
  73. /**
  74. * Returns the currently logged in user, or FALSE.
  75. *
  76. * @return mixed
  77. */
  78. public function get_user()
  79. {
  80. return $this->driver->get_user();
  81. }
  82. /**
  83. * Attempt to log in a user by using an ORM object and plain-text password.
  84. *
  85. * @param string username to log in
  86. * @param string password to check against
  87. * @param boolean enable auto-login
  88. * @return boolean
  89. */
  90. public function login($username, $password, $site_id, $remember = FALSE)
  91. {
  92. if (empty($password))
  93. return FALSE;
  94. if ( ! is_object($username))
  95. {
  96. // Load the owner
  97. $username = ORM::factory('owner', $username);
  98. if(!$username->loaded)
  99. return FALSE;
  100. }
  101. if (is_string($password))
  102. {
  103. // Get the salt from the stored password
  104. $salt = $this->find_salt($this->driver->password($username));
  105. // Create a hashed password using the salt from the stored password
  106. $password = $this->hash_password($password, $salt);
  107. }
  108. return $this->driver->login($username, $password, $site_id, $remember);
  109. }
  110. /**
  111. * Attempt to automatically log a user in.
  112. *
  113. * @return boolean
  114. */
  115. public function auto_login()
  116. {
  117. return $this->driver->auto_login();
  118. }
  119. /**
  120. * Force a login for a specific username.
  121. *
  122. * @param mixed username
  123. * @return boolean
  124. */
  125. public function force_login($username)
  126. {
  127. return $this->driver->force_login($username);
  128. }
  129. /**
  130. * Log out a user by removing the related session variables.
  131. *
  132. * @param boolean completely destroy the session
  133. * @return boolean
  134. */
  135. public function logout($destroy = FALSE)
  136. {
  137. return $this->driver->logout($destroy);
  138. }
  139. /**
  140. * Creates a hashed password from a plaintext password, inserting salt
  141. * based on the configured salt pattern.
  142. *
  143. * @param string plaintext password
  144. * @return string hashed password string
  145. */
  146. public function hash_password($password, $salt = FALSE)
  147. {
  148. if ($salt === FALSE)
  149. {
  150. // Create a salt seed, same length as the number of offsets in the pattern
  151. $salt = substr($this->hash(uniqid(NULL, TRUE)), 0, count($this->config['salt_pattern']));
  152. }
  153. // Password hash that the salt will be inserted into
  154. $hash = $this->hash($salt.$password);
  155. // Change salt to an array
  156. $salt = str_split($salt, 1);
  157. // Returned password
  158. $password = '';
  159. // Used to calculate the length of splits
  160. $last_offset = 0;
  161. foreach ($this->config['salt_pattern'] as $offset)
  162. {
  163. // Split a new part of the hash off
  164. $part = substr($hash, 0, $offset - $last_offset);
  165. // Cut the current part out of the hash
  166. $hash = substr($hash, $offset - $last_offset);
  167. // Add the part to the password, appending the salt character
  168. $password .= $part.array_shift($salt);
  169. // Set the last offset to the current offset
  170. $last_offset = $offset;
  171. }
  172. // Return the password, with the remaining hash appended
  173. return $password.$hash;
  174. }
  175. /**
  176. * Perform a hash, using the configured method.
  177. *
  178. * @param string string to hash
  179. * @return string
  180. */
  181. public function hash($str)
  182. {
  183. return hash($this->config['hash_method'], $str);
  184. }
  185. /**
  186. * Finds the salt from a password, based on the configured salt pattern.
  187. *
  188. * @param string hashed password
  189. * @return string
  190. */
  191. public function find_salt($password)
  192. {
  193. $salt = '';
  194. foreach ($this->config['salt_pattern'] as $i => $offset)
  195. {
  196. // Find salt characters, take a good long look...
  197. $salt .= $password[$offset + $i];
  198. }
  199. return $salt;
  200. }
  201. } // End Auth