Microsoft_WindowsAzure
[ class tree: Microsoft_WindowsAzure ] [ index: Microsoft_WindowsAzure ] [ all elements ]

Source for file SharedAccessSignatureCredentials.php

Documentation is available at SharedAccessSignatureCredentials.php

  1. <?php
  2. /**
  3.  * Copyright (c) 2009, RealDolmen
  4.  * All rights reserved.
  5.  *
  6.  * Redistribution and use in source and binary forms, with or without
  7.  * modification, are permitted provided that the following conditions are met:
  8.  *     * Redistributions of source code must retain the above copyright
  9.  *       notice, this list of conditions and the following disclaimer.
  10.  *     * Redistributions in binary form must reproduce the above copyright
  11.  *       notice, this list of conditions and the following disclaimer in the
  12.  *       documentation and/or other materials provided with the distribution.
  13.  *     * Neither the name of RealDolmen nor the
  14.  *       names of its contributors may be used to endorse or promote products
  15.  *       derived from this software without specific prior written permission.
  16.  *
  17.  * THIS SOFTWARE IS PROVIDED BY RealDolmen ''AS IS'' AND ANY
  18.  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20.  * DISCLAIMED. IN NO EVENT SHALL RealDolmen BE LIABLE FOR ANY
  21.  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22.  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  24.  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  26.  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27.  *
  28.  * @category   Microsoft
  29.  * @package    Microsoft_WindowsAzure
  30.  * @copyright  Copyright (c) 2009, RealDolmen (http://www.realdolmen.com)
  31.  * @license    http://phpazure.codeplex.com/license
  32.  * @version    $Id: SharedKeyCredentials.php 24305 2009-07-23 06:30:04Z unknown $
  33.  */
  34.  
  35. /**
  36.  * @see Microsoft_WindowsAzure_Credentials
  37.  */
  38. require_once 'Microsoft/WindowsAzure/Credentials.php';
  39.  
  40. /**
  41.  * @see Microsoft_WindowsAzure_Storage
  42.  */
  43. require_once 'Microsoft/WindowsAzure/Storage.php';
  44.  
  45. /**
  46.  * @see Microsoft_Http_Transport
  47.  */
  48. require_once 'Microsoft/Http/Transport.php';
  49.  
  50. /**
  51.  * @category   Microsoft
  52.  * @package    Microsoft_WindowsAzure
  53.  * @copyright  Copyright (c) 2009, RealDolmen (http://www.realdolmen.com)
  54.  * @license    http://phpazure.codeplex.com/license
  55.  */ 
  56. {
  57.     /**
  58.      * Permission set
  59.      * 
  60.      * @var array 
  61.      */
  62.     protected $_permissionSet = array();
  63.     
  64.     /**
  65.      * Creates a new Microsoft_WindowsAzure_SharedAccessSignatureCredentials instance
  66.      *
  67.      * @param string $accountName Account name for Windows Azure
  68.      * @param string $accountKey Account key for Windows Azure
  69.      * @param boolean $usePathStyleUri Use path-style URI's
  70.      * @param array $permissionSet Permission set
  71.      */
  72.     public function __construct($accountName Microsoft_WindowsAzure_Credentials::DEVSTORE_ACCOUNT$accountKey Microsoft_WindowsAzure_Credentials::DEVSTORE_KEY$usePathStyleUri false$permissionSet array())
  73.     {
  74.         parent::__construct($accountName$accountKey$usePathStyleUri);
  75.         $this->_permissionSet = $permissionSet;
  76.     }
  77.     
  78.     /**
  79.      * Get permission set
  80.      * 
  81.      * @return array 
  82.      */
  83.     public function getPermissionSet()
  84.     {
  85.         return $this->_permissionSet;   
  86.     }
  87.     
  88.     /**
  89.      * Set permisison set
  90.      * 
  91.      * Warning: fine-grained permissions should be added prior to coarse-grained permissions.
  92.      * For example: first add blob permissions, end with container-wide permissions.
  93.      * 
  94.      * Warning: the signed access signature URL must match the account name of the
  95.      * Microsoft_WindowsAzure_SharedAccessSignatureCredentials instance
  96.      * 
  97.      * @param array $value Permission set
  98.      */
  99.     public function setPermissionSet($value array())
  100.     {
  101.         foreach ($value as $url)
  102.         {
  103.             if (strpos($url$this->_accountName=== false)
  104.                 throw new Microsoft_WindowsAzure_Exception('The permission set can only contain URLs for the account name specified in the Microsoft_WindowsAzure_SharedAccessSignatureCredentials instance.');
  105.         }
  106.         $this->_permissionSet = $value;
  107.     }
  108.     
  109.     /**
  110.      * Create signature
  111.      * 
  112.      * @param string $path            Path for the request
  113.      * @param string $resource     Signed resource - container (c) - blob (b)
  114.      * @param string $permissions  Signed permissions - read (r), write (w), delete (d) and list (l)
  115.      * @param string $start        The time at which the Shared Access Signature becomes valid.
  116.      * @param string $expiry       The time at which the Shared Access Signature becomes invalid.
  117.      * @param string $identifier   Signed identifier
  118.      * @return string 
  119.      */
  120.     public function createSignature($path '/'$resource 'b'$permissions 'r'$start ''$expiry ''$identifier '')
  121.     {
  122.         // Determine path
  123.         if ($this->_usePathStyleUri)
  124.             $path substr($pathstrpos($path'/'));
  125.             
  126.         // Add trailing slash to $path
  127.         if (substr($path01!== '/')
  128.             $path '/' $path;
  129.  
  130.         // Build canonicalized resource string
  131.         $canonicalizedResource  '/' $this->_accountName;
  132.         if ($this->_usePathStyleUri)
  133.             $canonicalizedResource .= '/' $this->_accountName;
  134.         $canonicalizedResource .= $path;
  135.             
  136.         // Create string to sign   
  137.         $stringToSign array();
  138.         $stringToSign[$permissions;
  139.         $stringToSign[$start;
  140.         $stringToSign[$expiry;
  141.         $stringToSign[$canonicalizedResource;
  142.         $stringToSign[$identifier;
  143.  
  144.         $stringToSign implode("\n"$stringToSign);
  145.         $signature base64_encode(hash_hmac('sha256'$stringToSign$this->_accountKeytrue));
  146.  
  147.         return $signature;
  148.     }
  149.  
  150.     /**
  151.      * Create signed query string
  152.      * 
  153.      * @param string $path            Path for the request
  154.      * @param string $queryString  Query string for the request
  155.      * @param string $resource     Signed resource - container (c) - blob (b)
  156.      * @param string $permissions  Signed permissions - read (r), write (w), delete (d) and list (l)
  157.      * @param string $start        The time at which the Shared Access Signature becomes valid.
  158.      * @param string $expiry       The time at which the Shared Access Signature becomes invalid.
  159.      * @param string $identifier   Signed identifier
  160.      * @return string 
  161.      */
  162.     public function createSignedQueryString($path '/'$queryString ''$resource 'b'$permissions 'r'$start ''$expiry ''$identifier '')
  163.     {
  164.         // Parts
  165.         $parts array();
  166.         if ($start !== '')
  167.             $parts['st=' urlencode($start);
  168.         $parts['se=' urlencode($expiry);
  169.         $parts['sr=' $resource;
  170.         $parts['sp=' $permissions;
  171.         if ($identifier !== '')
  172.             $parts['si=' urlencode($identifier);
  173.         $parts['sig=' urlencode($this->createSignature($path$resource$permissions$start$expiry$identifier));
  174.  
  175.         // Assemble parts and query string
  176.         if ($queryString != '')
  177.             $queryString .= '&';
  178.         $queryString .= implode('&'$parts);
  179.  
  180.         return $queryString;
  181.     }
  182.     
  183.     /**
  184.      * Permission matches request?
  185.      *
  186.      * @param string $permissionUrl Permission URL
  187.      * @param string $requestUrl Request URL
  188.      * @param string $resourceType Resource type
  189.      * @param string $requiredPermission Required permission
  190.      * @return string Signed request URL
  191.      */
  192.     public function permissionMatchesRequest($permissionUrl ''$requestUrl ''$resourceType Microsoft_WindowsAzure_Storage::RESOURCE_UNKNOWN$requiredPermission Microsoft_WindowsAzure_Credentials::PERMISSION_READ)
  193.     {
  194.         // Build requirements
  195.         $requiredResourceType $resourceType;
  196.         if ($requiredResourceType == Microsoft_WindowsAzure_Storage::RESOURCE_BLOB)
  197.             $requiredResourceType .= Microsoft_WindowsAzure_Storage::RESOURCE_CONTAINER;
  198.  
  199.         // Parse permission url
  200.         $parsedPermissionUrl parse_url($permissionUrl);
  201.         
  202.         // Parse permission properties
  203.         $permissionParts explode('&'$parsedPermissionUrl['query']);
  204.         
  205.         // Parse request url
  206.         $parsedRequestUrl parse_url($requestUrl);
  207.         
  208.         // Check if permission matches request
  209.         $matches true;
  210.         foreach ($permissionParts as $part)
  211.         {
  212.             list($property$valueexplode('='$part2);
  213.             if ($property == 'sr')
  214.             {
  215.                 $matches $matches && (strpbrk($value$requiredResourceType!== false);
  216.             }
  217.             if ($property == 'sp')
  218.             {
  219.                 $matches $matches && (strpbrk($value$requiredPermission!== false);
  220.             }
  221.         }
  222.         
  223.         // Ok, but... does the resource match?
  224.         $matches $matches && (strpos($parsedRequestUrl['path']$parsedPermissionUrl['path']!== false);
  225.         
  226.         // Return
  227.         return $matches;
  228.     }    
  229.     
  230.     /**
  231.      * Sign request URL with credentials
  232.      *
  233.      * @param string $requestUrl Request URL
  234.      * @param string $resourceType Resource type
  235.      * @param string $requiredPermission Required permission
  236.      * @return string Signed request URL
  237.      */
  238.     public function signRequestUrl($requestUrl ''$resourceType Microsoft_WindowsAzure_Storage::RESOURCE_UNKNOWN$requiredPermission Microsoft_WindowsAzure_Credentials::PERMISSION_READ)
  239.     {
  240.         // Look for a matching permission
  241.         foreach ($this->getPermissionSet(as $permittedUrl)
  242.         {
  243.             if ($this->permissionMatchesRequest($permittedUrl$requestUrl$resourceType$requiredPermission))
  244.             {
  245.                 // This matches, append signature data
  246.                 $parsedPermittedUrl parse_url($permittedUrl);
  247.  
  248.                 if (strpos($requestUrl'?'=== false)
  249.                     $requestUrl .= '?';
  250.                 else
  251.                     $requestUrl .= '&';
  252.                 
  253.                 $requestUrl .= $parsedPermittedUrl['query'];
  254.  
  255.                 // Return url
  256.                 return $requestUrl;
  257.             }
  258.         }
  259.         
  260.         // Return url, will be unsigned...
  261.         return $requestUrl;
  262.     }
  263.     
  264.     /**
  265.      * Sign request with credentials
  266.      *
  267.      * @param string $httpVerb HTTP verb the request will use
  268.      * @param string $path Path for the request
  269.      * @param string $queryString Query string for the request
  270.      * @param array $headers x-ms headers to add
  271.      * @param boolean $forTableStorage Is the request for table storage?
  272.      * @param string $resourceType Resource type
  273.      * @param string $requiredPermission Required permission
  274.      * @return array Array of headers
  275.      */
  276.     public function signRequestHeaders($httpVerb Microsoft_Http_Transport::VERB_GET$path '/'$queryString ''$headers null$forTableStorage false$resourceType Microsoft_WindowsAzure_Storage::RESOURCE_UNKNOWN$requiredPermission Microsoft_WindowsAzure_Credentials::PERMISSION_READ)
  277.     {
  278.         return $headers;
  279.     }
  280. }

Documentation generated on Thu, 26 Nov 2009 08:05:20 +0100 by phpDocumentor 1.4.3