2011-01-21 23:10:45 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* PHP 5
|
|
|
|
*
|
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2012-03-13 02:46:46 +00:00
|
|
|
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2011-01-21 23:10:45 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2012-03-13 02:46:46 +00:00
|
|
|
* @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2011-01-21 23:10:45 +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
|
|
|
|
2011-03-06 00:07:56 +00:00
|
|
|
App::uses('BaseAuthenticate', 'Controller/Component/Auth');
|
2011-01-21 23:10:45 +00:00
|
|
|
|
2011-01-22 21:27:07 +00:00
|
|
|
/**
|
|
|
|
* Basic Authentication adapter for AuthComponent.
|
|
|
|
*
|
2012-12-22 22:48:15 +00:00
|
|
|
* Provides Basic HTTP authentication support for AuthComponent. Basic Auth will authenticate users
|
|
|
|
* against the configured userModel and verify the username and passwords match. Clients using Basic Authentication
|
|
|
|
* must support cookies. Since AuthComponent identifies users based on Session contents, clients using Basic
|
2011-01-22 21:27:07 +00:00
|
|
|
* Auth must support cookies.
|
|
|
|
*
|
|
|
|
* ### Using Basic 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('Basic')
|
|
|
|
* )
|
|
|
|
* );
|
|
|
|
* }}}
|
|
|
|
*
|
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.
|
|
|
|
*
|
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-21 23:10:45 +00:00
|
|
|
class BasicAuthenticate extends BaseAuthenticate {
|
2012-03-04 00:27:46 +00:00
|
|
|
|
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).`
|
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.
|
2012-12-22 22:48:15 +00:00
|
|
|
* - `realm` The realm authentication is for. Defaults the server name.
|
2011-01-22 01:28:24 +00:00
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
public $settings = array(
|
|
|
|
'fields' => array(
|
|
|
|
'username' => 'username',
|
|
|
|
'password' => 'password'
|
|
|
|
),
|
|
|
|
'userModel' => 'User',
|
|
|
|
'scope' => array(),
|
2011-10-19 15:54:08 +00:00
|
|
|
'recursive' => 0,
|
2012-04-05 02:06:54 +00:00
|
|
|
'contain' => null,
|
2011-01-22 01:28:24 +00:00
|
|
|
'realm' => '',
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor, completes configuration for basic 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 01:28:24 +00:00
|
|
|
*/
|
2011-02-18 04:17:07 +00:00
|
|
|
public function __construct(ComponentCollection $collection, $settings) {
|
|
|
|
parent::__construct($collection, $settings);
|
2011-01-22 01:28:24 +00:00
|
|
|
if (empty($this->settings['realm'])) {
|
|
|
|
$this->settings['realm'] = env('SERVER_NAME');
|
|
|
|
}
|
|
|
|
}
|
2011-01-22 21:27:07 +00:00
|
|
|
|
2011-01-21 23:10:45 +00:00
|
|
|
/**
|
2012-12-22 22:48:15 +00:00
|
|
|
* Authenticate a user using basic HTTP auth. Will use the configured User model and attempt a
|
2011-01-21 23:10:45 +00:00
|
|
|
* 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) {
|
2011-02-05 23:41:00 +00:00
|
|
|
$result = $this->getUser($request);
|
2011-01-22 01:28:24 +00:00
|
|
|
|
2011-01-22 01:52:38 +00:00
|
|
|
if (empty($result)) {
|
2011-01-22 01:28:24 +00:00
|
|
|
$response->header($this->loginHeaders());
|
|
|
|
$response->statusCode(401);
|
|
|
|
$response->send();
|
|
|
|
return false;
|
|
|
|
}
|
2011-01-22 01:52:38 +00:00
|
|
|
return $result;
|
2011-01-22 01:28:24 +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) {
|
|
|
|
$username = env('PHP_AUTH_USER');
|
|
|
|
$pass = env('PHP_AUTH_PW');
|
2011-08-16 03:55:08 +00:00
|
|
|
|
2011-02-05 23:41:00 +00:00
|
|
|
if (empty($username) || empty($pass)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return $this->_findUser($username, $pass);
|
|
|
|
}
|
|
|
|
|
2011-01-22 01:28:24 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
}
|
2012-03-04 00:27:46 +00:00
|
|
|
|
|
|
|
}
|