PageRenderTime 53ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/backwpup/vendor/WindowsAzure/Common/Internal/Authentication/StorageAuthScheme.php

https://gitlab.com/pankajmohale/chef2go
PHP | 215 lines | 68 code | 29 blank | 118 comment | 4 complexity | 9c73f0dd81064e57d470d82d1c6097bc MD5 | raw file
  1. <?php
  2. /**
  3. * LICENSE: Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. * http://www.apache.org/licenses/LICENSE-2.0
  7. *
  8. * Unless required by applicable law or agreed to in writing, software
  9. * distributed under the License is distributed on an "AS IS" BASIS,
  10. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. * See the License for the specific language governing permissions and
  12. * limitations under the License.
  13. *
  14. * PHP version 5
  15. *
  16. * @category Microsoft
  17. * @package WindowsAzure\Common\Internal\Authentication
  18. * @author Azure PHP SDK <azurephpsdk@microsoft.com>
  19. * @copyright 2012 Microsoft Corporation
  20. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
  21. * @link https://github.com/windowsazure/azure-sdk-for-php
  22. */
  23. namespace WindowsAzure\Common\Internal\Authentication;
  24. use WindowsAzure\Common\Internal\Resources;
  25. use WindowsAzure\Common\Internal\Utilities;
  26. use WindowsAzure\Common\Internal\Authentication\IAuthScheme;
  27. /**
  28. * Base class for azure authentication schemes.
  29. *
  30. * @category Microsoft
  31. * @package WindowsAzure\Common\Internal\Authentication
  32. * @author Azure PHP SDK <azurephpsdk@microsoft.com>
  33. * @copyright 2012 Microsoft Corporation
  34. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
  35. * @version Release: 0.4.1_2015-03
  36. * @link https://github.com/windowsazure/azure-sdk-for-php
  37. */
  38. abstract class StorageAuthScheme implements IAuthScheme
  39. {
  40. protected $accountName;
  41. protected $accountKey;
  42. /**
  43. * Constructor.
  44. *
  45. * @param string $accountName storage account name.
  46. * @param string $accountKey storage account primary or secondary key.
  47. *
  48. * @return
  49. * WindowsAzure\Common\Internal\Authentication\StorageAuthScheme
  50. */
  51. public function __construct($accountName, $accountKey)
  52. {
  53. $this->accountKey = $accountKey;
  54. $this->accountName = $accountName;
  55. }
  56. /**
  57. * Computes canonicalized headers for headers array.
  58. *
  59. * @param array $headers request headers.
  60. *
  61. * @see Constructing the Canonicalized Headers String section at
  62. * http://msdn.microsoft.com/en-us/library/windowsazure/dd179428.aspx
  63. *
  64. * @return array
  65. */
  66. protected function computeCanonicalizedHeaders($headers)
  67. {
  68. $canonicalizedHeaders = array();
  69. $normalizedHeaders = array();
  70. $validPrefix = Resources::X_MS_HEADER_PREFIX;
  71. if (is_null($normalizedHeaders)) {
  72. return $canonicalizedHeaders;
  73. }
  74. foreach ($headers as $header => $value) {
  75. // Convert header to lower case.
  76. $header = strtolower($header);
  77. // Retrieve all headers for the resource that begin with x-ms-,
  78. // including the x-ms-date header.
  79. if (Utilities::startsWith($header, $validPrefix)) {
  80. // Unfold the string by replacing any breaking white space
  81. // (meaning what splits the headers, which is \r\n) with a single
  82. // space.
  83. $value = str_replace("\r\n", ' ', $value);
  84. // Trim any white space around the colon in the header.
  85. $value = ltrim($value);
  86. $header = rtrim($header);
  87. $normalizedHeaders[$header] = $value;
  88. }
  89. }
  90. // Sort the headers lexicographically by header name, in ascending order.
  91. // Note that each header may appear only once in the string.
  92. ksort($normalizedHeaders);
  93. foreach ($normalizedHeaders as $key => $value) {
  94. $canonicalizedHeaders[] = $key . ':' . $value;
  95. }
  96. return $canonicalizedHeaders;
  97. }
  98. /**
  99. * Computes canonicalized resources from URL using Table formar
  100. *
  101. * @param string $url request url.
  102. * @param array $queryParams request query variables.
  103. *
  104. * @see Constructing the Canonicalized Resource String section at
  105. * http://msdn.microsoft.com/en-us/library/windowsazure/dd179428.aspx
  106. *
  107. * @return string
  108. */
  109. protected function computeCanonicalizedResourceForTable($url, $queryParams)
  110. {
  111. $queryParams = array_change_key_case($queryParams);
  112. // 1. Beginning with an empty string (""), append a forward slash (/),
  113. // followed by the name of the account that owns the accessed resource.
  114. $canonicalizedResource = '/' . $this->accountName;
  115. // 2. Append the resource's encoded URI path, without any query parameters.
  116. $canonicalizedResource .= parse_url($url, PHP_URL_PATH);
  117. // 3. The query string should include the question mark and the comp
  118. // parameter (for example, ?comp=metadata). No other parameters should
  119. // be included on the query string.
  120. if (array_key_exists(Resources::QP_COMP, $queryParams)) {
  121. $canonicalizedResource .= '?' . Resources::QP_COMP . '=';
  122. $canonicalizedResource .= $queryParams[Resources::QP_COMP];
  123. }
  124. return $canonicalizedResource;
  125. }
  126. /**
  127. * Computes canonicalized resources from URL.
  128. *
  129. * @param string $url request url.
  130. * @param array $queryParams request query variables.
  131. *
  132. * @see Constructing the Canonicalized Resource String section at
  133. * http://msdn.microsoft.com/en-us/library/windowsazure/dd179428.aspx
  134. *
  135. * @return string
  136. */
  137. protected function computeCanonicalizedResource($url, $queryParams)
  138. {
  139. $queryParams = array_change_key_case($queryParams);
  140. // 1. Beginning with an empty string (""), append a forward slash (/),
  141. // followed by the name of the account that owns the accessed resource.
  142. $canonicalizedResource = '/' . $this->accountName;
  143. // 2. Append the resource's encoded URI path, without any query parameters.
  144. $canonicalizedResource .= parse_url($url, PHP_URL_PATH);
  145. // 3. Retrieve all query parameters on the resource URI, including the comp
  146. // parameter if it exists.
  147. // 4. Sort the query parameters lexicographically by parameter name, in
  148. // ascending order.
  149. if (count($queryParams) > 0) {
  150. ksort($queryParams);
  151. }
  152. // 5. Convert all parameter names to lowercase.
  153. // 6. URL-decode each query parameter name and value.
  154. // 7. Append each query parameter name and value to the string in the
  155. // following format:
  156. // parameter-name:parameter-value
  157. // 9. Group query parameters
  158. // 10. Append a new line character (\n) after each name-value pair.
  159. foreach ($queryParams as $key => $value) {
  160. // Grouping query parameters
  161. $values = explode(Resources::SEPARATOR, $value);
  162. sort($values);
  163. $separated = implode(Resources::SEPARATOR, $values);
  164. $canonicalizedResource .= "\n" . $key . ':' . $separated;
  165. }
  166. return $canonicalizedResource;
  167. }
  168. /**
  169. * Computes the authorization signature.
  170. *
  171. * @param array $headers request headers.
  172. * @param string $url reuqest url.
  173. * @param array $queryParams query variables.
  174. * @param string $httpMethod request http method.
  175. *
  176. * @see check all authentication schemes at
  177. * http://msdn.microsoft.com/en-us/library/windowsazure/dd179428.aspx
  178. *
  179. * @abstract
  180. *
  181. * @return string
  182. */
  183. abstract protected function computeSignature($headers, $url, $queryParams,
  184. $httpMethod
  185. );
  186. }