AWSAuth.php 5.7 KB

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