DigestAuthenticateTest.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. <?php
  2. /**
  3. * DigestAuthenticateTest 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\DigestAuthenticate;
  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. /**
  25. * Test case for DigestAuthentication
  26. *
  27. */
  28. class DigestAuthenticateTest extends TestCase {
  29. /**
  30. * Fixtures
  31. *
  32. * @var array
  33. */
  34. public $fixtures = array('core.user', 'core.auth_user');
  35. /**
  36. * setup
  37. *
  38. * @return void
  39. */
  40. public function setUp() {
  41. parent::setUp();
  42. $this->Collection = $this->getMock('Cake\Controller\ComponentRegistry');
  43. $this->auth = new DigestAuthenticate($this->Collection, array(
  44. 'realm' => 'localhost',
  45. 'nonce' => 123,
  46. 'opaque' => '123abc'
  47. ));
  48. $password = DigestAuthenticate::password('mariano', 'cake', 'localhost');
  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 DigestAuthenticate($this->Collection, array(
  60. 'userModel' => 'AuthUser',
  61. 'fields' => array('username' => 'user', 'password' => 'pass'),
  62. 'nonce' => 123456
  63. ));
  64. $this->assertEquals('AuthUser', $object->config('userModel'));
  65. $this->assertEquals(array('username' => 'user', 'password' => 'pass'), $object->config('fields'));
  66. $this->assertEquals(123456, $object->config('nonce'));
  67. $this->assertEquals(env('SERVER_NAME'), $object->config('realm'));
  68. }
  69. /**
  70. * test the authenticate method
  71. *
  72. * @return void
  73. */
  74. public function testAuthenticateNoData() {
  75. $request = new Request('posts/index');
  76. $this->response->expects($this->never())
  77. ->method('header');
  78. $this->assertFalse($this->auth->getUser($request, $this->response));
  79. }
  80. /**
  81. * test the authenticate method
  82. *
  83. * @expectedException \Cake\Error\UnauthorizedException
  84. * @expectedExceptionCode 401
  85. * @return void
  86. */
  87. public function testAuthenticateWrongUsername() {
  88. $request = new Request('posts/index');
  89. $request->addParams(array('pass' => array()));
  90. $digest = <<<DIGEST
  91. Digest username="incorrect_user",
  92. realm="localhost",
  93. nonce="123456",
  94. uri="/dir/index.html",
  95. qop=auth,
  96. nc=00000001,
  97. cnonce="0a4f113b",
  98. response="6629fae49393a05397450978507c4ef1",
  99. opaque="123abc"
  100. DIGEST;
  101. $request->env('PHP_AUTH_DIGEST', $digest);
  102. $this->auth->unauthenticated($request, $this->response);
  103. }
  104. /**
  105. * test that challenge headers are sent when no credentials are found.
  106. *
  107. * @return void
  108. */
  109. public function testAuthenticateChallenge() {
  110. $request = new Request([
  111. 'url' => 'posts/index',
  112. 'environment' => ['REQUEST_METHOD' => 'GET']
  113. ]);
  114. $request->addParams(array('pass' => array()));
  115. try {
  116. $this->auth->unauthenticated($request, $this->response);
  117. } catch (Error\UnauthorizedException $e) {
  118. }
  119. $this->assertNotEmpty($e);
  120. $expected = array('WWW-Authenticate: Digest realm="localhost",qop="auth",nonce="123",opaque="123abc"');
  121. $this->assertEquals($expected, $e->responseHeader());
  122. }
  123. /**
  124. * test authenticate success
  125. *
  126. * @return void
  127. */
  128. public function testAuthenticateSuccess() {
  129. $request = new Request([
  130. 'url' => 'posts/index',
  131. 'environment' => ['REQUEST_METHOD' => 'GET']
  132. ]);
  133. $request->addParams(array('pass' => array()));
  134. $digest = <<<DIGEST
  135. Digest username="mariano",
  136. realm="localhost",
  137. nonce="123",
  138. uri="/dir/index.html",
  139. qop=auth,
  140. nc=1,
  141. cnonce="123",
  142. response="06b257a54befa2ddfb9bfa134224aa29",
  143. opaque="123abc"
  144. DIGEST;
  145. $request->env('PHP_AUTH_DIGEST', $digest);
  146. $result = $this->auth->authenticate($request, $this->response);
  147. $expected = array(
  148. 'id' => 1,
  149. 'username' => 'mariano',
  150. 'created' => new \DateTime('2007-03-17 01:16:23'),
  151. 'updated' => new \DateTime('2007-03-17 01:18:31')
  152. );
  153. $this->assertEquals($expected, $result);
  154. }
  155. /**
  156. * test scope failure.
  157. *
  158. * @expectedException \Cake\Error\UnauthorizedException
  159. * @expectedExceptionCode 401
  160. * @return void
  161. */
  162. public function testAuthenticateFailReChallenge() {
  163. $this->auth->config('scope.username', 'nate');
  164. $request = new Request([
  165. 'url' => 'posts/index',
  166. 'environment' => ['REQUEST_METHOD' => 'GET']
  167. ]);
  168. $request->addParams(array('pass' => array()));
  169. $digest = <<<DIGEST
  170. Digest username="mariano",
  171. realm="localhost",
  172. nonce="123",
  173. uri="/dir/index.html",
  174. qop=auth,
  175. nc=1,
  176. cnonce="123",
  177. response="6629fae49393a05397450978507c4ef1",
  178. opaque="123abc"
  179. DIGEST;
  180. $request->env('PHP_AUTH_DIGEST', $digest);
  181. $this->auth->unauthenticated($request, $this->response);
  182. }
  183. /**
  184. * testLoginHeaders method
  185. *
  186. * @return void
  187. */
  188. public function testLoginHeaders() {
  189. $request = new Request([
  190. 'environment' => ['SERVER_NAME' => 'localhost']
  191. ]);
  192. $this->auth = new DigestAuthenticate($this->Collection, array(
  193. 'realm' => 'localhost',
  194. 'nonce' => '123'
  195. ));
  196. $expected = 'WWW-Authenticate: Digest realm="localhost",qop="auth",nonce="123",opaque="421aa90e079fa326b6494f812ad13e79"';
  197. $result = $this->auth->loginHeaders($request);
  198. $this->assertEquals($expected, $result);
  199. }
  200. /**
  201. * testParseDigestAuthData method
  202. *
  203. * @return void
  204. */
  205. public function testParseAuthData() {
  206. $digest = <<<DIGEST
  207. Digest username="Mufasa",
  208. realm="testrealm@host.com",
  209. nonce="dcd98b7102dd2f0e8b11d0f600bfb0c093",
  210. uri="/dir/index.html",
  211. qop=auth,
  212. nc=00000001,
  213. cnonce="0a4f113b",
  214. response="6629fae49393a05397450978507c4ef1",
  215. opaque="5ccc069c403ebaf9f0171e9517f40e41"
  216. DIGEST;
  217. $expected = array(
  218. 'username' => 'Mufasa',
  219. 'realm' => 'testrealm@host.com',
  220. 'nonce' => 'dcd98b7102dd2f0e8b11d0f600bfb0c093',
  221. 'uri' => '/dir/index.html',
  222. 'qop' => 'auth',
  223. 'nc' => '00000001',
  224. 'cnonce' => '0a4f113b',
  225. 'response' => '6629fae49393a05397450978507c4ef1',
  226. 'opaque' => '5ccc069c403ebaf9f0171e9517f40e41'
  227. );
  228. $result = $this->auth->parseAuthData($digest);
  229. $this->assertSame($expected, $result);
  230. $result = $this->auth->parseAuthData('');
  231. $this->assertNull($result);
  232. }
  233. /**
  234. * test parsing digest information with email addresses
  235. *
  236. * @return void
  237. */
  238. public function testParseAuthEmailAddress() {
  239. $digest = <<<DIGEST
  240. Digest username="mark@example.com",
  241. realm="testrealm@host.com",
  242. nonce="dcd98b7102dd2f0e8b11d0f600bfb0c093",
  243. uri="/dir/index.html",
  244. qop=auth,
  245. nc=00000001,
  246. cnonce="0a4f113b",
  247. response="6629fae49393a05397450978507c4ef1",
  248. opaque="5ccc069c403ebaf9f0171e9517f40e41"
  249. DIGEST;
  250. $expected = array(
  251. 'username' => 'mark@example.com',
  252. 'realm' => 'testrealm@host.com',
  253. 'nonce' => 'dcd98b7102dd2f0e8b11d0f600bfb0c093',
  254. 'uri' => '/dir/index.html',
  255. 'qop' => 'auth',
  256. 'nc' => '00000001',
  257. 'cnonce' => '0a4f113b',
  258. 'response' => '6629fae49393a05397450978507c4ef1',
  259. 'opaque' => '5ccc069c403ebaf9f0171e9517f40e41'
  260. );
  261. $result = $this->auth->parseAuthData($digest);
  262. $this->assertSame($expected, $result);
  263. }
  264. /**
  265. * test password hashing
  266. *
  267. * @return void
  268. */
  269. public function testPassword() {
  270. $result = DigestAuthenticate::password('mark', 'password', 'localhost');
  271. $expected = md5('mark:localhost:password');
  272. $this->assertEquals($expected, $result);
  273. }
  274. }