PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/Service/WindowsAzure/Credentials/SharedKey.php

https://bitbucket.org/bigstylee/zend-framework
PHP | 173 lines | 94 code | 18 blank | 61 comment | 19 complexity | f058a3df2f6a70dd8a34f9c0f3629b04 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-2012 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 24593 2012-01-05 20:35:02Z matthew $
  20. */
  21. /**
  22. * @see Zend_Service_WindowsAzure_Credentials_CredentialsAbstract
  23. */
  24. require_once 'Zend/Service/WindowsAzure/Credentials/CredentialsAbstract.php';
  25. /**
  26. * @category Zend
  27. * @package Zend_Service_WindowsAzure
  28. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. */
  31. class Zend_Service_WindowsAzure_Credentials_SharedKey
  32. extends Zend_Service_WindowsAzure_Credentials_CredentialsAbstract
  33. {
  34. /**
  35. * Sign request URL with credentials
  36. *
  37. * @param string $requestUrl Request URL
  38. * @param string $resourceType Resource type
  39. * @param string $requiredPermission Required permission
  40. * @return string Signed request URL
  41. */
  42. public function signRequestUrl(
  43. $requestUrl = '',
  44. $resourceType = Zend_Service_WindowsAzure_Storage::RESOURCE_UNKNOWN,
  45. $requiredPermission = Zend_Service_WindowsAzure_Credentials_CredentialsAbstract::PERMISSION_READ
  46. ) {
  47. return $requestUrl;
  48. }
  49. /**
  50. * Sign request headers with credentials
  51. *
  52. * @param string $httpVerb HTTP verb the request will use
  53. * @param string $path Path for the request
  54. * @param string $queryString Query string for the request
  55. * @param array $headers x-ms headers to add
  56. * @param boolean $forTableStorage Is the request for table storage?
  57. * @param string $resourceType Resource type
  58. * @param string $requiredPermission Required permission
  59. * @param mixed $rawData Raw post data
  60. * @return array Array of headers
  61. */
  62. public function signRequestHeaders(
  63. $httpVerb = Zend_Http_Client::GET,
  64. $path = '/',
  65. $queryString = '',
  66. $headers = null,
  67. $forTableStorage = false,
  68. $resourceType = Zend_Service_WindowsAzure_Storage::RESOURCE_UNKNOWN,
  69. $requiredPermission = Zend_Service_WindowsAzure_Credentials_CredentialsAbstract::PERMISSION_READ,
  70. $rawData = null
  71. ) {
  72. // http://github.com/sriramk/winazurestorage/blob/214010a2f8931bac9c96dfeb337d56fe084ca63b/winazurestorage.py
  73. // Table storage?
  74. if ($forTableStorage) {
  75. require_once 'Zend/Service/WindowsAzure/Credentials/Exception.php';
  76. 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.');
  77. }
  78. // Determine path
  79. if ($this->_usePathStyleUri) {
  80. $path = substr($path, strpos($path, '/'));
  81. }
  82. // Determine query
  83. $queryString = $this->_prepareQueryStringForSigning($queryString);
  84. // Canonicalized headers
  85. $canonicalizedHeaders = array();
  86. // Request date
  87. $requestDate = '';
  88. if (isset($headers[Zend_Service_WindowsAzure_Credentials_CredentialsAbstract::PREFIX_STORAGE_HEADER . 'date'])) {
  89. $requestDate = $headers[Zend_Service_WindowsAzure_Credentials_CredentialsAbstract::PREFIX_STORAGE_HEADER . 'date'];
  90. } else {
  91. $requestDate = gmdate('D, d M Y H:i:s', time()) . ' GMT'; // RFC 1123
  92. $canonicalizedHeaders[] = Zend_Service_WindowsAzure_Credentials_CredentialsAbstract::PREFIX_STORAGE_HEADER . 'date:' . $requestDate;
  93. }
  94. // Build canonicalized headers
  95. if (!is_null($headers)) {
  96. foreach ($headers as $header => $value) {
  97. if (is_bool($value)) {
  98. $value = $value === true ? 'True' : 'False';
  99. }
  100. $headers[$header] = $value;
  101. if (substr($header, 0, strlen(Zend_Service_WindowsAzure_Credentials_CredentialsAbstract::PREFIX_STORAGE_HEADER)) == Zend_Service_WindowsAzure_Credentials_CredentialsAbstract::PREFIX_STORAGE_HEADER) {
  102. $canonicalizedHeaders[] = strtolower($header) . ':' . $value;
  103. }
  104. }
  105. }
  106. sort($canonicalizedHeaders);
  107. // Build canonicalized resource string
  108. $canonicalizedResource = '/' . $this->_accountName;
  109. if ($this->_usePathStyleUri) {
  110. $canonicalizedResource .= '/' . $this->_accountName;
  111. }
  112. $canonicalizedResource .= $path;
  113. if ($queryString !== '') {
  114. $queryStringItems = $this->_makeArrayOfQueryString($queryString);
  115. foreach ($queryStringItems as $key => $value) {
  116. $canonicalizedResource .= "\n" . strtolower($key) . ':' . urldecode($value);
  117. }
  118. }
  119. // Content-Length header
  120. $contentLength = '';
  121. if (strtoupper($httpVerb) != Zend_Http_Client::GET
  122. && strtoupper($httpVerb) != Zend_Http_Client::DELETE
  123. && strtoupper($httpVerb) != Zend_Http_Client::HEAD) {
  124. $contentLength = 0;
  125. if (!is_null($rawData)) {
  126. $contentLength = strlen($rawData);
  127. }
  128. }
  129. // Create string to sign
  130. $stringToSign = array();
  131. $stringToSign[] = strtoupper($httpVerb); // VERB
  132. $stringToSign[] = $this->_issetOr($headers, 'Content-Encoding', ''); // Content-Encoding
  133. $stringToSign[] = $this->_issetOr($headers, 'Content-Language', ''); // Content-Language
  134. $stringToSign[] = $contentLength; // Content-Length
  135. $stringToSign[] = $this->_issetOr($headers, 'Content-MD5', ''); // Content-MD5
  136. $stringToSign[] = $this->_issetOr($headers, 'Content-Type', ''); // Content-Type
  137. $stringToSign[] = ""; // Date
  138. $stringToSign[] = $this->_issetOr($headers, 'If-Modified-Since', ''); // If-Modified-Since
  139. $stringToSign[] = $this->_issetOr($headers, 'If-Match', ''); // If-Match
  140. $stringToSign[] = $this->_issetOr($headers, 'If-None-Match', ''); // If-None-Match
  141. $stringToSign[] = $this->_issetOr($headers, 'If-Unmodified-Since', ''); // If-Unmodified-Since
  142. $stringToSign[] = $this->_issetOr($headers, 'Range', ''); // Range
  143. if (!$forTableStorage && count($canonicalizedHeaders) > 0) {
  144. $stringToSign[] = implode("\n", $canonicalizedHeaders); // Canonicalized headers
  145. }
  146. $stringToSign[] = $canonicalizedResource; // Canonicalized resource
  147. $stringToSign = implode("\n", $stringToSign);
  148. $signString = base64_encode(hash_hmac('sha256', $stringToSign, $this->_accountKey, true));
  149. // Sign request
  150. $headers[Zend_Service_WindowsAzure_Credentials_CredentialsAbstract::PREFIX_STORAGE_HEADER . 'date'] = $requestDate;
  151. $headers['Authorization'] = 'SharedKey ' . $this->_accountName . ':' . $signString;
  152. // Return headers
  153. return $headers;
  154. }
  155. }