SharedAccessSignature.php 10 KB

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