2011-01-21 23:10:45 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* PHP 5
|
|
|
|
*
|
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
|
|
|
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
|
|
|
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
|
|
|
*/
|
|
|
|
App::import('Component', 'auth/base_authenticate');
|
2011-01-22 01:28:24 +00:00
|
|
|
App::import('Core', 'String');
|
|
|
|
|
2011-01-21 23:10:45 +00:00
|
|
|
|
|
|
|
class BasicAuthenticate extends BaseAuthenticate {
|
2011-01-22 01:28:24 +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).`
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
public $settings = array(
|
|
|
|
'fields' => array(
|
|
|
|
'username' => 'username',
|
|
|
|
'password' => 'password'
|
|
|
|
),
|
|
|
|
'userModel' => 'User',
|
|
|
|
'scope' => array(),
|
|
|
|
'realm' => '',
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor, completes configuration for basic authentication.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __construct($settings) {
|
|
|
|
parent::__construct($settings);
|
|
|
|
if (empty($this->settings['realm'])) {
|
|
|
|
$this->settings['realm'] = env('SERVER_NAME');
|
|
|
|
}
|
|
|
|
}
|
2011-01-21 23:10:45 +00:00
|
|
|
/**
|
|
|
|
* Authenticate a user using basic HTTP auth. Will use the configured User model and attempt a
|
|
|
|
* login using basic HTTP auth.
|
|
|
|
*
|
2011-01-22 01:28:24 +00:00
|
|
|
* @param CakeRequest $request The request to authenticate with.
|
|
|
|
* @param CakeResponse $response The response to add headers to.
|
2011-01-21 23:10:45 +00:00
|
|
|
* @return mixed Either false on failure, or an array of user data on success.
|
|
|
|
*/
|
2011-01-22 01:28:24 +00:00
|
|
|
public function authenticate(CakeRequest $request, CakeResponse $response) {
|
|
|
|
$username = env('PHP_AUTH_USER');
|
|
|
|
$pass = env('PHP_AUTH_PW');
|
|
|
|
if (empty($username) || empty($pass)) {
|
|
|
|
$response->header($this->loginHeaders());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$userModel = $this->settings['userModel'];
|
|
|
|
list($plugin, $model) = pluginSplit($userModel);
|
|
|
|
$fields = $this->settings['fields'];
|
|
|
|
|
|
|
|
$conditions = array(
|
|
|
|
$model . '.' . $fields['username'] => $username,
|
|
|
|
$model . '.' . $fields['password'] => $this->hash($pass),
|
|
|
|
);
|
|
|
|
if (!empty($this->settings['scope'])) {
|
|
|
|
$conditions = array_merge($conditions, $this->settings['scope']);
|
|
|
|
}
|
|
|
|
$result = ClassRegistry::init($userModel)->find('first', array(
|
|
|
|
'conditions' => $conditions,
|
|
|
|
'recursive' => 0
|
|
|
|
));
|
|
|
|
if (empty($result) || empty($result[$model])) {
|
|
|
|
$response->header($this->loginHeaders());
|
|
|
|
$response->header('Location', Router::reverse($request));
|
|
|
|
$response->statusCode(401);
|
|
|
|
$response->send();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
unset($result[$model][$fields['password']]);
|
|
|
|
return $result[$model];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate the login headers
|
|
|
|
*
|
|
|
|
* @return string Headers for logging in.
|
|
|
|
*/
|
|
|
|
public function loginHeaders() {
|
|
|
|
return sprintf('WWW-Authenticate: Basic realm="%s"', $this->settings['realm']);
|
2011-01-21 23:10:45 +00:00
|
|
|
}
|
|
|
|
}
|