PageRenderTime 53ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/Sabre/HTTP/AWSAuth.php

https://github.com/KOLANICH/SabreDAV
PHP | 227 lines | 99 code | 49 blank | 79 comment | 13 complexity | c7c8a3309ff7828a0f6bdd2e4f8ac24a MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. namespace Sabre\HTTP;
  3. /**
  4. * HTTP AWS Authentication handler
  5. *
  6. * Use this class to leverage amazon's AWS authentication header
  7. *
  8. * @copyright Copyright (C) 2007-2013 fruux GmbH (https://fruux.com/).
  9. * @author Evert Pot (http://evertpot.com/)
  10. * @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
  11. */
  12. class AWSAuth extends AbstractAuth {
  13. /**
  14. * The signature supplied by the HTTP client
  15. *
  16. * @var string
  17. */
  18. private $signature = null;
  19. /**
  20. * The accesskey supplied by the HTTP client
  21. *
  22. * @var string
  23. */
  24. private $accessKey = null;
  25. /**
  26. * An error code, if any
  27. *
  28. * This value will be filled with one of the ERR_* constants
  29. *
  30. * @var int
  31. */
  32. public $errorCode = 0;
  33. const ERR_NOAWSHEADER = 1;
  34. const ERR_MD5CHECKSUMWRONG = 2;
  35. const ERR_INVALIDDATEFORMAT = 3;
  36. const ERR_REQUESTTIMESKEWED = 4;
  37. const ERR_INVALIDSIGNATURE = 5;
  38. /**
  39. * Gathers all information from the headers
  40. *
  41. * This method needs to be called prior to anything else.
  42. *
  43. * @return bool
  44. */
  45. public function init() {
  46. $authHeader = $this->httpRequest->getHeader('Authorization');
  47. $authHeader = explode(' ',$authHeader);
  48. if ($authHeader[0]!='AWS' || !isset($authHeader[1])) {
  49. $this->errorCode = self::ERR_NOAWSHEADER;
  50. return false;
  51. }
  52. list($this->accessKey,$this->signature) = explode(':',$authHeader[1]);
  53. return true;
  54. }
  55. /**
  56. * Returns the username for the request
  57. *
  58. * @return string
  59. */
  60. public function getAccessKey() {
  61. return $this->accessKey;
  62. }
  63. /**
  64. * Validates the signature based on the secretKey
  65. *
  66. * @param string $secretKey
  67. * @return bool
  68. */
  69. public function validate($secretKey) {
  70. $contentMD5 = $this->httpRequest->getHeader('Content-MD5');
  71. if ($contentMD5) {
  72. // We need to validate the integrity of the request
  73. $body = $this->httpRequest->getBody(true);
  74. $this->httpRequest->setBody($body,true);
  75. if ($contentMD5!=base64_encode(md5($body,true))) {
  76. // content-md5 header did not match md5 signature of body
  77. $this->errorCode = self::ERR_MD5CHECKSUMWRONG;
  78. return false;
  79. }
  80. }
  81. if (!$requestDate = $this->httpRequest->getHeader('x-amz-date'))
  82. $requestDate = $this->httpRequest->getHeader('Date');
  83. if (!$this->validateRFC2616Date($requestDate))
  84. return false;
  85. $amzHeaders = $this->getAmzHeaders();
  86. $signature = base64_encode(
  87. $this->hmacsha1($secretKey,
  88. $this->httpRequest->getMethod() . "\n" .
  89. $contentMD5 . "\n" .
  90. $this->httpRequest->getHeader('Content-type') . "\n" .
  91. $requestDate . "\n" .
  92. $amzHeaders .
  93. $this->httpRequest->getURI()
  94. )
  95. );
  96. if ($this->signature != $signature) {
  97. $this->errorCode = self::ERR_INVALIDSIGNATURE;
  98. return false;
  99. }
  100. return true;
  101. }
  102. /**
  103. * Returns an HTTP 401 header, forcing login
  104. *
  105. * This should be called when username and password are incorrect, or not supplied at all
  106. *
  107. * @return void
  108. */
  109. public function requireLogin() {
  110. $this->httpResponse->setHeader('WWW-Authenticate','AWS');
  111. $this->httpResponse->sendStatus(401);
  112. }
  113. /**
  114. * Makes sure the supplied value is a valid RFC2616 date.
  115. *
  116. * If we would just use strtotime to get a valid timestamp, we have no way of checking if a
  117. * user just supplied the word 'now' for the date header.
  118. *
  119. * This function also makes sure the Date header is within 15 minutes of the operating
  120. * system date, to prevent replay attacks.
  121. *
  122. * @param string $dateHeader
  123. * @return bool
  124. */
  125. protected function validateRFC2616Date($dateHeader) {
  126. $date = Util::parseHTTPDate($dateHeader);
  127. // Unknown format
  128. if (!$date) {
  129. $this->errorCode = self::ERR_INVALIDDATEFORMAT;
  130. return false;
  131. }
  132. $min = new \DateTime('-15 minutes');
  133. $max = new \DateTime('+15 minutes');
  134. // We allow 15 minutes around the current date/time
  135. if ($date > $max || $date < $min) {
  136. $this->errorCode = self::ERR_REQUESTTIMESKEWED;
  137. return false;
  138. }
  139. return $date;
  140. }
  141. /**
  142. * Returns a list of AMZ headers
  143. *
  144. * @return string
  145. */
  146. protected function getAmzHeaders() {
  147. $amzHeaders = array();
  148. $headers = $this->httpRequest->getHeaders();
  149. foreach($headers as $headerName => $headerValue) {
  150. if (strpos(strtolower($headerName),'x-amz-')===0) {
  151. $amzHeaders[strtolower($headerName)] = str_replace(array("\r\n"),array(' '),$headerValue) . "\n";
  152. }
  153. }
  154. ksort($amzHeaders);
  155. $headerStr = '';
  156. foreach($amzHeaders as $h=>$v) {
  157. $headerStr.=$h.':'.$v;
  158. }
  159. return $headerStr;
  160. }
  161. /**
  162. * Generates an HMAC-SHA1 signature
  163. *
  164. * @param string $key
  165. * @param string $message
  166. * @return string
  167. */
  168. private function hmacsha1($key, $message) {
  169. $blocksize=64;
  170. if (strlen($key)>$blocksize)
  171. $key=pack('H*', sha1($key));
  172. $key=str_pad($key,$blocksize,chr(0x00));
  173. $ipad=str_repeat(chr(0x36),$blocksize);
  174. $opad=str_repeat(chr(0x5c),$blocksize);
  175. $hmac = pack('H*',sha1(($key^$opad).pack('H*',sha1(($key^$ipad).$message))));
  176. return $hmac;
  177. }
  178. }