FormAuthenticateTest.php 15 KB

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