2011-01-02 05:57:23 +00:00
|
|
|
<?php
|
2011-01-03 18:00:06 +00:00
|
|
|
/**
|
2017-06-10 21:33:55 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
|
2017-06-10 22:10:52 +00:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
2011-01-03 18:00:06 +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-03 18:00:06 +00:00
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2017-06-10 22:10:52 +00:00
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
2017-06-10 21:33:55 +00:00
|
|
|
* @link https://cakephp.org CakePHP(tm) Project
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Test.Case.Controller.Component.Auth
|
2011-01-03 18:00:06 +00:00
|
|
|
* @since CakePHP(tm) v 2.0
|
2013-05-30 22:11:14 +00:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
2011-01-03 18:00:06 +00:00
|
|
|
*/
|
2011-01-02 05:57:23 +00:00
|
|
|
|
2011-03-05 23:52:06 +00:00
|
|
|
App::uses('AuthComponent', 'Controller/Component');
|
|
|
|
App::uses('FormAuthenticate', 'Controller/Component/Auth');
|
|
|
|
App::uses('AppModel', 'Model');
|
|
|
|
App::uses('CakeRequest', 'Network');
|
|
|
|
App::uses('CakeResponse', 'Network');
|
2011-01-02 05:57:23 +00:00
|
|
|
|
2012-03-12 02:20:25 +00:00
|
|
|
require_once CAKE . 'Test' . DS . 'Case' . DS . 'Model' . DS . 'models.php';
|
2011-01-02 05:57:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test case for FormAuthentication
|
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Test.Case.Controller.Component.Auth
|
2011-01-02 05:57:23 +00:00
|
|
|
*/
|
|
|
|
class FormAuthenticateTest extends CakeTestCase {
|
|
|
|
|
2013-05-30 22:11:14 +00:00
|
|
|
/**
|
|
|
|
* Fixtrues
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2011-01-03 23:17:27 +00:00
|
|
|
public $fixtures = array('core.user', 'core.auth_user');
|
2011-01-02 05:57:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* setup
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function setUp() {
|
2011-01-02 05:57:23 +00:00
|
|
|
parent::setUp();
|
2011-02-18 04:17:07 +00:00
|
|
|
$this->Collection = $this->getMock('ComponentCollection');
|
|
|
|
$this->auth = new FormAuthenticate($this->Collection, array(
|
2011-01-02 05:57:23 +00:00
|
|
|
'fields' => array('username' => 'user', 'password' => 'password'),
|
|
|
|
'userModel' => 'User'
|
|
|
|
));
|
2011-01-21 21:22:29 +00:00
|
|
|
$password = Security::hash('password', null, true);
|
2011-06-21 18:59:18 +00:00
|
|
|
$User = ClassRegistry::init('User');
|
|
|
|
$User->updateAll(array('password' => $User->getDataSource()->value($password)));
|
2011-01-22 00:56:23 +00:00
|
|
|
$this->response = $this->getMock('CakeResponse');
|
2011-01-02 05:57:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test applying settings in the constructor
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testConstructor() {
|
2011-02-18 04:17:07 +00:00
|
|
|
$object = new FormAuthenticate($this->Collection, array(
|
2011-01-02 05:57:23 +00:00
|
|
|
'userModel' => 'AuthUser',
|
|
|
|
'fields' => array('username' => 'user', 'password' => 'password')
|
|
|
|
));
|
|
|
|
$this->assertEquals('AuthUser', $object->settings['userModel']);
|
|
|
|
$this->assertEquals(array('username' => 'user', 'password' => 'password'), $object->settings['fields']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test the authenticate method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testAuthenticateNoData() {
|
2011-01-02 05:57:23 +00:00
|
|
|
$request = new CakeRequest('posts/index', false);
|
|
|
|
$request->data = array();
|
2011-01-22 00:56:23 +00:00
|
|
|
$this->assertFalse($this->auth->authenticate($request, $this->response));
|
2011-01-02 05:57:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test the authenticate method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testAuthenticateNoUsername() {
|
2011-01-02 05:57:23 +00:00
|
|
|
$request = new CakeRequest('posts/index', false);
|
|
|
|
$request->data = array('User' => array('password' => 'foobar'));
|
2011-01-22 00:56:23 +00:00
|
|
|
$this->assertFalse($this->auth->authenticate($request, $this->response));
|
2011-01-02 05:57:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test the authenticate method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testAuthenticateNoPassword() {
|
2011-01-02 05:57:23 +00:00
|
|
|
$request = new CakeRequest('posts/index', false);
|
|
|
|
$request->data = array('User' => array('user' => 'mariano'));
|
2011-01-22 00:56:23 +00:00
|
|
|
$this->assertFalse($this->auth->authenticate($request, $this->response));
|
2011-01-02 05:57:23 +00:00
|
|
|
}
|
|
|
|
|
2012-08-15 17:49:31 +00:00
|
|
|
/**
|
|
|
|
* test authenticate password is false method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testAuthenticatePasswordIsFalse() {
|
|
|
|
$request = new CakeRequest('posts/index', false);
|
|
|
|
$request->data = array(
|
|
|
|
'User' => array(
|
|
|
|
'user' => 'mariano',
|
|
|
|
'password' => null
|
|
|
|
));
|
|
|
|
$this->assertFalse($this->auth->authenticate($request, $this->response));
|
|
|
|
}
|
|
|
|
|
2013-12-07 13:10:08 +00:00
|
|
|
/**
|
|
|
|
* Test for password as empty string with _checkFields() call skipped
|
|
|
|
* Refs https://github.com/cakephp/cakephp/pull/2441
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testAuthenticatePasswordIsEmptyString() {
|
|
|
|
$request = new CakeRequest('posts/index', false);
|
|
|
|
$request->data = array(
|
|
|
|
'User' => array(
|
|
|
|
'user' => 'mariano',
|
|
|
|
'password' => ''
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->auth = $this->getMock(
|
|
|
|
'FormAuthenticate',
|
|
|
|
array('_checkFields'),
|
|
|
|
array(
|
|
|
|
$this->Collection,
|
|
|
|
array(
|
|
|
|
'fields' => array('username' => 'user', 'password' => 'password'),
|
|
|
|
'userModel' => 'User'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
// Simulate that check for ensuring password is not empty is missing.
|
|
|
|
$this->auth->expects($this->once())
|
|
|
|
->method('_checkFields')
|
|
|
|
->will($this->returnValue(true));
|
|
|
|
|
|
|
|
$this->assertFalse($this->auth->authenticate($request, $this->response));
|
|
|
|
}
|
|
|
|
|
2013-04-24 20:33:24 +00:00
|
|
|
/**
|
|
|
|
* test authenticate field is not string
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testAuthenticateFieldsAreNotString() {
|
|
|
|
$request = new CakeRequest('posts/index', false);
|
|
|
|
$request->data = array(
|
|
|
|
'User' => array(
|
|
|
|
'user' => array('mariano', 'phpnut'),
|
|
|
|
'password' => 'my password'
|
|
|
|
));
|
|
|
|
$this->assertFalse($this->auth->authenticate($request, $this->response));
|
|
|
|
|
2014-06-03 01:58:50 +00:00
|
|
|
$request->data = array(
|
|
|
|
'User' => array(
|
|
|
|
'user' => array(),
|
|
|
|
'password' => 'my password'
|
|
|
|
));
|
|
|
|
$this->assertFalse($this->auth->authenticate($request, $this->response));
|
|
|
|
|
2013-04-24 20:33:24 +00:00
|
|
|
$request->data = array(
|
|
|
|
'User' => array(
|
|
|
|
'user' => 'mariano',
|
|
|
|
'password' => array('password1', 'password2')
|
|
|
|
));
|
|
|
|
$this->assertFalse($this->auth->authenticate($request, $this->response));
|
|
|
|
}
|
|
|
|
|
2011-01-02 05:57:23 +00:00
|
|
|
/**
|
|
|
|
* test the authenticate method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testAuthenticateInjection() {
|
2011-01-02 05:57:23 +00:00
|
|
|
$request = new CakeRequest('posts/index', false);
|
|
|
|
$request->data = array(
|
|
|
|
'User' => array(
|
|
|
|
'user' => '> 1',
|
|
|
|
'password' => "' OR 1 = 1"
|
|
|
|
));
|
2011-01-22 00:56:23 +00:00
|
|
|
$this->assertFalse($this->auth->authenticate($request, $this->response));
|
2011-01-02 05:57:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-02-23 23:29:53 +00:00
|
|
|
* test authenticate success
|
2011-01-02 05:57:23 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testAuthenticateSuccess() {
|
2011-01-02 05:57:23 +00:00
|
|
|
$request = new CakeRequest('posts/index', false);
|
|
|
|
$request->data = array('User' => array(
|
|
|
|
'user' => 'mariano',
|
2011-01-21 21:22:29 +00:00
|
|
|
'password' => 'password'
|
2011-01-02 05:57:23 +00:00
|
|
|
));
|
2011-01-22 00:56:23 +00:00
|
|
|
$result = $this->auth->authenticate($request, $this->response);
|
2011-01-02 05:57:23 +00:00
|
|
|
$expected = array(
|
|
|
|
'id' => 1,
|
|
|
|
'user' => 'mariano',
|
|
|
|
'created' => '2007-03-17 01:16:23',
|
|
|
|
'updated' => '2007-03-17 01:18:31'
|
|
|
|
);
|
|
|
|
$this->assertEquals($expected, $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test scope failure.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testAuthenticateScopeFail() {
|
2011-01-02 05:57:23 +00:00
|
|
|
$this->auth->settings['scope'] = array('user' => 'nate');
|
|
|
|
$request = new CakeRequest('posts/index', false);
|
|
|
|
$request->data = array('User' => array(
|
|
|
|
'user' => 'mariano',
|
2011-01-21 21:22:29 +00:00
|
|
|
'password' => 'password'
|
2011-01-02 05:57:23 +00:00
|
|
|
));
|
|
|
|
|
2011-01-22 00:56:23 +00:00
|
|
|
$this->assertFalse($this->auth->authenticate($request, $this->response));
|
2011-01-02 05:57:23 +00:00
|
|
|
}
|
|
|
|
|
2014-06-03 01:58:50 +00:00
|
|
|
/**
|
|
|
|
* Test that username of 0 works.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testAuthenticateUsernameZero() {
|
|
|
|
$User = ClassRegistry::init('User');
|
|
|
|
$User->updateAll(array('user' => $User->getDataSource()->value('0')), array('user' => 'mariano'));
|
|
|
|
|
|
|
|
$request = new CakeRequest('posts/index', false);
|
|
|
|
$request->data = array('User' => array(
|
|
|
|
'user' => '0',
|
|
|
|
'password' => 'password'
|
|
|
|
));
|
|
|
|
|
|
|
|
$expected = array(
|
|
|
|
'id' => 1,
|
|
|
|
'user' => '0',
|
|
|
|
'created' => '2007-03-17 01:16:23',
|
|
|
|
'updated' => '2007-03-17 01:18:31'
|
|
|
|
);
|
|
|
|
$this->assertEquals($expected, $this->auth->authenticate($request, $this->response));
|
|
|
|
}
|
|
|
|
|
2011-01-03 23:17:27 +00:00
|
|
|
/**
|
|
|
|
* test a model in a plugin.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testPluginModel() {
|
2011-01-03 23:17:27 +00:00
|
|
|
Cache::delete('object_map', '_cake_core_');
|
|
|
|
App::build(array(
|
2012-02-18 12:31:29 +00:00
|
|
|
'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS),
|
2012-02-18 12:04:54 +00:00
|
|
|
), App::RESET);
|
2011-05-13 06:03:11 +00:00
|
|
|
CakePlugin::load('TestPlugin');
|
2011-01-03 23:17:27 +00:00
|
|
|
|
|
|
|
$PluginModel = ClassRegistry::init('TestPlugin.TestPluginAuthUser');
|
|
|
|
$user['id'] = 1;
|
|
|
|
$user['username'] = 'gwoo';
|
|
|
|
$user['password'] = Security::hash(Configure::read('Security.salt') . 'cake');
|
|
|
|
$PluginModel->save($user, false);
|
2011-04-17 10:35:21 +00:00
|
|
|
|
2011-01-03 23:17:27 +00:00
|
|
|
$this->auth->settings['userModel'] = 'TestPlugin.TestPluginAuthUser';
|
|
|
|
$this->auth->settings['fields']['username'] = 'username';
|
2011-04-17 10:35:21 +00:00
|
|
|
|
2011-01-03 23:17:27 +00:00
|
|
|
$request = new CakeRequest('posts/index', false);
|
|
|
|
$request->data = array('TestPluginAuthUser' => array(
|
|
|
|
'username' => 'gwoo',
|
2011-01-21 21:22:29 +00:00
|
|
|
'password' => 'cake'
|
2011-01-03 23:17:27 +00:00
|
|
|
));
|
|
|
|
|
2011-01-22 00:56:23 +00:00
|
|
|
$result = $this->auth->authenticate($request, $this->response);
|
2011-01-03 23:17:27 +00:00
|
|
|
$expected = array(
|
|
|
|
'id' => 1,
|
|
|
|
'username' => 'gwoo',
|
2011-10-03 19:21:07 +00:00
|
|
|
'created' => '2007-03-17 01:16:23'
|
2011-01-03 23:17:27 +00:00
|
|
|
);
|
2015-07-21 08:22:53 +00:00
|
|
|
$this->assertEquals(static::date(), $result['updated']);
|
2011-10-03 19:21:07 +00:00
|
|
|
unset($result['updated']);
|
2011-01-03 23:17:27 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2011-05-13 06:03:11 +00:00
|
|
|
CakePlugin::unload();
|
2011-01-03 23:17:27 +00:00
|
|
|
}
|
|
|
|
|
2013-05-20 06:44:35 +00:00
|
|
|
/**
|
|
|
|
* test password hasher settings
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testPasswordHasherSettings() {
|
|
|
|
$this->auth->settings['passwordHasher'] = array(
|
|
|
|
'className' => 'Simple',
|
|
|
|
'hashType' => 'md5'
|
|
|
|
);
|
|
|
|
|
|
|
|
$passwordHasher = $this->auth->passwordHasher();
|
|
|
|
$result = $passwordHasher->config();
|
|
|
|
$this->assertEquals('md5', $result['hashType']);
|
|
|
|
|
|
|
|
$hash = Security::hash('mypass', 'md5', true);
|
|
|
|
$User = ClassRegistry::init('User');
|
|
|
|
$User->updateAll(
|
|
|
|
array('password' => $User->getDataSource()->value($hash)),
|
|
|
|
array('User.user' => 'mariano')
|
|
|
|
);
|
|
|
|
|
|
|
|
$request = new CakeRequest('posts/index', false);
|
|
|
|
$request->data = array('User' => array(
|
|
|
|
'user' => 'mariano',
|
|
|
|
'password' => 'mypass'
|
|
|
|
));
|
|
|
|
|
|
|
|
$result = $this->auth->authenticate($request, $this->response);
|
|
|
|
$expected = array(
|
|
|
|
'id' => 1,
|
|
|
|
'user' => 'mariano',
|
|
|
|
'created' => '2007-03-17 01:16:23',
|
|
|
|
'updated' => '2007-03-17 01:18:31'
|
|
|
|
);
|
|
|
|
$this->assertEquals($expected, $result);
|
|
|
|
|
|
|
|
$this->auth = new FormAuthenticate($this->Collection, array(
|
|
|
|
'fields' => array('username' => 'user', 'password' => 'password'),
|
|
|
|
'userModel' => 'User'
|
|
|
|
));
|
|
|
|
$this->auth->settings['passwordHasher'] = array(
|
|
|
|
'className' => 'Simple',
|
|
|
|
'hashType' => 'sha1'
|
|
|
|
);
|
|
|
|
$this->assertFalse($this->auth->authenticate($request, $this->response));
|
|
|
|
}
|
|
|
|
|
2011-04-17 10:35:21 +00:00
|
|
|
}
|