BasicAuthenticateTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. <?php
  2. /**
  3. * BasicAuthenticateTest file
  4. *
  5. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice.
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://cakephp.org CakePHP(tm) Project
  14. * @since 2.0.0
  15. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  16. */
  17. namespace Cake\Test\TestCase\Controller\Component\Auth;
  18. use Cake\Controller\Component\Auth\BasicAuthenticate;
  19. use Cake\Error;
  20. use Cake\Network\Request;
  21. use Cake\ORM\Entity;
  22. use Cake\ORM\TableRegistry;
  23. use Cake\TestSuite\TestCase;
  24. use Cake\Utility\Security;
  25. /**
  26. * Test case for BasicAuthentication
  27. *
  28. */
  29. class BasicAuthenticateTest extends TestCase {
  30. /**
  31. * Fixtures
  32. *
  33. * @var array
  34. */
  35. public $fixtures = array('core.user', 'core.auth_user');
  36. /**
  37. * setup
  38. *
  39. * @return void
  40. */
  41. public function setUp() {
  42. parent::setUp();
  43. $this->Collection = $this->getMock('Cake\Controller\ComponentRegistry');
  44. $this->auth = new BasicAuthenticate($this->Collection, array(
  45. 'userModel' => 'Users',
  46. 'realm' => 'localhost'
  47. ));
  48. $password = Security::hash('password', 'blowfish', false);
  49. $User = TableRegistry::get('Users');
  50. $User->updateAll(['password' => $password], []);
  51. $this->response = $this->getMock('Cake\Network\Response');
  52. }
  53. /**
  54. * test applying settings in the constructor
  55. *
  56. * @return void
  57. */
  58. public function testConstructor() {
  59. $object = new BasicAuthenticate($this->Collection, array(
  60. 'userModel' => 'AuthUser',
  61. 'fields' => array('username' => 'user', 'password' => 'password')
  62. ));
  63. $this->assertEquals('AuthUser', $object->config('userModel'));
  64. $this->assertEquals(array('username' => 'user', 'password' => 'password'), $object->config('fields'));
  65. }
  66. /**
  67. * test the authenticate method
  68. *
  69. * @return void
  70. */
  71. public function testAuthenticateNoData() {
  72. $request = new Request('posts/index');
  73. $this->response->expects($this->never())
  74. ->method('header');
  75. $this->assertFalse($this->auth->getUser($request));
  76. }
  77. /**
  78. * test the authenticate method
  79. *
  80. * @return void
  81. */
  82. public function testAuthenticateNoUsername() {
  83. $request = new Request([
  84. 'url' => 'posts/index',
  85. 'environment' => ['PHP_AUTH_PW' => 'foobar']
  86. ]);
  87. $this->assertFalse($this->auth->authenticate($request, $this->response));
  88. }
  89. /**
  90. * test the authenticate method
  91. *
  92. * @return void
  93. */
  94. public function testAuthenticateNoPassword() {
  95. $request = new Request([
  96. 'url' => 'posts/index',
  97. 'environment' => ['PHP_AUTH_USER' => 'mariano']
  98. ]);
  99. $this->assertFalse($this->auth->authenticate($request, $this->response));
  100. }
  101. /**
  102. * test the authenticate method
  103. *
  104. * @return void
  105. */
  106. public function testAuthenticateInjection() {
  107. $request = new Request([
  108. 'url' => 'posts/index',
  109. 'environment' => [
  110. 'PHP_AUTH_USER' => '> 1',
  111. 'PHP_AUTH_PW' => "' OR 1 = 1"
  112. ]
  113. ]);
  114. $request->addParams(array('pass' => array()));
  115. $this->assertFalse($this->auth->getUser($request));
  116. $this->assertFalse($this->auth->authenticate($request, $this->response));
  117. }
  118. /**
  119. * test that challenge headers are sent when no credentials are found.
  120. *
  121. * @return void
  122. */
  123. public function testAuthenticateChallenge() {
  124. $request = new Request('posts/index');
  125. $request->addParams(array('pass' => array()));
  126. try {
  127. $this->auth->unauthenticated($request, $this->response);
  128. } catch (Error\UnauthorizedException $e) {
  129. }
  130. $this->assertNotEmpty($e);
  131. $expected = array('WWW-Authenticate: Basic realm="localhost"');
  132. $this->assertEquals($expected, $e->responseHeader());
  133. }
  134. /**
  135. * test authenticate success
  136. *
  137. * @return void
  138. */
  139. public function testAuthenticateSuccess() {
  140. $request = new Request([
  141. 'url' => 'posts/index',
  142. 'environment' => [
  143. 'PHP_AUTH_USER' => 'mariano',
  144. 'PHP_AUTH_PW' => 'password'
  145. ]
  146. ]);
  147. $request->addParams(array('pass' => array()));
  148. $result = $this->auth->authenticate($request, $this->response);
  149. $expected = array(
  150. 'id' => 1,
  151. 'username' => 'mariano',
  152. 'created' => new \DateTime('2007-03-17 01:16:23'),
  153. 'updated' => new \DateTime('2007-03-17 01:18:31')
  154. );
  155. $this->assertEquals($expected, $result);
  156. }
  157. /**
  158. * test scope failure.
  159. *
  160. * @expectedException \Cake\Error\UnauthorizedException
  161. * @expectedExceptionCode 401
  162. * @return void
  163. */
  164. public function testAuthenticateFailReChallenge() {
  165. $this->auth->config('scope.username', 'nate');
  166. $request = new Request([
  167. 'url' => 'posts/index',
  168. 'environment' => [
  169. 'PHP_AUTH_USER' => 'mariano',
  170. 'PHP_AUTH_PW' => 'password'
  171. ]
  172. ]);
  173. $request->addParams(array('pass' => array()));
  174. $this->auth->unauthenticated($request, $this->response);
  175. }
  176. /**
  177. * testAuthenticateWithBlowfish
  178. *
  179. * @return void
  180. */
  181. public function testAuthenticateWithBlowfish() {
  182. $hash = Security::hash('password', 'blowfish');
  183. $this->skipIf(strpos($hash, '$2a$') === false, 'Skipping blowfish tests as hashing is not working');
  184. $request = new Request([
  185. 'url' => 'posts/index',
  186. 'environment' => [
  187. 'PHP_AUTH_USER' => 'mariano',
  188. 'PHP_AUTH_PW' => 'password'
  189. ]
  190. ]);
  191. $request->addParams(array('pass' => array()));
  192. $User = TableRegistry::get('Users');
  193. $User->updateAll(
  194. array('password' => $hash),
  195. array('username' => 'mariano')
  196. );
  197. $this->auth->config('passwordHasher', 'Blowfish');
  198. $result = $this->auth->authenticate($request, $this->response);
  199. $expected = array(
  200. 'id' => 1,
  201. 'username' => 'mariano',
  202. 'created' => new \DateTime('2007-03-17 01:16:23'),
  203. 'updated' => new \DateTime('2007-03-17 01:18:31')
  204. );
  205. $this->assertEquals($expected, $result);
  206. }
  207. }