DigestAuthenticateTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. <?php
  2. /**
  3. * DigestAuthenticateTest file
  4. *
  5. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  6. * Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
  13. * @link https://cakephp.org CakePHP(tm) Project
  14. * @since 2.0.0
  15. * @license https://opensource.org/licenses/mit-license.php MIT License
  16. */
  17. namespace Cake\Test\TestCase\Auth;
  18. use Cake\Auth\DigestAuthenticate;
  19. use Cake\Controller\ComponentRegistry;
  20. use Cake\Core\Configure;
  21. use Cake\Http\Exception\UnauthorizedException;
  22. use Cake\Http\Response;
  23. use Cake\Http\ServerRequest;
  24. use Cake\I18n\Time;
  25. use Cake\ORM\Entity;
  26. use Cake\TestSuite\TestCase;
  27. use Cake\Utility\Security;
  28. /**
  29. * Entity for testing with hidden fields.
  30. */
  31. class ProtectedUser extends Entity
  32. {
  33. protected $_hidden = ['password'];
  34. }
  35. /**
  36. * Test case for DigestAuthentication
  37. */
  38. class DigestAuthenticateTest extends TestCase
  39. {
  40. /**
  41. * Fixtures
  42. *
  43. * @var array
  44. */
  45. public $fixtures = ['core.AuthUsers', 'core.Users'];
  46. /**
  47. * @var \Cake\Auth\DigestAuthenticate
  48. */
  49. protected $auth;
  50. /**
  51. * setup
  52. *
  53. * @return void
  54. */
  55. public function setUp()
  56. {
  57. parent::setUp();
  58. $this->Collection = $this->getMockBuilder(ComponentRegistry::class)->getMock();
  59. $this->auth = new DigestAuthenticate($this->Collection, [
  60. 'realm' => 'localhost',
  61. 'nonce' => 123,
  62. 'opaque' => '123abc',
  63. 'secret' => Security::getSalt(),
  64. 'passwordHasher' => 'ShouldNeverTryToUsePasswordHasher',
  65. ]);
  66. $password = DigestAuthenticate::password('mariano', 'cake', 'localhost');
  67. $User = $this->getTableLocator()->get('Users');
  68. $User->updateAll(['password' => $password], []);
  69. $this->response = $this->getMockBuilder(Response::class)->getMock();
  70. }
  71. /**
  72. * test applying settings in the constructor
  73. *
  74. * @return void
  75. */
  76. public function testConstructor()
  77. {
  78. $object = new DigestAuthenticate($this->Collection, [
  79. 'userModel' => 'AuthUser',
  80. 'fields' => ['username' => 'user', 'password' => 'pass'],
  81. 'nonce' => 123456,
  82. ]);
  83. $this->assertEquals('AuthUser', $object->getConfig('userModel'));
  84. $this->assertEquals(['username' => 'user', 'password' => 'pass'], $object->getConfig('fields'));
  85. $this->assertEquals(123456, $object->getConfig('nonce'));
  86. $this->assertEquals(env('SERVER_NAME'), $object->getConfig('realm'));
  87. }
  88. /**
  89. * test the authenticate method
  90. *
  91. * @return void
  92. */
  93. public function testAuthenticateNoData()
  94. {
  95. $request = new ServerRequest('posts/index');
  96. $this->response->expects($this->never())
  97. ->method('header');
  98. $this->assertFalse($this->auth->getUser($request, $this->response));
  99. }
  100. /**
  101. * test the authenticate method
  102. *
  103. * @return void
  104. */
  105. public function testAuthenticateWrongUsername()
  106. {
  107. $request = new ServerRequest(['url' => 'posts/index']);
  108. $data = [
  109. 'username' => 'incorrect_user',
  110. 'realm' => 'localhost',
  111. 'nonce' => $this->generateNonce(),
  112. 'uri' => '/dir/index.html',
  113. 'qop' => 'auth',
  114. 'nc' => 0000001,
  115. 'cnonce' => '0a4f113b',
  116. ];
  117. $data['response'] = $this->auth->generateResponseHash($data, '09faa9931501bf30f0d4253fa7763022', 'GET');
  118. $request = $request->withEnv('PHP_AUTH_DIGEST', $this->digestHeader($data));
  119. $this->assertFalse($this->auth->authenticate($request, new Response()));
  120. $this->expectException(UnauthorizedException::class);
  121. $this->expectExceptionCode(401);
  122. $this->auth->unauthenticated($request, $this->response);
  123. }
  124. /**
  125. * test that challenge headers are sent when no credentials are found.
  126. *
  127. * @return void
  128. */
  129. public function testAuthenticateChallenge()
  130. {
  131. $request = new ServerRequest([
  132. 'url' => 'posts/index',
  133. 'environment' => ['REQUEST_METHOD' => 'GET'],
  134. ]);
  135. try {
  136. $this->auth->unauthenticated($request, $this->response);
  137. } catch (UnauthorizedException $e) {
  138. }
  139. $this->assertNotEmpty($e);
  140. $header = $e->responseHeader();
  141. $this->assertRegexp(
  142. '/^Digest realm="localhost",qop="auth",nonce="[a-zA-Z0-9=]+",opaque="123abc"$/',
  143. $header['WWW-Authenticate']
  144. );
  145. }
  146. /**
  147. * test that challenge headers include stale when the nonce is stale
  148. *
  149. * @return void
  150. */
  151. public function testAuthenticateChallengeIncludesStaleAttributeOnStaleNonce()
  152. {
  153. $request = new ServerRequest([
  154. 'url' => 'posts/index',
  155. 'environment' => ['REQUEST_METHOD' => 'GET'],
  156. ]);
  157. $data = [
  158. 'uri' => '/dir/index.html',
  159. 'nonce' => $this->generateNonce(null, 5, strtotime('-10 minutes')),
  160. 'nc' => 1,
  161. 'cnonce' => '123',
  162. 'qop' => 'auth',
  163. ];
  164. $data['response'] = $this->auth->generateResponseHash($data, '09faa9931501bf30f0d4253fa7763022', 'GET');
  165. $request = $request->withEnv('PHP_AUTH_DIGEST', $this->digestHeader($data));
  166. try {
  167. $this->auth->unauthenticated($request, $this->response);
  168. } catch (UnauthorizedException $e) {
  169. }
  170. $this->assertNotEmpty($e);
  171. $header = $e->responseHeader()['WWW-Authenticate'];
  172. $this->assertContains('stale=true', $header);
  173. }
  174. /**
  175. * Test that authentication fails when a nonce is stale
  176. *
  177. * @return void
  178. */
  179. public function testAuthenticateFailsOnStaleNonce()
  180. {
  181. $request = new ServerRequest([
  182. 'url' => 'posts/index',
  183. 'environment' => ['REQUEST_METHOD' => 'GET'],
  184. ]);
  185. $data = [
  186. 'uri' => '/dir/index.html',
  187. 'nonce' => $this->generateNonce(null, 5, strtotime('-10 minutes')),
  188. 'nc' => 1,
  189. 'cnonce' => '123',
  190. 'qop' => 'auth',
  191. ];
  192. $data['response'] = $this->auth->generateResponseHash($data, '09faa9931501bf30f0d4253fa7763022', 'GET');
  193. $request = $request->withEnv('PHP_AUTH_DIGEST', $this->digestHeader($data));
  194. $result = $this->auth->authenticate($request, $this->response);
  195. $this->assertFalse($result, 'Stale nonce should fail');
  196. }
  197. /**
  198. * Test that nonces are required.
  199. *
  200. * @return void
  201. */
  202. public function testAuthenticateValidUsernamePasswordNoNonce()
  203. {
  204. $request = new ServerRequest([
  205. 'url' => 'posts/index',
  206. 'environment' => ['REQUEST_METHOD' => 'GET'],
  207. ]);
  208. $data = [
  209. 'username' => 'mariano',
  210. 'realm' => 'localhos',
  211. 'uri' => '/dir/index.html',
  212. 'nonce' => '',
  213. 'nc' => 1,
  214. 'cnonce' => '123',
  215. 'qop' => 'auth',
  216. ];
  217. $data['response'] = $this->auth->generateResponseHash($data, '09faa9931501bf30f0d4253fa7763022', 'GET');
  218. $request = $request->withEnv('PHP_AUTH_DIGEST', $this->digestHeader($data));
  219. $result = $this->auth->authenticate($request, $this->response);
  220. $this->assertFalse($result, 'Empty nonce should fail');
  221. }
  222. /**
  223. * test authenticate success
  224. *
  225. * @return void
  226. */
  227. public function testAuthenticateSuccess()
  228. {
  229. $request = new ServerRequest([
  230. 'url' => 'posts/index',
  231. 'environment' => ['REQUEST_METHOD' => 'GET'],
  232. ]);
  233. $data = [
  234. 'uri' => '/dir/index.html',
  235. 'nonce' => $this->generateNonce(),
  236. 'nc' => 1,
  237. 'cnonce' => '123',
  238. 'qop' => 'auth',
  239. ];
  240. $data['response'] = $this->auth->generateResponseHash($data, '09faa9931501bf30f0d4253fa7763022', 'GET');
  241. $request = $request->withEnv('PHP_AUTH_DIGEST', $this->digestHeader($data));
  242. $result = $this->auth->authenticate($request, $this->response);
  243. $expected = [
  244. 'id' => 1,
  245. 'username' => 'mariano',
  246. 'created' => new Time('2007-03-17 01:16:23'),
  247. 'updated' => new Time('2007-03-17 01:18:31'),
  248. ];
  249. $this->assertEquals($expected, $result);
  250. }
  251. /**
  252. * test authenticate success even when digest 'password' is a hidden field.
  253. *
  254. * @return void
  255. */
  256. public function testAuthenticateSuccessHiddenPasswordField()
  257. {
  258. $User = $this->getTableLocator()->get('Users');
  259. $User->setEntityClass(ProtectedUser::class);
  260. $request = new ServerRequest([
  261. 'url' => 'posts/index',
  262. 'environment' => ['REQUEST_METHOD' => 'GET'],
  263. ]);
  264. $data = [
  265. 'uri' => '/dir/index.html',
  266. 'nonce' => $this->generateNonce(),
  267. 'nc' => 1,
  268. 'cnonce' => '123',
  269. 'qop' => 'auth',
  270. ];
  271. $data['response'] = $this->auth->generateResponseHash($data, '09faa9931501bf30f0d4253fa7763022', 'GET');
  272. $request = $request->withEnv('PHP_AUTH_DIGEST', $this->digestHeader($data));
  273. $result = $this->auth->authenticate($request, $this->response);
  274. $expected = [
  275. 'id' => 1,
  276. 'username' => 'mariano',
  277. 'created' => new Time('2007-03-17 01:16:23'),
  278. 'updated' => new Time('2007-03-17 01:18:31'),
  279. ];
  280. $this->assertEquals($expected, $result);
  281. }
  282. /**
  283. * test authenticate success
  284. *
  285. * @return void
  286. */
  287. public function testAuthenticateSuccessSimulatedRequestMethod()
  288. {
  289. $request = new ServerRequest([
  290. 'url' => 'posts/index',
  291. 'post' => ['_method' => 'PUT'],
  292. 'environment' => ['REQUEST_METHOD' => 'GET'],
  293. ]);
  294. $data = [
  295. 'username' => 'mariano',
  296. 'uri' => '/dir/index.html',
  297. 'nonce' => $this->generateNonce(),
  298. 'nc' => 1,
  299. 'cnonce' => '123',
  300. 'qop' => 'auth',
  301. ];
  302. $data['response'] = $this->auth->generateResponseHash($data, '09faa9931501bf30f0d4253fa7763022', 'GET');
  303. $request = $request->withEnv('PHP_AUTH_DIGEST', $this->digestHeader($data));
  304. $result = $this->auth->authenticate($request, $this->response);
  305. $expected = [
  306. 'id' => 1,
  307. 'username' => 'mariano',
  308. 'created' => new Time('2007-03-17 01:16:23'),
  309. 'updated' => new Time('2007-03-17 01:18:31'),
  310. ];
  311. $this->assertEquals($expected, $result);
  312. }
  313. /**
  314. * test scope failure.
  315. *
  316. * @return void
  317. */
  318. public function testAuthenticateFailReChallenge()
  319. {
  320. $this->expectException(\Cake\Http\Exception\UnauthorizedException::class);
  321. $this->expectExceptionCode(401);
  322. $this->auth->setConfig('scope.username', 'nate');
  323. $request = new ServerRequest([
  324. 'url' => 'posts/index',
  325. 'environment' => ['REQUEST_METHOD' => 'GET'],
  326. ]);
  327. $data = [
  328. 'username' => 'invalid',
  329. 'uri' => '/dir/index.html',
  330. 'nonce' => $this->generateNonce(),
  331. 'nc' => 1,
  332. 'cnonce' => '123',
  333. 'qop' => 'auth',
  334. ];
  335. $data['response'] = $this->auth->generateResponseHash($data, '09faa9931501bf30f0d4253fa7763022', 'GET');
  336. $request = $request->withEnv('PHP_AUTH_DIGEST', $this->digestHeader($data));
  337. $this->auth->unauthenticated($request, $this->response);
  338. }
  339. /**
  340. * testLoginHeaders method
  341. *
  342. * @return void
  343. */
  344. public function testLoginHeaders()
  345. {
  346. $request = new ServerRequest([
  347. 'environment' => ['SERVER_NAME' => 'localhost'],
  348. ]);
  349. $this->auth = new DigestAuthenticate($this->Collection, [
  350. 'realm' => 'localhost',
  351. ]);
  352. $result = $this->auth->loginHeaders($request);
  353. $this->assertRegexp(
  354. '/^Digest realm="localhost",qop="auth",nonce="[a-zA-Z0-9=]+",opaque="[a-f0-9]+"$/',
  355. $result['WWW-Authenticate']
  356. );
  357. }
  358. /**
  359. * testParseDigestAuthData method
  360. *
  361. * @return void
  362. */
  363. public function testParseAuthData()
  364. {
  365. $digest = <<<DIGEST
  366. Digest username="Mufasa",
  367. realm="testrealm@host.com",
  368. nonce="dcd98b7102dd2f0e8b11d0f600bfb0c093",
  369. uri="/dir/index.html?query=string&value=some%20value",
  370. qop=auth,
  371. nc=00000001,
  372. cnonce="0a4f113b",
  373. response="6629fae49393a05397450978507c4ef1",
  374. opaque="5ccc069c403ebaf9f0171e9517f40e41"
  375. DIGEST;
  376. $expected = [
  377. 'username' => 'Mufasa',
  378. 'realm' => 'testrealm@host.com',
  379. 'nonce' => 'dcd98b7102dd2f0e8b11d0f600bfb0c093',
  380. 'uri' => '/dir/index.html?query=string&value=some%20value',
  381. 'qop' => 'auth',
  382. 'nc' => '00000001',
  383. 'cnonce' => '0a4f113b',
  384. 'response' => '6629fae49393a05397450978507c4ef1',
  385. 'opaque' => '5ccc069c403ebaf9f0171e9517f40e41',
  386. ];
  387. $result = $this->auth->parseAuthData($digest);
  388. $this->assertSame($expected, $result);
  389. $result = $this->auth->parseAuthData('');
  390. $this->assertNull($result);
  391. }
  392. /**
  393. * Test parsing a full URI. While not part of the spec some mobile clients will do it wrong.
  394. *
  395. * @return void
  396. */
  397. public function testParseAuthDataFullUri()
  398. {
  399. $digest = <<<DIGEST
  400. Digest username="admin",
  401. realm="192.168.0.2",
  402. nonce="53a7f9b83f61b",
  403. uri="http://192.168.0.2/pvcollection/sites/pull/HFD%200001.json#fragment",
  404. qop=auth,
  405. nc=00000001,
  406. cnonce="b85ff144e496e6e18d1c73020566ea3b",
  407. response="5894f5d9cd41d012bac09eeb89d2ddf2",
  408. opaque="6f65e91667cf98dd13464deaf2739fde"
  409. DIGEST;
  410. $expected = 'http://192.168.0.2/pvcollection/sites/pull/HFD%200001.json#fragment';
  411. $result = $this->auth->parseAuthData($digest);
  412. $this->assertSame($expected, $result['uri']);
  413. }
  414. /**
  415. * test parsing digest information with email addresses
  416. *
  417. * @return void
  418. */
  419. public function testParseAuthEmailAddress()
  420. {
  421. $digest = <<<DIGEST
  422. Digest username="mark@example.com",
  423. realm="testrealm@host.com",
  424. nonce="dcd98b7102dd2f0e8b11d0f600bfb0c093",
  425. uri="/dir/index.html",
  426. qop=auth,
  427. nc=00000001,
  428. cnonce="0a4f113b",
  429. response="6629fae49393a05397450978507c4ef1",
  430. opaque="5ccc069c403ebaf9f0171e9517f40e41"
  431. DIGEST;
  432. $expected = [
  433. 'username' => 'mark@example.com',
  434. 'realm' => 'testrealm@host.com',
  435. 'nonce' => 'dcd98b7102dd2f0e8b11d0f600bfb0c093',
  436. 'uri' => '/dir/index.html',
  437. 'qop' => 'auth',
  438. 'nc' => '00000001',
  439. 'cnonce' => '0a4f113b',
  440. 'response' => '6629fae49393a05397450978507c4ef1',
  441. 'opaque' => '5ccc069c403ebaf9f0171e9517f40e41',
  442. ];
  443. $result = $this->auth->parseAuthData($digest);
  444. $this->assertSame($expected, $result);
  445. }
  446. /**
  447. * test password hashing
  448. *
  449. * @return void
  450. */
  451. public function testPassword()
  452. {
  453. $result = DigestAuthenticate::password('mark', 'password', 'localhost');
  454. $expected = md5('mark:localhost:password');
  455. $this->assertEquals($expected, $result);
  456. }
  457. /**
  458. * Generate a nonce for testing.
  459. *
  460. * @param string $secret The secret to use.
  461. * @param int $expires Time to live
  462. * @return string
  463. */
  464. protected function generateNonce($secret = null, $expires = 300, $time = null)
  465. {
  466. $secret = $secret ?: Configure::read('Security.salt');
  467. $time = $time ?: microtime(true);
  468. $expiryTime = $time + $expires;
  469. $signatureValue = hash_hmac('sha256', $expiryTime . ':' . $secret, $secret);
  470. $nonceValue = $expiryTime . ':' . $signatureValue;
  471. return base64_encode($nonceValue);
  472. }
  473. /**
  474. * Create a digest header string from an array of data.
  475. *
  476. * @param array $data the data to convert into a header.
  477. * @return string
  478. */
  479. protected function digestHeader($data)
  480. {
  481. $data += [
  482. 'username' => 'mariano',
  483. 'realm' => 'localhost',
  484. 'opaque' => '123abc',
  485. ];
  486. $digest = <<<DIGEST
  487. Digest username="{$data['username']}",
  488. realm="{$data['realm']}",
  489. nonce="{$data['nonce']}",
  490. uri="{$data['uri']}",
  491. qop={$data['qop']},
  492. nc={$data['nc']},
  493. cnonce="{$data['cnonce']}",
  494. response="{$data['response']}",
  495. opaque="{$data['opaque']}"
  496. DIGEST;
  497. return $digest;
  498. }
  499. }