FormAuthenticateTest.php 12 KB

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