PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/libs/Microsoft/WindowsAzure/Credentials/SharedKeyLite.php

https://code.google.com/
PHP | 162 lines | 68 code | 13 blank | 81 comment | 11 complexity | febe8faf662fd7fa05238e21b6931500 MD5 | raw file
Possible License(s): Apache-2.0, GPL-2.0
  1. <?php
  2. /**
  3. * Copyright (c) 2009 - 2012, RealDolmen
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * * Neither the name of RealDolmen nor the
  14. * names of its contributors may be used to endorse or promote products
  15. * derived from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY RealDolmen ''AS IS'' AND ANY
  18. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. * DISCLAIMED. IN NO EVENT SHALL RealDolmen BE LIABLE FOR ANY
  21. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  24. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  26. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. *
  28. * @category Microsoft
  29. * @package Microsoft_WindowsAzure
  30. * @copyright Copyright (c) 2009 - 2012, RealDolmen (http://www.realdolmen.com)
  31. * @license http://phpazure.codeplex.com/license
  32. * @version $Id: SharedKeyCredentials.php 14561 2009-05-07 08:05:12Z unknown $
  33. */
  34. /**
  35. * @see Microsoft_AutoLoader
  36. */
  37. //require_once dirname(__FILE__) . '/../../AutoLoader.php';
  38. /**
  39. * @category Microsoft
  40. * @package Microsoft_WindowsAzure
  41. * @copyright Copyright (c) 2009 - 2012, RealDolmen (http://www.realdolmen.com)
  42. * @license http://phpazure.codeplex.com/license
  43. */
  44. class Microsoft_WindowsAzure_Credentials_SharedKeyLite
  45. extends Microsoft_WindowsAzure_Credentials_CredentialsAbstract
  46. {
  47. /**
  48. * Sign request URL with credentials
  49. *
  50. * @param string $requestUrl Request URL
  51. * @param string $resourceType Resource type
  52. * @param string $requiredPermission Required permission
  53. * @return string Signed request URL
  54. */
  55. public function signRequestUrl(
  56. $requestUrl = '',
  57. $resourceType = Microsoft_WindowsAzure_Storage::RESOURCE_UNKNOWN,
  58. $requiredPermission = Microsoft_WindowsAzure_Credentials_CredentialsAbstract::PERMISSION_READ
  59. ) {
  60. return $requestUrl;
  61. }
  62. /**
  63. * Sign request headers with credentials
  64. *
  65. * @param string $httpVerb HTTP verb the request will use
  66. * @param string $path Path for the request
  67. * @param array $query Query arguments for the request (key/value pairs)
  68. * @param array $headers x-ms headers to add
  69. * @param boolean $forTableStorage Is the request for table storage?
  70. * @param string $resourceType Resource type
  71. * @param string $requiredPermission Required permission
  72. * @param mixed $rawData Raw post data
  73. * @return array Array of headers
  74. */
  75. public function signRequestHeaders(
  76. $httpVerb = Microsoft_Http_Client::GET,
  77. $path = '/',
  78. $query = array(),
  79. $headers = null,
  80. $forTableStorage = false,
  81. $resourceType = Microsoft_WindowsAzure_Storage::RESOURCE_UNKNOWN,
  82. $requiredPermission = Microsoft_WindowsAzure_Credentials_CredentialsAbstract::PERMISSION_READ,
  83. $rawData = null
  84. ) {
  85. // Table storage?
  86. if (!$forTableStorage) {
  87. throw new Microsoft_WindowsAzure_Credentials_Exception('The Windows Azure SDK for PHP does not support SharedKeyLite authentication on blob or queue storage. Use SharedKey authentication instead.');
  88. }
  89. // Determine path
  90. if ($this->_usePathStyleUri) {
  91. $path = substr($path, strpos($path, '/'));
  92. }
  93. // Build canonicalized resource string
  94. $canonicalizedResource = '/' . $this->_accountName;
  95. if ($this->_usePathStyleUri) {
  96. $canonicalizedResource .= '/' . $this->_accountName;
  97. }
  98. $canonicalizedResource .= $path;
  99. if (isset($query['comp'])) {
  100. $canonicalizedResource .= '?comp=' . rawurlencode($query['comp']);
  101. }
  102. // Request date
  103. $requestDate = '';
  104. if (isset($headers[Microsoft_WindowsAzure_Credentials_CredentialsAbstract::PREFIX_STORAGE_HEADER . 'date'])) {
  105. $requestDate = $headers[Microsoft_WindowsAzure_Credentials_CredentialsAbstract::PREFIX_STORAGE_HEADER . 'date'];
  106. } else {
  107. $requestDate = gmdate('D, d M Y H:i:s', time()) . ' GMT'; // RFC 1123
  108. }
  109. // Create string to sign
  110. $stringToSign = array();
  111. $stringToSign[] = $requestDate; // Date
  112. $stringToSign[] = $canonicalizedResource; // Canonicalized resource
  113. $stringToSign = implode("\n", $stringToSign);
  114. $signString = base64_encode(hash_hmac('sha256', $stringToSign, $this->_accountKey, true));
  115. // Sign request
  116. $headers[Microsoft_WindowsAzure_Credentials_CredentialsAbstract::PREFIX_STORAGE_HEADER . 'date'] = $requestDate;
  117. $headers['Authorization'] = 'SharedKeyLite ' . $this->_accountName . ':' . $signString;
  118. // Return headers
  119. return $headers;
  120. }
  121. /**
  122. * Prepare query string for signing
  123. *
  124. * @param string $value Original query string
  125. * @return string Query string for signing
  126. */
  127. protected function _prepareQueryStringForSigning($value)
  128. {
  129. // Check for 'comp='
  130. if (strpos($value, 'comp=') === false) {
  131. // If not found, no query string needed
  132. return '';
  133. } else {
  134. // If found, make sure it is the only parameter being used
  135. if (strlen($value) > 0 && strpos($value, '?') === 0) {
  136. $value = substr($value, 1);
  137. }
  138. // Split parts
  139. $queryParts = explode('&', $value);
  140. foreach ($queryParts as $queryPart) {
  141. if (strpos($queryPart, 'comp=') !== false) {
  142. return '?' . $queryPart;
  143. }
  144. }
  145. // Should never happen...
  146. return '';
  147. }
  148. }
  149. }