/Zend/Service/WindowsAzure/Credentials/SharedKeyLite.php

https://github.com/ftaiolivista/Zend-Framework-Namespaced- · PHP · 173 lines · 76 code · 17 blank · 80 comment · 11 complexity · d3256ee5fa44b7e7aa820541fa89d448 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-2010 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: SharedKeyLite.php 22773 2010-08-03 07:18:27Z maartenba $
  20. */
  21. /**
  22. * @namespace
  23. */
  24. namespace Zend\Service\WindowsAzure\Credentials;
  25. use Zend\Service\WindowsAzure\Storage;
  26. use Zend\Http\Client;
  27. /**
  28. * @see Zend_Service_WindowsAzure_Credentials_CredentialsAbstract
  29. */
  30. require_once 'Zend/Service/WindowsAzure/Credentials/CredentialsAbstract.php';
  31. /**
  32. * @see Zend_Service_WindowsAzure_Storage
  33. */
  34. require_once 'Zend/Service/WindowsAzure/Storage.php';
  35. /**
  36. * @see Zend_Service_WindowsAzure_Credentials_SharedKey
  37. */
  38. require_once 'Zend/Service/WindowsAzure/Credentials/SharedKey.php';
  39. /**
  40. * @see Zend_Service_WindowsAzure_Credentials_Exception
  41. */
  42. require_once 'Zend/Service/WindowsAzure/Credentials/Exception.php';
  43. /**
  44. * @category Zend
  45. * @package Zend_Service_WindowsAzure
  46. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  47. * @license http://framework.zend.com/license/new-bsd New BSD License
  48. */
  49. class SharedKeyLite
  50. extends CredentialsAbstract
  51. {
  52. /**
  53. * Sign request URL with credentials
  54. *
  55. * @param string $requestUrl Request URL
  56. * @param string $resourceType Resource type
  57. * @param string $requiredPermission Required permission
  58. * @return string Signed request URL
  59. */
  60. public function signRequestUrl(
  61. $requestUrl = '',
  62. $resourceType = Storage\StorageStorage\Storage::RESOURCE_UNKNOWN,
  63. $requiredPermission = CredentialsAbstractCredentialsAbstract::PERMISSION_READ
  64. ) {
  65. return $requestUrl;
  66. }
  67. /**
  68. * Sign request headers with credentials
  69. *
  70. * @param string $httpVerb HTTP verb the request will use
  71. * @param string $path Path for the request
  72. * @param string $queryString Query string for the request
  73. * @param array $headers x-ms headers to add
  74. * @param boolean $forTableStorage Is the request for table storage?
  75. * @param string $resourceType Resource type
  76. * @param string $requiredPermission Required permission
  77. * @param mixed $rawData Raw post data
  78. * @return array Array of headers
  79. */
  80. public function signRequestHeaders(
  81. $httpVerb = Client\ClientClient\Client::GET,
  82. $path = '/',
  83. $queryString = '',
  84. $headers = null,
  85. $forTableStorage = false,
  86. $resourceType = Storage\StorageStorage\Storage::RESOURCE_UNKNOWN,
  87. $requiredPermission = CredentialsAbstractCredentialsAbstract::PERMISSION_READ,
  88. $rawData = null
  89. ) {
  90. // Table storage?
  91. if (!$forTableStorage) {
  92. throw new Exception('The Windows Azure SDK for PHP does not support SharedKeyLite authentication on blob or queue storage. Use SharedKey authentication instead.');
  93. }
  94. // Determine path
  95. if ($this->_usePathStyleUri) {
  96. $path = substr($path, strpos($path, '/'));
  97. }
  98. // Determine query
  99. $queryString = $this->_prepareQueryStringForSigning($queryString);
  100. // Build canonicalized resource string
  101. $canonicalizedResource = '/' . $this->_accountName;
  102. if ($this->_usePathStyleUri) {
  103. $canonicalizedResource .= '/' . $this->_accountName;
  104. }
  105. $canonicalizedResource .= $path;
  106. if ($queryString !== '') {
  107. $canonicalizedResource .= $queryString;
  108. }
  109. // Request date
  110. $requestDate = '';
  111. if (isset($headers[CredentialsAbstract::PREFIX_STORAGE_HEADER . 'date'])) {
  112. $requestDate = $headers[CredentialsAbstract::PREFIX_STORAGE_HEADER . 'date'];
  113. } else {
  114. $requestDate = gmdate('D, d M Y H:i:s', time()) . ' GMT'; // RFC 1123
  115. }
  116. // Create string to sign
  117. $stringToSign = array();
  118. $stringToSign[] = $requestDate; // Date
  119. $stringToSign[] = $canonicalizedResource; // Canonicalized resource
  120. $stringToSign = implode("\n", $stringToSign);
  121. $signString = base64_encode(hash_hmac('sha256', $stringToSign, $this->_accountKey, true));
  122. // Sign request
  123. $headers[CredentialsAbstract::PREFIX_STORAGE_HEADER . 'date'] = $requestDate;
  124. $headers['Authorization'] = 'SharedKeyLite ' . $this->_accountName . ':' . $signString;
  125. // Return headers
  126. return $headers;
  127. }
  128. /**
  129. * Prepare query string for signing
  130. *
  131. * @param string $value Original query string
  132. * @return string Query string for signing
  133. */
  134. protected function _prepareQueryStringForSigning($value)
  135. {
  136. // Check for 'comp='
  137. if (strpos($value, 'comp=') === false) {
  138. // If not found, no query string needed
  139. return '';
  140. } else {
  141. // If found, make sure it is the only parameter being used
  142. if (strlen($value) > 0 && strpos($value, '?') === 0) {
  143. $value = substr($value, 1);
  144. }
  145. // Split parts
  146. $queryParts = explode('&', $value);
  147. foreach ($queryParts as $queryPart) {
  148. if (strpos($queryPart, 'comp=') !== false) {
  149. return '?' . $queryPart;
  150. }
  151. }
  152. // Should never happen...
  153. return '';
  154. }
  155. }
  156. }