PageRenderTime 26ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/zend/1.10.7/Zend/Service/WindowsAzure/Credentials/SharedAccessSignature.php

https://github.com/vivid-planet/library
PHP | 305 lines | 138 code | 31 blank | 136 comment | 19 complexity | 9f0b53b0240155f7d4943b73ade507f1 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-2010 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: SharedAccessSignature.php 20785 2010-01-31 09:43:03Z mikaelkael $
  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_SharedAccessSignature
  40. extends Zend_Service_WindowsAzure_Credentials_CredentialsAbstract
  41. {
  42. /**
  43. * Permission set
  44. *
  45. * @var array
  46. */
  47. protected $_permissionSet = array();
  48. /**
  49. * Creates a new Zend_Service_WindowsAzure_Credentials_SharedAccessSignature instance
  50. *
  51. * @param string $accountName Account name for Windows Azure
  52. * @param string $accountKey Account key for Windows Azure
  53. * @param boolean $usePathStyleUri Use path-style URI's
  54. * @param array $permissionSet Permission set
  55. */
  56. public function __construct(
  57. $accountName = Zend_Service_WindowsAzure_Credentials_CredentialsAbstract::DEVSTORE_ACCOUNT,
  58. $accountKey = Zend_Service_WindowsAzure_Credentials_CredentialsAbstract::DEVSTORE_KEY,
  59. $usePathStyleUri = false, $permissionSet = array()
  60. ) {
  61. parent::__construct($accountName, $accountKey, $usePathStyleUri);
  62. $this->_permissionSet = $permissionSet;
  63. }
  64. /**
  65. * Get permission set
  66. *
  67. * @return array
  68. */
  69. public function getPermissionSet()
  70. {
  71. return $this->_permissionSet;
  72. }
  73. /**
  74. * Set permisison set
  75. *
  76. * Warning: fine-grained permissions should be added prior to coarse-grained permissions.
  77. * For example: first add blob permissions, end with container-wide permissions.
  78. *
  79. * Warning: the signed access signature URL must match the account name of the
  80. * Zend_Service_WindowsAzure_Credentials_Zend_Service_WindowsAzure_Credentials_SharedAccessSignature instance
  81. *
  82. * @param array $value Permission set
  83. * @return void
  84. */
  85. public function setPermissionSet($value = array())
  86. {
  87. foreach ($value as $url) {
  88. if (strpos($url, $this->_accountName) === false) {
  89. throw new Zend_Service_WindowsAzure_Exception('The permission set can only contain URLs for the account name specified in the Zend_Service_WindowsAzure_Credentials_SharedAccessSignature instance.');
  90. }
  91. }
  92. $this->_permissionSet = $value;
  93. }
  94. /**
  95. * Create signature
  96. *
  97. * @param string $path Path for the request
  98. * @param string $resource Signed resource - container (c) - blob (b)
  99. * @param string $permissions Signed permissions - read (r), write (w), delete (d) and list (l)
  100. * @param string $start The time at which the Shared Access Signature becomes valid.
  101. * @param string $expiry The time at which the Shared Access Signature becomes invalid.
  102. * @param string $identifier Signed identifier
  103. * @return string
  104. */
  105. public function createSignature(
  106. $path = '/',
  107. $resource = 'b',
  108. $permissions = 'r',
  109. $start = '',
  110. $expiry = '',
  111. $identifier = ''
  112. ) {
  113. // Determine path
  114. if ($this->_usePathStyleUri) {
  115. $path = substr($path, strpos($path, '/'));
  116. }
  117. // Add trailing slash to $path
  118. if (substr($path, 0, 1) !== '/') {
  119. $path = '/' . $path;
  120. }
  121. // Build canonicalized resource string
  122. $canonicalizedResource = '/' . $this->_accountName;
  123. /*if ($this->_usePathStyleUri) {
  124. $canonicalizedResource .= '/' . $this->_accountName;
  125. }*/
  126. $canonicalizedResource .= $path;
  127. // Create string to sign
  128. $stringToSign = array();
  129. $stringToSign[] = $permissions;
  130. $stringToSign[] = $start;
  131. $stringToSign[] = $expiry;
  132. $stringToSign[] = $canonicalizedResource;
  133. $stringToSign[] = $identifier;
  134. $stringToSign = implode("\n", $stringToSign);
  135. $signature = base64_encode(hash_hmac('sha256', $stringToSign, $this->_accountKey, true));
  136. return $signature;
  137. }
  138. /**
  139. * Create signed query string
  140. *
  141. * @param string $path Path for the request
  142. * @param string $queryString Query string for the request
  143. * @param string $resource Signed resource - container (c) - blob (b)
  144. * @param string $permissions Signed permissions - read (r), write (w), delete (d) and list (l)
  145. * @param string $start The time at which the Shared Access Signature becomes valid.
  146. * @param string $expiry The time at which the Shared Access Signature becomes invalid.
  147. * @param string $identifier Signed identifier
  148. * @return string
  149. */
  150. public function createSignedQueryString(
  151. $path = '/',
  152. $queryString = '',
  153. $resource = 'b',
  154. $permissions = 'r',
  155. $start = '',
  156. $expiry = '',
  157. $identifier = ''
  158. ) {
  159. // Parts
  160. $parts = array();
  161. if ($start !== '') {
  162. $parts[] = 'st=' . urlencode($start);
  163. }
  164. $parts[] = 'se=' . urlencode($expiry);
  165. $parts[] = 'sr=' . $resource;
  166. $parts[] = 'sp=' . $permissions;
  167. if ($identifier !== '') {
  168. $parts[] = 'si=' . urlencode($identifier);
  169. }
  170. $parts[] = 'sig=' . urlencode($this->createSignature($path, $resource, $permissions, $start, $expiry, $identifier));
  171. // Assemble parts and query string
  172. if ($queryString != '') {
  173. $queryString .= '&';
  174. }
  175. $queryString .= implode('&', $parts);
  176. return $queryString;
  177. }
  178. /**
  179. * Permission matches request?
  180. *
  181. * @param string $permissionUrl Permission URL
  182. * @param string $requestUrl Request URL
  183. * @param string $resourceType Resource type
  184. * @param string $requiredPermission Required permission
  185. * @return string Signed request URL
  186. */
  187. public function permissionMatchesRequest(
  188. $permissionUrl = '',
  189. $requestUrl = '',
  190. $resourceType = Zend_Service_WindowsAzure_Storage::RESOURCE_UNKNOWN,
  191. $requiredPermission = Zend_Service_WindowsAzure_Credentials_CredentialsAbstract::PERMISSION_READ
  192. ) {
  193. // Build requirements
  194. $requiredResourceType = $resourceType;
  195. if ($requiredResourceType == Zend_Service_WindowsAzure_Storage::RESOURCE_BLOB) {
  196. $requiredResourceType .= Zend_Service_WindowsAzure_Storage::RESOURCE_CONTAINER;
  197. }
  198. // Parse permission url
  199. $parsedPermissionUrl = parse_url($permissionUrl);
  200. // Parse permission properties
  201. $permissionParts = explode('&', $parsedPermissionUrl['query']);
  202. // Parse request url
  203. $parsedRequestUrl = parse_url($requestUrl);
  204. // Check if permission matches request
  205. $matches = true;
  206. foreach ($permissionParts as $part) {
  207. list($property, $value) = explode('=', $part, 2);
  208. if ($property == 'sr') {
  209. $matches = $matches && (strpbrk($value, $requiredResourceType) !== false);
  210. }
  211. if ($property == 'sp') {
  212. $matches = $matches && (strpbrk($value, $requiredPermission) !== false);
  213. }
  214. }
  215. // Ok, but... does the resource match?
  216. $matches = $matches && (strpos($parsedRequestUrl['path'], $parsedPermissionUrl['path']) !== false);
  217. // Return
  218. return $matches;
  219. }
  220. /**
  221. * Sign request URL with credentials
  222. *
  223. * @param string $requestUrl Request URL
  224. * @param string $resourceType Resource type
  225. * @param string $requiredPermission Required permission
  226. * @return string Signed request URL
  227. */
  228. public function signRequestUrl(
  229. $requestUrl = '',
  230. $resourceType = Zend_Service_WindowsAzure_Storage::RESOURCE_UNKNOWN,
  231. $requiredPermission = Zend_Service_WindowsAzure_Credentials_CredentialsAbstract::PERMISSION_READ
  232. ) {
  233. // Look for a matching permission
  234. foreach ($this->getPermissionSet() as $permittedUrl) {
  235. if ($this->permissionMatchesRequest($permittedUrl, $requestUrl, $resourceType, $requiredPermission)) {
  236. // This matches, append signature data
  237. $parsedPermittedUrl = parse_url($permittedUrl);
  238. if (strpos($requestUrl, '?') === false) {
  239. $requestUrl .= '?';
  240. } else {
  241. $requestUrl .= '&';
  242. }
  243. $requestUrl .= $parsedPermittedUrl['query'];
  244. // Return url
  245. return $requestUrl;
  246. }
  247. }
  248. // Return url, will be unsigned...
  249. return $requestUrl;
  250. }
  251. /**
  252. * Sign request with credentials
  253. *
  254. * @param string $httpVerb HTTP verb the request will use
  255. * @param string $path Path for the request
  256. * @param string $queryString Query string for the request
  257. * @param array $headers x-ms headers to add
  258. * @param boolean $forTableStorage Is the request for table storage?
  259. * @param string $resourceType Resource type
  260. * @param string $requiredPermission Required permission
  261. * @return array Array of headers
  262. */
  263. public function signRequestHeaders(
  264. $httpVerb = Zend_Http_Client::GET,
  265. $path = '/',
  266. $queryString = '',
  267. $headers = null,
  268. $forTableStorage = false,
  269. $resourceType = Zend_Service_WindowsAzure_Storage::RESOURCE_UNKNOWN,
  270. $requiredPermission = Zend_Service_WindowsAzure_Credentials_CredentialsAbstract::PERMISSION_READ
  271. ) {
  272. return $headers;
  273. }
  274. }