FormAuthenticateTest.php 15 KB

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