2011-01-22 18:29:56 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* PHP 5
|
|
|
|
*
|
|
|
|
* 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-22 18:29:56 +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-22 18:29:56 +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-22 18:29:56 +00:00
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
|
|
|
*/
|
2011-03-05 22:10:42 +00:00
|
|
|
|
|
|
|
App::uses('BaseAuthenticate', 'Controller/Component/Auth');
|
2011-01-22 18:29:56 +00:00
|
|
|
|
2011-01-22 21:27:07 +00:00
|
|
|
/**
|
|
|
|
* Digest Authentication adapter for AuthComponent.
|
|
|
|
*
|
2012-12-22 22:48:15 +00:00
|
|
|
* Provides Digest HTTP authentication support for AuthComponent. Unlike most AuthComponent adapters,
|
|
|
|
* DigestAuthenticate requires a special password hash that conforms to RFC2617. You can create this
|
|
|
|
* password using `DigestAuthenticate::password()`. If you wish to use digest authentication alongside other
|
2011-01-22 21:27:07 +00:00
|
|
|
* authentication methods, its recommended that you store the digest authentication separately.
|
|
|
|
*
|
2012-12-22 22:48:15 +00:00
|
|
|
* Clients using Digest Authentication must support cookies. Since AuthComponent identifies users based
|
2011-01-22 21:27:07 +00:00
|
|
|
* on Session contents, clients without support for cookies will not function properly.
|
|
|
|
*
|
|
|
|
* ### Using Digest auth
|
2011-08-16 03:55:08 +00:00
|
|
|
*
|
2011-01-22 21:27:07 +00:00
|
|
|
* In your controller's components array, add auth + the required settings.
|
|
|
|
* {{{
|
2011-08-19 02:16:16 +00:00
|
|
|
* public $components = array(
|
2011-01-22 21:27:07 +00:00
|
|
|
* 'Auth' => array(
|
|
|
|
* 'authenticate' => array('Digest')
|
|
|
|
* )
|
|
|
|
* );
|
|
|
|
* }}}
|
|
|
|
*
|
2012-12-22 22:48:15 +00:00
|
|
|
* In your login function just call `$this->Auth->login()` without any checks for POST data. This
|
2011-01-22 21:27:07 +00:00
|
|
|
* will send the authentication headers, and trigger the login dialog in the browser/client.
|
|
|
|
*
|
|
|
|
* ### Generating passwords compatible with Digest authentication.
|
|
|
|
*
|
2012-12-22 22:48:15 +00:00
|
|
|
* Due to the Digest authentication specification, digest auth requires a special password value. You
|
2011-01-22 21:27:07 +00:00
|
|
|
* can generate this password using `DigestAuthenticate::password()`
|
|
|
|
*
|
|
|
|
* `$digestPass = DigestAuthenticate::password($username, env('SERVER_NAME'), $password);`
|
|
|
|
*
|
2011-08-16 03:55:08 +00:00
|
|
|
* Its recommended that you store this digest auth only password separate from password hashes used for other
|
2012-12-22 22:48:15 +00:00
|
|
|
* login methods. For example `User.digest_pass` could be used for a digest password, while `User.password` would
|
2011-01-22 21:27:07 +00:00
|
|
|
* store the password hash for use with other methods like Basic or Form.
|
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Controller.Component.Auth
|
2011-01-22 21:27:07 +00:00
|
|
|
* @since 2.0
|
|
|
|
*/
|
2011-01-22 18:29:56 +00:00
|
|
|
class DigestAuthenticate extends BaseAuthenticate {
|
2012-03-04 00:27:46 +00:00
|
|
|
|
2011-01-22 18:29:56 +00:00
|
|
|
/**
|
|
|
|
* Settings for this object.
|
|
|
|
*
|
|
|
|
* - `fields` The fields to use to identify a user by.
|
|
|
|
* - `userModel` The model name of the User, defaults to User.
|
|
|
|
* - `scope` Additional conditions to use when looking up and authenticating users,
|
|
|
|
* i.e. `array('User.is_active' => 1).`
|
2012-04-02 23:26:46 +00:00
|
|
|
* - `recursive` The value of the recursive key passed to find(). Defaults to 0.
|
|
|
|
* - `contain` Extra models to contain and store in session.
|
2011-01-22 21:27:07 +00:00
|
|
|
* - `realm` The realm authentication is for, Defaults to the servername.
|
2012-12-22 22:48:15 +00:00
|
|
|
* - `nonce` A nonce used for authentication. Defaults to `uniqid()`.
|
2011-01-22 21:27:07 +00:00
|
|
|
* - `qop` Defaults to auth, no other values are supported at this time.
|
2012-07-18 01:55:29 +00:00
|
|
|
* - `opaque` A string that must be returned unchanged by clients.
|
2012-03-04 00:27:46 +00:00
|
|
|
* Defaults to `md5($settings['realm'])`
|
2011-01-22 18:29:56 +00:00
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
public $settings = array(
|
|
|
|
'fields' => array(
|
|
|
|
'username' => 'username',
|
|
|
|
'password' => 'password'
|
|
|
|
),
|
|
|
|
'userModel' => 'User',
|
|
|
|
'scope' => array(),
|
2012-03-05 10:06:42 +00:00
|
|
|
'recursive' => 0,
|
2012-04-05 02:06:54 +00:00
|
|
|
'contain' => null,
|
2011-01-22 18:29:56 +00:00
|
|
|
'realm' => '',
|
|
|
|
'qop' => 'auth',
|
|
|
|
'nonce' => '',
|
|
|
|
'opaque' => ''
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor, completes configuration for digest authentication.
|
|
|
|
*
|
2011-02-18 04:17:07 +00:00
|
|
|
* @param ComponentCollection $collection The Component collection used on this request.
|
2011-01-22 21:27:07 +00:00
|
|
|
* @param array $settings An array of settings.
|
2011-01-22 18:29:56 +00:00
|
|
|
*/
|
2011-02-18 04:17:07 +00:00
|
|
|
public function __construct(ComponentCollection $collection, $settings) {
|
|
|
|
parent::__construct($collection, $settings);
|
2011-01-22 18:29:56 +00:00
|
|
|
if (empty($this->settings['realm'])) {
|
|
|
|
$this->settings['realm'] = env('SERVER_NAME');
|
|
|
|
}
|
|
|
|
if (empty($this->settings['nonce'])) {
|
2011-01-22 20:46:21 +00:00
|
|
|
$this->settings['nonce'] = uniqid('');
|
2011-01-22 18:29:56 +00:00
|
|
|
}
|
|
|
|
if (empty($this->settings['opaque'])) {
|
|
|
|
$this->settings['opaque'] = md5($this->settings['realm']);
|
|
|
|
}
|
|
|
|
}
|
2011-12-06 20:52:48 +00:00
|
|
|
|
2011-01-22 18:29:56 +00:00
|
|
|
/**
|
2012-12-22 22:48:15 +00:00
|
|
|
* Authenticate a user using Digest HTTP auth. Will use the configured User model and attempt a
|
2011-01-22 18:29:56 +00:00
|
|
|
* login using Digest HTTP auth.
|
|
|
|
*
|
|
|
|
* @param CakeRequest $request The request to authenticate with.
|
|
|
|
* @param CakeResponse $response The response to add headers to.
|
|
|
|
* @return mixed Either false on failure, or an array of user data on success.
|
|
|
|
*/
|
|
|
|
public function authenticate(CakeRequest $request, CakeResponse $response) {
|
2011-02-05 23:41:00 +00:00
|
|
|
$user = $this->getUser($request);
|
2011-01-22 18:29:56 +00:00
|
|
|
|
2011-02-05 23:41:00 +00:00
|
|
|
if (empty($user)) {
|
2011-01-22 18:29:56 +00:00
|
|
|
$response->header($this->loginHeaders());
|
2011-02-05 23:41:00 +00:00
|
|
|
$response->statusCode(401);
|
2011-01-22 18:29:56 +00:00
|
|
|
$response->send();
|
|
|
|
return false;
|
|
|
|
}
|
2011-02-05 23:41:00 +00:00
|
|
|
return $user;
|
|
|
|
}
|
2011-01-22 18:29:56 +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. Used by cookie-less auth for stateless clients.
|
2011-02-05 23:41:00 +00:00
|
|
|
*
|
|
|
|
* @param CakeRequest $request Request object.
|
|
|
|
* @return mixed Either false or an array of user information
|
|
|
|
*/
|
|
|
|
public function getUser($request) {
|
|
|
|
$digest = $this->_getDigest();
|
|
|
|
if (empty($digest)) {
|
2011-01-22 18:29:56 +00:00
|
|
|
return false;
|
|
|
|
}
|
2012-08-30 13:48:13 +00:00
|
|
|
$user = $this->_findUser($digest['username']);
|
2011-02-05 23:41:00 +00:00
|
|
|
if (empty($user)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$password = $user[$this->settings['fields']['password']];
|
|
|
|
unset($user[$this->settings['fields']['password']]);
|
|
|
|
if ($digest['response'] === $this->generateResponseHash($digest, $password)) {
|
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
return false;
|
2011-01-22 18:29:56 +00:00
|
|
|
}
|
2011-12-06 20:52:48 +00:00
|
|
|
|
2011-01-22 19:48:12 +00:00
|
|
|
/**
|
|
|
|
* Find a user record using the standard options.
|
|
|
|
*
|
|
|
|
* @param string $username The username/identifier.
|
|
|
|
* @param string $password Unused password, digest doesn't require passwords.
|
|
|
|
* @return Mixed Either false on failure, or an array of user data.
|
|
|
|
*/
|
2012-08-30 13:46:29 +00:00
|
|
|
protected function _findUser($username, $password = null) {
|
2011-01-22 19:48:12 +00:00
|
|
|
$userModel = $this->settings['userModel'];
|
2012-12-20 12:47:03 +00:00
|
|
|
list(, $model) = pluginSplit($userModel);
|
2011-01-22 19:48:12 +00:00
|
|
|
$fields = $this->settings['fields'];
|
|
|
|
|
|
|
|
$conditions = array(
|
|
|
|
$model . '.' . $fields['username'] => $username,
|
|
|
|
);
|
|
|
|
if (!empty($this->settings['scope'])) {
|
|
|
|
$conditions = array_merge($conditions, $this->settings['scope']);
|
|
|
|
}
|
|
|
|
$result = ClassRegistry::init($userModel)->find('first', array(
|
|
|
|
'conditions' => $conditions,
|
2012-11-07 01:27:28 +00:00
|
|
|
'recursive' => $this->settings['recursive']
|
2011-01-22 19:48:12 +00:00
|
|
|
));
|
|
|
|
if (empty($result) || empty($result[$model])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return $result[$model];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the digest headers from the request/environment.
|
|
|
|
*
|
|
|
|
* @return array Array of digest information.
|
|
|
|
*/
|
|
|
|
protected function _getDigest() {
|
|
|
|
$digest = env('PHP_AUTH_DIGEST');
|
|
|
|
if (empty($digest) && function_exists('apache_request_headers')) {
|
|
|
|
$headers = apache_request_headers();
|
|
|
|
if (!empty($headers['Authorization']) && substr($headers['Authorization'], 0, 7) == 'Digest ') {
|
|
|
|
$digest = substr($headers['Authorization'], 7);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (empty($digest)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return $this->parseAuthData($digest);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse the digest authentication headers and split them up.
|
|
|
|
*
|
|
|
|
* @param string $digest The raw digest authentication headers.
|
|
|
|
* @return array An array of digest authentication headers
|
|
|
|
*/
|
|
|
|
public function parseAuthData($digest) {
|
|
|
|
if (substr($digest, 0, 7) == 'Digest ') {
|
|
|
|
$digest = substr($digest, 7);
|
|
|
|
}
|
|
|
|
$keys = $match = array();
|
|
|
|
$req = array('nonce' => 1, 'nc' => 1, 'cnonce' => 1, 'qop' => 1, 'username' => 1, 'uri' => 1, 'response' => 1);
|
|
|
|
preg_match_all('/(\w+)=([\'"]?)([a-zA-Z0-9@=.\/_-]+)\2/', $digest, $match, PREG_SET_ORDER);
|
|
|
|
|
|
|
|
foreach ($match as $i) {
|
|
|
|
$keys[$i[1]] = $i[3];
|
|
|
|
unset($req[$i[1]]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($req)) {
|
|
|
|
return $keys;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate the response hash for a given digest array.
|
|
|
|
*
|
|
|
|
* @param array $digest Digest information containing data from DigestAuthenticate::parseAuthData().
|
|
|
|
* @param string $password The digest hash password generated with DigestAuthenticate::password()
|
|
|
|
* @return string Response hash
|
|
|
|
*/
|
|
|
|
public function generateResponseHash($digest, $password) {
|
|
|
|
return md5(
|
|
|
|
$password .
|
|
|
|
':' . $digest['nonce'] . ':' . $digest['nc'] . ':' . $digest['cnonce'] . ':' . $digest['qop'] . ':' .
|
|
|
|
md5(env('REQUEST_METHOD') . ':' . $digest['uri'])
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-08-16 03:55:08 +00:00
|
|
|
* Creates an auth digest password hash to store
|
2011-01-22 19:48:12 +00:00
|
|
|
*
|
2011-02-06 21:59:47 +00:00
|
|
|
* @param string $username The username to use in the digest hash.
|
2011-01-22 19:48:12 +00:00
|
|
|
* @param string $password The unhashed password to make a digest hash for.
|
2011-02-06 21:59:47 +00:00
|
|
|
* @param string $realm The realm the password is for.
|
2011-01-22 19:48:12 +00:00
|
|
|
* @return string the hashed password that can later be used with Digest authentication.
|
|
|
|
*/
|
2011-02-06 21:59:47 +00:00
|
|
|
public static function password($username, $password, $realm) {
|
2011-01-22 19:48:12 +00:00
|
|
|
return md5($username . ':' . $realm . ':' . $password);
|
|
|
|
}
|
|
|
|
|
2011-01-22 18:29:56 +00:00
|
|
|
/**
|
|
|
|
* Generate the login headers
|
|
|
|
*
|
|
|
|
* @return string Headers for logging in.
|
|
|
|
*/
|
|
|
|
public function loginHeaders() {
|
|
|
|
$options = array(
|
|
|
|
'realm' => $this->settings['realm'],
|
|
|
|
'qop' => $this->settings['qop'],
|
|
|
|
'nonce' => $this->settings['nonce'],
|
|
|
|
'opaque' => $this->settings['opaque']
|
|
|
|
);
|
|
|
|
$opts = array();
|
|
|
|
foreach ($options as $k => $v) {
|
|
|
|
$opts[] = sprintf('%s="%s"', $k, $v);
|
|
|
|
}
|
|
|
|
return 'WWW-Authenticate: Digest ' . implode(',', $opts);
|
|
|
|
}
|
2012-03-04 00:27:46 +00:00
|
|
|
|
|
|
|
}
|