FormAuthenticateTest.php 8.8 KB

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