FormAuthenticateTest.php 13 KB

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