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 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.         // 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($path, strpos($path, '/'));
  96.         }
  97.  
  98.         // Canonicalized headers
  99.         $canonicalizedHeaders = array();
  100.         
  101.         // Request date
  102.         $requestDate = '';
  103.         if (isset($headers[Microsoft_WindowsAzure_Credentials_CredentialsAbstract::PREFIX_STORAGE_HEADER . 'date'])) {
  104.             $requestDate = $headers[Microsoft_WindowsAzure_Credentials_CredentialsAbstract::PREFIX_STORAGE_HEADER . 'date'];
  105.         } else {
  106.             $requestDate = gmdate('D, d M Y H:i:s', time()) . ' GMT'; // RFC 1123
  107.             $canonicalizedHeaders[] = Microsoft_WindowsAzure_Credentials_CredentialsAbstract::PREFIX_STORAGE_HEADER . 'date:' . $requestDate;
  108.         }
  109.         
  110.         // Build canonicalized headers
  111.         if (!is_null($headers)) {
  112.             foreach ($headers as $header => $value) {
  113.                 if (is_bool($value)) {
  114.                     $value = $value === true ? 'True' : 'False';
  115.                 }
  116.  
  117.                 $headers[$header] = $value;
  118.                 if (substr($header, 0, strlen(Microsoft_WindowsAzure_Credentials_CredentialsAbstract::PREFIX_STORAGE_HEADER)) == Microsoft_WindowsAzure_Credentials_CredentialsAbstract::PREFIX_STORAGE_HEADER) {
  119.                     $canonicalizedHeaders[] = strtolower($header) . ':' . $value;
  120.                 }
  121.             }
  122.         }
  123.         sort($canonicalizedHeaders);
  124.  
  125.         // Build canonicalized resource string
  126.         $canonicalizedResource  = '/' . $this->_accountName;
  127.         if ($this->_usePathStyleUri) {
  128.             $canonicalizedResource .= '/' . $this->_accountName;
  129.         }
  130.         $canonicalizedResource .= $path;
  131.         if (count($query) > 0) {
  132.             ksort($query);
  133.  
  134.             foreach ($query as $key => $value) {
  135.                 $canonicalizedResource .= "\n" . strtolower($key) . ':' . rawurldecode($value);
  136.             }
  137.         }
  138.  
  139.         // Content-Length header
  140.         $contentLength = '';
  141.         if (strtoupper($httpVerb) != Microsoft_Http_Client::GET
  142.              && strtoupper($httpVerb) != Microsoft_Http_Client::DELETE
  143.              && strtoupper($httpVerb) != Microsoft_Http_Client::HEAD) {
  144.             $contentLength = 0;
  145.             
  146.             if (!is_null($rawData)) {
  147.                 $contentLength = strlen($rawData);
  148.             }
  149.         }
  150.  
  151.         // Create string to sign   
  152.         $stringToSign   = array();
  153.         $stringToSign[] = strtoupper($httpVerb);                                     // VERB
  154.         $stringToSign[] = $this->_issetOr($headers, 'Content-Encoding', '');        // Content-Encoding
  155.         $stringToSign[] = $this->_issetOr($headers, 'Content-Language', '');        // Content-Language
  156.         $stringToSign[] = $contentLength;                                             // Content-Length
  157.         $stringToSign[] = $this->_issetOr($headers, 'Content-MD5', '');                // Content-MD5
  158.         $stringToSign[] = $this->_issetOr($headers, 'Content-Type', '');            // Content-Type
  159.         $stringToSign[] = "";                                                        // Date
  160.         $stringToSign[] = $this->_issetOr($headers, 'If-Modified-Since', '');        // If-Modified-Since
  161.         $stringToSign[] = $this->_issetOr($headers, 'If-Match', '');                // If-Match
  162.         $stringToSign[] = $this->_issetOr($headers, 'If-None-Match', '');            // If-None-Match
  163.         $stringToSign[] = $this->_issetOr($headers, 'If-Unmodified-Since', '');        // If-Unmodified-Since
  164.         $stringToSign[] = $this->_issetOr($headers, 'Range', '');                    // Range
  165.         
  166.         if (!$forTableStorage && count($canonicalizedHeaders) > 0) {
  167.             $stringToSign[] = implode("\n", $canonicalizedHeaders); // Canonicalized headers
  168.         }
  169.             
  170.         $stringToSign[] = $canonicalizedResource;                     // Canonicalized resource
  171.         $stringToSign   = implode("\n", $stringToSign);
  172.         $signString     = base64_encode(hash_hmac('sha256', $stringToSign, $this->_accountKey, true));
  173.  
  174.         // Sign request
  175.         $headers[Microsoft_WindowsAzure_Credentials_CredentialsAbstract::PREFIX_STORAGE_HEADER . 'date'] = $requestDate;
  176.         $headers['Authorization'] = 'SharedKey ' . $this->_accountName . ':' . $signString;
  177.         
  178.         // Return headers
  179.         return $headers;
  180.     }
  181. }

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