FormAuthenticateTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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\Core\Configure;
  18. use Cake\Core\Plugin;
  19. use Cake\I18n\Time;
  20. use Cake\Network\Request;
  21. use Cake\ORM\Entity;
  22. use Cake\ORM\TableRegistry;
  23. use Cake\TestSuite\TestCase;
  24. use Cake\Utility\Security;
  25. /**
  26. * Test case for FormAuthentication
  27. *
  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->getMock('Cake\Controller\ComponentRegistry');
  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->getMock('Cake\Network\Response');
  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->config('userModel'));
  71. $this->assertEquals(['username' => 'user', 'password' => 'password'], $object->config('fields'));
  72. }
  73. /**
  74. * test the authenticate method
  75. *
  76. * @return void
  77. */
  78. public function testAuthenticateNoData()
  79. {
  80. $request = new Request('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 Request('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 Request('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 Request('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 Request('posts/index', false);
  129. $request->data = [
  130. 'username' => 'mariano',
  131. 'password' => ''
  132. ];
  133. $this->auth = $this->getMock(
  134. 'Cake\Auth\FormAuthenticate',
  135. ['_checkFields'],
  136. [
  137. $this->Collection,
  138. [
  139. 'userModel' => 'Users'
  140. ]
  141. ]
  142. );
  143. // Simulate that check for ensuring password is not empty is missing.
  144. $this->auth->expects($this->once())
  145. ->method('_checkFields')
  146. ->will($this->returnValue(true));
  147. $this->assertFalse($this->auth->authenticate($request, $this->response));
  148. }
  149. /**
  150. * test authenticate field is not string
  151. *
  152. * @return void
  153. */
  154. public function testAuthenticateFieldsAreNotString()
  155. {
  156. $request = new Request('posts/index', false);
  157. $request->data = [
  158. 'username' => ['mariano', 'phpnut'],
  159. 'password' => 'my password'
  160. ];
  161. $this->assertFalse($this->auth->authenticate($request, $this->response));
  162. $request->data = [
  163. 'username' => 'mariano',
  164. 'password' => ['password1', 'password2']
  165. ];
  166. $this->assertFalse($this->auth->authenticate($request, $this->response));
  167. }
  168. /**
  169. * test the authenticate method
  170. *
  171. * @return void
  172. */
  173. public function testAuthenticateInjection()
  174. {
  175. $request = new Request('posts/index');
  176. $request->data = [
  177. 'username' => '> 1',
  178. 'password' => "' OR 1 = 1"
  179. ];
  180. $this->assertFalse($this->auth->authenticate($request, $this->response));
  181. }
  182. /**
  183. * test authenticate success
  184. *
  185. * @return void
  186. */
  187. public function testAuthenticateSuccess()
  188. {
  189. $request = new Request('posts/index');
  190. $request->data = [
  191. 'username' => 'mariano',
  192. 'password' => 'password'
  193. ];
  194. $result = $this->auth->authenticate($request, $this->response);
  195. $expected = [
  196. 'id' => 1,
  197. 'username' => 'mariano',
  198. 'created' => new Time('2007-03-17 01:16:23'),
  199. 'updated' => new Time('2007-03-17 01:18:31')
  200. ];
  201. $this->assertEquals($expected, $result);
  202. }
  203. /**
  204. * Test that authenticate() includes virtual fields.
  205. *
  206. * @return void
  207. */
  208. public function testAuthenticateIncludesVirtualFields()
  209. {
  210. $users = TableRegistry::get('Users');
  211. $users->entityClass('TestApp\Model\Entity\VirtualUser');
  212. $request = new Request('posts/index');
  213. $request->data = [
  214. 'username' => 'mariano',
  215. 'password' => 'password'
  216. ];
  217. $result = $this->auth->authenticate($request, $this->response);
  218. $expected = [
  219. 'id' => 1,
  220. 'username' => 'mariano',
  221. 'bonus' => 'bonus',
  222. 'created' => new Time('2007-03-17 01:16:23'),
  223. 'updated' => new Time('2007-03-17 01:18:31')
  224. ];
  225. $this->assertEquals($expected, $result);
  226. }
  227. /**
  228. * test a model in a plugin.
  229. *
  230. * @return void
  231. */
  232. public function testPluginModel()
  233. {
  234. Plugin::load('TestPlugin');
  235. $PluginModel = TableRegistry::get('TestPlugin.AuthUsers');
  236. $user['id'] = 1;
  237. $user['username'] = 'gwoo';
  238. $user['password'] = password_hash(Security::salt() . 'cake', PASSWORD_BCRYPT);
  239. $PluginModel->save(new Entity($user));
  240. $this->auth->config('userModel', 'TestPlugin.AuthUsers');
  241. $request = new Request('posts/index');
  242. $request->data = [
  243. 'username' => 'gwoo',
  244. 'password' => 'cake'
  245. ];
  246. $result = $this->auth->authenticate($request, $this->response);
  247. $expected = [
  248. 'id' => 1,
  249. 'username' => 'gwoo',
  250. 'created' => new Time('2007-03-17 01:16:23'),
  251. 'updated' => new Time('2007-03-17 01:18:31')
  252. ];
  253. $this->assertEquals($expected, $result);
  254. Plugin::unload();
  255. }
  256. /**
  257. * Test using custom finder
  258. *
  259. * @return void
  260. */
  261. public function testFinder()
  262. {
  263. $request = new Request('posts/index');
  264. $request->data = [
  265. 'username' => 'mariano',
  266. 'password' => 'password'
  267. ];
  268. $this->auth->config([
  269. 'userModel' => 'AuthUsers',
  270. 'finder' => 'auth'
  271. ]);
  272. $result = $this->auth->authenticate($request, $this->response);
  273. $expected = [
  274. 'id' => 1,
  275. 'username' => 'mariano',
  276. ];
  277. $this->assertEquals($expected, $result, 'Result should not contain "created" and "modified" fields');
  278. $this->auth->config([
  279. 'finder' => ['auth' => ['return_created' => true]]
  280. ]);
  281. $result = $this->auth->authenticate($request, $this->response);
  282. $expected = [
  283. 'id' => 1,
  284. 'username' => 'mariano',
  285. 'created' => new Time('2007-03-17 01:16:23'),
  286. ];
  287. $this->assertEquals($expected, $result);
  288. }
  289. /**
  290. * test password hasher settings
  291. *
  292. * @return void
  293. */
  294. public function testPasswordHasherSettings()
  295. {
  296. $this->auth->config('passwordHasher', [
  297. 'className' => 'Default',
  298. 'hashType' => PASSWORD_BCRYPT
  299. ]);
  300. $passwordHasher = $this->auth->passwordHasher();
  301. $result = $passwordHasher->config();
  302. $this->assertEquals(PASSWORD_BCRYPT, $result['hashType']);
  303. $hash = password_hash('mypass', PASSWORD_BCRYPT);
  304. $User = TableRegistry::get('Users');
  305. $User->updateAll(
  306. ['password' => $hash],
  307. ['username' => 'mariano']
  308. );
  309. $request = new Request('posts/index');
  310. $request->data = [
  311. 'username' => 'mariano',
  312. 'password' => 'mypass'
  313. ];
  314. $result = $this->auth->authenticate($request, $this->response);
  315. $expected = [
  316. 'id' => 1,
  317. 'username' => 'mariano',
  318. 'created' => new Time('2007-03-17 01:16:23'),
  319. 'updated' => new Time('2007-03-17 01:18:31')
  320. ];
  321. $this->assertEquals($expected, $result);
  322. $this->auth = new FormAuthenticate($this->Collection, [
  323. 'fields' => ['username' => 'username', 'password' => 'password'],
  324. 'userModel' => 'Users'
  325. ]);
  326. $this->auth->config('passwordHasher', [
  327. 'className' => 'Default'
  328. ]);
  329. $this->assertEquals($expected, $this->auth->authenticate($request, $this->response));
  330. $User->updateAll(
  331. ['password' => '$2y$10$/G9GBQDZhWUM4w/WLes3b.XBZSK1hGohs5dMi0vh/oen0l0a7DUyK'],
  332. ['username' => 'mariano']
  333. );
  334. $this->assertFalse($this->auth->authenticate($request, $this->response));
  335. }
  336. /**
  337. * Tests that using default means password don't need to be rehashed
  338. *
  339. * @return void
  340. */
  341. public function testAuthenticateNoRehash()
  342. {
  343. $request = new Request('posts/index');
  344. $request->data = [
  345. 'username' => 'mariano',
  346. 'password' => 'password'
  347. ];
  348. $result = $this->auth->authenticate($request, $this->response);
  349. $this->assertNotEmpty($result);
  350. $this->assertFalse($this->auth->needsPasswordRehash());
  351. }
  352. /**
  353. * Tests that not using the Default password hasher means that the password
  354. * needs to be rehashed
  355. *
  356. * @return void
  357. */
  358. public function testAuthenticateRehash()
  359. {
  360. $this->auth = new FormAuthenticate($this->Collection, [
  361. 'userModel' => 'Users',
  362. 'passwordHasher' => 'Weak'
  363. ]);
  364. $password = $this->auth->passwordHasher()->hash('password');
  365. TableRegistry::get('Users')->updateAll(['password' => $password], []);
  366. $request = new Request('posts/index');
  367. $request->data = [
  368. 'username' => 'mariano',
  369. 'password' => 'password'
  370. ];
  371. $result = $this->auth->authenticate($request, $this->response);
  372. $this->assertNotEmpty($result);
  373. $this->assertTrue($this->auth->needsPasswordRehash());
  374. }
  375. }