FormAuthenticateTest.php 14 KB

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