PageRenderTime 48ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/zend/1.10.2-minapp/library/Zend/Service/WindowsAzure/Credentials/SharedKey.php

http://github.com/pmjones/php-framework-benchmarks
PHP | 154 lines | 71 code | 17 blank | 66 comment | 11 complexity | de1365d1db327a0da7dc6579ac7dec0e MD5 | raw file
Possible License(s): LGPL-3.0, Apache-2.0, BSD-3-Clause, ISC, AGPL-3.0, LGPL-2.1
  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: SharedKey.php 35999 2009-12-21 07:56:42Z unknown $
  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_Http_Client
  31. */
  32. require_once 'Zend/Http/Client.php';
  33. /**
  34. * @category Zend
  35. * @package Zend_Service_WindowsAzure
  36. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  37. * @license http://framework.zend.com/license/new-bsd New BSD License
  38. */
  39. class Zend_Service_WindowsAzure_Credentials_SharedKey
  40. extends Zend_Service_WindowsAzure_Credentials_CredentialsAbstract
  41. {
  42. /**
  43. * Sign request URL with credentials
  44. *
  45. * @param string $requestUrl Request URL
  46. * @param string $resourceType Resource type
  47. * @param string $requiredPermission Required permission
  48. * @return string Signed request URL
  49. */
  50. public function signRequestUrl(
  51. $requestUrl = '',
  52. $resourceType = Zend_Service_WindowsAzure_Storage::RESOURCE_UNKNOWN,
  53. $requiredPermission = Zend_Service_WindowsAzure_Credentials_CredentialsAbstract::PERMISSION_READ
  54. ) {
  55. return $requestUrl;
  56. }
  57. /**
  58. * Sign request headers with credentials
  59. *
  60. * @param string $httpVerb HTTP verb the request will use
  61. * @param string $path Path for the request
  62. * @param string $queryString Query string for the request
  63. * @param array $headers x-ms headers to add
  64. * @param boolean $forTableStorage Is the request for table storage?
  65. * @param string $resourceType Resource type
  66. * @param string $requiredPermission Required permission
  67. * @return array Array of headers
  68. */
  69. public function signRequestHeaders(
  70. $httpVerb = Zend_Http_Client::GET,
  71. $path = '/',
  72. $queryString = '',
  73. $headers = null,
  74. $forTableStorage = false,
  75. $resourceType = Zend_Service_WindowsAzure_Storage::RESOURCE_UNKNOWN,
  76. $requiredPermission = Zend_Service_WindowsAzure_Credentials_CredentialsAbstract::PERMISSION_READ
  77. ) {
  78. // http://github.com/sriramk/winazurestorage/blob/214010a2f8931bac9c96dfeb337d56fe084ca63b/winazurestorage.py
  79. // Determine path
  80. if ($this->_usePathStyleUri) {
  81. $path = substr($path, strpos($path, '/'));
  82. }
  83. // Determine query
  84. $queryString = $this->_prepareQueryStringForSigning($queryString);
  85. // Canonicalized headers
  86. $canonicalizedHeaders = array();
  87. // Request date
  88. $requestDate = '';
  89. if (isset($headers[Zend_Service_WindowsAzure_Credentials_CredentialsAbstract::PREFIX_STORAGE_HEADER . 'date'])) {
  90. $requestDate = $headers[Zend_Service_WindowsAzure_Credentials_CredentialsAbstract::PREFIX_STORAGE_HEADER . 'date'];
  91. } else {
  92. $requestDate = gmdate('D, d M Y H:i:s', time()) . ' GMT'; // RFC 1123
  93. $canonicalizedHeaders[] = Zend_Service_WindowsAzure_Credentials_CredentialsAbstract::PREFIX_STORAGE_HEADER . 'date:' . $requestDate;
  94. }
  95. // Build canonicalized headers
  96. if (!is_null($headers)) {
  97. foreach ($headers as $header => $value) {
  98. if (is_bool($value)) {
  99. $value = $value === true ? 'True' : 'False';
  100. }
  101. $headers[$header] = $value;
  102. if (substr($header, 0, strlen(Zend_Service_WindowsAzure_Credentials_CredentialsAbstract::PREFIX_STORAGE_HEADER)) == Zend_Service_WindowsAzure_Credentials_CredentialsAbstract::PREFIX_STORAGE_HEADER) {
  103. $canonicalizedHeaders[] = strtolower($header) . ':' . $value;
  104. }
  105. }
  106. }
  107. sort($canonicalizedHeaders);
  108. // Build canonicalized resource string
  109. $canonicalizedResource = '/' . $this->_accountName;
  110. if ($this->_usePathStyleUri) {
  111. $canonicalizedResource .= '/' . $this->_accountName;
  112. }
  113. $canonicalizedResource .= $path;
  114. if ($queryString !== '') {
  115. $canonicalizedResource .= $queryString;
  116. }
  117. // Create string to sign
  118. $stringToSign = array();
  119. $stringToSign[] = strtoupper($httpVerb); // VERB
  120. $stringToSign[] = ""; // Content-MD5
  121. $stringToSign[] = ""; // Content-Type
  122. $stringToSign[] = "";
  123. // Date already in $canonicalizedHeaders
  124. // $stringToSign[] = self::PREFIX_STORAGE_HEADER . 'date:' . $requestDate; // Date
  125. if (!$forTableStorage && count($canonicalizedHeaders) > 0) {
  126. $stringToSign[] = implode("\n", $canonicalizedHeaders); // Canonicalized headers
  127. }
  128. $stringToSign[] = $canonicalizedResource; // Canonicalized resource
  129. $stringToSign = implode("\n", $stringToSign);
  130. $signString = base64_encode(hash_hmac('sha256', $stringToSign, $this->_accountKey, true));
  131. // Sign request
  132. $headers[Zend_Service_WindowsAzure_Credentials_CredentialsAbstract::PREFIX_STORAGE_HEADER . 'date'] = $requestDate;
  133. $headers['Authorization'] = 'SharedKey ' . $this->_accountName . ':' . $signString;
  134. // Return headers
  135. return $headers;
  136. }
  137. }