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

Source for file SharedKey.php

Documentation is available at SharedKey.php

  1. <?php
  2. /**
  3.  * Copyright (c) 2009 - 2011, 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 - 2011, RealDolmen (http://www.realdolmen.com)
  31.  * @license    http://phpazure.codeplex.com/license
  32.  * @version    $Id$
  33.  */
  34.  
  35. /**
  36.  * @see Microsoft_AutoLoader
  37.  */
  38. require_once dirname(__FILE__'/../../AutoLoader.php';
  39.  
  40. /**
  41.  * @category   Microsoft
  42.  * @package    Microsoft_WindowsAzure
  43.  * @copyright  Copyright (c) 2009 - 2011, RealDolmen (http://www.realdolmen.com)
  44.  * @license    http://phpazure.codeplex.com/license
  45.  */ 
  46. {
  47.     /**
  48.      * Sign request URL with credentials
  49.      *
  50.      * @param string $requestUrl Request URL
  51.      * @param string $resourceType Resource type
  52.      * @param string $requiredPermission Required permission
  53.      * @return string Signed request URL
  54.      */
  55.     public function signRequestUrl(
  56.         $requestUrl '',
  57.         $resourceType Microsoft_WindowsAzure_Storage::RESOURCE_UNKNOWN,
  58.         $requiredPermission Microsoft_WindowsAzure_Credentials_CredentialsAbstract::PERMISSION_READ
  59.     {
  60.         return $requestUrl;
  61.     }
  62.     
  63.     /**
  64.      * Sign request headers with credentials
  65.      *
  66.      * @param string $httpVerb HTTP verb the request will use
  67.      * @param string $path Path for the request
  68.      * @param string $queryString Query string for the request
  69.      * @param array $headers x-ms headers to add
  70.      * @param boolean $forTableStorage Is the request for table storage?
  71.      * @param string $resourceType Resource type
  72.      * @param string $requiredPermission Required permission
  73.      * @param mixed  $rawData Raw post data
  74.      * @return array Array of headers
  75.      */
  76.     public function signRequestHeaders(
  77.         $httpVerb Microsoft_Http_Client::GET,
  78.         $path '/',
  79.         $queryString '',
  80.         $headers null,
  81.         $forTableStorage false,
  82.         $resourceType Microsoft_WindowsAzure_Storage::RESOURCE_UNKNOWN,
  83.         $requiredPermission Microsoft_WindowsAzure_Credentials_CredentialsAbstract::PERMISSION_READ,
  84.         $rawData null
  85.     {
  86.         // http://github.com/sriramk/winazurestorage/blob/214010a2f8931bac9c96dfeb337d56fe084ca63b/winazurestorage.py
  87.  
  88.         // Table storage?
  89.         if ($forTableStorage{
  90.             throw new Microsoft_WindowsAzure_Credentials_Exception('The Windows Azure SDK for PHP does not support SharedKey authentication on table storage. Use SharedKeyLite authentication instead.');
  91.         }
  92.         
  93.         // Determine path
  94.         if ($this->_usePathStyleUri{
  95.             $path substr($pathstrpos($path'/'));
  96.         }
  97.  
  98.         // Determine query
  99.         $queryString $this->_prepareQueryStringForSigning($queryString);
  100.     
  101.         // Canonicalized headers
  102.         $canonicalizedHeaders array();
  103.         
  104.         // Request date
  105.         $requestDate '';
  106.         if (isset($headers[Microsoft_WindowsAzure_Credentials_CredentialsAbstract::PREFIX_STORAGE_HEADER 'date'])) {
  107.             $requestDate $headers[Microsoft_WindowsAzure_Credentials_CredentialsAbstract::PREFIX_STORAGE_HEADER 'date'];
  108.         else {
  109.             $requestDate gmdate('D, d M Y H:i:s'time()) ' GMT'// RFC 1123
  110.             $canonicalizedHeaders[Microsoft_WindowsAzure_Credentials_CredentialsAbstract::PREFIX_STORAGE_HEADER 'date:' $requestDate;
  111.         }
  112.         
  113.         // Build canonicalized headers
  114.         if (!is_null($headers)) {
  115.             foreach ($headers as $header => $value{
  116.                 if (is_bool($value)) {
  117.                     $value $value === true 'True' 'False';
  118.                 }
  119.  
  120.                 $headers[$header$value;
  121.                 if (substr($header0strlen(Microsoft_WindowsAzure_Credentials_CredentialsAbstract::PREFIX_STORAGE_HEADER)) == Microsoft_WindowsAzure_Credentials_CredentialsAbstract::PREFIX_STORAGE_HEADER{
  122.                     $canonicalizedHeaders[strtolower($header':' $value;
  123.                 }
  124.             }
  125.         }
  126.         sort($canonicalizedHeaders);
  127.  
  128.         // Build canonicalized resource string
  129.         $canonicalizedResource  '/' $this->_accountName;
  130.         if ($this->_usePathStyleUri{
  131.             $canonicalizedResource .= '/' $this->_accountName;
  132.         }
  133.         $canonicalizedResource .= $path;
  134.         if ($queryString !== ''{
  135.             $queryStringItems $this->_makeArrayOfQueryString($queryString);
  136.             foreach ($queryStringItems as $key => $value{
  137.                 $canonicalizedResource .= "\n" strtolower($key':' urldecode($value);
  138.             }
  139.         }
  140.         
  141.         // Content-Length header
  142.         $contentLength '';
  143.         if (strtoupper($httpVerb!= Microsoft_Http_Client::GET
  144.              && strtoupper($httpVerb!= Microsoft_Http_Client::DELETE
  145.              && strtoupper($httpVerb!= Microsoft_Http_Client::HEAD{
  146.             $contentLength 0;
  147.             
  148.             if (!is_null($rawData)) {
  149.                 $contentLength strlen($rawData);
  150.             }
  151.         }
  152.  
  153.         // Create string to sign   
  154.         $stringToSign   array();
  155.         $stringToSign[strtoupper($httpVerb);                                     // VERB
  156.         $stringToSign[$this->_issetOr($headers'Content-Encoding''');        // Content-Encoding
  157.         $stringToSign[$this->_issetOr($headers'Content-Language''');        // Content-Language
  158.         $stringToSign[$contentLength;                                             // Content-Length
  159.         $stringToSign[$this->_issetOr($headers'Content-MD5''');                // Content-MD5
  160.         $stringToSign[$this->_issetOr($headers'Content-Type''');            // Content-Type
  161.         $stringToSign["";                                                        // Date
  162.         $stringToSign[$this->_issetOr($headers'If-Modified-Since''');        // If-Modified-Since
  163.         $stringToSign[$this->_issetOr($headers'If-Match''');                // If-Match
  164.         $stringToSign[$this->_issetOr($headers'If-None-Match''');            // If-None-Match
  165.         $stringToSign[$this->_issetOr($headers'If-Unmodified-Since''');        // If-Unmodified-Since
  166.         $stringToSign[$this->_issetOr($headers'Range''');                    // Range
  167.         
  168.         if (!$forTableStorage && count($canonicalizedHeaders0{
  169.             $stringToSign[implode("\n"$canonicalizedHeaders)// Canonicalized headers
  170.         }
  171.             
  172.         $stringToSign[$canonicalizedResource;                     // Canonicalized resource
  173.         $stringToSign   implode("\n"$stringToSign);
  174.         $signString     base64_encode(hash_hmac('sha256'$stringToSign$this->_accountKeytrue));
  175.  
  176.         // Sign request
  177.         $headers[Microsoft_WindowsAzure_Credentials_CredentialsAbstract::PREFIX_STORAGE_HEADER 'date'$requestDate;
  178.         $headers['Authorization''SharedKey ' $this->_accountName . ':' $signString;
  179.         
  180.         // Return headers
  181.         return $headers;
  182.     }
  183. }

Documentation generated on Wed, 18 May 2011 12:06:50 +0200 by phpDocumentor 1.4.3