BasicAuthenticateTest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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\Controller\ComponentRegistry;
  18. use Cake\Http\Response;
  19. use Cake\Http\ServerRequest;
  20. use Cake\I18n\Time;
  21. use Cake\Network\Exception\UnauthorizedException;
  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->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. {
  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. $request->addParams(['pass' => []]);
  120. $this->assertFalse($this->auth->getUser($request));
  121. $this->assertFalse($this->auth->authenticate($request, $this->response));
  122. }
  123. /**
  124. * Test that username of 0 works.
  125. *
  126. * @return void
  127. */
  128. public function testAuthenticateUsernameZero()
  129. {
  130. $User = TableRegistry::get('Users');
  131. $User->updateAll(['username' => '0'], ['username' => 'mariano']);
  132. $request = new ServerRequest('posts/index');
  133. $request->data = ['User' => [
  134. 'user' => '0',
  135. 'password' => 'password'
  136. ]];
  137. $_SERVER['PHP_AUTH_USER'] = '0';
  138. $_SERVER['PHP_AUTH_PW'] = 'password';
  139. $expected = [
  140. 'id' => 1,
  141. 'username' => '0',
  142. 'created' => new Time('2007-03-17 01:16:23'),
  143. 'updated' => new Time('2007-03-17 01:18:31'),
  144. ];
  145. $this->assertEquals($expected, $this->auth->authenticate($request, $this->response));
  146. }
  147. /**
  148. * test that challenge headers are sent when no credentials are found.
  149. *
  150. * @return void
  151. */
  152. public function testAuthenticateChallenge()
  153. {
  154. $request = new ServerRequest('posts/index');
  155. $request->addParams(['pass' => []]);
  156. try {
  157. $this->auth->unauthenticated($request, $this->response);
  158. } catch (UnauthorizedException $e) {
  159. }
  160. $this->assertNotEmpty($e);
  161. $expected = ['WWW-Authenticate: Basic realm="localhost"'];
  162. $this->assertEquals($expected, $e->responseHeader());
  163. }
  164. /**
  165. * test authenticate success
  166. *
  167. * @return void
  168. */
  169. public function testAuthenticateSuccess()
  170. {
  171. $request = new ServerRequest([
  172. 'url' => 'posts/index',
  173. 'environment' => [
  174. 'PHP_AUTH_USER' => 'mariano',
  175. 'PHP_AUTH_PW' => 'password'
  176. ]
  177. ]);
  178. $request->addParams(['pass' => []]);
  179. $result = $this->auth->authenticate($request, $this->response);
  180. $expected = [
  181. 'id' => 1,
  182. 'username' => 'mariano',
  183. 'created' => new Time('2007-03-17 01:16:23'),
  184. 'updated' => new Time('2007-03-17 01:18:31')
  185. ];
  186. $this->assertEquals($expected, $result);
  187. }
  188. /**
  189. * test scope failure.
  190. *
  191. * @expectedException \Cake\Network\Exception\UnauthorizedException
  192. * @expectedExceptionCode 401
  193. * @return void
  194. */
  195. public function testAuthenticateFailReChallenge()
  196. {
  197. $this->auth->config('scope.username', 'nate');
  198. $request = new ServerRequest([
  199. 'url' => 'posts/index',
  200. 'environment' => [
  201. 'PHP_AUTH_USER' => 'mariano',
  202. 'PHP_AUTH_PW' => 'password'
  203. ]
  204. ]);
  205. $request->addParams(['pass' => []]);
  206. $this->auth->unauthenticated($request, $this->response);
  207. }
  208. }