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

https://github.com/grjones/qframe · PHP · 166 lines · 73 code · 16 blank · 77 comment · 11 complexity · 543002f14adf2d8b6d4910d90eaf0a4f 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$
  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_Service_WindowsAzure_Credentials_SharedKey
  31. */
  32. require_once 'Zend/Service/WindowsAzure/Credentials/SharedKey.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-2010 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_SharedKeyLite
  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. // Table storage?
  85. if (!$forTableStorage) {
  86. throw new Zend_Service_WindowsAzure_Credentials_Exception('The Windows Azure SDK for PHP does not support SharedKeyLite authentication on blob or queue storage. Use SharedKey authentication instead.');
  87. }
  88. // Determine path
  89. if ($this->_usePathStyleUri) {
  90. $path = substr($path, strpos($path, '/'));
  91. }
  92. // Determine query
  93. $queryString = $this->_prepareQueryStringForSigning($queryString);
  94. // Build canonicalized resource string
  95. $canonicalizedResource = '/' . $this->_accountName;
  96. if ($this->_usePathStyleUri) {
  97. $canonicalizedResource .= '/' . $this->_accountName;
  98. }
  99. $canonicalizedResource .= $path;
  100. if ($queryString !== '') {
  101. $canonicalizedResource .= $queryString;
  102. }
  103. // Request date
  104. $requestDate = '';
  105. if (isset($headers[Zend_Service_WindowsAzure_Credentials_CredentialsAbstract::PREFIX_STORAGE_HEADER . 'date'])) {
  106. $requestDate = $headers[Zend_Service_WindowsAzure_Credentials_CredentialsAbstract::PREFIX_STORAGE_HEADER . 'date'];
  107. } else {
  108. $requestDate = gmdate('D, d M Y H:i:s', time()) . ' GMT'; // RFC 1123
  109. }
  110. // Create string to sign
  111. $stringToSign = array();
  112. $stringToSign[] = $requestDate; // Date
  113. $stringToSign[] = $canonicalizedResource; // Canonicalized resource
  114. $stringToSign = implode("\n", $stringToSign);
  115. $signString = base64_encode(hash_hmac('sha256', $stringToSign, $this->_accountKey, true));
  116. // Sign request
  117. $headers[Zend_Service_WindowsAzure_Credentials_CredentialsAbstract::PREFIX_STORAGE_HEADER . 'date'] = $requestDate;
  118. $headers['Authorization'] = 'SharedKeyLite ' . $this->_accountName . ':' . $signString;
  119. // Return headers
  120. return $headers;
  121. }
  122. /**
  123. * Prepare query string for signing
  124. *
  125. * @param string $value Original query string
  126. * @return string Query string for signing
  127. */
  128. protected function _prepareQueryStringForSigning($value)
  129. {
  130. // Check for 'comp='
  131. if (strpos($value, 'comp=') === false) {
  132. // If not found, no query string needed
  133. return '';
  134. } else {
  135. // If found, make sure it is the only parameter being used
  136. if (strlen($value) > 0 && strpos($value, '?') === 0) {
  137. $value = substr($value, 1);
  138. }
  139. // Split parts
  140. $queryParts = explode('&', $value);
  141. foreach ($queryParts as $queryPart) {
  142. if (strpos($queryPart, 'comp=') !== false) {
  143. return '?' . $queryPart;
  144. }
  145. }
  146. // Should never happen...
  147. return '';
  148. }
  149. }
  150. }