BasicAuthenticateTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. use Cake\Utility\Time;
  26. /**
  27. * Test case for BasicAuthentication
  28. *
  29. */
  30. class BasicAuthenticateTest extends TestCase {
  31. /**
  32. * Fixtures
  33. *
  34. * @var array
  35. */
  36. public $fixtures = array('core.user', 'core.auth_user');
  37. /**
  38. * setup
  39. *
  40. * @return void
  41. */
  42. public function setUp() {
  43. parent::setUp();
  44. $this->Collection = $this->getMock('Cake\Controller\ComponentRegistry');
  45. $this->auth = new BasicAuthenticate($this->Collection, array(
  46. 'userModel' => 'Users',
  47. 'realm' => 'localhost'
  48. ));
  49. $password = Security::hash('password', 'blowfish', false);
  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. $object = new BasicAuthenticate($this->Collection, array(
  61. 'userModel' => 'AuthUser',
  62. 'fields' => array('username' => 'user', 'password' => 'password')
  63. ));
  64. $this->assertEquals('AuthUser', $object->config('userModel'));
  65. $this->assertEquals(array('username' => 'user', 'password' => 'password'), $object->config('fields'));
  66. }
  67. /**
  68. * test the authenticate method
  69. *
  70. * @return void
  71. */
  72. public function testAuthenticateNoData() {
  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. $request = new Request([
  85. 'url' => 'posts/index',
  86. 'environment' => ['PHP_AUTH_PW' => 'foobar']
  87. ]);
  88. $this->assertFalse($this->auth->authenticate($request, $this->response));
  89. }
  90. /**
  91. * test the authenticate method
  92. *
  93. * @return void
  94. */
  95. public function testAuthenticateNoPassword() {
  96. $request = new Request([
  97. 'url' => 'posts/index',
  98. 'environment' => ['PHP_AUTH_USER' => 'mariano']
  99. ]);
  100. $this->assertFalse($this->auth->authenticate($request, $this->response));
  101. }
  102. /**
  103. * test the authenticate method
  104. *
  105. * @return void
  106. */
  107. public function testAuthenticateInjection() {
  108. $request = new Request([
  109. 'url' => 'posts/index',
  110. 'environment' => [
  111. 'PHP_AUTH_USER' => '> 1',
  112. 'PHP_AUTH_PW' => "' OR 1 = 1"
  113. ]
  114. ]);
  115. $request->addParams(array('pass' => array()));
  116. $this->assertFalse($this->auth->getUser($request));
  117. $this->assertFalse($this->auth->authenticate($request, $this->response));
  118. }
  119. /**
  120. * test that challenge headers are sent when no credentials are found.
  121. *
  122. * @return void
  123. */
  124. public function testAuthenticateChallenge() {
  125. $request = new Request('posts/index');
  126. $request->addParams(array('pass' => array()));
  127. try {
  128. $this->auth->unauthenticated($request, $this->response);
  129. } catch (Error\UnauthorizedException $e) {
  130. }
  131. $this->assertNotEmpty($e);
  132. $expected = array('WWW-Authenticate: Basic realm="localhost"');
  133. $this->assertEquals($expected, $e->responseHeader());
  134. }
  135. /**
  136. * test authenticate success
  137. *
  138. * @return void
  139. */
  140. public function testAuthenticateSuccess() {
  141. $request = new Request([
  142. 'url' => 'posts/index',
  143. 'environment' => [
  144. 'PHP_AUTH_USER' => 'mariano',
  145. 'PHP_AUTH_PW' => 'password'
  146. ]
  147. ]);
  148. $request->addParams(array('pass' => array()));
  149. $result = $this->auth->authenticate($request, $this->response);
  150. $expected = array(
  151. 'id' => 1,
  152. 'username' => 'mariano',
  153. 'created' => new Time('2007-03-17 01:16:23'),
  154. 'updated' => new Time('2007-03-17 01:18:31')
  155. );
  156. $this->assertEquals($expected, $result);
  157. }
  158. /**
  159. * test scope failure.
  160. *
  161. * @expectedException \Cake\Error\UnauthorizedException
  162. * @expectedExceptionCode 401
  163. * @return void
  164. */
  165. public function testAuthenticateFailReChallenge() {
  166. $this->auth->config('scope.username', 'nate');
  167. $request = new Request([
  168. 'url' => 'posts/index',
  169. 'environment' => [
  170. 'PHP_AUTH_USER' => 'mariano',
  171. 'PHP_AUTH_PW' => 'password'
  172. ]
  173. ]);
  174. $request->addParams(array('pass' => array()));
  175. $this->auth->unauthenticated($request, $this->response);
  176. }
  177. /**
  178. * testAuthenticateWithBlowfish
  179. *
  180. * @return void
  181. */
  182. public function testAuthenticateWithBlowfish() {
  183. $hash = Security::hash('password', 'blowfish');
  184. $this->skipIf(strpos($hash, '$2a$') === false, 'Skipping blowfish tests as hashing is not working');
  185. $request = new Request([
  186. 'url' => 'posts/index',
  187. 'environment' => [
  188. 'PHP_AUTH_USER' => 'mariano',
  189. 'PHP_AUTH_PW' => 'password'
  190. ]
  191. ]);
  192. $request->addParams(array('pass' => array()));
  193. $User = TableRegistry::get('Users');
  194. $User->updateAll(
  195. array('password' => $hash),
  196. array('username' => 'mariano')
  197. );
  198. $this->auth->config('passwordHasher', 'Blowfish');
  199. $result = $this->auth->authenticate($request, $this->response);
  200. $expected = array(
  201. 'id' => 1,
  202. 'username' => 'mariano',
  203. 'created' => new Time('2007-03-17 01:16:23'),
  204. 'updated' => new Time('2007-03-17 01:18:31')
  205. );
  206. $this->assertEquals($expected, $result);
  207. }
  208. }