PageRenderTime 40ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/Microsoft/WindowsAzure/Credentials/SharedKey.php

https://bitbucket.org/ktos/tinyshare
PHP | 183 lines | 92 code | 18 blank | 73 comment | 19 complexity | 16d2752bd25197f5c3a5c78248fc82bf MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Copyright (c) 2009 - 2011, RealDolmen
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * * Neither the name of RealDolmen nor the
  14. * names of its contributors may be used to endorse or promote products
  15. * derived from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY RealDolmen ''AS IS'' AND ANY
  18. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. * DISCLAIMED. IN NO EVENT SHALL RealDolmen BE LIABLE FOR ANY
  21. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  24. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  26. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. *
  28. * @category Microsoft
  29. * @package Microsoft_WindowsAzure
  30. * @copyright Copyright (c) 2009 - 2011, RealDolmen (http://www.realdolmen.com)
  31. * @license http://phpazure.codeplex.com/license
  32. * @version $Id$
  33. */
  34. /**
  35. * @see Microsoft_AutoLoader
  36. */
  37. require_once dirname(__FILE__) . '/../../AutoLoader.php';
  38. /**
  39. * @category Microsoft
  40. * @package Microsoft_WindowsAzure
  41. * @copyright Copyright (c) 2009 - 2011, RealDolmen (http://www.realdolmen.com)
  42. * @license http://phpazure.codeplex.com/license
  43. */
  44. class Microsoft_WindowsAzure_Credentials_SharedKey
  45. extends Microsoft_WindowsAzure_Credentials_CredentialsAbstract
  46. {
  47. /**
  48. * Sign request URL with credentials
  49. *
  50. * @param string $requestUrl Request URL
  51. * @param string $resourceType Resource type
  52. * @param string $requiredPermission Required permission
  53. * @return string Signed request URL
  54. */
  55. public function signRequestUrl(
  56. $requestUrl = '',
  57. $resourceType = Microsoft_WindowsAzure_Storage::RESOURCE_UNKNOWN,
  58. $requiredPermission = Microsoft_WindowsAzure_Credentials_CredentialsAbstract::PERMISSION_READ
  59. ) {
  60. return $requestUrl;
  61. }
  62. /**
  63. * Sign request headers with credentials
  64. *
  65. * @param string $httpVerb HTTP verb the request will use
  66. * @param string $path Path for the request
  67. * @param array $query Query arguments for the request (key/value pairs)
  68. * @param array $headers x-ms headers to add
  69. * @param boolean $forTableStorage Is the request for table storage?
  70. * @param string $resourceType Resource type
  71. * @param string $requiredPermission Required permission
  72. * @param mixed $rawData Raw post data
  73. * @return array Array of headers
  74. */
  75. public function signRequestHeaders(
  76. $httpVerb = Microsoft_Http_Client::GET,
  77. $path = '/',
  78. $query = array(),
  79. $headers = null,
  80. $forTableStorage = false,
  81. $resourceType = Microsoft_WindowsAzure_Storage::RESOURCE_UNKNOWN,
  82. $requiredPermission = Microsoft_WindowsAzure_Credentials_CredentialsAbstract::PERMISSION_READ,
  83. $rawData = null
  84. ) {
  85. // http://github.com/sriramk/winazurestorage/blob/214010a2f8931bac9c96dfeb337d56fe084ca63b/winazurestorage.py
  86. // Table storage?
  87. if ($forTableStorage) {
  88. throw new Microsoft_WindowsAzure_Credentials_Exception('The Windows Azure SDK for PHP does not support SharedKey authentication on table storage. Use SharedKeyLite authentication instead.');
  89. }
  90. // Determine path
  91. if ($this->_usePathStyleUri) {
  92. $path = substr($path, strpos($path, '/'));
  93. }
  94. // Canonicalized headers
  95. $canonicalizedHeaders = array();
  96. // Request date
  97. $requestDate = '';
  98. if (isset($headers[Microsoft_WindowsAzure_Credentials_CredentialsAbstract::PREFIX_STORAGE_HEADER . 'date'])) {
  99. $requestDate = $headers[Microsoft_WindowsAzure_Credentials_CredentialsAbstract::PREFIX_STORAGE_HEADER . 'date'];
  100. } else {
  101. $requestDate = gmdate('D, d M Y H:i:s', time()) . ' GMT'; // RFC 1123
  102. $canonicalizedHeaders[] = Microsoft_WindowsAzure_Credentials_CredentialsAbstract::PREFIX_STORAGE_HEADER . 'date:' . $requestDate;
  103. }
  104. // Build canonicalized headers
  105. if (!is_null($headers)) {
  106. foreach ($headers as $header => $value) {
  107. if (is_bool($value)) {
  108. $value = $value === true ? 'True' : 'False';
  109. }
  110. $headers[$header] = $value;
  111. if (substr($header, 0, strlen(Microsoft_WindowsAzure_Credentials_CredentialsAbstract::PREFIX_STORAGE_HEADER)) == Microsoft_WindowsAzure_Credentials_CredentialsAbstract::PREFIX_STORAGE_HEADER) {
  112. $canonicalizedHeaders[] = strtolower($header) . ':' . $value;
  113. }
  114. }
  115. }
  116. sort($canonicalizedHeaders);
  117. // Build canonicalized resource string
  118. $canonicalizedResource = '/' . $this->_accountName;
  119. if ($this->_usePathStyleUri) {
  120. $canonicalizedResource .= '/' . $this->_accountName;
  121. }
  122. $canonicalizedResource .= $path;
  123. if (count($query) > 0) {
  124. ksort($query);
  125. foreach ($query as $key => $value) {
  126. $canonicalizedResource .= "\n" . strtolower($key) . ':' . rawurldecode($value);
  127. }
  128. }
  129. // Content-Length header
  130. $contentLength = '';
  131. if (strtoupper($httpVerb) != Microsoft_Http_Client::GET
  132. && strtoupper($httpVerb) != Microsoft_Http_Client::DELETE
  133. && strtoupper($httpVerb) != Microsoft_Http_Client::HEAD) {
  134. $contentLength = 0;
  135. if (!is_null($rawData)) {
  136. $contentLength = strlen($rawData);
  137. }
  138. }
  139. // Create string to sign
  140. $stringToSign = array();
  141. $stringToSign[] = strtoupper($httpVerb); // VERB
  142. $stringToSign[] = $this->_issetOr($headers, 'Content-Encoding', ''); // Content-Encoding
  143. $stringToSign[] = $this->_issetOr($headers, 'Content-Language', ''); // Content-Language
  144. $stringToSign[] = $contentLength; // Content-Length
  145. $stringToSign[] = $this->_issetOr($headers, 'Content-MD5', ''); // Content-MD5
  146. $stringToSign[] = $this->_issetOr($headers, 'Content-Type', ''); // Content-Type
  147. $stringToSign[] = ""; // Date
  148. $stringToSign[] = $this->_issetOr($headers, 'If-Modified-Since', ''); // If-Modified-Since
  149. $stringToSign[] = $this->_issetOr($headers, 'If-Match', ''); // If-Match
  150. $stringToSign[] = $this->_issetOr($headers, 'If-None-Match', ''); // If-None-Match
  151. $stringToSign[] = $this->_issetOr($headers, 'If-Unmodified-Since', ''); // If-Unmodified-Since
  152. $stringToSign[] = $this->_issetOr($headers, 'Range', ''); // Range
  153. if (!$forTableStorage && count($canonicalizedHeaders) > 0) {
  154. $stringToSign[] = implode("\n", $canonicalizedHeaders); // Canonicalized headers
  155. }
  156. $stringToSign[] = $canonicalizedResource; // Canonicalized resource
  157. $stringToSign = implode("\n", $stringToSign);
  158. $signString = base64_encode(hash_hmac('sha256', $stringToSign, $this->_accountKey, true));
  159. // Sign request
  160. $headers[Microsoft_WindowsAzure_Credentials_CredentialsAbstract::PREFIX_STORAGE_HEADER . 'date'] = $requestDate;
  161. $headers['Authorization'] = 'SharedKey ' . $this->_accountName . ':' . $signString;
  162. // Return headers
  163. return $headers;
  164. }
  165. }