BasicAuthenticateTest.php 6.3 KB

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