DigestTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * Redistributions of files must retain the above copyright notice.
  8. *
  9. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  10. * @link http://cakephp.org CakePHP(tm) Project
  11. * @since 3.0.0
  12. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  13. */
  14. namespace Cake\Test\TestCase\Network\Http\Auth;
  15. use Cake\Network\Http\Auth\Digest;
  16. use Cake\Network\Http\Request;
  17. use Cake\Network\Http\Response;
  18. use Cake\TestSuite\TestCase;
  19. /**
  20. * Digest authentication test
  21. */
  22. class DigestTest extends TestCase {
  23. /**
  24. * Setup
  25. *
  26. * @return void
  27. */
  28. public function setUp() {
  29. parent::setUp();
  30. $this->client = $this->getMock(
  31. 'Cake\Network\Http\Client',
  32. ['send']
  33. );
  34. $this->auth = new Digest($this->client);
  35. }
  36. /**
  37. * test getting data from additional request method
  38. *
  39. * @return void
  40. */
  41. public function testRealmAndNonceFromExtraRequest() {
  42. $headers = [
  43. 'WWW-Authenticate: Digest realm="The batcave",nonce="4cded326c6c51"'
  44. ];
  45. $response = new Response($headers, '');
  46. $this->client->expects($this->once())
  47. ->method('send')
  48. ->will($this->returnValue($response));
  49. $auth = ['username' => 'admin', 'password' => '1234'];
  50. $request = (new Request())->method(Request::METHOD_GET)
  51. ->url('http://example.com/some/path');
  52. $this->auth->authentication($request, $auth);
  53. $result = $request->header('Authorization');
  54. $this->assertContains('Digest', $result);
  55. $this->assertContains('realm="The batcave"', $result);
  56. $this->assertContains('nonce="4cded326c6c51"', $result);
  57. $this->assertContains('response="a21a874c0b29165929f5d24d1aad2c47"', $result);
  58. $this->assertContains('uri="/some/path"', $result);
  59. $this->assertNotContains('qop=', $result);
  60. $this->assertNotContains('nc=', $result);
  61. }
  62. /**
  63. * testQop method
  64. *
  65. * @return void
  66. */
  67. public function testQop() {
  68. $headers = [
  69. 'WWW-Authenticate: Digest realm="The batcave",nonce="4cded326c6c51",qop="auth"'
  70. ];
  71. $response = new Response($headers, '');
  72. $this->client->expects($this->once())
  73. ->method('send')
  74. ->will($this->returnValue($response));
  75. $auth = ['username' => 'admin', 'password' => '1234'];
  76. $request = (new Request())->method(Request::METHOD_GET)
  77. ->url('http://example.com/some/path');
  78. $this->auth->authentication($request, $auth);
  79. $result = $request->header('Authorization');
  80. $this->assertContains('qop="auth"', $result);
  81. $this->assertContains('nc=00000001', $result);
  82. $this->assertRegexp('/cnonce="[a-z0-9]+"/', $result);
  83. }
  84. /**
  85. * testOpaque method
  86. *
  87. * @return void
  88. */
  89. public function testOpaque() {
  90. $headers = [
  91. 'WWW-Authenticate: Digest realm="The batcave",nonce="4cded326c6c51",opaque="d8ea7aa61a1693024c4cc3a516f49b3c"'
  92. ];
  93. $response = new Response($headers, '');
  94. $this->client->expects($this->once())
  95. ->method('send')
  96. ->will($this->returnValue($response));
  97. $auth = ['username' => 'admin', 'password' => '1234'];
  98. $request = (new Request())->method(Request::METHOD_GET)
  99. ->url('http://example.com/some/path');
  100. $this->auth->authentication($request, $auth);
  101. $result = $request->header('Authorization');
  102. $this->assertContains('opaque="d8ea7aa61a1693024c4cc3a516f49b3c"', $result);
  103. }
  104. }