SharedAccessSignature.php 9.9 KB

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