2006-03-12 00:03:24 +00:00
|
|
|
<?php
|
|
|
|
/* SVN FILE: $Id$ */
|
|
|
|
/**
|
|
|
|
* Short description for file.
|
|
|
|
*
|
|
|
|
* Long description for file
|
|
|
|
*
|
|
|
|
* PHP versions 4 and 5
|
|
|
|
*
|
2007-02-02 10:39:45 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework <http://www.cakephp.org/>
|
|
|
|
* Copyright 2005-2007, Cake Software Foundation, Inc.
|
2006-05-26 05:29:17 +00:00
|
|
|
* 1785 E. Sahara Avenue, Suite 490-204
|
|
|
|
* Las Vegas, Nevada 89104
|
2006-03-12 00:03:24 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
|
|
|
* @filesource
|
2007-02-02 10:39:45 +00:00
|
|
|
* @copyright Copyright 2005-2007, Cake Software Foundation, Inc.
|
|
|
|
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
|
2006-05-26 05:29:17 +00:00
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.libs.controller.components
|
2007-02-02 10:39:45 +00:00
|
|
|
* @since CakePHP(tm) v 0.10.8.2156
|
2006-05-26 05:29:17 +00:00
|
|
|
* @version $Revision$
|
|
|
|
* @modifiedby $LastChangedBy$
|
|
|
|
* @lastmodified $Date$
|
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
|
2006-03-12 00:03:24 +00:00
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* Short description for file.
|
|
|
|
*
|
|
|
|
* Long description for file
|
|
|
|
*
|
2006-05-26 05:29:17 +00:00
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.libs.controller.components
|
2006-03-12 00:03:24 +00:00
|
|
|
*/
|
2006-06-16 19:45:33 +00:00
|
|
|
class SecurityComponent extends Object {
|
2006-11-28 00:00:23 +00:00
|
|
|
/**
|
2006-12-01 23:51:43 +00:00
|
|
|
* Holds an instance of the core Security object
|
2006-11-28 00:00:23 +00:00
|
|
|
*
|
2006-12-01 23:51:43 +00:00
|
|
|
* @var object Security
|
|
|
|
* @access public
|
2006-11-28 00:00:23 +00:00
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
var $Security = null;
|
2006-11-28 00:00:23 +00:00
|
|
|
/**
|
2006-12-01 23:51:43 +00:00
|
|
|
* The controller method that will be called if this request is black-hole'd
|
2006-11-28 00:00:23 +00:00
|
|
|
*
|
|
|
|
* @var string
|
2006-12-01 23:51:43 +00:00
|
|
|
* @access public
|
2006-11-28 00:00:23 +00:00
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
var $blackHoleCallback = null;
|
2006-11-28 00:00:23 +00:00
|
|
|
/**
|
2006-12-01 23:51:43 +00:00
|
|
|
* List of controller actions for which a POST request is required
|
2006-11-28 00:00:23 +00:00
|
|
|
*
|
|
|
|
* @var array
|
2006-12-01 23:51:43 +00:00
|
|
|
* @access public
|
|
|
|
* @see SecurityComponent::requirePost()
|
2006-11-28 00:00:23 +00:00
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
var $requirePost = array();
|
2006-11-28 00:00:23 +00:00
|
|
|
/**
|
2006-12-01 23:51:43 +00:00
|
|
|
* List of actions that require an SSL-secured connection
|
2006-11-28 00:00:23 +00:00
|
|
|
*
|
|
|
|
* @var array
|
2006-12-01 23:51:43 +00:00
|
|
|
* @access public
|
|
|
|
* @see SecurityComponent::requireSecure()
|
2006-11-28 00:00:23 +00:00
|
|
|
*/
|
2006-11-08 02:18:41 +00:00
|
|
|
var $requireSecure = array();
|
2006-11-28 00:00:23 +00:00
|
|
|
/**
|
2006-12-01 23:51:43 +00:00
|
|
|
* List of actions that require a valid authentication key
|
2006-11-28 00:00:23 +00:00
|
|
|
*
|
|
|
|
* @var array
|
2006-12-01 23:51:43 +00:00
|
|
|
* @access public
|
|
|
|
* @see SecurityComponent::requireAuth()
|
2006-11-28 00:00:23 +00:00
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
var $requireAuth = array();
|
2006-11-28 00:00:23 +00:00
|
|
|
/**
|
2006-12-01 23:51:43 +00:00
|
|
|
* List of actions that require an HTTP-authenticated login (basic or digest)
|
2006-11-28 00:00:23 +00:00
|
|
|
*
|
|
|
|
* @var array
|
2006-12-01 23:51:43 +00:00
|
|
|
* @access public
|
|
|
|
* @see SecurityComponent::requireLogin()
|
2006-11-28 00:00:23 +00:00
|
|
|
*/
|
2006-06-16 19:45:33 +00:00
|
|
|
var $requireLogin = array();
|
2006-11-28 00:00:23 +00:00
|
|
|
/**
|
2006-12-01 23:51:43 +00:00
|
|
|
* Login options for SecurityComponent::requireLogin()
|
2006-11-28 00:00:23 +00:00
|
|
|
*
|
|
|
|
* @var array
|
2006-12-01 23:51:43 +00:00
|
|
|
* @access public
|
|
|
|
* @see SecurityComponent::requireLogin()
|
2006-11-28 00:00:23 +00:00
|
|
|
*/
|
2006-07-31 23:55:29 +00:00
|
|
|
var $loginOptions = array('type' => '');
|
2006-11-28 00:00:23 +00:00
|
|
|
/**
|
2006-12-01 23:51:43 +00:00
|
|
|
* An associative array of usernames/passwords used for HTTP-authenticated logins.
|
|
|
|
* If using digest authentication, passwords should be MD5-hashed.
|
2006-11-28 00:00:23 +00:00
|
|
|
*
|
|
|
|
* @var array
|
2006-12-01 23:51:43 +00:00
|
|
|
* @access public
|
|
|
|
* @see SecurityComponent::requireLogin()
|
2006-11-28 00:00:23 +00:00
|
|
|
*/
|
2006-06-16 19:45:33 +00:00
|
|
|
var $loginUsers = array();
|
2006-11-28 00:00:23 +00:00
|
|
|
/**
|
2006-12-06 20:37:12 +00:00
|
|
|
* Controllers from which actions of the current controller are allowed to receive
|
|
|
|
* requests.
|
2006-11-28 00:00:23 +00:00
|
|
|
*
|
|
|
|
* @var array
|
2006-12-06 20:37:12 +00:00
|
|
|
* @see SecurityComponent::requireAuth()
|
2006-11-28 00:00:23 +00:00
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
var $allowedControllers = array();
|
2006-11-28 00:00:23 +00:00
|
|
|
/**
|
2006-12-06 20:37:12 +00:00
|
|
|
* Actions from which actions of the current controller are allowed to receive
|
|
|
|
* requests.
|
2006-11-28 00:00:23 +00:00
|
|
|
*
|
|
|
|
* @var array
|
2006-12-06 20:37:12 +00:00
|
|
|
* @see SecurityComponent::requireAuth()
|
2006-11-28 00:00:23 +00:00
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
var $allowedActions = array();
|
2006-11-28 00:00:23 +00:00
|
|
|
/**
|
2006-12-06 20:37:12 +00:00
|
|
|
* Other components used by the Security component
|
2006-11-28 00:00:23 +00:00
|
|
|
*
|
|
|
|
* @var array
|
2006-12-06 20:37:12 +00:00
|
|
|
* @access public
|
2006-11-28 00:00:23 +00:00
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
var $components = array('RequestHandler', 'Session');
|
2006-03-12 00:03:24 +00:00
|
|
|
/**
|
|
|
|
* Security class constructor
|
|
|
|
*/
|
2006-06-16 19:45:33 +00:00
|
|
|
function __construct () {
|
2006-07-05 14:23:21 +00:00
|
|
|
$this->Security =& Security::getInstance();
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
2006-11-28 00:00:23 +00:00
|
|
|
/**
|
2006-12-06 20:37:12 +00:00
|
|
|
* Component startup. All security checking happens here.
|
2006-11-28 00:00:23 +00:00
|
|
|
*
|
|
|
|
* @param object $controller
|
|
|
|
* @return unknown
|
|
|
|
* @access public
|
|
|
|
*/
|
2006-06-16 19:45:33 +00:00
|
|
|
function startup(&$controller) {
|
|
|
|
// Check requirePost
|
|
|
|
if (is_array($this->requirePost) && !empty($this->requirePost)) {
|
2006-11-28 00:00:23 +00:00
|
|
|
|
2006-06-16 19:45:33 +00:00
|
|
|
if (in_array($controller->action, $this->requirePost) || $this->requirePost == array('*')) {
|
|
|
|
|
|
|
|
if (!$this->RequestHandler->isPost()) {
|
2006-11-28 00:00:23 +00:00
|
|
|
|
2006-06-16 19:45:33 +00:00
|
|
|
if (!$this->blackHole($controller, 'post')) {
|
2006-05-26 05:29:17 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-11-08 02:18:41 +00:00
|
|
|
// Check requireSecure
|
|
|
|
if (is_array($this->requireSecure) && !empty($this->requireSecure)) {
|
2006-11-28 00:00:23 +00:00
|
|
|
|
2006-11-08 02:18:41 +00:00
|
|
|
if (in_array($controller->action, $this->requireSecure) || $this->requireSecure == array('*')) {
|
|
|
|
|
|
|
|
if (!$this->RequestHandler->isSSL()) {
|
2006-11-28 00:00:23 +00:00
|
|
|
|
2006-11-08 02:18:41 +00:00
|
|
|
if (!$this->blackHole($controller, 'secure')) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-06-16 19:45:33 +00:00
|
|
|
// Check requireAuth
|
|
|
|
if (is_array($this->requireAuth) && !empty($this->requireAuth) && !empty($controller->params['form'])) {
|
2006-11-28 00:00:23 +00:00
|
|
|
|
2006-06-16 19:45:33 +00:00
|
|
|
if (in_array($controller->action, $this->requireAuth) || $this->requireAuth == array('*')) {
|
|
|
|
|
|
|
|
if (!isset($controller->params['data']['_Token'])) {
|
2006-11-28 00:00:23 +00:00
|
|
|
|
2006-06-16 19:45:33 +00:00
|
|
|
if (!$this->blackHole($controller, 'auth')) {
|
2006-05-26 05:29:17 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$token = $controller->params['data']['_Token']['key'];
|
|
|
|
|
2006-06-16 19:45:33 +00:00
|
|
|
if ($this->Session->check('_Token')) {
|
2006-08-04 08:03:39 +00:00
|
|
|
$tData = unserialize($this->Session->read('_Token'));
|
2006-11-28 00:00:23 +00:00
|
|
|
|
2006-08-04 08:03:39 +00:00
|
|
|
if ($tData['expires'] < time() || $tData['key'] !== $token) {
|
2006-11-28 00:00:23 +00:00
|
|
|
|
2006-06-16 19:45:33 +00:00
|
|
|
if (!$this->blackHole($controller, 'auth')) {
|
2006-05-26 05:29:17 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2006-06-16 19:45:33 +00:00
|
|
|
if (!empty($tData['allowedControllers']) && !in_array($controller->params['controller'], $tData['allowedControllers']) ||!empty($tData['allowedActions']) && !in_array($controller->params['action'], $tData['allowedActions'])) {
|
2006-11-28 00:00:23 +00:00
|
|
|
|
2006-06-16 19:45:33 +00:00
|
|
|
if (!$this->blackHole($controller, 'auth')) {
|
2006-05-26 05:29:17 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2006-06-16 19:45:33 +00:00
|
|
|
} else {
|
|
|
|
if (!$this->blackHole($controller, 'auth')) {
|
2006-05-26 05:29:17 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-06-16 19:45:33 +00:00
|
|
|
// Check requireLogin
|
|
|
|
if (is_array($this->requireLogin) && !empty($this->requireLogin)) {
|
|
|
|
|
2006-11-28 00:00:23 +00:00
|
|
|
if (in_array($controller->action, $this->requireLogin) || $this->requireLogin == array('*')) {
|
2006-06-16 19:45:33 +00:00
|
|
|
$login = $this->loginCredentials($this->loginOptions['type']);
|
2006-11-28 00:00:23 +00:00
|
|
|
|
2006-06-16 19:45:33 +00:00
|
|
|
if ($login == null) {
|
|
|
|
// User hasn't been authenticated yet
|
|
|
|
header($this->loginRequest());
|
|
|
|
if (isset($this->loginOptions['prompt'])) {
|
|
|
|
$this->__callback($controller, $this->loginOptions['prompt']);
|
|
|
|
} else {
|
|
|
|
$this->blackHole($controller, 'login');
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (isset($this->loginOptions['login'])) {
|
|
|
|
$this->__callback($controller, $this->loginOptions['login'], array($login));
|
|
|
|
} else {
|
|
|
|
if (low($this->loginOptions['type']) == 'digest') {
|
|
|
|
// Do digest authentication
|
|
|
|
} else {
|
2006-06-16 20:06:11 +00:00
|
|
|
if (!(in_array($login['username'], array_keys($this->loginUsers)) && $this->loginUsers[$login['username']] == $login['password'])) {
|
2006-06-16 19:45:33 +00:00
|
|
|
$this->blackHole($controller, 'login');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-05-26 05:29:17 +00:00
|
|
|
// Add auth key for new form posts
|
|
|
|
$authKey = Security::generateAuthKey();
|
2006-08-04 08:07:50 +00:00
|
|
|
$expires = strtotime('+'.Security::inactiveMins().' minutes');
|
2006-12-01 23:51:43 +00:00
|
|
|
$token = array(
|
|
|
|
'key' => $authKey,
|
|
|
|
'expires' => $expires,
|
|
|
|
'allowedControllers' => $this->allowedControllers,
|
|
|
|
'allowedActions' => $this->allowedActions
|
|
|
|
);
|
2006-05-26 05:29:17 +00:00
|
|
|
|
2006-06-16 19:45:33 +00:00
|
|
|
if (!isset($controller->params['data'])) {
|
2006-05-26 05:29:17 +00:00
|
|
|
$controller->params['data'] = array();
|
|
|
|
}
|
|
|
|
$controller->params['_Token'] = $token;
|
2006-08-04 08:03:39 +00:00
|
|
|
$this->Session->write('_Token', serialize($token));
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
2006-03-12 00:03:24 +00:00
|
|
|
/**
|
|
|
|
* Black-hole an invalid request with a 404 error or custom callback
|
|
|
|
*
|
2006-11-28 00:00:23 +00:00
|
|
|
* @param object $controller
|
|
|
|
* @param string $error
|
|
|
|
* @return Controller blackHoleCallback
|
|
|
|
* @access public
|
2006-03-12 00:03:24 +00:00
|
|
|
*/
|
2006-06-16 19:45:33 +00:00
|
|
|
function blackHole(&$controller, $error = '') {
|
|
|
|
if ($this->blackHoleCallback == null) {
|
2006-11-08 02:18:41 +00:00
|
|
|
$code = 404;
|
2006-06-16 19:45:33 +00:00
|
|
|
if ($error == 'login') {
|
2006-11-08 02:18:41 +00:00
|
|
|
$code = 401;
|
2006-06-16 19:45:33 +00:00
|
|
|
}
|
2006-11-08 02:18:41 +00:00
|
|
|
$controller->redirect(null, $code);
|
2006-05-26 05:29:17 +00:00
|
|
|
exit();
|
2006-06-16 20:06:11 +00:00
|
|
|
} else {
|
|
|
|
return $this->__callback($controller, $this->blackHoleCallback, array($error));
|
2006-06-16 19:45:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Sets the actions that require a POST request, or empty for all actions
|
2006-12-01 23:51:43 +00:00
|
|
|
*
|
2006-11-28 00:00:23 +00:00
|
|
|
* @access public
|
2006-12-01 23:51:43 +00:00
|
|
|
* @return void
|
2006-06-16 19:45:33 +00:00
|
|
|
*/
|
|
|
|
function requirePost() {
|
|
|
|
$this->requirePost = func_get_args();
|
|
|
|
if (empty($this->requirePost)) {
|
|
|
|
$this->requirePost = array('*');
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
2006-06-16 19:45:33 +00:00
|
|
|
}
|
2006-11-08 02:18:41 +00:00
|
|
|
/**
|
|
|
|
* Sets the actions that require a request that is SSL-secured, or empty for all actions
|
2006-12-01 23:51:43 +00:00
|
|
|
*
|
2006-11-28 00:00:23 +00:00
|
|
|
* @access public
|
2006-12-01 23:51:43 +00:00
|
|
|
* @return void
|
2006-11-08 02:18:41 +00:00
|
|
|
*/
|
|
|
|
function requireSecure() {
|
|
|
|
$this->requireSecure = func_get_args();
|
|
|
|
if (empty($this->requireSecure)) {
|
|
|
|
$this->requireSecure = array('*');
|
|
|
|
}
|
|
|
|
}
|
2006-06-16 19:45:33 +00:00
|
|
|
/**
|
|
|
|
* Sets the actions that require an authenticated request, or empty for all actions
|
2006-12-01 23:51:43 +00:00
|
|
|
*
|
2006-11-28 00:00:23 +00:00
|
|
|
* @access public
|
2006-12-01 23:51:43 +00:00
|
|
|
* @return void
|
2006-06-16 19:45:33 +00:00
|
|
|
*/
|
|
|
|
function requireAuth() {
|
|
|
|
$this->requireAuth = func_get_args();
|
|
|
|
if (empty($this->requireAuth)) {
|
|
|
|
$this->requireAuth = array('*');
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
|
|
|
}
|
2006-06-16 19:45:33 +00:00
|
|
|
/**
|
|
|
|
* Sets the actions that require an HTTP-authenticated request, or empty for all actions
|
2006-12-01 23:51:43 +00:00
|
|
|
*
|
2006-11-28 00:00:23 +00:00
|
|
|
* @access public
|
2006-12-01 23:51:43 +00:00
|
|
|
* @return void
|
2006-06-16 19:45:33 +00:00
|
|
|
*/
|
|
|
|
function requireLogin() {
|
|
|
|
$args = func_get_args();
|
|
|
|
foreach ($args as $arg) {
|
|
|
|
if (is_array($arg)) {
|
|
|
|
$this->loginOptions = $arg;
|
|
|
|
} else {
|
|
|
|
$this->requireLogin[] = $arg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (empty($this->requireLogin)) {
|
|
|
|
$this->requireLogin = array('*');
|
|
|
|
}
|
|
|
|
if (isset($this->loginOptions['users'])) {
|
|
|
|
$this->loginUsers =& $this->loginOptions['users'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Gets the login credentials for an HTTP-authenticated request
|
|
|
|
*
|
2006-12-01 23:51:43 +00:00
|
|
|
* @param string $type Either 'basic', 'digest', or null. If null/empty, will try both.
|
2006-06-16 19:45:33 +00:00
|
|
|
* @return mixed If successful, returns an array with login name and password, otherwise null.
|
2006-11-28 00:00:23 +00:00
|
|
|
* @access public
|
2006-06-16 19:45:33 +00:00
|
|
|
*/
|
2006-12-01 23:51:43 +00:00
|
|
|
function loginCredentials($type = null) {
|
2006-05-26 05:29:17 +00:00
|
|
|
|
2006-12-01 23:51:43 +00:00
|
|
|
if (empty($type) || low($type) == 'basic') {
|
2006-06-16 20:06:11 +00:00
|
|
|
$login = array('username' => env('PHP_AUTH_USER'), 'password' => env('PHP_AUTH_PW'));
|
|
|
|
if ($login['username'] != null) {
|
2006-06-16 19:45:33 +00:00
|
|
|
return $login;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($type == '' || low($type) == 'digest') {
|
|
|
|
|
|
|
|
$digest = null;
|
|
|
|
if (version_compare(phpversion(), '5.1') != -1) {
|
|
|
|
$digest = env('PHP_AUTH_DIGEST');
|
|
|
|
|
|
|
|
} elseif (function_exists('apache_request_headers')) {
|
|
|
|
$headers = apache_request_headers();
|
2006-11-23 23:06:19 +00:00
|
|
|
if (isset($headers['Authorization']) && !empty($headers['Authorization']) && substr($headers['Authorization'], 0, 7) == 'Digest ') {
|
|
|
|
$digest = substr($headers['Authorization'], 7);
|
2006-06-16 19:45:33 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Server doesn't support digest-auth headers
|
2006-12-22 22:49:47 +00:00
|
|
|
trigger_error(__('SecurityComponent::loginCredentials() - Server does not support digest authentication'), E_USER_WARNING);
|
2006-06-16 19:45:33 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($digest == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$data = $this->parseDigestAuthData($digest);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
2006-06-16 19:45:33 +00:00
|
|
|
/**
|
|
|
|
* Sets the default login options for an HTTP-authenticated request
|
|
|
|
*
|
2006-11-28 00:00:23 +00:00
|
|
|
* @param unknown_type $options
|
|
|
|
* @access private
|
2006-06-16 19:45:33 +00:00
|
|
|
*/
|
|
|
|
function __setLoginDefaults(&$options) {
|
2006-12-01 23:51:43 +00:00
|
|
|
$options = am(
|
|
|
|
array(
|
|
|
|
'type' => 'basic',
|
|
|
|
'realm' => env('SERVER_NAME'),
|
|
|
|
'qop' => 'auth',
|
|
|
|
'nonce' => uniqid()
|
|
|
|
),
|
|
|
|
array_filter($options)
|
|
|
|
);
|
2006-11-23 23:06:19 +00:00
|
|
|
$options = am(array('opaque' => md5($options['realm'])), $options);
|
2006-06-16 19:45:33 +00:00
|
|
|
}
|
|
|
|
/**
|
2006-11-28 00:00:23 +00:00
|
|
|
* Generates the text of an HTTP-authentication request header from an array of options..
|
2006-06-16 19:45:33 +00:00
|
|
|
*
|
2006-11-28 00:00:23 +00:00
|
|
|
* @param array $options
|
|
|
|
* @return unknown
|
|
|
|
* @access public
|
2006-06-16 19:45:33 +00:00
|
|
|
*/
|
|
|
|
function loginRequest($options = array()) {
|
2006-12-06 20:37:12 +00:00
|
|
|
$options = am($this->loginOptions, $options);
|
2006-06-16 19:45:33 +00:00
|
|
|
$this->__setLoginDefaults($options);
|
2006-12-01 23:51:43 +00:00
|
|
|
$data = 'WWW-Authenticate: ' . ucfirst($options['type']) . ' realm="' . $options['realm'] . '"';
|
2006-08-04 08:03:39 +00:00
|
|
|
|
2006-06-16 19:45:33 +00:00
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
/**
|
2006-11-28 00:00:23 +00:00
|
|
|
* Parses an HTTP digest authentication response, and returns an array of the data, or null on failure.
|
2006-06-16 19:45:33 +00:00
|
|
|
*
|
2006-11-28 00:00:23 +00:00
|
|
|
* @param string $digest
|
2006-12-01 23:51:43 +00:00
|
|
|
* @return array Digest authentication parameters
|
2006-11-28 00:00:23 +00:00
|
|
|
* @access public
|
2006-06-16 19:45:33 +00:00
|
|
|
*/
|
|
|
|
function parseDigestAuthData($digest) {
|
|
|
|
if (substr($digest, 0, 7) == 'Digest ') {
|
|
|
|
$digest = substr($digest, 7);
|
|
|
|
}
|
2006-05-26 05:29:17 +00:00
|
|
|
|
2006-06-16 19:45:33 +00:00
|
|
|
$keys = array();
|
|
|
|
$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;
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
2006-06-16 20:06:11 +00:00
|
|
|
/**
|
2006-12-01 23:51:43 +00:00
|
|
|
* Calls a controller callback method
|
2006-06-16 20:06:11 +00:00
|
|
|
*
|
2006-11-28 00:00:23 +00:00
|
|
|
* @param object $controller
|
|
|
|
* @param string $method
|
|
|
|
* @param array $params
|
|
|
|
* @return Contrtoller callback method
|
|
|
|
* @access private
|
2006-06-16 20:06:11 +00:00
|
|
|
*/
|
|
|
|
function __callback(&$controller, $method, $params = array()) {
|
|
|
|
if (is_callable(array($controller, $method))) {
|
|
|
|
return call_user_func_array(array(&$controller, $method), empty($params) ? null : $params);
|
|
|
|
} else {
|
|
|
|
// Debug::warning('Callback method ' . $method . ' in controller ' . get_class($controller)
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2006-03-12 00:03:24 +00:00
|
|
|
}
|
|
|
|
?>
|