FormAuthenticateTest.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. <?php
  2. /**
  3. * FormAuthenticateTest file
  4. *
  5. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice.
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://cakephp.org CakePHP(tm) Project
  14. * @since 2.0.0
  15. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  16. */
  17. namespace Cake\Test\TestCase\Auth;
  18. use Cake\Auth\FormAuthenticate;
  19. use Cake\Cache\Cache;
  20. use Cake\Core\App;
  21. use Cake\Core\Configure;
  22. use Cake\Core\Plugin;
  23. use Cake\Network\Request;
  24. use Cake\ORM\Entity;
  25. use Cake\ORM\TableRegistry;
  26. use Cake\TestSuite\TestCase;
  27. use Cake\Utility\Security;
  28. use Cake\Utility\Time;
  29. /**
  30. * Test case for FormAuthentication
  31. *
  32. */
  33. class FormAuthenticateTest extends TestCase {
  34. /**
  35. * Fixtrues
  36. *
  37. * @var array
  38. */
  39. public $fixtures = ['core.user', 'core.auth_user'];
  40. /**
  41. * setup
  42. *
  43. * @return void
  44. */
  45. public function setUp() {
  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. $Users = TableRegistry::get('Users');
  53. $Users->updateAll(['password' => $password], []);
  54. $this->response = $this->getMock('Cake\Network\Response');
  55. }
  56. /**
  57. * test applying settings in the constructor
  58. *
  59. * @return void
  60. */
  61. public function testConstructor() {
  62. $object = new FormAuthenticate($this->Collection, [
  63. 'userModel' => 'AuthUsers',
  64. 'fields' => ['username' => 'user', 'password' => 'password']
  65. ]);
  66. $this->assertEquals('AuthUsers', $object->config('userModel'));
  67. $this->assertEquals(['username' => 'user', 'password' => 'password'], $object->config('fields'));
  68. }
  69. /**
  70. * test the authenticate method
  71. *
  72. * @return void
  73. */
  74. public function testAuthenticateNoData() {
  75. $request = new Request('posts/index');
  76. $request->data = [];
  77. $this->assertFalse($this->auth->authenticate($request, $this->response));
  78. }
  79. /**
  80. * test the authenticate method
  81. *
  82. * @return void
  83. */
  84. public function testAuthenticateNoUsername() {
  85. $request = new Request('posts/index');
  86. $request->data = ['password' => 'foobar'];
  87. $this->assertFalse($this->auth->authenticate($request, $this->response));
  88. }
  89. /**
  90. * test the authenticate method
  91. *
  92. * @return void
  93. */
  94. public function testAuthenticateNoPassword() {
  95. $request = new Request('posts/index');
  96. $request->data = ['username' => 'mariano'];
  97. $this->assertFalse($this->auth->authenticate($request, $this->response));
  98. }
  99. /**
  100. * test authenticate password is false method
  101. *
  102. * @return void
  103. */
  104. public function testAuthenticatePasswordIsFalse() {
  105. $request = new Request('posts/index', false);
  106. $request->data = [
  107. 'username' => 'mariano',
  108. 'password' => null
  109. ];
  110. $this->assertFalse($this->auth->authenticate($request, $this->response));
  111. }
  112. /**
  113. * Test for password as empty string with _checkFields() call skipped
  114. * Refs https://github.com/cakephp/cakephp/pull/2441
  115. *
  116. * @return void
  117. */
  118. public function testAuthenticatePasswordIsEmptyString() {
  119. $request = new Request('posts/index', false);
  120. $request->data = [
  121. 'username' => 'mariano',
  122. 'password' => ''
  123. ];
  124. $this->auth = $this->getMock(
  125. 'Cake\Auth\FormAuthenticate',
  126. ['_checkFields'],
  127. [
  128. $this->Collection,
  129. [
  130. 'userModel' => 'Users'
  131. ]
  132. ]
  133. );
  134. // Simulate that check for ensuring password is not empty is missing.
  135. $this->auth->expects($this->once())
  136. ->method('_checkFields')
  137. ->will($this->returnValue(true));
  138. $this->assertFalse($this->auth->authenticate($request, $this->response));
  139. }
  140. /**
  141. * test authenticate field is not string
  142. *
  143. * @return void
  144. */
  145. public function testAuthenticateFieldsAreNotString() {
  146. $request = new Request('posts/index', false);
  147. $request->data = [
  148. 'username' => ['mariano', 'phpnut'],
  149. 'password' => 'my password'
  150. ];
  151. $this->assertFalse($this->auth->authenticate($request, $this->response));
  152. $request->data = [
  153. 'username' => 'mariano',
  154. 'password' => ['password1', 'password2']
  155. ];
  156. $this->assertFalse($this->auth->authenticate($request, $this->response));
  157. }
  158. /**
  159. * test the authenticate method
  160. *
  161. * @return void
  162. */
  163. public function testAuthenticateInjection() {
  164. $request = new Request('posts/index');
  165. $request->data = [
  166. 'username' => '> 1',
  167. 'password' => "' OR 1 = 1"
  168. ];
  169. $this->assertFalse($this->auth->authenticate($request, $this->response));
  170. }
  171. /**
  172. * test authenticate success
  173. *
  174. * @return void
  175. */
  176. public function testAuthenticateSuccess() {
  177. $request = new Request('posts/index');
  178. $request->data = [
  179. 'username' => 'mariano',
  180. 'password' => 'password'
  181. ];
  182. $result = $this->auth->authenticate($request, $this->response);
  183. $expected = [
  184. 'id' => 1,
  185. 'username' => 'mariano',
  186. 'created' => new Time('2007-03-17 01:16:23'),
  187. 'updated' => new Time('2007-03-17 01:18:31')
  188. ];
  189. $this->assertEquals($expected, $result);
  190. }
  191. /**
  192. * test scope failure.
  193. *
  194. * @return void
  195. */
  196. public function testAuthenticateScopeFail() {
  197. $this->auth->config('scope', ['Users.id' => 2]);
  198. $request = new Request('posts/index');
  199. $request->data = [
  200. 'username' => 'mariano',
  201. 'password' => 'password'
  202. ];
  203. $this->assertFalse($this->auth->authenticate($request, $this->response));
  204. }
  205. /**
  206. * test a model in a plugin.
  207. *
  208. * @return void
  209. */
  210. public function testPluginModel() {
  211. Cache::delete('object_map', '_cake_core_');
  212. Plugin::load('TestPlugin');
  213. $PluginModel = TableRegistry::get('TestPlugin.AuthUsers');
  214. $user['id'] = 1;
  215. $user['username'] = 'gwoo';
  216. $user['password'] = Security::hash(Configure::read('Security.salt') . 'cake', 'blowfish', false);
  217. $PluginModel->save(new Entity($user));
  218. $this->auth->config('userModel', 'TestPlugin.AuthUsers');
  219. $request = new Request('posts/index');
  220. $request->data = [
  221. 'username' => 'gwoo',
  222. 'password' => 'cake'
  223. ];
  224. $result = $this->auth->authenticate($request, $this->response);
  225. $expected = [
  226. 'id' => 1,
  227. 'username' => 'gwoo',
  228. 'created' => new Time('2007-03-17 01:16:23'),
  229. 'updated' => new Time('2007-03-17 01:18:31')
  230. ];
  231. $this->assertEquals($expected, $result);
  232. Plugin::unload();
  233. }
  234. /**
  235. * test password hasher settings
  236. *
  237. * @return void
  238. */
  239. public function testPasswordHasherSettings() {
  240. $this->auth->config('passwordHasher', [
  241. 'className' => 'Default',
  242. 'hashType' => PASSWORD_BCRYPT
  243. ]);
  244. $passwordHasher = $this->auth->passwordHasher();
  245. $result = $passwordHasher->config();
  246. $this->assertEquals(PASSWORD_BCRYPT, $result['hashType']);
  247. $hash = password_hash('mypass', PASSWORD_BCRYPT);
  248. $User = TableRegistry::get('Users');
  249. $User->updateAll(
  250. ['password' => $hash],
  251. ['username' => 'mariano']
  252. );
  253. $request = new Request('posts/index');
  254. $request->data = [
  255. 'username' => 'mariano',
  256. 'password' => 'mypass'
  257. ];
  258. $result = $this->auth->authenticate($request, $this->response);
  259. $expected = [
  260. 'id' => 1,
  261. 'username' => 'mariano',
  262. 'created' => new Time('2007-03-17 01:16:23'),
  263. 'updated' => new Time('2007-03-17 01:18:31')
  264. ];
  265. $this->assertEquals($expected, $result);
  266. $this->auth = new FormAuthenticate($this->Collection, [
  267. 'fields' => ['username' => 'username', 'password' => 'password'],
  268. 'userModel' => 'Users'
  269. ]);
  270. $this->auth->config('passwordHasher', [
  271. 'className' => 'Default'
  272. ]);
  273. $this->assertEquals($expected, $this->auth->authenticate($request, $this->response));
  274. $User->updateAll(
  275. ['password' => '$2y$10$/G9GBQDZhWUM4w/WLes3b.XBZSK1hGohs5dMi0vh/oen0l0a7DUyK'],
  276. ['username' => 'mariano']
  277. );
  278. $this->assertFalse($this->auth->authenticate($request, $this->response));
  279. }
  280. /**
  281. * Tests that using default means password don't need to be rehashed
  282. *
  283. * @return void
  284. */
  285. public function testAuthenticateNoRehash() {
  286. $request = new Request('posts/index');
  287. $request->data = [
  288. 'username' => 'mariano',
  289. 'password' => 'password'
  290. ];
  291. $result = $this->auth->authenticate($request, $this->response);
  292. $this->assertNotEmpty($result);
  293. $this->assertFalse($this->auth->needsPasswordRehash());
  294. }
  295. /**
  296. * Tests that not using the Default password hasher means that the password
  297. * needs to be rehashed
  298. *
  299. * @return void
  300. */
  301. public function testAuthenticateRehash() {
  302. $this->auth = new FormAuthenticate($this->Collection, [
  303. 'userModel' => 'Users',
  304. 'passwordHasher' => 'Weak'
  305. ]);
  306. $password = $this->auth->passwordHasher()->hash('password');
  307. TableRegistry::get('Users')->updateAll(['password' => $password], []);
  308. $request = new Request('posts/index');
  309. $request->data = [
  310. 'username' => 'mariano',
  311. 'password' => 'password'
  312. ];
  313. $result = $this->auth->authenticate($request, $this->response);
  314. $this->assertNotEmpty($result);
  315. $this->assertTrue($this->auth->needsPasswordRehash());
  316. }
  317. }