CredentialsAbstract.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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-2009 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: SharedKeyCredentials.php 14561 2009-05-07 08:05:12Z unknown $
  20. */
  21. /**
  22. * @see Zend_Http_Client
  23. */
  24. require_once 'Zend/Http/Client.php';
  25. /**
  26. * @category Zend
  27. * @package Zend_Service_WindowsAzure
  28. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. */
  31. abstract class Zend_Service_WindowsAzure_Credentials_CredentialsAbstract
  32. {
  33. /**
  34. * Development storage account and key
  35. */
  36. const DEVSTORE_ACCOUNT = "devstoreaccount1";
  37. const DEVSTORE_KEY = "Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==";
  38. /**
  39. * HTTP header prefixes
  40. */
  41. const PREFIX_PROPERTIES = "x-ms-prop-";
  42. const PREFIX_METADATA = "x-ms-meta-";
  43. const PREFIX_STORAGE_HEADER = "x-ms-";
  44. /**
  45. * Permissions
  46. */
  47. const PERMISSION_READ = "r";
  48. const PERMISSION_WRITE = "w";
  49. const PERMISSION_DELETE = "d";
  50. const PERMISSION_LIST = "l";
  51. /**
  52. * Account name for Windows Azure
  53. *
  54. * @var string
  55. */
  56. protected $_accountName = '';
  57. /**
  58. * Account key for Windows Azure
  59. *
  60. * @var string
  61. */
  62. protected $_accountKey = '';
  63. /**
  64. * Use path-style URI's
  65. *
  66. * @var boolean
  67. */
  68. protected $_usePathStyleUri = false;
  69. /**
  70. * Creates a new Zend_Service_WindowsAzure_Credentials_CredentialsAbstract instance
  71. *
  72. * @param string $accountName Account name for Windows Azure
  73. * @param string $accountKey Account key for Windows Azure
  74. * @param boolean $usePathStyleUri Use path-style URI's
  75. */
  76. public function __construct(
  77. $accountName = Zend_Service_WindowsAzure_Credentials_CredentialsAbstract::DEVSTORE_ACCOUNT,
  78. $accountKey = Zend_Service_WindowsAzure_Credentials_CredentialsAbstract::DEVSTORE_KEY,
  79. $usePathStyleUri = false
  80. ) {
  81. $this->_accountName = $accountName;
  82. $this->_accountKey = base64_decode($accountKey);
  83. $this->_usePathStyleUri = $usePathStyleUri;
  84. }
  85. /**
  86. * Set account name for Windows Azure
  87. *
  88. * @param string $value
  89. * @return Zend_Service_WindowsAzure_Credentials_CredentialsAbstract
  90. */
  91. public function setAccountName($value = Zend_Service_WindowsAzure_Credentials_CredentialsAbstract::DEVSTORE_ACCOUNT)
  92. {
  93. $this->_accountName = $value;
  94. return $this;
  95. }
  96. /**
  97. * Set account key for Windows Azure
  98. *
  99. * @param string $value
  100. * @return Zend_Service_WindowsAzure_Credentials_CredentialsAbstract
  101. */
  102. public function setAccountkey($value = Zend_Service_WindowsAzure_Credentials_CredentialsAbstract::DEVSTORE_KEY)
  103. {
  104. $this->_accountKey = base64_decode($value);
  105. return $this;
  106. }
  107. /**
  108. * Set use path-style URI's
  109. *
  110. * @param boolean $value
  111. * @return Zend_Service_WindowsAzure_Credentials_CredentialsAbstract
  112. */
  113. public function setUsePathStyleUri($value = false)
  114. {
  115. $this->_usePathStyleUri = $value;
  116. return $this;
  117. }
  118. /**
  119. * Sign request URL with credentials
  120. *
  121. * @param string $requestUrl Request URL
  122. * @param string $resourceType Resource type
  123. * @param string $requiredPermission Required permission
  124. * @return string Signed request URL
  125. */
  126. abstract public function signRequestUrl(
  127. $requestUrl = '',
  128. $resourceType = Zend_Service_WindowsAzure_Storage::RESOURCE_UNKNOWN,
  129. $requiredPermission = Zend_Service_WindowsAzure_Credentials_CredentialsAbstract::PERMISSION_READ
  130. );
  131. /**
  132. * Sign request headers with credentials
  133. *
  134. * @param string $httpVerb HTTP verb the request will use
  135. * @param string $path Path for the request
  136. * @param string $queryString Query string for the request
  137. * @param array $headers x-ms headers to add
  138. * @param boolean $forTableStorage Is the request for table storage?
  139. * @param string $resourceType Resource type
  140. * @param string $requiredPermission Required permission
  141. * @return array Array of headers
  142. */
  143. abstract public function signRequestHeaders(
  144. $httpVerb = Zend_Http_Client::GET,
  145. $path = '/',
  146. $queryString = '',
  147. $headers = null,
  148. $forTableStorage = false,
  149. $resourceType = Zend_Service_WindowsAzure_Storage::RESOURCE_UNKNOWN,
  150. $requiredPermission = Zend_Service_WindowsAzure_Credentials_CredentialsAbstract::PERMISSION_READ
  151. );
  152. /**
  153. * Prepare query string for signing
  154. *
  155. * @param string $value Original query string
  156. * @return string Query string for signing
  157. */
  158. protected function _prepareQueryStringForSigning($value)
  159. {
  160. // Check for 'comp='
  161. if (strpos($value, 'comp=') === false) {
  162. // If not found, no query string needed
  163. return '';
  164. } else {
  165. // If found, make sure it is the only parameter being used
  166. if (strlen($value) > 0 && strpos($value, '?') === 0) {
  167. $value = substr($value, 1);
  168. }
  169. // Split parts
  170. $queryParts = explode('&', $value);
  171. foreach ($queryParts as $queryPart) {
  172. if (strpos($queryPart, 'comp=') !== false) {
  173. return '?' . $queryPart;
  174. }
  175. }
  176. // Should never happen...
  177. return '';
  178. }
  179. }
  180. }