BaseAuthenticate.php 6.5 KB

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