DigestAuthenticationTest.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. /**
  3. * DigestAuthenticationTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  8. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * For full copyright and license information, please see the LICENSE.txt
  12. * Redistributions of files must retain the above copyright notice
  13. *
  14. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  15. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  16. * @package Cake.Test.Case.Network.Http
  17. * @since CakePHP(tm) v 2.0.0
  18. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  19. */
  20. App::uses('HttpSocket', 'Network/Http');
  21. App::uses('DigestAuthentication', 'Network/Http');
  22. class DigestHttpSocket extends HttpSocket {
  23. /**
  24. * nextHeader attribute
  25. *
  26. * @var string
  27. */
  28. public $nextHeader = '';
  29. /**
  30. * request method
  31. *
  32. * @param mixed $request
  33. * @return void
  34. */
  35. public function request($request = array()) {
  36. if ($request === false) {
  37. if (isset($this->response['header']['WWW-Authenticate'])) {
  38. unset($this->response['header']['WWW-Authenticate']);
  39. }
  40. return;
  41. }
  42. $this->response['header']['WWW-Authenticate'] = $this->nextHeader;
  43. }
  44. }
  45. /**
  46. * DigestAuthenticationTest class
  47. *
  48. * @package Cake.Test.Case.Network.Http
  49. */
  50. class DigestAuthenticationTest extends CakeTestCase {
  51. /**
  52. * Socket property
  53. *
  54. * @var mixed null
  55. */
  56. public $HttpSocket = null;
  57. /**
  58. * This function sets up a HttpSocket instance we are going to use for testing
  59. *
  60. * @return void
  61. */
  62. public function setUp() {
  63. $this->HttpSocket = new DigestHttpSocket();
  64. $this->HttpSocket->request['method'] = 'GET';
  65. $this->HttpSocket->request['uri']['path'] = '/';
  66. }
  67. /**
  68. * We use this function to clean up after the test case was executed
  69. *
  70. * @return void
  71. */
  72. public function tearDown() {
  73. unset($this->HttpSocket);
  74. }
  75. /**
  76. * testBasic method
  77. *
  78. * @return void
  79. */
  80. public function testBasic() {
  81. $this->HttpSocket->nextHeader = 'Digest realm="The batcave",nonce="4cded326c6c51"';
  82. $this->assertFalse(isset($this->HttpSocket->request['header']['Authorization']));
  83. $auth = array('user' => 'admin', 'pass' => '1234');
  84. DigestAuthentication::authentication($this->HttpSocket, $auth);
  85. $this->assertTrue(isset($this->HttpSocket->request['header']['Authorization']));
  86. $this->assertEquals('The batcave', $auth['realm']);
  87. $this->assertEquals('4cded326c6c51', $auth['nonce']);
  88. }
  89. /**
  90. * testQop method
  91. *
  92. * @return void
  93. */
  94. public function testQop() {
  95. $this->HttpSocket->nextHeader = 'Digest realm="The batcave",nonce="4cded326c6c51"';
  96. $auth = array('user' => 'admin', 'pass' => '1234');
  97. DigestAuthentication::authentication($this->HttpSocket, $auth);
  98. $expected = 'Digest username="admin", realm="The batcave", nonce="4cded326c6c51", uri="/", response="da7e2a46b471d77f70a9bb3698c8902b"';
  99. $this->assertEquals($expected, $this->HttpSocket->request['header']['Authorization']);
  100. $this->assertFalse(isset($auth['qop']));
  101. $this->assertFalse(isset($auth['nc']));
  102. $this->HttpSocket->nextHeader = 'Digest realm="The batcave",nonce="4cded326c6c51",qop="auth"';
  103. $auth = array('user' => 'admin', 'pass' => '1234');
  104. DigestAuthentication::authentication($this->HttpSocket, $auth);
  105. $expected = '@Digest username="admin", realm="The batcave", nonce="4cded326c6c51", uri="/", response="[a-z0-9]{32}", qop="auth", nc=00000001, cnonce="[a-z0-9]+"@';
  106. $this->assertRegExp($expected, $this->HttpSocket->request['header']['Authorization']);
  107. $this->assertEquals('auth', $auth['qop']);
  108. $this->assertEquals(2, $auth['nc']);
  109. }
  110. /**
  111. * testOpaque method
  112. *
  113. * @return void
  114. */
  115. public function testOpaque() {
  116. $this->HttpSocket->nextHeader = 'Digest realm="The batcave",nonce="4cded326c6c51"';
  117. $auth = array('user' => 'admin', 'pass' => '1234');
  118. DigestAuthentication::authentication($this->HttpSocket, $auth);
  119. $this->assertFalse(strpos($this->HttpSocket->request['header']['Authorization'], 'opaque="d8ea7aa61a1693024c4cc3a516f49b3c"'));
  120. $this->HttpSocket->nextHeader = 'Digest realm="The batcave",nonce="4cded326c6c51",opaque="d8ea7aa61a1693024c4cc3a516f49b3c"';
  121. $auth = array('user' => 'admin', 'pass' => '1234');
  122. DigestAuthentication::authentication($this->HttpSocket, $auth);
  123. $this->assertTrue(strpos($this->HttpSocket->request['header']['Authorization'], 'opaque="d8ea7aa61a1693024c4cc3a516f49b3c"') > 0);
  124. }
  125. /**
  126. * testMultipleRequest method
  127. *
  128. * @return void
  129. */
  130. public function testMultipleRequest() {
  131. $this->HttpSocket->nextHeader = 'Digest realm="The batcave",nonce="4cded326c6c51",qop="auth"';
  132. $auth = array('user' => 'admin', 'pass' => '1234');
  133. DigestAuthentication::authentication($this->HttpSocket, $auth);
  134. $this->assertTrue(strpos($this->HttpSocket->request['header']['Authorization'], 'nc=00000001') > 0);
  135. $this->assertEquals(2, $auth['nc']);
  136. DigestAuthentication::authentication($this->HttpSocket, $auth);
  137. $this->assertTrue(strpos($this->HttpSocket->request['header']['Authorization'], 'nc=00000002') > 0);
  138. $this->assertEquals(3, $auth['nc']);
  139. $responsePos = strpos($this->HttpSocket->request['header']['Authorization'], 'response=');
  140. $response = substr($this->HttpSocket->request['header']['Authorization'], $responsePos + 10, 32);
  141. $this->HttpSocket->nextHeader = '';
  142. DigestAuthentication::authentication($this->HttpSocket, $auth);
  143. $this->assertTrue(strpos($this->HttpSocket->request['header']['Authorization'], 'nc=00000003') > 0);
  144. $this->assertEquals(4, $auth['nc']);
  145. $responsePos = strpos($this->HttpSocket->request['header']['Authorization'], 'response=');
  146. $responseB = substr($this->HttpSocket->request['header']['Authorization'], $responsePos + 10, 32);
  147. $this->assertNotEquals($response, $responseB);
  148. }
  149. /**
  150. * testPathChanged method
  151. *
  152. * @return void
  153. */
  154. public function testPathChanged() {
  155. $this->HttpSocket->nextHeader = 'Digest realm="The batcave",nonce="4cded326c6c51"';
  156. $this->HttpSocket->request['uri']['path'] = '/admin';
  157. $auth = array('user' => 'admin', 'pass' => '1234');
  158. DigestAuthentication::authentication($this->HttpSocket, $auth);
  159. $responsePos = strpos($this->HttpSocket->request['header']['Authorization'], 'response=');
  160. $response = substr($this->HttpSocket->request['header']['Authorization'], $responsePos + 10, 32);
  161. $this->assertNotEquals('da7e2a46b471d77f70a9bb3698c8902b', $response);
  162. }
  163. /**
  164. * testNoDigestResponse method
  165. *
  166. * @return void
  167. */
  168. public function testNoDigestResponse() {
  169. $this->HttpSocket->nextHeader = false;
  170. $this->HttpSocket->request['uri']['path'] = '/admin';
  171. $auth = array('user' => 'admin', 'pass' => '1234');
  172. DigestAuthentication::authentication($this->HttpSocket, $auth);
  173. $this->assertFalse(isset($this->HttpSocket->request['header']['Authorization']));
  174. }
  175. }