2011-01-21 23:09:41 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2013-02-08 11:59:49 +00:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2011-01-21 23:09:41 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
2013-02-08 12:22:51 +00:00
|
|
|
* For full copyright and license information, please see the LICENSE.txt
|
2011-01-21 23:09:41 +00:00
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2013-02-08 11:59:49 +00:00
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2011-01-21 23:09:41 +00:00
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
2013-05-30 22:11:14 +00:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
2011-01-21 23:09:41 +00:00
|
|
|
*/
|
2013-05-30 22:11:14 +00:00
|
|
|
|
2011-03-05 22:10:42 +00:00
|
|
|
App::uses('Security', 'Utility');
|
2012-03-11 01:57:18 +00:00
|
|
|
App::uses('Hash', 'Utility');
|
2011-01-21 23:09:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Base Authentication class with common methods and properties.
|
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Controller.Component.Auth
|
2011-01-21 23:09:41 +00:00
|
|
|
*/
|
|
|
|
abstract class BaseAuthenticate {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Settings for this object.
|
|
|
|
*
|
|
|
|
* - `fields` The fields to use to identify a user by.
|
|
|
|
* - `userModel` The model name of the User, defaults to User.
|
2014-10-31 19:00:19 +00:00
|
|
|
* - `userFields` Array of fields to retrieve from User model, null to retrieve all. Defaults to null.
|
2011-01-21 23:09:41 +00:00
|
|
|
* - `scope` Additional conditions to use when looking up and authenticating users,
|
|
|
|
* i.e. `array('User.is_active' => 1).`
|
2011-10-19 15:54:08 +00:00
|
|
|
* - `recursive` The value of the recursive key passed to find(). Defaults to 0.
|
2012-04-02 23:26:46 +00:00
|
|
|
* - `contain` Extra models to contain and store in session.
|
2013-05-20 06:44:35 +00:00
|
|
|
* - `passwordHasher` Password hasher class. Can be a string specifying class name
|
|
|
|
* or an array containing `className` key, any other keys will be passed as
|
|
|
|
* settings to the class. Defaults to 'Simple'.
|
2011-01-21 23:09:41 +00:00
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
public $settings = array(
|
|
|
|
'fields' => array(
|
|
|
|
'username' => 'username',
|
|
|
|
'password' => 'password'
|
|
|
|
),
|
|
|
|
'userModel' => 'User',
|
2014-10-31 19:00:19 +00:00
|
|
|
'userFields' => null,
|
2011-10-19 15:54:08 +00:00
|
|
|
'scope' => array(),
|
2012-04-02 23:26:46 +00:00
|
|
|
'recursive' => 0,
|
2012-04-05 02:06:54 +00:00
|
|
|
'contain' => null,
|
2013-05-20 06:44:35 +00:00
|
|
|
'passwordHasher' => 'Simple'
|
2011-01-21 23:09:41 +00:00
|
|
|
);
|
|
|
|
|
2011-02-18 04:17:07 +00:00
|
|
|
/**
|
|
|
|
* A Component collection, used to get more components.
|
|
|
|
*
|
|
|
|
* @var ComponentCollection
|
|
|
|
*/
|
|
|
|
protected $_Collection;
|
|
|
|
|
2013-05-20 06:44:35 +00:00
|
|
|
/**
|
|
|
|
* Password hasher instance.
|
|
|
|
*
|
|
|
|
* @var AbstractPasswordHasher
|
|
|
|
*/
|
|
|
|
protected $_passwordHasher;
|
|
|
|
|
2011-01-21 23:09:41 +00:00
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
2011-02-18 04:17:07 +00:00
|
|
|
* @param ComponentCollection $collection The Component collection used on this request.
|
2011-01-21 23:09:41 +00:00
|
|
|
* @param array $settings Array of settings to use.
|
|
|
|
*/
|
2011-02-18 04:17:07 +00:00
|
|
|
public function __construct(ComponentCollection $collection, $settings) {
|
|
|
|
$this->_Collection = $collection;
|
2012-03-11 01:57:18 +00:00
|
|
|
$this->settings = Hash::merge($this->settings, $settings);
|
2011-01-21 23:09:41 +00:00
|
|
|
}
|
|
|
|
|
2011-01-22 01:52:38 +00:00
|
|
|
/**
|
|
|
|
* Find a user record using the standard options.
|
|
|
|
*
|
2013-05-20 06:44:35 +00:00
|
|
|
* The $username parameter can be a (string)username or an array containing
|
|
|
|
* conditions for Model::find('first'). If the $password param is not provided
|
|
|
|
* the password field will be present in returned array.
|
2012-08-15 17:49:31 +00:00
|
|
|
*
|
2013-07-02 01:51:17 +00:00
|
|
|
* Input passwords will be hashed even when a user doesn't exist. This
|
|
|
|
* helps mitigate timing attacks that are attempting to find valid usernames.
|
|
|
|
*
|
2013-05-20 06:44:35 +00:00
|
|
|
* @param string|array $username The username/identifier, or an array of find conditions.
|
|
|
|
* @param string $password The password, only used if $username param is string.
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool|array Either false on failure, or an array of user data.
|
2011-01-22 01:52:38 +00:00
|
|
|
*/
|
2013-05-20 06:44:35 +00:00
|
|
|
protected function _findUser($username, $password = null) {
|
2011-01-22 01:52:38 +00:00
|
|
|
$userModel = $this->settings['userModel'];
|
2012-12-20 12:47:03 +00:00
|
|
|
list(, $model) = pluginSplit($userModel);
|
2011-01-22 01:52:38 +00:00
|
|
|
$fields = $this->settings['fields'];
|
|
|
|
|
2013-05-20 06:44:35 +00:00
|
|
|
if (is_array($username)) {
|
|
|
|
$conditions = $username;
|
|
|
|
} else {
|
2012-08-15 17:49:31 +00:00
|
|
|
$conditions = array(
|
2013-05-20 06:44:35 +00:00
|
|
|
$model . '.' . $fields['username'] => $username
|
2012-08-15 17:49:31 +00:00
|
|
|
);
|
|
|
|
}
|
2013-05-20 06:44:35 +00:00
|
|
|
|
2011-01-22 01:52:38 +00:00
|
|
|
if (!empty($this->settings['scope'])) {
|
|
|
|
$conditions = array_merge($conditions, $this->settings['scope']);
|
|
|
|
}
|
2013-05-20 06:44:35 +00:00
|
|
|
|
2014-10-31 19:00:19 +00:00
|
|
|
$userFields = $this->settings['userFields'];
|
|
|
|
if ($password !== null && $userFields !== null) {
|
|
|
|
$userFields[] = $fields['password'];
|
|
|
|
}
|
|
|
|
|
2011-01-22 01:52:38 +00:00
|
|
|
$result = ClassRegistry::init($userModel)->find('first', array(
|
|
|
|
'conditions' => $conditions,
|
2012-11-07 01:27:28 +00:00
|
|
|
'recursive' => $this->settings['recursive'],
|
2014-10-31 19:00:19 +00:00
|
|
|
'fields' => $userFields,
|
2012-04-02 23:26:46 +00:00
|
|
|
'contain' => $this->settings['contain'],
|
2011-01-22 01:52:38 +00:00
|
|
|
));
|
2013-05-20 06:44:35 +00:00
|
|
|
if (empty($result[$model])) {
|
2013-07-02 01:51:17 +00:00
|
|
|
$this->passwordHasher()->hash($password);
|
2011-01-22 01:52:38 +00:00
|
|
|
return false;
|
|
|
|
}
|
2013-05-20 06:44:35 +00:00
|
|
|
|
2012-04-02 23:26:46 +00:00
|
|
|
$user = $result[$model];
|
2013-12-07 13:10:08 +00:00
|
|
|
if ($password !== null) {
|
2013-05-20 06:44:35 +00:00
|
|
|
if (!$this->passwordHasher()->check($password, $user[$fields['password']])) {
|
|
|
|
return false;
|
|
|
|
}
|
2012-08-15 17:49:31 +00:00
|
|
|
unset($user[$fields['password']]);
|
|
|
|
}
|
2013-05-20 06:44:35 +00:00
|
|
|
|
2012-04-02 23:26:46 +00:00
|
|
|
unset($result[$model]);
|
|
|
|
return array_merge($user, $result);
|
2011-09-21 11:38:22 +00:00
|
|
|
}
|
|
|
|
|
2013-05-20 06:44:35 +00:00
|
|
|
/**
|
|
|
|
* Return password hasher object
|
|
|
|
*
|
|
|
|
* @return AbstractPasswordHasher Password hasher instance
|
|
|
|
* @throws CakeException If password hasher class not found or
|
|
|
|
* it does not extend AbstractPasswordHasher
|
|
|
|
*/
|
|
|
|
public function passwordHasher() {
|
|
|
|
if ($this->_passwordHasher) {
|
|
|
|
return $this->_passwordHasher;
|
|
|
|
}
|
|
|
|
|
|
|
|
$config = array();
|
|
|
|
if (is_string($this->settings['passwordHasher'])) {
|
|
|
|
$class = $this->settings['passwordHasher'];
|
|
|
|
} else {
|
|
|
|
$class = $this->settings['passwordHasher']['className'];
|
|
|
|
$config = $this->settings['passwordHasher'];
|
|
|
|
unset($config['className']);
|
|
|
|
}
|
|
|
|
list($plugin, $class) = pluginSplit($class, true);
|
|
|
|
$className = $class . 'PasswordHasher';
|
|
|
|
App::uses($className, $plugin . 'Controller/Component/Auth');
|
|
|
|
if (!class_exists($className)) {
|
|
|
|
throw new CakeException(__d('cake_dev', 'Password hasher class "%s" was not found.', $class));
|
|
|
|
}
|
|
|
|
if (!is_subclass_of($className, 'AbstractPasswordHasher')) {
|
|
|
|
throw new CakeException(__d('cake_dev', 'Password hasher must extend AbstractPasswordHasher class.'));
|
|
|
|
}
|
|
|
|
$this->_passwordHasher = new $className($config);
|
|
|
|
return $this->_passwordHasher;
|
|
|
|
}
|
|
|
|
|
2011-09-21 11:38:22 +00:00
|
|
|
/**
|
2011-12-02 05:58:09 +00:00
|
|
|
* Hash the plain text password so that it matches the hashed/encrypted password
|
2011-09-21 11:38:22 +00:00
|
|
|
* in the datasource.
|
|
|
|
*
|
|
|
|
* @param string $password The plain text password.
|
|
|
|
* @return string The hashed form of the password.
|
2014-09-02 15:03:22 +00:00
|
|
|
* @deprecated 3.0.0 Since 2.4. Use a PasswordHasher class instead.
|
2011-09-21 11:38:22 +00:00
|
|
|
*/
|
|
|
|
protected function _password($password) {
|
|
|
|
return Security::hash($password, null, true);
|
2011-01-22 01:52:38 +00:00
|
|
|
}
|
|
|
|
|
2011-01-21 23:09:41 +00:00
|
|
|
/**
|
|
|
|
* Authenticate a user based on the request information.
|
|
|
|
*
|
|
|
|
* @param CakeRequest $request Request to get authentication information from.
|
2011-01-22 00:56:23 +00:00
|
|
|
* @param CakeResponse $response A response object that can have headers added.
|
2011-01-21 23:09:41 +00:00
|
|
|
* @return mixed Either false on failure, or an array of user data on success.
|
|
|
|
*/
|
2011-01-22 00:56:23 +00:00
|
|
|
abstract public function authenticate(CakeRequest $request, CakeResponse $response);
|
2011-02-05 23:41:00 +00:00
|
|
|
|
2011-07-03 16:53:21 +00:00
|
|
|
/**
|
2011-08-16 03:55:08 +00:00
|
|
|
* Allows you to hook into AuthComponent::logout(),
|
2011-12-02 05:58:09 +00:00
|
|
|
* and implement specialized logout behavior.
|
2011-08-16 03:55:08 +00:00
|
|
|
*
|
|
|
|
* All attached authentication objects will have this method
|
2011-07-03 16:53:21 +00:00
|
|
|
* called when a user logs out.
|
|
|
|
*
|
|
|
|
* @param array $user The user about to be logged out.
|
|
|
|
* @return void
|
|
|
|
*/
|
2012-03-04 00:27:46 +00:00
|
|
|
public function logout($user) {
|
|
|
|
}
|
2011-07-03 16:53:21 +00:00
|
|
|
|
2011-02-05 23:41:00 +00:00
|
|
|
/**
|
2012-12-22 22:48:15 +00:00
|
|
|
* Get a user based on information in the request. Primarily used by stateless authentication
|
2011-02-05 23:41:00 +00:00
|
|
|
* systems like basic and digest auth.
|
|
|
|
*
|
|
|
|
* @param CakeRequest $request Request object.
|
|
|
|
* @return mixed Either false or an array of user information
|
|
|
|
*/
|
2013-02-09 12:39:11 +00:00
|
|
|
public function getUser(CakeRequest $request) {
|
2011-02-05 23:41:00 +00:00
|
|
|
return false;
|
|
|
|
}
|
2012-03-04 00:27:46 +00:00
|
|
|
|
2013-03-03 20:42:52 +00:00
|
|
|
/**
|
|
|
|
* Handle unauthenticated access attempt.
|
|
|
|
*
|
|
|
|
* @param CakeRequest $request A request object.
|
|
|
|
* @param CakeResponse $response A response object.
|
|
|
|
* @return mixed Either true to indicate the unauthenticated request has been
|
|
|
|
* dealt with and no more action is required by AuthComponent or void (default).
|
|
|
|
*/
|
|
|
|
public function unauthenticated(CakeRequest $request, CakeResponse $response) {
|
|
|
|
}
|
|
|
|
|
2011-07-03 16:53:21 +00:00
|
|
|
}
|