| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535 |
- <?php
- /**
- * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
- * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
- *
- * Licensed under The MIT License
- * For full copyright and license information, please see the LICENSE.txt
- * Redistributions of files must retain the above copyright notice.
- *
- * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
- * @link https://cakephp.org CakePHP(tm) Project
- * @since 2.0.0
- * @license https://opensource.org/licenses/mit-license.php MIT License
- */
- namespace Cake\Test\TestCase\Auth;
- use Cake\Auth\FormAuthenticate;
- use Cake\Controller\ComponentRegistry;
- use Cake\Core\Plugin;
- use Cake\Http\Response;
- use Cake\Http\ServerRequest;
- use Cake\I18n\Time;
- use Cake\ORM\Entity;
- use Cake\TestSuite\TestCase;
- use Cake\Utility\Security;
- use TestApp\Auth\CallCounterPasswordHasher;
- /**
- * Test case for FormAuthentication
- */
- class FormAuthenticateTest extends TestCase
- {
- /**
- * Fixtures
- *
- * @var array
- */
- public $fixtures = ['core.AuthUsers', 'core.Users'];
- /**
- * @var \Cake\Auth\FormAuthenticate
- */
- protected $auth;
- /**
- * setup
- *
- * @return void
- */
- public function setUp()
- {
- parent::setUp();
- $this->Collection = $this->getMockBuilder(ComponentRegistry::class)->getMock();
- $this->auth = new FormAuthenticate($this->Collection, [
- 'userModel' => 'Users',
- ]);
- $password = password_hash('password', PASSWORD_DEFAULT);
- $this->getTableLocator()->clear();
- $Users = $this->getTableLocator()->get('Users');
- $Users->updateAll(['password' => $password], []);
- $AuthUsers = $this->getTableLocator()->get('AuthUsers', [
- 'className' => 'TestApp\Model\Table\AuthUsersTable',
- ]);
- $AuthUsers->updateAll(['password' => $password], []);
- $this->response = $this->getMockBuilder(Response::class)->getMock();
- }
- /**
- * test applying settings in the constructor
- *
- * @return void
- */
- public function testConstructor()
- {
- $object = new FormAuthenticate($this->Collection, [
- 'userModel' => 'AuthUsers',
- 'fields' => ['username' => 'user', 'password' => 'password'],
- ]);
- $this->assertEquals('AuthUsers', $object->getConfig('userModel'));
- $this->assertEquals(['username' => 'user', 'password' => 'password'], $object->getConfig('fields'));
- }
- /**
- * test the authenticate method
- *
- * @return void
- */
- public function testAuthenticateNoData()
- {
- $request = new ServerRequest([
- 'url' => 'posts/index',
- 'post' => [],
- ]);
- $this->assertFalse($this->auth->authenticate($request, $this->response));
- }
- /**
- * test the authenticate method
- *
- * @return void
- */
- public function testAuthenticateNoUsername()
- {
- $request = new ServerRequest([
- 'url' => 'posts/index',
- 'post' => ['password' => 'foobar'],
- ]);
- $this->assertFalse($this->auth->authenticate($request, $this->response));
- }
- /**
- * test the authenticate method
- *
- * @return void
- */
- public function testAuthenticateNoPassword()
- {
- $request = new ServerRequest([
- 'url' => 'posts/index',
- 'post' => ['username' => 'mariano'],
- ]);
- $this->assertFalse($this->auth->authenticate($request, $this->response));
- }
- /**
- * test authenticate password is false method
- *
- * @return void
- */
- public function testAuthenticatePasswordIsFalse()
- {
- $request = new ServerRequest('posts/index', false);
- $request = new ServerRequest([
- 'url' => 'posts/index',
- 'post' => [
- 'username' => 'mariano',
- 'password' => null,
- ],
- ]);
- $this->assertFalse($this->auth->authenticate($request, $this->response));
- }
- /**
- * 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 ServerRequest([
- 'url' => 'posts/index',
- 'post' => [
- 'username' => 'mariano',
- 'password' => '',
- ],
- ]);
- $this->auth = $this->getMockBuilder(FormAuthenticate::class)
- ->setMethods(['_checkFields'])
- ->setConstructorArgs([
- $this->Collection,
- ['userModel' => 'Users'],
- ])
- ->getMock();
- // 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));
- }
- /**
- * test authenticate field is not string
- *
- * @return void
- */
- public function testAuthenticateFieldsAreNotString()
- {
- $request = new ServerRequest([
- 'url' => 'posts/index',
- 'post' => [
- 'username' => ['mariano', 'phpnut'],
- 'password' => 'my password',
- ],
- ]);
- $this->assertFalse($this->auth->authenticate($request, $this->response));
- $request = new ServerRequest([
- 'url' => 'posts/index',
- 'post' => [
- 'username' => 'mariano',
- 'password' => ['password1', 'password2'],
- ],
- ]);
- $this->assertFalse($this->auth->authenticate($request, $this->response));
- }
- /**
- * test the authenticate method
- *
- * @return void
- */
- public function testAuthenticateInjection()
- {
- $request = new ServerRequest([
- 'url' => 'posts/index',
- 'post' => [
- 'username' => '> 1',
- 'password' => "' OR 1 = 1",
- ],
- ]);
- $this->assertFalse($this->auth->authenticate($request, $this->response));
- }
- /**
- * test authenticate success
- *
- * @return void
- */
- public function testAuthenticateSuccess()
- {
- $request = new ServerRequest([
- 'url' => 'posts/index',
- 'post' => [
- 'username' => 'mariano',
- 'password' => 'password',
- ],
- ]);
- $result = $this->auth->authenticate($request, $this->response);
- $expected = [
- 'id' => 1,
- 'username' => 'mariano',
- 'created' => new Time('2007-03-17 01:16:23'),
- 'updated' => new Time('2007-03-17 01:18:31'),
- ];
- $this->assertEquals($expected, $result);
- }
- /**
- * Test that authenticate() includes virtual fields.
- *
- * @return void
- */
- public function testAuthenticateIncludesVirtualFields()
- {
- $users = $this->getTableLocator()->get('Users');
- $users->setEntityClass('TestApp\Model\Entity\VirtualUser');
- $request = new ServerRequest([
- 'url' => 'posts/index',
- 'post' => [
- 'username' => 'mariano',
- 'password' => 'password',
- ],
- ]);
- $result = $this->auth->authenticate($request, $this->response);
- $expected = [
- 'id' => 1,
- 'username' => 'mariano',
- 'bonus' => 'bonus',
- 'created' => new Time('2007-03-17 01:16:23'),
- 'updated' => new Time('2007-03-17 01:18:31'),
- ];
- $this->assertEquals($expected, $result);
- }
- /**
- * test a model in a plugin.
- *
- * @return void
- */
- public function testPluginModel()
- {
- $this->loadPlugins(['TestPlugin']);
- $PluginModel = $this->getTableLocator()->get('TestPlugin.AuthUsers');
- $user['id'] = 1;
- $user['username'] = 'gwoo';
- $user['password'] = password_hash('cake', PASSWORD_BCRYPT);
- $PluginModel->save(new Entity($user));
- $this->auth->setConfig('userModel', 'TestPlugin.AuthUsers');
- $request = new ServerRequest([
- 'url' => 'posts/index',
- 'post' => [
- 'username' => 'gwoo',
- 'password' => 'cake',
- ],
- ]);
- $result = $this->auth->authenticate($request, $this->response);
- $expected = [
- 'id' => 1,
- 'username' => 'gwoo',
- 'created' => new Time('2007-03-17 01:16:23'),
- 'updated' => new Time('2007-03-17 01:18:31'),
- ];
- $this->assertEquals($expected, $result);
- $this->clearPlugins();
- }
- /**
- * Test using custom finder
- *
- * @return void
- */
- public function testFinder()
- {
- $request = new ServerRequest([
- 'url' => 'posts/index',
- 'post' => [
- 'username' => 'mariano',
- 'password' => 'password',
- ],
- ]);
- $this->auth->setConfig([
- 'userModel' => 'AuthUsers',
- 'finder' => 'auth',
- ]);
- $result = $this->auth->authenticate($request, $this->response);
- $expected = [
- 'id' => 1,
- 'username' => 'mariano',
- ];
- $this->assertEquals($expected, $result, 'Result should not contain "created" and "modified" fields');
- $this->auth->setConfig([
- 'finder' => ['auth' => ['return_created' => true]],
- ]);
- $result = $this->auth->authenticate($request, $this->response);
- $expected = [
- 'id' => 1,
- 'username' => 'mariano',
- 'created' => new Time('2007-03-17 01:16:23'),
- ];
- $this->assertEquals($expected, $result);
- }
- /**
- * Test using custom finder
- *
- * @return void
- */
- public function testFinderOptions()
- {
- $request = new ServerRequest([
- 'url' => 'posts/index',
- 'post' => [
- 'username' => 'mariano',
- 'password' => 'password',
- ],
- ]);
- $this->auth->setConfig([
- 'userModel' => 'AuthUsers',
- 'finder' => 'username',
- ]);
- $result = $this->auth->authenticate($request, $this->response);
- $expected = [
- 'id' => 1,
- 'username' => 'mariano',
- ];
- $this->assertEquals($expected, $result);
- $this->auth->setConfig([
- 'finder' => ['username' => ['username' => 'nate']],
- ]);
- $result = $this->auth->authenticate($request, $this->response);
- $expected = [
- 'id' => 5,
- 'username' => 'nate',
- ];
- $this->assertEquals($expected, $result);
- }
- /**
- * test password hasher settings
- *
- * @return void
- */
- public function testPasswordHasherSettings()
- {
- $this->auth->setConfig('passwordHasher', [
- 'className' => 'Default',
- 'hashType' => PASSWORD_BCRYPT,
- ]);
- $passwordHasher = $this->auth->passwordHasher();
- $result = $passwordHasher->getConfig();
- $this->assertEquals(PASSWORD_BCRYPT, $result['hashType']);
- $hash = password_hash('mypass', PASSWORD_BCRYPT);
- $User = $this->getTableLocator()->get('Users');
- $User->updateAll(
- ['password' => $hash],
- ['username' => 'mariano']
- );
- $request = new ServerRequest([
- 'url' => 'posts/index',
- 'post' => [
- 'username' => 'mariano',
- 'password' => 'mypass',
- ],
- ]);
- $result = $this->auth->authenticate($request, $this->response);
- $expected = [
- 'id' => 1,
- 'username' => 'mariano',
- 'created' => new Time('2007-03-17 01:16:23'),
- 'updated' => new Time('2007-03-17 01:18:31'),
- ];
- $this->assertEquals($expected, $result);
- $this->auth = new FormAuthenticate($this->Collection, [
- 'fields' => ['username' => 'username', 'password' => 'password'],
- 'userModel' => 'Users',
- ]);
- $this->auth->setConfig('passwordHasher', [
- 'className' => 'Default',
- ]);
- $this->assertEquals($expected, $this->auth->authenticate($request, $this->response));
- $User->updateAll(
- ['password' => '$2y$10$/G9GBQDZhWUM4w/WLes3b.XBZSK1hGohs5dMi0vh/oen0l0a7DUyK'],
- ['username' => 'mariano']
- );
- $this->assertFalse($this->auth->authenticate($request, $this->response));
- }
- /**
- * Tests that using default means password don't need to be rehashed
- *
- * @return void
- */
- public function testAuthenticateNoRehash()
- {
- $request = new ServerRequest([
- 'url' => 'posts/index',
- 'post' => [
- 'username' => 'mariano',
- 'password' => 'password',
- ],
- ]);
- $result = $this->auth->authenticate($request, $this->response);
- $this->assertNotEmpty($result);
- $this->assertFalse($this->auth->needsPasswordRehash());
- }
- /**
- * Tests that not using the Default password hasher means that the password
- * needs to be rehashed
- *
- * @return void
- */
- public function testAuthenticateRehash()
- {
- $this->auth = new FormAuthenticate($this->Collection, [
- 'userModel' => 'Users',
- 'passwordHasher' => 'Weak',
- ]);
- $password = $this->auth->passwordHasher()->hash('password');
- $this->getTableLocator()->get('Users')->updateAll(['password' => $password], []);
- $request = new ServerRequest([
- 'url' => 'posts/index',
- 'post' => [
- 'username' => 'mariano',
- 'password' => 'password',
- ],
- ]);
- $result = $this->auth->authenticate($request, $this->response);
- $this->assertNotEmpty($result);
- $this->assertTrue($this->auth->needsPasswordRehash());
- }
- /**
- * Tests that password hasher function is called exactly once in all cases.
- *
- * @param string $username
- * @param string|null $password
- * @return void
- * @dataProvider userList
- */
- public function testAuthenticateSingleHash($username, $password)
- {
- $this->auth = new FormAuthenticate($this->Collection, [
- 'userModel' => 'Users',
- 'passwordHasher' => CallCounterPasswordHasher::class,
- ]);
- $this->getTableLocator()->get('Users')->updateAll(
- ['password' => $password],
- ['username' => $username]
- );
- $request = new ServerRequest([
- 'url' => 'posts/index',
- 'post' => [
- 'username' => $username,
- 'password' => 'anything',
- ],
- ]);
- $result = $this->auth->authenticate($request, new Response());
- $this->assertFalse($result);
- /** @var \TestApp\Auth\CallCounterPasswordHasher $passwordHasher */
- $passwordHasher = $this->auth->passwordHasher();
- $this->assertInstanceOf(CallCounterPasswordHasher::class, $passwordHasher);
- $this->assertSame(1, $passwordHasher->callCount);
- }
- public function userList()
- {
- return [
- ['notexist', ''],
- ['mariano', null],
- ['mariano', ''],
- ['mariano', 'somehash'],
- ];
- }
- }
|