PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/CoreVersions/0.2.0/Frameworks/Versions/SabreDAV.1.5.7-stable/lib/Sabre/HTTP/AWSAuth.php

http://github.com/jeromeschneider/Baikal
PHP | 226 lines | 98 code | 48 blank | 80 comment | 13 complexity | 2310e298e9162a34edef2b46e43a638e MD5 | raw file
Possible License(s): GPL-3.0, BSD-3-Clause
  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_* contants
  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. * @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 = Sabre_HTTP_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 void
  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. }