FormAuthenticateTest.php 14 KB

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