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

Source for file SharedKeyLite.php

Documentation is available at SharedKeyLite.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: SharedKeyCredentials.php 14561 2009-05-07 08:05:12Z unknown $
  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 array $query Query arguments for the request (key/value pairs)
  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.         $query = array(),
  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.         // Table storage?
  87.         if (!$forTableStorage) {
  88.             throw new Microsoft_WindowsAzure_Credentials_Exception('The Windows Azure SDK for PHP does not support SharedKeyLite authentication on blob or queue storage. Use SharedKey authentication instead.');
  89.         }
  90.         
  91.         // Determine path
  92.         if ($this->_usePathStyleUri) {
  93.             $path = substr($path, strpos($path, '/'));
  94.         }
  95.  
  96.         // Build canonicalized resource string
  97.         $canonicalizedResource  = '/' . $this->_accountName;
  98.         if ($this->_usePathStyleUri) {
  99.             $canonicalizedResource .= '/' . $this->_accountName;
  100.         }
  101.         $canonicalizedResource .= $path;
  102.         
  103.         if (isset($query['comp'])) {
  104.             $canonicalizedResource .= '?comp=' . rawurlencode($query['comp']); 
  105.         }
  106.  
  107.         // Request date
  108.         $requestDate = '';
  109.         if (isset($headers[Microsoft_WindowsAzure_Credentials_CredentialsAbstract::PREFIX_STORAGE_HEADER . 'date'])) {
  110.             $requestDate = $headers[Microsoft_WindowsAzure_Credentials_CredentialsAbstract::PREFIX_STORAGE_HEADER . 'date'];
  111.         } else {
  112.             $requestDate = gmdate('D, d M Y H:i:s', time()) . ' GMT'; // RFC 1123
  113.         }
  114.  
  115.         // Create string to sign   
  116.         $stringToSign   = array();
  117.         $stringToSign[] = $requestDate; // Date
  118.         $stringToSign[] = $canonicalizedResource;                     // Canonicalized resource
  119.         $stringToSign   = implode("\n", $stringToSign);
  120.         $signString     = base64_encode(hash_hmac('sha256', $stringToSign, $this->_accountKey, true));
  121.  
  122.         // Sign request
  123.         $headers[Microsoft_WindowsAzure_Credentials_CredentialsAbstract::PREFIX_STORAGE_HEADER . 'date'] = $requestDate;
  124.         $headers['Authorization'] = 'SharedKeyLite ' . $this->_accountName . ':' . $signString;
  125.         
  126.         // Return headers
  127.         return $headers;
  128.     }
  129.     
  130.     /**
  131.      * Prepare query string for signing
  132.      * 
  133.      * @param  string $value Original query string
  134.      * @return string        Query string for signing
  135.      */
  136.     protected function _prepareQueryStringForSigning($value)
  137.     {
  138.         // Check for 'comp='
  139.         if (strpos($value, 'comp=') === false) {
  140.             // If not found, no query string needed
  141.             return '';
  142.         } else {
  143.             // If found, make sure it is the only parameter being used      
  144.             if (strlen($value) > 0 && strpos($value, '?') === 0) {
  145.                 $value = substr($value, 1);
  146.             }
  147.             
  148.             // Split parts
  149.             $queryParts = explode('&', $value);
  150.             foreach ($queryParts as $queryPart) {
  151.                 if (strpos($queryPart, 'comp=') !== false) {
  152.                     return '?' . $queryPart;
  153.                 }
  154.             }
  155.  
  156.             // Should never happen...
  157.             return '';
  158.         }
  159.     }
  160. }

Documentation generated on Sat, 03 Dec 2011 13:59:38 +0100 by phpDocumentor 1.4.3