BasicAuthenticateTest.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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\BasicAuthenticate;
  17. use Cake\I18n\Time;
  18. use Cake\Network\Exception\UnauthorizedException;
  19. use Cake\Network\Request;
  20. use Cake\ORM\Entity;
  21. use Cake\ORM\TableRegistry;
  22. use Cake\TestSuite\TestCase;
  23. use Cake\Utility\Security;
  24. /**
  25. * Test case for BasicAuthentication
  26. *
  27. */
  28. class BasicAuthenticateTest extends TestCase
  29. {
  30. /**
  31. * Fixtures
  32. *
  33. * @var array
  34. */
  35. public $fixtures = ['core.auth_users', 'core.users'];
  36. /**
  37. * setup
  38. *
  39. * @return void
  40. */
  41. public function setUp()
  42. {
  43. parent::setUp();
  44. $this->Collection = $this->getMock('Cake\Controller\ComponentRegistry');
  45. $this->auth = new BasicAuthenticate($this->Collection, [
  46. 'userModel' => 'Users',
  47. 'realm' => 'localhost'
  48. ]);
  49. $password = password_hash('password', PASSWORD_BCRYPT);
  50. $User = TableRegistry::get('Users');
  51. $User->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. {
  61. $object = new BasicAuthenticate($this->Collection, [
  62. 'userModel' => 'AuthUser',
  63. 'fields' => ['username' => 'user', 'password' => 'password']
  64. ]);
  65. $this->assertEquals('AuthUser', $object->config('userModel'));
  66. $this->assertEquals(['username' => 'user', 'password' => 'password'], $object->config('fields'));
  67. }
  68. /**
  69. * test the authenticate method
  70. *
  71. * @return void
  72. */
  73. public function testAuthenticateNoData()
  74. {
  75. $request = new Request('posts/index');
  76. $this->response->expects($this->never())
  77. ->method('header');
  78. $this->assertFalse($this->auth->getUser($request));
  79. }
  80. /**
  81. * test the authenticate method
  82. *
  83. * @return void
  84. */
  85. public function testAuthenticateNoUsername()
  86. {
  87. $request = new Request([
  88. 'url' => 'posts/index',
  89. 'environment' => ['PHP_AUTH_PW' => 'foobar']
  90. ]);
  91. $this->assertFalse($this->auth->authenticate($request, $this->response));
  92. }
  93. /**
  94. * test the authenticate method
  95. *
  96. * @return void
  97. */
  98. public function testAuthenticateNoPassword()
  99. {
  100. $request = new Request([
  101. 'url' => 'posts/index',
  102. 'environment' => ['PHP_AUTH_USER' => 'mariano']
  103. ]);
  104. $this->assertFalse($this->auth->authenticate($request, $this->response));
  105. }
  106. /**
  107. * test the authenticate method
  108. *
  109. * @return void
  110. */
  111. public function testAuthenticateInjection()
  112. {
  113. $request = new Request([
  114. 'url' => 'posts/index',
  115. 'environment' => [
  116. 'PHP_AUTH_USER' => '> 1',
  117. 'PHP_AUTH_PW' => "' OR 1 = 1"
  118. ]
  119. ]);
  120. $request->addParams(['pass' => []]);
  121. $this->assertFalse($this->auth->getUser($request));
  122. $this->assertFalse($this->auth->authenticate($request, $this->response));
  123. }
  124. /**
  125. * Test that username of 0 works.
  126. *
  127. * @return void
  128. */
  129. public function testAuthenticateUsernameZero()
  130. {
  131. $User = TableRegistry::get('Users');
  132. $User->updateAll(['username' => '0'], ['username' => 'mariano']);
  133. $request = new Request('posts/index');
  134. $request->data = ['User' => [
  135. 'user' => '0',
  136. 'password' => 'password'
  137. ]];
  138. $_SERVER['PHP_AUTH_USER'] = '0';
  139. $_SERVER['PHP_AUTH_PW'] = 'password';
  140. $expected = [
  141. 'id' => 1,
  142. 'username' => '0',
  143. 'created' => new Time('2007-03-17 01:16:23'),
  144. 'updated' => new Time('2007-03-17 01:18:31'),
  145. ];
  146. $this->assertEquals($expected, $this->auth->authenticate($request, $this->response));
  147. }
  148. /**
  149. * test that challenge headers are sent when no credentials are found.
  150. *
  151. * @return void
  152. */
  153. public function testAuthenticateChallenge()
  154. {
  155. $request = new Request('posts/index');
  156. $request->addParams(['pass' => []]);
  157. try {
  158. $this->auth->unauthenticated($request, $this->response);
  159. } catch (UnauthorizedException $e) {
  160. }
  161. $this->assertNotEmpty($e);
  162. $expected = ['WWW-Authenticate: Basic realm="localhost"'];
  163. $this->assertEquals($expected, $e->responseHeader());
  164. }
  165. /**
  166. * test authenticate success
  167. *
  168. * @return void
  169. */
  170. public function testAuthenticateSuccess()
  171. {
  172. $request = new Request([
  173. 'url' => 'posts/index',
  174. 'environment' => [
  175. 'PHP_AUTH_USER' => 'mariano',
  176. 'PHP_AUTH_PW' => 'password'
  177. ]
  178. ]);
  179. $request->addParams(['pass' => []]);
  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. * @expectedException \Cake\Network\Exception\UnauthorizedException
  193. * @expectedExceptionCode 401
  194. * @return void
  195. */
  196. public function testAuthenticateFailReChallenge()
  197. {
  198. $this->auth->config('scope.username', 'nate');
  199. $request = new Request([
  200. 'url' => 'posts/index',
  201. 'environment' => [
  202. 'PHP_AUTH_USER' => 'mariano',
  203. 'PHP_AUTH_PW' => 'password'
  204. ]
  205. ]);
  206. $request->addParams(['pass' => []]);
  207. $this->auth->unauthenticated($request, $this->response);
  208. }
  209. }