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

Source for file SharedKeyCredentials.php

Documentation is available at SharedKeyCredentials.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 28585 2009-09-07 12:12:56Z 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.      * Sign request URL with credentials
  59.      *
  60.      * @param string $requestUrl Request URL
  61.      * @param string $resourceType Resource type
  62.      * @param string $requiredPermission Required permission
  63.      * @return string Signed request URL
  64.      */
  65.     public function signRequestUrl($requestUrl ''$resourceType Microsoft_WindowsAzure_Storage::RESOURCE_UNKNOWN$requiredPermission Microsoft_WindowsAzure_Credentials::PERMISSION_READ)
  66.     {
  67.         return $requestUrl;
  68.     }
  69.     
  70.     /**
  71.      * Sign request headers with credentials
  72.      *
  73.      * @param string $httpVerb HTTP verb the request will use
  74.      * @param string $path Path for the request
  75.      * @param string $queryString Query string for the request
  76.      * @param array $headers x-ms headers to add
  77.      * @param boolean $forTableStorage Is the request for table storage?
  78.      * @param string $resourceType Resource type
  79.      * @param string $requiredPermission Required permission
  80.      * @return array Array of headers
  81.      */
  82.     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)
  83.     {
  84.         // http://github.com/sriramk/winazurestorage/blob/214010a2f8931bac9c96dfeb337d56fe084ca63b/winazurestorage.py
  85.  
  86.         // Determine path
  87.         if ($this->_usePathStyleUri)
  88.             $path substr($pathstrpos($path'/'));
  89.  
  90.         // Determine query
  91.         $queryString $this->prepareQueryStringForSigning($queryString);
  92.     
  93.         // Canonicalized headers
  94.         $canonicalizedHeaders array();
  95.         
  96.         // Request date
  97.         $requestDate '';
  98.         if (isset($headers[self::PREFIX_STORAGE_HEADER 'date']))
  99.         {
  100.             $requestDate $headers[self::PREFIX_STORAGE_HEADER 'date'];
  101.         }
  102.         else 
  103.         {
  104.             $requestDate gmdate('D, d M Y H:i:s'time()) ' GMT'// RFC 1123
  105.             $canonicalizedHeaders[self::PREFIX_STORAGE_HEADER 'date:' $requestDate;
  106.         }
  107.         
  108.         // Build canonicalized headers
  109.         if (!is_null($headers))
  110.         {
  111.             foreach ($headers as $header => $value{
  112.                 if (is_bool($value))
  113.                     $value $value === true 'True' 'False';
  114.  
  115.                 $headers[$header$value;
  116.                 if (substr($header0strlen(self::PREFIX_STORAGE_HEADER)) == self::PREFIX_STORAGE_HEADER)
  117.                     $canonicalizedHeaders[strtolower($header':' $value;
  118.             }
  119.         }
  120.         sort($canonicalizedHeaders);
  121.  
  122.         // Build canonicalized resource string
  123.         $canonicalizedResource  '/' $this->_accountName;
  124.         if ($this->_usePathStyleUri)
  125.             $canonicalizedResource .= '/' $this->_accountName;
  126.         $canonicalizedResource .= $path;
  127.         if ($queryString !== '')
  128.             $canonicalizedResource .= $queryString;
  129.  
  130.         // Create string to sign   
  131.         $stringToSign array();
  132.         $stringToSign[strtoupper($httpVerb);     // VERB
  133.         $stringToSign["";                        // Content-MD5
  134.         $stringToSign["";                        // Content-Type
  135.         $stringToSign["";
  136.         // Date already in $canonicalizedHeaders
  137.         // $stringToSign[] = self::PREFIX_STORAGE_HEADER . 'date:' . $requestDate; // Date
  138.         
  139.         if (!$forTableStorage && count($canonicalizedHeaders0)
  140.             $stringToSign[implode("\n"$canonicalizedHeaders)// Canonicalized headers
  141.             
  142.         $stringToSign[$canonicalizedResource;                     // Canonicalized resource
  143.         $stringToSign implode("\n"$stringToSign);
  144.         $signString base64_encode(hash_hmac('sha256'$stringToSign$this->_accountKeytrue));
  145.  
  146.         // Sign request
  147.         $headers[self::PREFIX_STORAGE_HEADER 'date'$requestDate;
  148.         $headers['Authorization''SharedKey ' $this->_accountName . ':' $signString;
  149.         
  150.         // Return headers
  151.         return $headers;
  152.     }
  153. }

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