BasicAuthenticateTest.php 6.4 KB

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