AuthTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /**
  3. * Lithium: the most rad php framework
  4. *
  5. * @copyright Copyright 2013, Union of RAD (http://union-of-rad.org)
  6. * @license http://opensource.org/licenses/bsd-license.php The BSD License
  7. */
  8. namespace lithium\tests\cases\net\http;
  9. use lithium\net\http\Auth;
  10. class AuthTest extends \lithium\test\Unit {
  11. public function testBasicEncode() {
  12. $username = 'gwoo';
  13. $password = 'li3';
  14. $response = base64_encode("{$username}:{$password}");
  15. $expected = compact('username', 'response');
  16. $result = Auth::encode($username, $password);
  17. $this->assertEqual($expected, $result);
  18. }
  19. public function testDigestEncode() {
  20. $username = 'gwoo';
  21. $password = 'li3';
  22. $nc = '00000001';
  23. $cnonce = md5(time());
  24. $user = md5("gwoo:app:li3");
  25. $nonce = "4bca0fbca7bd0:{$nc}:{$cnonce}:auth";
  26. $req = md5("GET:/http_auth");
  27. $response = md5("{$user}:{$nonce}:{$req}");
  28. $data = array(
  29. 'realm' => 'app',
  30. 'method' => 'GET',
  31. 'uri' => '/http_auth',
  32. 'qop' => 'auth',
  33. 'nonce' => '4bca0fbca7bd0',
  34. 'opaque' => 'd3fb67a7aa4d887ec4bf83040a820a46'
  35. );
  36. $expected = $data + compact('username', 'response', 'nc', 'cnonce');
  37. $result = Auth::encode($username, $password, $data);
  38. $this->assertEqual($expected, $result);
  39. }
  40. public function testBasicHeader() {
  41. $username = 'gwoo';
  42. $password = 'li3';
  43. $response = base64_encode("{$username}:{$password}");
  44. $data = Auth::encode($username, $password);
  45. $expected = "Basic " . $response;
  46. $result = Auth::header($data);
  47. $this->assertEqual($expected, $result);
  48. }
  49. public function testDigestHeader() {
  50. $username = 'gwoo';
  51. $password = 'li3';
  52. $nc = '00000001';
  53. $cnonce = md5(time());
  54. $user = md5("gwoo:app:li3");
  55. $nonce = "4bca0fbca7bd0:{$nc}:{$cnonce}:auth";
  56. $req = md5("GET:/http_auth");
  57. $hash = md5("{$user}:{$nonce}:{$req}");
  58. $data = array(
  59. 'realm' => 'app',
  60. 'method' => 'GET',
  61. 'uri' => '/http_auth',
  62. 'qop' => 'auth',
  63. 'nonce' => '4bca0fbca7bd0',
  64. 'opaque' => 'd3fb67a7aa4d887ec4bf83040a820a46'
  65. );
  66. $data = Auth::encode($username, $password, $data);
  67. $header = Auth::header($data);
  68. $this->assertPattern('/Digest/', $header);
  69. preg_match('/response="(.*?)"/', $header, $matches);
  70. list($match, $response) = $matches;
  71. $expected = $hash;
  72. $result = $response;
  73. $this->assertEqual($expected, $result);
  74. }
  75. public function testDecode() {
  76. $header = 'qop="auth",nonce="4bca0fbca7bd0",';
  77. $header .= 'nc="00000001",cnonce="95b2cd1e179bf5414e52ed62811481cf",';
  78. $header .= 'uri="/http_auth",realm="app",';
  79. $header .= 'opaque="d3fb67a7aa4d887ec4bf83040a820a46",username="gwoo",';
  80. $header .= 'response="04d7d878c67f289f37e553d2025e3a52"';
  81. $expected = array(
  82. 'qop' => 'auth', 'nonce' => '4bca0fbca7bd0',
  83. 'nc' => '00000001', 'cnonce' => '95b2cd1e179bf5414e52ed62811481cf',
  84. 'uri' => '/http_auth', 'realm' => 'app',
  85. 'opaque' => 'd3fb67a7aa4d887ec4bf83040a820a46', 'username' => 'gwoo',
  86. 'response' => '04d7d878c67f289f37e553d2025e3a52'
  87. );
  88. $result = Auth::decode($header);
  89. $this->assertEqual($expected, $result);
  90. }
  91. }
  92. ?>