PageRenderTime 52ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/tests/Sabre/HTTP/AWSAuthTest.php

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