BaseAuthenticate.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  13. */
  14. namespace Cake\Controller\Component\Auth;
  15. use Cake\Controller\ComponentRegistry;
  16. use Cake\Controller\Component\Auth\AbstractPasswordHasher;
  17. use Cake\Core\App;
  18. use Cake\Core\InstanceConfigTrait;
  19. use Cake\Error;
  20. use Cake\Network\Request;
  21. use Cake\Network\Response;
  22. use Cake\ORM\TableRegistry;
  23. use Cake\Utility\Hash;
  24. use Cake\Utility\Security;
  25. /**
  26. * Base Authentication class with common methods and properties.
  27. *
  28. */
  29. abstract class BaseAuthenticate {
  30. use InstanceConfigTrait;
  31. /**
  32. * Default config for this object.
  33. *
  34. * - `fields` The fields to use to identify a user by.
  35. * - `userModel` The alias for users table, defaults to Users.
  36. * - `scope` Additional conditions to use when looking up and authenticating users,
  37. * i.e. `['Users.is_active' => 1].`
  38. * - `contain` Extra models to contain and store in session.
  39. * - `passwordHasher` Password hasher class. Can be a string specifying class name
  40. * or an array containing `className` key, any other keys will be passed as
  41. * config to the class. Defaults to 'Blowfish'.
  42. *
  43. * @var array
  44. */
  45. protected $_defaultConfig = [
  46. 'fields' => [
  47. 'username' => 'username',
  48. 'password' => 'password'
  49. ],
  50. 'userModel' => 'Users',
  51. 'scope' => [],
  52. 'contain' => null,
  53. 'passwordHasher' => 'Blowfish'
  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 $config Array of config to use.
  72. */
  73. public function __construct(ComponentRegistry $registry, $config) {
  74. $this->_registry = $registry;
  75. $this->config($config);
  76. }
  77. /**
  78. * Find a user record using the username and password provided.
  79. *
  80. * Input passwords will be hashed even when a user doesn't exist. This
  81. * helps mitigate timing attacks that are attempting to find valid usernames.
  82. *
  83. * @param string $username The username/identifier.
  84. * @param string $password The password, if not provide password checking is skipped
  85. * and result of find is returned.
  86. * @return bool|array Either false on failure, or an array of user data.
  87. */
  88. protected function _findUser($username, $password = null) {
  89. $userModel = $this->_config['userModel'];
  90. list(, $model) = pluginSplit($userModel);
  91. $fields = $this->_config['fields'];
  92. $conditions = [$model . '.' . $fields['username'] => $username];
  93. $scope = $this->_config['scope'];
  94. if ($scope) {
  95. $conditions = array_merge($conditions, $scope);
  96. }
  97. $table = TableRegistry::get($userModel)->find('all');
  98. $contain = $this->_config['contain'];
  99. if ($contain) {
  100. $table = $table->contain($contain);
  101. }
  102. $result = $table
  103. ->where($conditions)
  104. ->hydrate(false)
  105. ->first();
  106. if (empty($result)) {
  107. return false;
  108. }
  109. if ($password !== null) {
  110. if (!$this->passwordHasher()->check($password, $result[$fields['password']])) {
  111. return false;
  112. }
  113. unset($result[$fields['password']]);
  114. }
  115. return $result;
  116. }
  117. /**
  118. * Return password hasher object
  119. *
  120. * @return AbstractPasswordHasher Password hasher instance
  121. * @throws \Cake\Error\Exception If password hasher class not found or
  122. * it does not extend AbstractPasswordHasher
  123. */
  124. public function passwordHasher() {
  125. if ($this->_passwordHasher) {
  126. return $this->_passwordHasher;
  127. }
  128. $passwordHasher = $this->_config['passwordHasher'];
  129. $config = array();
  130. if (is_string($passwordHasher)) {
  131. $class = $passwordHasher;
  132. } else {
  133. $class = $passwordHasher['className'];
  134. $config = $passwordHasher;
  135. unset($config['className']);
  136. }
  137. list($plugin, $class) = pluginSplit($class, true);
  138. $className = App::classname($class, 'Controller/Component/Auth', 'PasswordHasher');
  139. if (!class_exists($className)) {
  140. throw new Error\Exception(sprintf('Password hasher class "%s" was not found.', $class));
  141. }
  142. $this->_passwordHasher = new $className($config);
  143. if (!($this->_passwordHasher instanceof AbstractPasswordHasher)) {
  144. throw new Error\Exception('Password hasher must extend AbstractPasswordHasher class.');
  145. }
  146. return $this->_passwordHasher;
  147. }
  148. /**
  149. * Authenticate a user based on the request information.
  150. *
  151. * @param \Cake\Network\Request $request Request to get authentication information from.
  152. * @param \Cake\Network\Response $response A response object that can have headers added.
  153. * @return mixed Either false on failure, or an array of user data on success.
  154. */
  155. abstract public function authenticate(Request $request, Response $response);
  156. /**
  157. * Allows you to hook into AuthComponent::logout(),
  158. * and implement specialized logout behavior.
  159. *
  160. * All attached authentication objects will have this method
  161. * called when a user logs out.
  162. *
  163. * @param array $user The user about to be logged out.
  164. * @return void
  165. */
  166. public function logout(array $user) {
  167. }
  168. /**
  169. * Get a user based on information in the request. Primarily used by stateless authentication
  170. * systems like basic and digest auth.
  171. *
  172. * @param \Cake\Network\Request $request Request object.
  173. * @return mixed Either false or an array of user information
  174. */
  175. public function getUser(Request $request) {
  176. return false;
  177. }
  178. /**
  179. * Handle unauthenticated access attempt. In implementation valid return values
  180. * can be:
  181. *
  182. * - Null - No action taken, AuthComponent should return appropriate response.
  183. * - Cake\Network\Response - A response object, which will cause AuthComponent to
  184. * simply return that response.
  185. *
  186. * @param \Cake\Network\Request $request A request object.
  187. * @param \Cake\Network\Response $response A response object.
  188. * @return void
  189. */
  190. public function unauthenticated(Request $request, Response $response) {
  191. }
  192. }