DigestAuthenticateTest.php 10 KB

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