DigestAuthenticateTest.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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\Auth;
  18. use Cake\Auth\DigestAuthenticate;
  19. use Cake\I18n\Time;
  20. use Cake\Network\Exception\UnauthorizedException;
  21. use Cake\Network\Request;
  22. use Cake\ORM\Entity;
  23. use Cake\ORM\TableRegistry;
  24. use Cake\TestSuite\TestCase;
  25. /**
  26. * Test case for DigestAuthentication
  27. *
  28. */
  29. class DigestAuthenticateTest extends TestCase {
  30. /**
  31. * Fixtures
  32. *
  33. * @var array
  34. */
  35. public $fixtures = array('core.users', 'core.auth_users');
  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 DigestAuthenticate($this->Collection, array(
  45. 'realm' => 'localhost',
  46. 'nonce' => 123,
  47. 'opaque' => '123abc'
  48. ));
  49. $password = DigestAuthenticate::password('mariano', 'cake', 'localhost');
  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 DigestAuthenticate($this->Collection, array(
  61. 'userModel' => 'AuthUser',
  62. 'fields' => array('username' => 'user', 'password' => 'pass'),
  63. 'nonce' => 123456
  64. ));
  65. $this->assertEquals('AuthUser', $object->config('userModel'));
  66. $this->assertEquals(array('username' => 'user', 'password' => 'pass'), $object->config('fields'));
  67. $this->assertEquals(123456, $object->config('nonce'));
  68. $this->assertEquals(env('SERVER_NAME'), $object->config('realm'));
  69. }
  70. /**
  71. * test the authenticate method
  72. *
  73. * @return void
  74. */
  75. public function testAuthenticateNoData() {
  76. $request = new Request('posts/index');
  77. $this->response->expects($this->never())
  78. ->method('header');
  79. $this->assertFalse($this->auth->getUser($request, $this->response));
  80. }
  81. /**
  82. * test the authenticate method
  83. *
  84. * @expectedException \Cake\Network\Exception\UnauthorizedException
  85. * @expectedExceptionCode 401
  86. * @return void
  87. */
  88. public function testAuthenticateWrongUsername() {
  89. $request = new Request('posts/index');
  90. $request->addParams(array('pass' => array()));
  91. $digest = <<<DIGEST
  92. Digest username="incorrect_user",
  93. realm="localhost",
  94. nonce="123456",
  95. uri="/dir/index.html",
  96. qop=auth,
  97. nc=00000001,
  98. cnonce="0a4f113b",
  99. response="6629fae49393a05397450978507c4ef1",
  100. opaque="123abc"
  101. DIGEST;
  102. $request->env('PHP_AUTH_DIGEST', $digest);
  103. $this->auth->unauthenticated($request, $this->response);
  104. }
  105. /**
  106. * test that challenge headers are sent when no credentials are found.
  107. *
  108. * @return void
  109. */
  110. public function testAuthenticateChallenge() {
  111. $request = new Request([
  112. 'url' => 'posts/index',
  113. 'environment' => ['REQUEST_METHOD' => 'GET']
  114. ]);
  115. $request->addParams(array('pass' => array()));
  116. try {
  117. $this->auth->unauthenticated($request, $this->response);
  118. } catch (UnauthorizedException $e) {
  119. }
  120. $this->assertNotEmpty($e);
  121. $expected = array('WWW-Authenticate: Digest realm="localhost",qop="auth",nonce="123",opaque="123abc"');
  122. $this->assertEquals($expected, $e->responseHeader());
  123. }
  124. /**
  125. * test authenticate success
  126. *
  127. * @return void
  128. */
  129. public function testAuthenticateSuccess() {
  130. $request = new Request([
  131. 'url' => 'posts/index',
  132. 'environment' => ['REQUEST_METHOD' => 'GET']
  133. ]);
  134. $request->addParams(array('pass' => array()));
  135. $digest = <<<DIGEST
  136. Digest username="mariano",
  137. realm="localhost",
  138. nonce="123",
  139. uri="/dir/index.html",
  140. qop=auth,
  141. nc=1,
  142. cnonce="123",
  143. response="06b257a54befa2ddfb9bfa134224aa29",
  144. opaque="123abc"
  145. DIGEST;
  146. $request->env('PHP_AUTH_DIGEST', $digest);
  147. $result = $this->auth->authenticate($request, $this->response);
  148. $expected = array(
  149. 'id' => 1,
  150. 'username' => 'mariano',
  151. 'created' => new Time('2007-03-17 01:16:23'),
  152. 'updated' => new Time('2007-03-17 01:18:31')
  153. );
  154. $this->assertEquals($expected, $result);
  155. }
  156. /**
  157. * test authenticate success
  158. *
  159. * @return void
  160. */
  161. public function testAuthenticateSuccessSimulatedRequestMethod() {
  162. $request = new Request([
  163. 'url' => 'posts/index',
  164. 'post' => ['_method' => 'PUT'],
  165. 'environment' => ['REQUEST_METHOD' => 'GET']
  166. ]);
  167. $request->addParams(array('pass' => array()));
  168. $digest = <<<DIGEST
  169. Digest username="mariano",
  170. realm="localhost",
  171. nonce="123",
  172. uri="/dir/index.html",
  173. qop=auth,
  174. nc=1,
  175. cnonce="123",
  176. response="06b257a54befa2ddfb9bfa134224aa29",
  177. opaque="123abc"
  178. DIGEST;
  179. $request->env('PHP_AUTH_DIGEST', $digest);
  180. $result = $this->auth->authenticate($request, $this->response);
  181. $expected = array(
  182. 'id' => 1,
  183. 'username' => 'mariano',
  184. 'created' => new Time('2007-03-17 01:16:23'),
  185. 'updated' => new Time('2007-03-17 01:18:31')
  186. );
  187. $this->assertEquals($expected, $result);
  188. }
  189. /**
  190. * test scope failure.
  191. *
  192. * @expectedException \Cake\Network\Exception\UnauthorizedException
  193. * @expectedExceptionCode 401
  194. * @return void
  195. */
  196. public function testAuthenticateFailReChallenge() {
  197. $this->auth->config('scope.username', 'nate');
  198. $request = new Request([
  199. 'url' => 'posts/index',
  200. 'environment' => ['REQUEST_METHOD' => 'GET']
  201. ]);
  202. $request->addParams(array('pass' => array()));
  203. $digest = <<<DIGEST
  204. Digest username="mariano",
  205. realm="localhost",
  206. nonce="123",
  207. uri="/dir/index.html",
  208. qop=auth,
  209. nc=1,
  210. cnonce="123",
  211. response="6629fae49393a05397450978507c4ef1",
  212. opaque="123abc"
  213. DIGEST;
  214. $request->env('PHP_AUTH_DIGEST', $digest);
  215. $this->auth->unauthenticated($request, $this->response);
  216. }
  217. /**
  218. * testLoginHeaders method
  219. *
  220. * @return void
  221. */
  222. public function testLoginHeaders() {
  223. $request = new Request([
  224. 'environment' => ['SERVER_NAME' => 'localhost']
  225. ]);
  226. $this->auth = new DigestAuthenticate($this->Collection, array(
  227. 'realm' => 'localhost',
  228. 'nonce' => '123'
  229. ));
  230. $expected = 'WWW-Authenticate: Digest realm="localhost",qop="auth",nonce="123",opaque="421aa90e079fa326b6494f812ad13e79"';
  231. $result = $this->auth->loginHeaders($request);
  232. $this->assertEquals($expected, $result);
  233. }
  234. /**
  235. * testParseDigestAuthData method
  236. *
  237. * @return void
  238. */
  239. public function testParseAuthData() {
  240. $digest = <<<DIGEST
  241. Digest username="Mufasa",
  242. realm="testrealm@host.com",
  243. nonce="dcd98b7102dd2f0e8b11d0f600bfb0c093",
  244. uri="/dir/index.html",
  245. qop=auth,
  246. nc=00000001,
  247. cnonce="0a4f113b",
  248. response="6629fae49393a05397450978507c4ef1",
  249. opaque="5ccc069c403ebaf9f0171e9517f40e41"
  250. DIGEST;
  251. $expected = array(
  252. 'username' => 'Mufasa',
  253. 'realm' => 'testrealm@host.com',
  254. 'nonce' => 'dcd98b7102dd2f0e8b11d0f600bfb0c093',
  255. 'uri' => '/dir/index.html',
  256. 'qop' => 'auth',
  257. 'nc' => '00000001',
  258. 'cnonce' => '0a4f113b',
  259. 'response' => '6629fae49393a05397450978507c4ef1',
  260. 'opaque' => '5ccc069c403ebaf9f0171e9517f40e41'
  261. );
  262. $result = $this->auth->parseAuthData($digest);
  263. $this->assertSame($expected, $result);
  264. $result = $this->auth->parseAuthData('');
  265. $this->assertNull($result);
  266. }
  267. /**
  268. * Test parsing a full URI. While not part of the spec some mobile clients will do it wrong.
  269. *
  270. * @return void
  271. */
  272. public function testParseAuthDataFullUri() {
  273. $digest = <<<DIGEST
  274. Digest username="admin",
  275. realm="192.168.0.2",
  276. nonce="53a7f9b83f61b",
  277. uri="http://192.168.0.2/pvcollection/sites/pull/HFD%200001.json#fragment",
  278. qop=auth,
  279. nc=00000001,
  280. cnonce="b85ff144e496e6e18d1c73020566ea3b",
  281. response="5894f5d9cd41d012bac09eeb89d2ddf2",
  282. opaque="6f65e91667cf98dd13464deaf2739fde"
  283. DIGEST;
  284. $expected = 'http://192.168.0.2/pvcollection/sites/pull/HFD%200001.json#fragment';
  285. $result = $this->auth->parseAuthData($digest);
  286. $this->assertSame($expected, $result['uri']);
  287. }
  288. /**
  289. * test parsing digest information with email addresses
  290. *
  291. * @return void
  292. */
  293. public function testParseAuthEmailAddress() {
  294. $digest = <<<DIGEST
  295. Digest username="mark@example.com",
  296. realm="testrealm@host.com",
  297. nonce="dcd98b7102dd2f0e8b11d0f600bfb0c093",
  298. uri="/dir/index.html",
  299. qop=auth,
  300. nc=00000001,
  301. cnonce="0a4f113b",
  302. response="6629fae49393a05397450978507c4ef1",
  303. opaque="5ccc069c403ebaf9f0171e9517f40e41"
  304. DIGEST;
  305. $expected = array(
  306. 'username' => 'mark@example.com',
  307. 'realm' => 'testrealm@host.com',
  308. 'nonce' => 'dcd98b7102dd2f0e8b11d0f600bfb0c093',
  309. 'uri' => '/dir/index.html',
  310. 'qop' => 'auth',
  311. 'nc' => '00000001',
  312. 'cnonce' => '0a4f113b',
  313. 'response' => '6629fae49393a05397450978507c4ef1',
  314. 'opaque' => '5ccc069c403ebaf9f0171e9517f40e41'
  315. );
  316. $result = $this->auth->parseAuthData($digest);
  317. $this->assertSame($expected, $result);
  318. }
  319. /**
  320. * test password hashing
  321. *
  322. * @return void
  323. */
  324. public function testPassword() {
  325. $result = DigestAuthenticate::password('mark', 'password', 'localhost');
  326. $expected = md5('mark:localhost:password');
  327. $this->assertEquals($expected, $result);
  328. }
  329. }