FormAuthenticateTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 2.0.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Auth;
  16. use Cake\Auth\FormAuthenticate;
  17. use Cake\Controller\ComponentRegistry;
  18. use Cake\Core\Plugin;
  19. use Cake\Http\Response;
  20. use Cake\Http\ServerRequest;
  21. use Cake\I18n\Time;
  22. use Cake\ORM\Entity;
  23. use Cake\TestSuite\TestCase;
  24. use Cake\Utility\Security;
  25. use TestApp\Auth\CallCounterPasswordHasher;
  26. /**
  27. * Test case for FormAuthentication
  28. */
  29. class FormAuthenticateTest extends TestCase
  30. {
  31. /**
  32. * Fixtures
  33. *
  34. * @var array
  35. */
  36. public $fixtures = ['core.AuthUsers', 'core.Users'];
  37. /**
  38. * @var \Cake\Auth\FormAuthenticate
  39. */
  40. protected $auth;
  41. /**
  42. * setup
  43. *
  44. * @return void
  45. */
  46. public function setUp()
  47. {
  48. parent::setUp();
  49. $this->Collection = $this->getMockBuilder(ComponentRegistry::class)->getMock();
  50. $this->auth = new FormAuthenticate($this->Collection, [
  51. 'userModel' => 'Users',
  52. ]);
  53. $password = password_hash('password', PASSWORD_DEFAULT);
  54. $this->getTableLocator()->clear();
  55. $Users = $this->getTableLocator()->get('Users');
  56. $Users->updateAll(['password' => $password], []);
  57. $AuthUsers = $this->getTableLocator()->get('AuthUsers', [
  58. 'className' => 'TestApp\Model\Table\AuthUsersTable',
  59. ]);
  60. $AuthUsers->updateAll(['password' => $password], []);
  61. $this->response = $this->getMockBuilder(Response::class)->getMock();
  62. }
  63. /**
  64. * test applying settings in the constructor
  65. *
  66. * @return void
  67. */
  68. public function testConstructor()
  69. {
  70. $object = new FormAuthenticate($this->Collection, [
  71. 'userModel' => 'AuthUsers',
  72. 'fields' => ['username' => 'user', 'password' => 'password'],
  73. ]);
  74. $this->assertEquals('AuthUsers', $object->getConfig('userModel'));
  75. $this->assertEquals(['username' => 'user', 'password' => 'password'], $object->getConfig('fields'));
  76. }
  77. /**
  78. * test the authenticate method
  79. *
  80. * @return void
  81. */
  82. public function testAuthenticateNoData()
  83. {
  84. $request = new ServerRequest([
  85. 'url' => 'posts/index',
  86. 'post' => [],
  87. ]);
  88. $this->assertFalse($this->auth->authenticate($request, $this->response));
  89. }
  90. /**
  91. * test the authenticate method
  92. *
  93. * @return void
  94. */
  95. public function testAuthenticateNoUsername()
  96. {
  97. $request = new ServerRequest([
  98. 'url' => 'posts/index',
  99. 'post' => ['password' => 'foobar'],
  100. ]);
  101. $this->assertFalse($this->auth->authenticate($request, $this->response));
  102. }
  103. /**
  104. * test the authenticate method
  105. *
  106. * @return void
  107. */
  108. public function testAuthenticateNoPassword()
  109. {
  110. $request = new ServerRequest([
  111. 'url' => 'posts/index',
  112. 'post' => ['username' => 'mariano'],
  113. ]);
  114. $this->assertFalse($this->auth->authenticate($request, $this->response));
  115. }
  116. /**
  117. * test authenticate password is false method
  118. *
  119. * @return void
  120. */
  121. public function testAuthenticatePasswordIsFalse()
  122. {
  123. $request = new ServerRequest('posts/index', false);
  124. $request = new ServerRequest([
  125. 'url' => 'posts/index',
  126. 'post' => [
  127. 'username' => 'mariano',
  128. 'password' => null,
  129. ],
  130. ]);
  131. $this->assertFalse($this->auth->authenticate($request, $this->response));
  132. }
  133. /**
  134. * Test for password as empty string with _checkFields() call skipped
  135. * Refs https://github.com/cakephp/cakephp/pull/2441
  136. *
  137. * @return void
  138. */
  139. public function testAuthenticatePasswordIsEmptyString()
  140. {
  141. $request = new ServerRequest([
  142. 'url' => 'posts/index',
  143. 'post' => [
  144. 'username' => 'mariano',
  145. 'password' => '',
  146. ],
  147. ]);
  148. $this->auth = $this->getMockBuilder(FormAuthenticate::class)
  149. ->setMethods(['_checkFields'])
  150. ->setConstructorArgs([
  151. $this->Collection,
  152. ['userModel' => 'Users'],
  153. ])
  154. ->getMock();
  155. // Simulate that check for ensuring password is not empty is missing.
  156. $this->auth->expects($this->once())
  157. ->method('_checkFields')
  158. ->will($this->returnValue(true));
  159. $this->assertFalse($this->auth->authenticate($request, $this->response));
  160. }
  161. /**
  162. * test authenticate field is not string
  163. *
  164. * @return void
  165. */
  166. public function testAuthenticateFieldsAreNotString()
  167. {
  168. $request = new ServerRequest([
  169. 'url' => 'posts/index',
  170. 'post' => [
  171. 'username' => ['mariano', 'phpnut'],
  172. 'password' => 'my password',
  173. ],
  174. ]);
  175. $this->assertFalse($this->auth->authenticate($request, $this->response));
  176. $request = new ServerRequest([
  177. 'url' => 'posts/index',
  178. 'post' => [
  179. 'username' => 'mariano',
  180. 'password' => ['password1', 'password2'],
  181. ],
  182. ]);
  183. $this->assertFalse($this->auth->authenticate($request, $this->response));
  184. }
  185. /**
  186. * test the authenticate method
  187. *
  188. * @return void
  189. */
  190. public function testAuthenticateInjection()
  191. {
  192. $request = new ServerRequest([
  193. 'url' => 'posts/index',
  194. 'post' => [
  195. 'username' => '> 1',
  196. 'password' => "' OR 1 = 1",
  197. ],
  198. ]);
  199. $this->assertFalse($this->auth->authenticate($request, $this->response));
  200. }
  201. /**
  202. * test authenticate success
  203. *
  204. * @return void
  205. */
  206. public function testAuthenticateSuccess()
  207. {
  208. $request = new ServerRequest([
  209. 'url' => 'posts/index',
  210. 'post' => [
  211. 'username' => 'mariano',
  212. 'password' => 'password',
  213. ],
  214. ]);
  215. $result = $this->auth->authenticate($request, $this->response);
  216. $expected = [
  217. 'id' => 1,
  218. 'username' => 'mariano',
  219. 'created' => new Time('2007-03-17 01:16:23'),
  220. 'updated' => new Time('2007-03-17 01:18:31'),
  221. ];
  222. $this->assertEquals($expected, $result);
  223. }
  224. /**
  225. * Test that authenticate() includes virtual fields.
  226. *
  227. * @return void
  228. */
  229. public function testAuthenticateIncludesVirtualFields()
  230. {
  231. $users = $this->getTableLocator()->get('Users');
  232. $users->setEntityClass('TestApp\Model\Entity\VirtualUser');
  233. $request = new ServerRequest([
  234. 'url' => 'posts/index',
  235. 'post' => [
  236. 'username' => 'mariano',
  237. 'password' => 'password',
  238. ],
  239. ]);
  240. $result = $this->auth->authenticate($request, $this->response);
  241. $expected = [
  242. 'id' => 1,
  243. 'username' => 'mariano',
  244. 'bonus' => 'bonus',
  245. 'created' => new Time('2007-03-17 01:16:23'),
  246. 'updated' => new Time('2007-03-17 01:18:31'),
  247. ];
  248. $this->assertEquals($expected, $result);
  249. }
  250. /**
  251. * test a model in a plugin.
  252. *
  253. * @return void
  254. */
  255. public function testPluginModel()
  256. {
  257. $this->loadPlugins(['TestPlugin']);
  258. $PluginModel = $this->getTableLocator()->get('TestPlugin.AuthUsers');
  259. $user['id'] = 1;
  260. $user['username'] = 'gwoo';
  261. $user['password'] = password_hash('cake', PASSWORD_BCRYPT);
  262. $PluginModel->save(new Entity($user));
  263. $this->auth->setConfig('userModel', 'TestPlugin.AuthUsers');
  264. $request = new ServerRequest([
  265. 'url' => 'posts/index',
  266. 'post' => [
  267. 'username' => 'gwoo',
  268. 'password' => 'cake',
  269. ],
  270. ]);
  271. $result = $this->auth->authenticate($request, $this->response);
  272. $expected = [
  273. 'id' => 1,
  274. 'username' => 'gwoo',
  275. 'created' => new Time('2007-03-17 01:16:23'),
  276. 'updated' => new Time('2007-03-17 01:18:31'),
  277. ];
  278. $this->assertEquals($expected, $result);
  279. $this->clearPlugins();
  280. }
  281. /**
  282. * Test using custom finder
  283. *
  284. * @return void
  285. */
  286. public function testFinder()
  287. {
  288. $request = new ServerRequest([
  289. 'url' => 'posts/index',
  290. 'post' => [
  291. 'username' => 'mariano',
  292. 'password' => 'password',
  293. ],
  294. ]);
  295. $this->auth->setConfig([
  296. 'userModel' => 'AuthUsers',
  297. 'finder' => 'auth',
  298. ]);
  299. $result = $this->auth->authenticate($request, $this->response);
  300. $expected = [
  301. 'id' => 1,
  302. 'username' => 'mariano',
  303. ];
  304. $this->assertEquals($expected, $result, 'Result should not contain "created" and "modified" fields');
  305. $this->auth->setConfig([
  306. 'finder' => ['auth' => ['return_created' => true]],
  307. ]);
  308. $result = $this->auth->authenticate($request, $this->response);
  309. $expected = [
  310. 'id' => 1,
  311. 'username' => 'mariano',
  312. 'created' => new Time('2007-03-17 01:16:23'),
  313. ];
  314. $this->assertEquals($expected, $result);
  315. }
  316. /**
  317. * Test using custom finder
  318. *
  319. * @return void
  320. */
  321. public function testFinderOptions()
  322. {
  323. $request = new ServerRequest([
  324. 'url' => 'posts/index',
  325. 'post' => [
  326. 'username' => 'mariano',
  327. 'password' => 'password',
  328. ],
  329. ]);
  330. $this->auth->setConfig([
  331. 'userModel' => 'AuthUsers',
  332. 'finder' => 'username',
  333. ]);
  334. $result = $this->auth->authenticate($request, $this->response);
  335. $expected = [
  336. 'id' => 1,
  337. 'username' => 'mariano',
  338. ];
  339. $this->assertEquals($expected, $result);
  340. $this->auth->setConfig([
  341. 'finder' => ['username' => ['username' => 'nate']],
  342. ]);
  343. $result = $this->auth->authenticate($request, $this->response);
  344. $expected = [
  345. 'id' => 5,
  346. 'username' => 'nate',
  347. ];
  348. $this->assertEquals($expected, $result);
  349. }
  350. /**
  351. * test password hasher settings
  352. *
  353. * @return void
  354. */
  355. public function testPasswordHasherSettings()
  356. {
  357. $this->auth->setConfig('passwordHasher', [
  358. 'className' => 'Default',
  359. 'hashType' => PASSWORD_BCRYPT,
  360. ]);
  361. $passwordHasher = $this->auth->passwordHasher();
  362. $result = $passwordHasher->getConfig();
  363. $this->assertEquals(PASSWORD_BCRYPT, $result['hashType']);
  364. $hash = password_hash('mypass', PASSWORD_BCRYPT);
  365. $User = $this->getTableLocator()->get('Users');
  366. $User->updateAll(
  367. ['password' => $hash],
  368. ['username' => 'mariano']
  369. );
  370. $request = new ServerRequest([
  371. 'url' => 'posts/index',
  372. 'post' => [
  373. 'username' => 'mariano',
  374. 'password' => 'mypass',
  375. ],
  376. ]);
  377. $result = $this->auth->authenticate($request, $this->response);
  378. $expected = [
  379. 'id' => 1,
  380. 'username' => 'mariano',
  381. 'created' => new Time('2007-03-17 01:16:23'),
  382. 'updated' => new Time('2007-03-17 01:18:31'),
  383. ];
  384. $this->assertEquals($expected, $result);
  385. $this->auth = new FormAuthenticate($this->Collection, [
  386. 'fields' => ['username' => 'username', 'password' => 'password'],
  387. 'userModel' => 'Users',
  388. ]);
  389. $this->auth->setConfig('passwordHasher', [
  390. 'className' => 'Default',
  391. ]);
  392. $this->assertEquals($expected, $this->auth->authenticate($request, $this->response));
  393. $User->updateAll(
  394. ['password' => '$2y$10$/G9GBQDZhWUM4w/WLes3b.XBZSK1hGohs5dMi0vh/oen0l0a7DUyK'],
  395. ['username' => 'mariano']
  396. );
  397. $this->assertFalse($this->auth->authenticate($request, $this->response));
  398. }
  399. /**
  400. * Tests that using default means password don't need to be rehashed
  401. *
  402. * @return void
  403. */
  404. public function testAuthenticateNoRehash()
  405. {
  406. $request = new ServerRequest([
  407. 'url' => 'posts/index',
  408. 'post' => [
  409. 'username' => 'mariano',
  410. 'password' => 'password',
  411. ],
  412. ]);
  413. $result = $this->auth->authenticate($request, $this->response);
  414. $this->assertNotEmpty($result);
  415. $this->assertFalse($this->auth->needsPasswordRehash());
  416. }
  417. /**
  418. * Tests that not using the Default password hasher means that the password
  419. * needs to be rehashed
  420. *
  421. * @return void
  422. */
  423. public function testAuthenticateRehash()
  424. {
  425. $this->auth = new FormAuthenticate($this->Collection, [
  426. 'userModel' => 'Users',
  427. 'passwordHasher' => 'Weak',
  428. ]);
  429. $password = $this->auth->passwordHasher()->hash('password');
  430. $this->getTableLocator()->get('Users')->updateAll(['password' => $password], []);
  431. $request = new ServerRequest([
  432. 'url' => 'posts/index',
  433. 'post' => [
  434. 'username' => 'mariano',
  435. 'password' => 'password',
  436. ],
  437. ]);
  438. $result = $this->auth->authenticate($request, $this->response);
  439. $this->assertNotEmpty($result);
  440. $this->assertTrue($this->auth->needsPasswordRehash());
  441. }
  442. /**
  443. * Tests that password hasher function is called exactly once in all cases.
  444. *
  445. * @param string $username
  446. * @param string|null $password
  447. * @return void
  448. * @dataProvider userList
  449. */
  450. public function testAuthenticateSingleHash($username, $password)
  451. {
  452. $this->auth = new FormAuthenticate($this->Collection, [
  453. 'userModel' => 'Users',
  454. 'passwordHasher' => CallCounterPasswordHasher::class,
  455. ]);
  456. $this->getTableLocator()->get('Users')->updateAll(
  457. ['password' => $password],
  458. ['username' => $username]
  459. );
  460. $request = new ServerRequest([
  461. 'url' => 'posts/index',
  462. 'post' => [
  463. 'username' => $username,
  464. 'password' => 'anything',
  465. ],
  466. ]);
  467. $result = $this->auth->authenticate($request, new Response());
  468. $this->assertFalse($result);
  469. /** @var \TestApp\Auth\CallCounterPasswordHasher $passwordHasher */
  470. $passwordHasher = $this->auth->passwordHasher();
  471. $this->assertInstanceOf(CallCounterPasswordHasher::class, $passwordHasher);
  472. $this->assertSame(1, $passwordHasher->callCount);
  473. }
  474. public function userList()
  475. {
  476. return [
  477. ['notexist', ''],
  478. ['mariano', null],
  479. ['mariano', ''],
  480. ['mariano', 'somehash'],
  481. ];
  482. }
  483. }