DigestAuthenticateTest.php 10 KB

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