FormAuthenticateTest.php 13 KB

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