/system/libraries/webshare/tests/Sabre/HTTP/AWSAuthTest.php

https://github.com/isS/Microweber · PHP · 237 lines · 160 code · 70 blank · 7 comment · 1 complexity · e84fd4fa0667218a2a9532cf39826f21 MD5 · raw file

  1. <?php
  2. require_once 'Sabre/HTTP/ResponseMock.php';
  3. class Sabre_HTTP_AWSAuthTest extends PHPUnit_Framework_TestCase {
  4. private $response;
  5. private $request;
  6. private $basicAuth;
  7. const REALM = 'SabreDAV unittest';
  8. public function setUp() {
  9. $this->response = new Sabre_HTTP_ResponseMock();
  10. $this->auth = new Sabre_HTTP_AWSAuth();
  11. $this->auth->setRealm(self::REALM);
  12. $this->auth->setHTTPResponse($this->response);
  13. }
  14. public function testNoHeader() {
  15. $request = new Sabre_HTTP_Request(array(
  16. 'REQUEST_METHOD' => 'GET',
  17. ));
  18. $this->auth->setHTTPRequest($request);
  19. $result = $this->auth->init();
  20. $this->assertFalse($result,'No AWS Authorization header was supplied, so we should have gotten false');
  21. $this->assertEquals(Sabre_HTTP_AWSAuth::ERR_NOAWSHEADER,$this->auth->errorCode);
  22. }
  23. public function testIncorrectContentMD5() {
  24. $accessKey = 'accessKey';
  25. $secretKey = 'secretKey';
  26. $request = new Sabre_HTTP_Request(array(
  27. 'REQUEST_METHOD' => 'GET',
  28. 'HTTP_AUTHORIZATION' => "AWS $accessKey:sig",
  29. 'HTTP_CONTENT_MD5' => 'garbage',
  30. 'REQUEST_URI' => '/',
  31. ));
  32. $this->auth->setHTTPRequest($request);
  33. $this->auth->init();
  34. $result = $this->auth->validate($secretKey);
  35. $this->assertFalse($result);
  36. $this->assertEquals(Sabre_HTTP_AWSAuth::ERR_MD5CHECKSUMWRONG,$this->auth->errorCode);
  37. }
  38. public function testNoDate() {
  39. $accessKey = 'accessKey';
  40. $secretKey = 'secretKey';
  41. $content = 'thisisthebody';
  42. $contentMD5 = base64_encode(md5($content,true));
  43. $request = new Sabre_HTTP_Request(array(
  44. 'REQUEST_METHOD' => 'POST',
  45. 'HTTP_AUTHORIZATION' => "AWS $accessKey:sig",
  46. 'HTTP_CONTENT_MD5' => $contentMD5,
  47. ));
  48. $request->setBody($content);
  49. $this->auth->setHTTPRequest($request);
  50. $this->auth->init();
  51. $result = $this->auth->validate($secretKey);
  52. $this->assertFalse($result);
  53. $this->assertEquals(Sabre_HTTP_AWSAuth::ERR_INVALIDDATEFORMAT,$this->auth->errorCode);
  54. }
  55. public function testFutureDate() {
  56. $accessKey = 'accessKey';
  57. $secretKey = 'secretKey';
  58. $content = 'thisisthebody';
  59. $contentMD5 = base64_encode(md5($content,true));
  60. $date = new DateTime('@' . time() + (60*20));
  61. $date->setTimeZone(new DateTimeZone('GMT'));
  62. $date = $date->format('D, d M Y H:i:s \\G\\M\\T');
  63. $request = new Sabre_HTTP_Request(array(
  64. 'REQUEST_METHOD' => 'POST',
  65. 'HTTP_AUTHORIZATION' => "AWS $accessKey:sig",
  66. 'HTTP_CONTENT_MD5' => $contentMD5,
  67. 'HTTP_DATE' => $date,
  68. ));
  69. $request->setBody($content);
  70. $this->auth->setHTTPRequest($request);
  71. $this->auth->init();
  72. $result = $this->auth->validate($secretKey);
  73. $this->assertFalse($result);
  74. $this->assertEquals(Sabre_HTTP_AWSAuth::ERR_REQUESTTIMESKEWED,$this->auth->errorCode);
  75. }
  76. public function testPastDate() {
  77. $accessKey = 'accessKey';
  78. $secretKey = 'secretKey';
  79. $content = 'thisisthebody';
  80. $contentMD5 = base64_encode(md5($content,true));
  81. $date = new DateTime('@' . time() - (60*20));
  82. $date->setTimeZone(new DateTimeZone('GMT'));
  83. $date = $date->format('D, d M Y H:i:s \\G\\M\\T');
  84. $request = new Sabre_HTTP_Request(array(
  85. 'REQUEST_METHOD' => 'POST',
  86. 'HTTP_AUTHORIZATION' => "AWS $accessKey:sig",
  87. 'HTTP_CONTENT_MD5' => $contentMD5,
  88. 'HTTP_X_AMZ_DATE' => $date,
  89. ));
  90. $request->setBody($content);
  91. $this->auth->setHTTPRequest($request);
  92. $this->auth->init();
  93. $result = $this->auth->validate($secretKey);
  94. $this->assertFalse($result);
  95. $this->assertEquals(Sabre_HTTP_AWSAuth::ERR_REQUESTTIMESKEWED,$this->auth->errorCode);
  96. }
  97. public function testIncorrectSignature() {
  98. $accessKey = 'accessKey';
  99. $secretKey = 'secretKey';
  100. $content = 'thisisthebody';
  101. $contentMD5 = base64_encode(md5($content,true));
  102. $date = new DateTime('now');
  103. $date->setTimeZone(new DateTimeZone('GMT'));
  104. $date = $date->format('D, d M Y H:i:s \\G\\M\\T');
  105. $request = new Sabre_HTTP_Request(array(
  106. 'REQUEST_METHOD' => 'POST',
  107. 'HTTP_AUTHORIZATION' => "AWS $accessKey:sig",
  108. 'HTTP_CONTENT_MD5' => $contentMD5,
  109. 'HTTP_X_AMZ_DATE' => $date,
  110. 'REQUEST_URI' => '/',
  111. ));
  112. $request->setBody($content);
  113. $this->auth->setHTTPRequest($request);
  114. $this->auth->init();
  115. $result = $this->auth->validate($secretKey);
  116. $this->assertFalse($result);
  117. $this->assertEquals(Sabre_HTTP_AWSAuth::ERR_INVALIDSIGNATURE,$this->auth->errorCode);
  118. }
  119. public function testValidRequest() {
  120. $accessKey = 'accessKey';
  121. $secretKey = 'secretKey';
  122. $content = 'thisisthebody';
  123. $contentMD5 = base64_encode(md5($content,true));
  124. $date = new DateTime('now');
  125. $date->setTimeZone(new DateTimeZone('GMT'));
  126. $date = $date->format('D, d M Y H:i:s \\G\\M\\T');
  127. $sig = base64_encode($this->hmacsha1($secretKey,
  128. "POST\n$contentMD5\n\n$date\nx-amz-date:$date\n/evert"
  129. ));
  130. $request = new Sabre_HTTP_Request(array(
  131. 'REQUEST_METHOD' => 'POST',
  132. 'HTTP_AUTHORIZATION' => "AWS $accessKey:$sig",
  133. 'HTTP_CONTENT_MD5' => $contentMD5,
  134. 'HTTP_X_AMZ_DATE' => $date,
  135. 'REQUEST_URI' => '/evert',
  136. ));
  137. $request->setBody($content);
  138. $this->auth->setHTTPRequest($request);
  139. $this->auth->init();
  140. $result = $this->auth->validate($secretKey);
  141. $this->assertTrue($result,'Signature did not validate, got errorcode ' . $this->auth->errorCode);
  142. $this->assertEquals($accessKey,$this->auth->getAccessKey());
  143. }
  144. public function test401() {
  145. $this->auth->requireLogin();
  146. $test = preg_match('/^AWS$/',$this->response->headers['WWW-Authenticate'],$matches);
  147. $this->assertTrue($test==true,'The WWW-Authenticate response didn\'t match our pattern');
  148. }
  149. /**
  150. * Generates an HMAC-SHA1 signature
  151. *
  152. * @param string $key
  153. * @param string $message
  154. * @return string
  155. */
  156. private function hmacsha1($key, $message) {
  157. $blocksize=64;
  158. if (strlen($key)>$blocksize)
  159. $key=pack('H*', sha1($key));
  160. $key=str_pad($key,$blocksize,chr(0x00));
  161. $ipad=str_repeat(chr(0x36),$blocksize);
  162. $opad=str_repeat(chr(0x5c),$blocksize);
  163. $hmac = pack('H*',sha1(($key^$opad).pack('H*',sha1(($key^$ipad).$message))));
  164. return $hmac;
  165. }
  166. }
  167. ?>