Skip to content

Commit

Permalink
Adding a response parameter to authenticate() both basic and digest a…
Browse files Browse the repository at this point in the history
…uth need to set response headers.
  • Loading branch information
markstory committed Feb 4, 2011
1 parent 332b6cf commit ba02483
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 13 deletions.
16 changes: 12 additions & 4 deletions cake/libs/controller/components/auth.php
Expand Up @@ -34,7 +34,7 @@
*/
class AuthComponent extends Component {

const ALL = '*';
const ALL = 'all';

/**
* Maintains current user login state.
Expand Down Expand Up @@ -231,6 +231,13 @@ class AuthComponent extends Component {
*/
public $request;

/**
* Response object
*
* @var CakeResponse
*/
public $response;

/**
* Method list for bound controller
*
Expand All @@ -246,6 +253,7 @@ class AuthComponent extends Component {
*/
public function initialize($controller) {
$this->request = $controller->request;
$this->response = $controller->response;
$this->_methods = $controller->methods;

if (Configure::read('debug') > 0) {
Expand Down Expand Up @@ -507,7 +515,7 @@ public function login($user = null) {
$this->_loggedIn = false;

if (empty($user)) {
$user = $this->identify($this->request);
$user = $this->identify($this->request, $this->response);
}
if ($user) {
$this->Session->write(self::$sessionKey, $user);
Expand Down Expand Up @@ -587,12 +595,12 @@ public function redirect($url = null) {
* @param CakeRequest $request The request that contains authentication data.
* @return array User record data, or false, if the user could not be identified.
*/
public function identify(CakeRequest $request) {
public function identify(CakeRequest $request, CakeResponse $response) {
if (empty($this->_authenticateObjects)) {
$this->constructAuthenticate();
}
foreach ($this->_authenticateObjects as $auth) {
$result = $auth->authenticate($request);
$result = $auth->authenticate($request, $response);
if (!empty($result) && is_array($result)) {
return $result;
}
Expand Down
3 changes: 2 additions & 1 deletion cake/libs/controller/components/auth/base_authenticate.php
Expand Up @@ -63,7 +63,8 @@ public function hash($password) {
* Authenticate a user based on the request information.
*
* @param CakeRequest $request Request to get authentication information from.
* @param CakeResponse $response A response object that can have headers added.
* @return mixed Either false on failure, or an array of user data on success.
*/
abstract public function authenticate(CakeRequest $request);
abstract public function authenticate(CakeRequest $request, CakeResponse $response);
}
3 changes: 2 additions & 1 deletion cake/libs/controller/components/auth/form_authenticate.php
Expand Up @@ -41,9 +41,10 @@ class FormAuthenticate extends BaseAuthenticate {
* there is no post data, either username or password is missing, of if the scope conditions have not been met.
*
* @param CakeRequest $request The request that contains login information.
* @param CakeResponse $response Unused response object.
* @return mixed. False on login failure. An array of User data on success.
*/
public function authenticate(CakeRequest $request) {
public function authenticate(CakeRequest $request, CakeResponse $response) {
$userModel = $this->settings['userModel'];
list($plugin, $model) = pluginSplit($userModel);

Expand Down
Expand Up @@ -17,6 +17,7 @@
App::import('Component', 'auth/form_authenticate');
App::import('Model', 'AppModel');
App::import('Core', 'CakeRequest');
App::import('Core', 'CakeResponse');

require_once CAKE_TESTS . 'cases' . DS . 'libs' . DS . 'model' . DS . 'models.php';

Expand All @@ -42,6 +43,7 @@ function setUp() {
));
$password = Security::hash('password', null, true);
ClassRegistry::init('User')->updateAll(array('password' => '"' . $password . '"'));
$this->response = $this->getMock('CakeResponse');
}

/**
Expand All @@ -66,7 +68,7 @@ function testConstructor() {
function testAuthenticateNoData() {
$request = new CakeRequest('posts/index', false);
$request->data = array();
$this->assertFalse($this->auth->authenticate($request));
$this->assertFalse($this->auth->authenticate($request, $this->response));
}

/**
Expand All @@ -77,7 +79,7 @@ function testAuthenticateNoData() {
function testAuthenticateNoUsername() {
$request = new CakeRequest('posts/index', false);
$request->data = array('User' => array('password' => 'foobar'));
$this->assertFalse($this->auth->authenticate($request));
$this->assertFalse($this->auth->authenticate($request, $this->response));
}

/**
Expand All @@ -88,7 +90,7 @@ function testAuthenticateNoUsername() {
function testAuthenticateNoPassword() {
$request = new CakeRequest('posts/index', false);
$request->data = array('User' => array('user' => 'mariano'));
$this->assertFalse($this->auth->authenticate($request));
$this->assertFalse($this->auth->authenticate($request, $this->response));
}

/**
Expand All @@ -103,7 +105,7 @@ function testAuthenticateInjection() {
'user' => '> 1',
'password' => "' OR 1 = 1"
));
$this->assertFalse($this->auth->authenticate($request));
$this->assertFalse($this->auth->authenticate($request, $this->response));
}

/**
Expand All @@ -117,7 +119,7 @@ function testAuthenticateSuccess() {
'user' => 'mariano',
'password' => 'password'
));
$result = $this->auth->authenticate($request);
$result = $this->auth->authenticate($request, $this->response);
$expected = array(
'id' => 1,
'user' => 'mariano',
Expand All @@ -140,7 +142,7 @@ function testAuthenticateScopeFail() {
'password' => 'password'
));

$this->assertFalse($this->auth->authenticate($request));
$this->assertFalse($this->auth->authenticate($request, $this->response));
}

/**
Expand Down Expand Up @@ -170,7 +172,7 @@ function testPluginModel() {
'password' => 'cake'
));

$result = $this->auth->authenticate($request);
$result = $this->auth->authenticate($request, $this->response);
$expected = array(
'id' => 1,
'username' => 'gwoo',
Expand Down

0 comments on commit ba02483

Please sign in to comment.