BaseAuthenticate.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. /**
  3. * PHP 5
  4. *
  5. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice.
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://cakephp.org CakePHP(tm) Project
  14. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  15. */
  16. App::uses('Security', 'Utility');
  17. App::uses('Hash', 'Utility');
  18. /**
  19. * Base Authentication class with common methods and properties.
  20. *
  21. * @package Cake.Controller.Component.Auth
  22. */
  23. abstract class BaseAuthenticate {
  24. /**
  25. * Settings for this object.
  26. *
  27. * - `fields` The fields to use to identify a user by.
  28. * - `userModel` The model name of the User, defaults to User.
  29. * - `scope` Additional conditions to use when looking up and authenticating users,
  30. * i.e. `array('User.is_active' => 1).`
  31. * - `recursive` The value of the recursive key passed to find(). Defaults to 0.
  32. * - `contain` Extra models to contain and store in session.
  33. * - `passwordHasher` Password hasher class. Can be a string specifying class name
  34. * or an array containing `className` key, any other keys will be passed as
  35. * settings to the class. Defaults to 'Simple'.
  36. *
  37. * @var array
  38. */
  39. public $settings = array(
  40. 'fields' => array(
  41. 'username' => 'username',
  42. 'password' => 'password'
  43. ),
  44. 'userModel' => 'User',
  45. 'scope' => array(),
  46. 'recursive' => 0,
  47. 'contain' => null,
  48. 'passwordHasher' => 'Simple'
  49. );
  50. /**
  51. * A Component collection, used to get more components.
  52. *
  53. * @var ComponentCollection
  54. */
  55. protected $_Collection;
  56. /**
  57. * Password hasher instance.
  58. *
  59. * @var AbstractPasswordHasher
  60. */
  61. protected $_passwordHasher;
  62. /**
  63. * Constructor
  64. *
  65. * @param ComponentCollection $collection The Component collection used on this request.
  66. * @param array $settings Array of settings to use.
  67. */
  68. public function __construct(ComponentCollection $collection, $settings) {
  69. $this->_Collection = $collection;
  70. $this->settings = Hash::merge($this->settings, $settings);
  71. }
  72. /**
  73. * Find a user record using the standard options.
  74. *
  75. * The $username parameter can be a (string)username or an array containing
  76. * conditions for Model::find('first'). If the $password param is not provided
  77. * the password field will be present in returned array.
  78. *
  79. * Input passwords will be hashed even when a user doesn't exist. This
  80. * helps mitigate timing attacks that are attempting to find valid usernames.
  81. *
  82. * @param string|array $username The username/identifier, or an array of find conditions.
  83. * @param string $password The password, only used if $username param is string.
  84. * @return boolean|array Either false on failure, or an array of user data.
  85. */
  86. protected function _findUser($username, $password = null) {
  87. $userModel = $this->settings['userModel'];
  88. list(, $model) = pluginSplit($userModel);
  89. $fields = $this->settings['fields'];
  90. if (is_array($username)) {
  91. $conditions = $username;
  92. } else {
  93. $conditions = array(
  94. $model . '.' . $fields['username'] => $username
  95. );
  96. }
  97. if (!empty($this->settings['scope'])) {
  98. $conditions = array_merge($conditions, $this->settings['scope']);
  99. }
  100. $result = ClassRegistry::init($userModel)->find('first', array(
  101. 'conditions' => $conditions,
  102. 'recursive' => $this->settings['recursive'],
  103. 'contain' => $this->settings['contain'],
  104. ));
  105. if (empty($result[$model])) {
  106. $this->passwordHasher()->hash($password);
  107. return false;
  108. }
  109. $user = $result[$model];
  110. if ($password) {
  111. if (!$this->passwordHasher()->check($password, $user[$fields['password']])) {
  112. return false;
  113. }
  114. unset($user[$fields['password']]);
  115. }
  116. unset($result[$model]);
  117. return array_merge($user, $result);
  118. }
  119. /**
  120. * Return password hasher object
  121. *
  122. * @return AbstractPasswordHasher Password hasher instance
  123. * @throws CakeException If password hasher class not found or
  124. * it does not extend AbstractPasswordHasher
  125. */
  126. public function passwordHasher() {
  127. if ($this->_passwordHasher) {
  128. return $this->_passwordHasher;
  129. }
  130. $config = array();
  131. if (is_string($this->settings['passwordHasher'])) {
  132. $class = $this->settings['passwordHasher'];
  133. } else {
  134. $class = $this->settings['passwordHasher']['className'];
  135. $config = $this->settings['passwordHasher'];
  136. unset($config['className']);
  137. }
  138. list($plugin, $class) = pluginSplit($class, true);
  139. $className = $class . 'PasswordHasher';
  140. App::uses($className, $plugin . 'Controller/Component/Auth');
  141. if (!class_exists($className)) {
  142. throw new CakeException(__d('cake_dev', 'Password hasher class "%s" was not found.', $class));
  143. }
  144. if (!is_subclass_of($className, 'AbstractPasswordHasher')) {
  145. throw new CakeException(__d('cake_dev', 'Password hasher must extend AbstractPasswordHasher class.'));
  146. }
  147. $this->_passwordHasher = new $className($config);
  148. return $this->_passwordHasher;
  149. }
  150. /**
  151. * Hash the plain text password so that it matches the hashed/encrypted password
  152. * in the datasource.
  153. *
  154. * @param string $password The plain text password.
  155. * @return string The hashed form of the password.
  156. * @deprecated Since 2.4. Use a PasswordHasher class instead.
  157. */
  158. protected function _password($password) {
  159. return Security::hash($password, null, true);
  160. }
  161. /**
  162. * Authenticate a user based on the request information.
  163. *
  164. * @param CakeRequest $request Request to get authentication information from.
  165. * @param CakeResponse $response A response object that can have headers added.
  166. * @return mixed Either false on failure, or an array of user data on success.
  167. */
  168. abstract public function authenticate(CakeRequest $request, CakeResponse $response);
  169. /**
  170. * Allows you to hook into AuthComponent::logout(),
  171. * and implement specialized logout behavior.
  172. *
  173. * All attached authentication objects will have this method
  174. * called when a user logs out.
  175. *
  176. * @param array $user The user about to be logged out.
  177. * @return void
  178. */
  179. public function logout($user) {
  180. }
  181. /**
  182. * Get a user based on information in the request. Primarily used by stateless authentication
  183. * systems like basic and digest auth.
  184. *
  185. * @param CakeRequest $request Request object.
  186. * @return mixed Either false or an array of user information
  187. */
  188. public function getUser(CakeRequest $request) {
  189. return false;
  190. }
  191. /**
  192. * Handle unauthenticated access attempt.
  193. *
  194. * @param CakeRequest $request A request object.
  195. * @param CakeResponse $response A response object.
  196. * @return mixed Either true to indicate the unauthenticated request has been
  197. * dealt with and no more action is required by AuthComponent or void (default).
  198. */
  199. public function unauthenticated(CakeRequest $request, CakeResponse $response) {
  200. }
  201. }