PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Service/WindowsAzure/Credentials/SharedKey.php

https://bitbucket.org/luizbrandaoj/mini-blog
PHP | 187 lines | 96 code | 21 blank | 70 comment | 19 complexity | cc4fbd28d67b1d4de31130008edfaca6 MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Service_WindowsAzure
  17. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: SharedKey.php 23775 2011-03-01 17:25:24Z ralph $
  20. */
  21. /**
  22. * @see Zend_Service_WindowsAzure_Credentials_CredentialsAbstract
  23. */
  24. require_once 'Zend/Service/WindowsAzure/Credentials/CredentialsAbstract.php';
  25. /**
  26. * @see Zend_Service_WindowsAzure_Storage
  27. */
  28. require_once 'Zend/Service/WindowsAzure/Storage.php';
  29. /**
  30. * @see Zend_Http_Client
  31. */
  32. require_once 'Zend/Http/Client.php';
  33. /**
  34. * @see Zend_Service_WindowsAzure_Credentials_Exception
  35. */
  36. require_once 'Zend/Service/WindowsAzure/Credentials/Exception.php';
  37. /**
  38. * @category Zend
  39. * @package Zend_Service_WindowsAzure
  40. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  41. * @license http://framework.zend.com/license/new-bsd New BSD License
  42. */
  43. class Zend_Service_WindowsAzure_Credentials_SharedKey
  44. extends Zend_Service_WindowsAzure_Credentials_CredentialsAbstract
  45. {
  46. /**
  47. * Sign request URL with credentials
  48. *
  49. * @param string $requestUrl Request URL
  50. * @param string $resourceType Resource type
  51. * @param string $requiredPermission Required permission
  52. * @return string Signed request URL
  53. */
  54. public function signRequestUrl(
  55. $requestUrl = '',
  56. $resourceType = Zend_Service_WindowsAzure_Storage::RESOURCE_UNKNOWN,
  57. $requiredPermission = Zend_Service_WindowsAzure_Credentials_CredentialsAbstract::PERMISSION_READ
  58. ) {
  59. return $requestUrl;
  60. }
  61. /**
  62. * Sign request headers with credentials
  63. *
  64. * @param string $httpVerb HTTP verb the request will use
  65. * @param string $path Path for the request
  66. * @param string $queryString Query string for the request
  67. * @param array $headers x-ms headers to add
  68. * @param boolean $forTableStorage Is the request for table storage?
  69. * @param string $resourceType Resource type
  70. * @param string $requiredPermission Required permission
  71. * @param mixed $rawData Raw post data
  72. * @return array Array of headers
  73. */
  74. public function signRequestHeaders(
  75. $httpVerb = Zend_Http_Client::GET,
  76. $path = '/',
  77. $queryString = '',
  78. $headers = null,
  79. $forTableStorage = false,
  80. $resourceType = Zend_Service_WindowsAzure_Storage::RESOURCE_UNKNOWN,
  81. $requiredPermission = Zend_Service_WindowsAzure_Credentials_CredentialsAbstract::PERMISSION_READ,
  82. $rawData = null
  83. ) {
  84. // http://github.com/sriramk/winazurestorage/blob/214010a2f8931bac9c96dfeb337d56fe084ca63b/winazurestorage.py
  85. // Table storage?
  86. if ($forTableStorage) {
  87. throw new Zend_Service_WindowsAzure_Credentials_Exception('The Windows Azure SDK for PHP does not support SharedKey authentication on table storage. Use SharedKeyLite authentication instead.');
  88. }
  89. // Determine path
  90. if ($this->_usePathStyleUri) {
  91. $path = substr($path, strpos($path, '/'));
  92. }
  93. // Determine query
  94. $queryString = $this->_prepareQueryStringForSigning($queryString);
  95. // Canonicalized headers
  96. $canonicalizedHeaders = array();
  97. // Request date
  98. $requestDate = '';
  99. if (isset($headers[Zend_Service_WindowsAzure_Credentials_CredentialsAbstract::PREFIX_STORAGE_HEADER . 'date'])) {
  100. $requestDate = $headers[Zend_Service_WindowsAzure_Credentials_CredentialsAbstract::PREFIX_STORAGE_HEADER . 'date'];
  101. } else {
  102. $requestDate = gmdate('D, d M Y H:i:s', time()) . ' GMT'; // RFC 1123
  103. $canonicalizedHeaders[] = Zend_Service_WindowsAzure_Credentials_CredentialsAbstract::PREFIX_STORAGE_HEADER . 'date:' . $requestDate;
  104. }
  105. // Build canonicalized headers
  106. if ($headers !== null) {
  107. foreach ($headers as $header => $value) {
  108. if (is_bool($value)) {
  109. $value = $value === true ? 'True' : 'False';
  110. }
  111. $headers[$header] = $value;
  112. if (substr($header, 0, strlen(Zend_Service_WindowsAzure_Credentials_CredentialsAbstract::PREFIX_STORAGE_HEADER)) == Zend_Service_WindowsAzure_Credentials_CredentialsAbstract::PREFIX_STORAGE_HEADER) {
  113. $canonicalizedHeaders[] = strtolower($header) . ':' . $value;
  114. }
  115. }
  116. }
  117. sort($canonicalizedHeaders);
  118. // Build canonicalized resource string
  119. $canonicalizedResource = '/' . $this->_accountName;
  120. if ($this->_usePathStyleUri) {
  121. $canonicalizedResource .= '/' . $this->_accountName;
  122. }
  123. $canonicalizedResource .= $path;
  124. if ($queryString !== '') {
  125. $queryStringItems = $this->_makeArrayOfQueryString($queryString);
  126. foreach ($queryStringItems as $key => $value) {
  127. $canonicalizedResource .= "\n" . strtolower($key) . ':' . $value;
  128. }
  129. }
  130. // Content-Length header
  131. $contentLength = '';
  132. if (strtoupper($httpVerb) != Zend_Http_Client::GET
  133. && strtoupper($httpVerb) != Zend_Http_Client::DELETE
  134. && strtoupper($httpVerb) != Zend_Http_Client::HEAD) {
  135. $contentLength = 0;
  136. if ($rawData !== null) {
  137. $contentLength = strlen($rawData);
  138. }
  139. }
  140. // Create string to sign
  141. $stringToSign = array();
  142. $stringToSign[] = strtoupper($httpVerb); // VERB
  143. $stringToSign[] = $this->_issetOr($headers, 'Content-Encoding', ''); // Content-Encoding
  144. $stringToSign[] = $this->_issetOr($headers, 'Content-Language', ''); // Content-Language
  145. $stringToSign[] = $contentLength; // Content-Length
  146. $stringToSign[] = $this->_issetOr($headers, 'Content-MD5', ''); // Content-MD5
  147. $stringToSign[] = $this->_issetOr($headers, 'Content-Type', ''); // Content-Type
  148. $stringToSign[] = ""; // Date
  149. $stringToSign[] = $this->_issetOr($headers, 'If-Modified-Since', ''); // If-Modified-Since
  150. $stringToSign[] = $this->_issetOr($headers, 'If-Match', ''); // If-Match
  151. $stringToSign[] = $this->_issetOr($headers, 'If-None-Match', ''); // If-None-Match
  152. $stringToSign[] = $this->_issetOr($headers, 'If-Unmodified-Since', ''); // If-Unmodified-Since
  153. $stringToSign[] = $this->_issetOr($headers, 'Range', ''); // Range
  154. if (!$forTableStorage && count($canonicalizedHeaders) > 0) {
  155. $stringToSign[] = implode("\n", $canonicalizedHeaders); // Canonicalized headers
  156. }
  157. $stringToSign[] = $canonicalizedResource; // Canonicalized resource
  158. $stringToSign = implode("\n", $stringToSign);
  159. $signString = base64_encode(hash_hmac('sha256', $stringToSign, $this->_accountKey, true));
  160. // Sign request
  161. $headers[Zend_Service_WindowsAzure_Credentials_CredentialsAbstract::PREFIX_STORAGE_HEADER . 'date'] = $requestDate;
  162. $headers['Authorization'] = 'SharedKey ' . $this->_accountName . ':' . $signString;
  163. // Return headers
  164. return $headers;
  165. }
  166. }