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

https://github.com/timglabisch/pimcore · PHP · 152 lines · 69 code · 13 blank · 70 comment · 11 complexity · 00c57c0955830c31ce13f343d3f44725 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: SharedKeyLite.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_SharedKeyLite
  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. // Table storage?
  73. if (!$forTableStorage) {
  74. // require_once 'Zend/Service/WindowsAzure/Credentials/Exception.php';
  75. 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.');
  76. }
  77. // Determine path
  78. if ($this->_usePathStyleUri) {
  79. $path = substr($path, strpos($path, '/'));
  80. }
  81. // Determine query
  82. $queryString = $this->_prepareQueryStringForSigning($queryString);
  83. // Build canonicalized resource string
  84. $canonicalizedResource = '/' . $this->_accountName;
  85. if ($this->_usePathStyleUri) {
  86. $canonicalizedResource .= '/' . $this->_accountName;
  87. }
  88. $canonicalizedResource .= $path;
  89. if ($queryString !== '') {
  90. $canonicalizedResource .= $queryString;
  91. }
  92. // Request date
  93. $requestDate = '';
  94. if (isset($headers[Zend_Service_WindowsAzure_Credentials_CredentialsAbstract::PREFIX_STORAGE_HEADER . 'date'])) {
  95. $requestDate = $headers[Zend_Service_WindowsAzure_Credentials_CredentialsAbstract::PREFIX_STORAGE_HEADER . 'date'];
  96. } else {
  97. $requestDate = gmdate('D, d M Y H:i:s', time()) . ' GMT'; // RFC 1123
  98. }
  99. // Create string to sign
  100. $stringToSign = array();
  101. $stringToSign[] = $requestDate; // Date
  102. $stringToSign[] = $canonicalizedResource; // Canonicalized resource
  103. $stringToSign = implode("\n", $stringToSign);
  104. $signString = base64_encode(hash_hmac('sha256', $stringToSign, $this->_accountKey, true));
  105. // Sign request
  106. $headers[Zend_Service_WindowsAzure_Credentials_CredentialsAbstract::PREFIX_STORAGE_HEADER . 'date'] = $requestDate;
  107. $headers['Authorization'] = 'SharedKeyLite ' . $this->_accountName . ':' . $signString;
  108. // Return headers
  109. return $headers;
  110. }
  111. /**
  112. * Prepare query string for signing
  113. *
  114. * @param string $value Original query string
  115. * @return string Query string for signing
  116. */
  117. protected function _prepareQueryStringForSigning($value)
  118. {
  119. // Check for 'comp='
  120. if (strpos($value, 'comp=') === false) {
  121. // If not found, no query string needed
  122. return '';
  123. } else {
  124. // If found, make sure it is the only parameter being used
  125. if (strlen($value) > 0 && strpos($value, '?') === 0) {
  126. $value = substr($value, 1);
  127. }
  128. // Split parts
  129. $queryParts = explode('&', $value);
  130. foreach ($queryParts as $queryPart) {
  131. if (strpos($queryPart, 'comp=') !== false) {
  132. return '?' . $queryPart;
  133. }
  134. }
  135. // Should never happen...
  136. return '';
  137. }
  138. }
  139. }